[ Index ]

PHP Cross Reference of phpBB-3.3.5-deutsch

title

Body

[close]

/vendor/symfony/twig-bridge/ -> TwigEngine.php (source)

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


Generated: Mon Oct 4 17:42:11 2021 Cross-referenced by PHPXref 0.7.1