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

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. @trigger_error('The '.TwigEngine::class.' class is deprecated since version 4.3 and will be removed in 5.0; use \Twig\Environment instead.'E_USER_DEPRECATED);
  12. use Symfony\Component\Templating\EngineInterface;
  13. use Symfony\Component\Templating\StreamingEngineInterface;
  14. use Symfony\Component\Templating\TemplateNameParserInterface;
  15. use Symfony\Component\Templating\TemplateReferenceInterface;
  16. use Twig\Environment;
  17. use Twig\Error\Error;
  18. use Twig\Error\LoaderError;
  19. use Twig\Loader\ExistsLoaderInterface;
  20. use Twig\Template;
  21. /**
  22.  * This engine knows how to render Twig templates.
  23.  *
  24.  * @author Fabien Potencier <fabien@symfony.com>
  25.  *
  26.  * @deprecated since version 4.3, to be removed in 5.0; use Twig instead.
  27.  */
  28. class TwigEngine implements EngineInterfaceStreamingEngineInterface
  29. {
  30.     protected $environment;
  31.     protected $parser;
  32.     public function __construct(Environment $environmentTemplateNameParserInterface $parser)
  33.     {
  34.         $this->environment $environment;
  35.         $this->parser $parser;
  36.     }
  37.     /**
  38.      * {@inheritdoc}
  39.      *
  40.      * It also supports Template as name parameter.
  41.      *
  42.      * @throws Error if something went wrong like a thrown exception while rendering the template
  43.      */
  44.     public function render($name, array $parameters = [])
  45.     {
  46.         return $this->load($name)->render($parameters);
  47.     }
  48.     /**
  49.      * {@inheritdoc}
  50.      *
  51.      * It also supports Template as name parameter.
  52.      *
  53.      * @throws Error if something went wrong like a thrown exception while rendering the template
  54.      */
  55.     public function stream($name, array $parameters = [])
  56.     {
  57.         $this->load($name)->display($parameters);
  58.     }
  59.     /**
  60.      * {@inheritdoc}
  61.      *
  62.      * It also supports Template as name parameter.
  63.      */
  64.     public function exists($name)
  65.     {
  66.         if ($name instanceof Template) {
  67.             return true;
  68.         }
  69.         $loader $this->environment->getLoader();
  70.         if ($loader instanceof ExistsLoaderInterface || method_exists($loader'exists')) {
  71.             return $loader->exists((string) $name);
  72.         }
  73.         try {
  74.             // cast possible TemplateReferenceInterface to string because the
  75.             // EngineInterface supports them but LoaderInterface does not
  76.             $loader->getSourceContext((string) $name)->getCode();
  77.         } catch (LoaderError $e) {
  78.             return false;
  79.         }
  80.         return true;
  81.     }
  82.     /**
  83.      * {@inheritdoc}
  84.      *
  85.      * It also supports Template as name parameter.
  86.      */
  87.     public function supports($name)
  88.     {
  89.         if ($name instanceof Template) {
  90.             return true;
  91.         }
  92.         $template $this->parser->parse($name);
  93.         return 'twig' === $template->get('engine');
  94.     }
  95.     /**
  96.      * Loads the given template.
  97.      *
  98.      * @param string|TemplateReferenceInterface|Template $name A template name or an instance of
  99.      *                                                         TemplateReferenceInterface or Template
  100.      *
  101.      * @return Template
  102.      *
  103.      * @throws \InvalidArgumentException if the template does not exist
  104.      */
  105.     protected function load($name)
  106.     {
  107.         if ($name instanceof Template) {
  108.             return $name;
  109.         }
  110.         try {
  111.             return $this->environment->loadTemplate((string) $name);
  112.         } catch (LoaderError $e) {
  113.             throw new \InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
  114.         }
  115.     }
  116. }