vendor/symfony/symfony/src/Symfony/Bridge/Twig/TwigEngine.php line 131

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Bridge\Twig;
  11. use Symfony\Component\Templating\EngineInterface;
  12. use Symfony\Component\Templating\StreamingEngineInterface;
  13. use Symfony\Component\Templating\TemplateNameParserInterface;
  14. use Symfony\Component\Templating\TemplateReferenceInterface;
  15. use Twig\Environment;
  16. use Twig\Error\Error;
  17. use Twig\Error\LoaderError;
  18. use Twig\Loader\ExistsLoaderInterface;
  19. use Twig\Loader\SourceContextLoaderInterface;
  20. use Twig\Template;
  21. /**
  22.  * This engine knows how to render Twig templates.
  23.  *
  24.  * @author Fabien Potencier <fabien@symfony.com>
  25.  */
  26. class TwigEngine implements EngineInterfaceStreamingEngineInterface
  27. {
  28.     protected $environment;
  29.     protected $parser;
  30.     public function __construct(Environment $environmentTemplateNameParserInterface $parser)
  31.     {
  32.         $this->environment $environment;
  33.         $this->parser $parser;
  34.     }
  35.     /**
  36.      * {@inheritdoc}
  37.      *
  38.      * It also supports Template as name parameter.
  39.      *
  40.      * @throws Error if something went wrong like a thrown exception while rendering the template
  41.      */
  42.     public function render($name, array $parameters = [])
  43.     {
  44.         return $this->load($name)->render($parameters);
  45.     }
  46.     /**
  47.      * {@inheritdoc}
  48.      *
  49.      * It also supports Template as name parameter.
  50.      *
  51.      * @throws Error if something went wrong like a thrown exception while rendering the template
  52.      */
  53.     public function stream($name, array $parameters = [])
  54.     {
  55.         $this->load($name)->display($parameters);
  56.     }
  57.     /**
  58.      * {@inheritdoc}
  59.      *
  60.      * It also supports Template as name parameter.
  61.      */
  62.     public function exists($name)
  63.     {
  64.         if ($name instanceof Template) {
  65.             return true;
  66.         }
  67.         $loader $this->environment->getLoader();
  68.         if (=== Environment::MAJOR_VERSION && !$loader instanceof ExistsLoaderInterface) {
  69.             try {
  70.                 // cast possible TemplateReferenceInterface to string because the
  71.                 // EngineInterface supports them but LoaderInterface does not
  72.                 if ($loader instanceof SourceContextLoaderInterface) {
  73.                     $loader->getSourceContext((string) $name);
  74.                 } else {
  75.                     $loader->getSource((string) $name);
  76.                 }
  77.                 return true;
  78.             } catch (LoaderError $e) {
  79.             }
  80.             return false;
  81.         }
  82.         return $loader->exists((string) $name);
  83.     }
  84.     /**
  85.      * {@inheritdoc}
  86.      *
  87.      * It also supports Template as name parameter.
  88.      */
  89.     public function supports($name)
  90.     {
  91.         if ($name instanceof Template) {
  92.             return true;
  93.         }
  94.         $template $this->parser->parse($name);
  95.         return 'twig' === $template->get('engine');
  96.     }
  97.     /**
  98.      * Loads the given template.
  99.      *
  100.      * @param string|TemplateReferenceInterface|Template $name A template name or an instance of
  101.      *                                                         TemplateReferenceInterface or Template
  102.      *
  103.      * @return Template
  104.      *
  105.      * @throws \InvalidArgumentException if the template does not exist
  106.      */
  107.     protected function load($name)
  108.     {
  109.         if ($name instanceof Template) {
  110.             return $name;
  111.         }
  112.         try {
  113.             return $this->environment->loadTemplate((string) $name);
  114.         } catch (LoaderError $e) {
  115.             throw new \InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
  116.         }
  117.     }
  118. }