[ Index ]

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


Generated: Wed Nov 11 20:33:01 2020 Cross-referenced by PHPXref 0.7.1