vendor/symfony/twig-bridge/TwigEngine.php line 54

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