vendor/pimcore/pimcore/lib/Templating/TwigDefaultDelegatingEngine.php line 69

Open in your IDE?
  1. <?php
  2. /**
  3.  * Pimcore
  4.  *
  5.  * This source file is available under two different licenses:
  6.  * - GNU General Public License version 3 (GPLv3)
  7.  * - Pimcore Commercial License (PCL)
  8.  * Full copyright and license information is available in
  9.  * LICENSE.md which is distributed with this source code.
  10.  *
  11.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  12.  *  @license    http://www.pimcore.org/license     GPLv3 and PCL
  13.  */
  14. namespace Pimcore\Templating;
  15. use Symfony\Component\HttpFoundation\Response;
  16. use Symfony\Component\Templating\DelegatingEngine as BaseDelegatingEngine;
  17. use Symfony\Component\Templating\EngineInterface;
  18. use Twig\Environment;
  19. /**
  20.  * @internal
  21.  */
  22. class TwigDefaultDelegatingEngine extends BaseDelegatingEngine
  23. {
  24.     /**
  25.      * @var Environment
  26.      */
  27.     protected $twig;
  28.     /**
  29.      * @var bool
  30.      */
  31.     protected $delegate false;
  32.     /**
  33.      * @param Environment $twig
  34.      * @param EngineInterface[] $engines
  35.      */
  36.     public function __construct(Environment $twig, array $engines = [])
  37.     {
  38.         $this->twig $twig;
  39.         parent::__construct($engines);
  40.     }
  41.     /**
  42.      * {@inheritdoc}
  43.      */
  44.     public function exists($name): bool
  45.     {
  46.         if (!$this->delegate) {
  47.             return $this->twig->getLoader()->exists($name);
  48.         } else {
  49.             return parent::exists($name);
  50.         }
  51.     }
  52.     /**
  53.      * {@inheritdoc}
  54.      *
  55.      * @throws \Exception
  56.      */
  57.     public function render($name, array $parameters = []): string
  58.     {
  59.         if (!$this->delegate) {
  60.             return $this->twig->render($name$parameters);
  61.         } else {
  62.             return parent::render($name$parameters);
  63.         }
  64.     }
  65.     /**
  66.      * {@inheritdoc}
  67.      */
  68.     public function supports($name): bool
  69.     {
  70.         if (!$this->delegate) {
  71.             return true;
  72.         } else {
  73.             return parent::supports($name);
  74.         }
  75.     }
  76.     /**
  77.      * @param bool $delegate
  78.      */
  79.     public function setDelegate(bool $delegate)
  80.     {
  81.         $this->delegate $delegate;
  82.     }
  83.     /**
  84.      * @return bool $delegate
  85.      */
  86.     public function isDelegate()
  87.     {
  88.         return $this->delegate;
  89.     }
  90.     /**
  91.      * @param string $view
  92.      * @param array $parameters
  93.      * @param Response|null $response
  94.      *
  95.      * @return Response
  96.      *
  97.      * @throws \Exception
  98.      */
  99.     public function renderResponse($view, array $parameters = [], Response $response null)
  100.     {
  101.         if (null === $response) {
  102.             $response = new Response();
  103.         }
  104.         $response->setContent($this->render($view$parameters));
  105.         return $response;
  106.     }
  107. }