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

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 Pimcore\Config;
  16. use Symfony\Component\HttpFoundation\Response;
  17. use Symfony\Component\Templating\DelegatingEngine as BaseDelegatingEngine;
  18. use Symfony\Component\Templating\EngineInterface;
  19. use Twig\Environment;
  20. use Twig\Extension\SandboxExtension;
  21. /**
  22.  * @internal
  23.  */
  24. class TwigDefaultDelegatingEngine extends BaseDelegatingEngine
  25. {
  26.     /**
  27.      * @var bool
  28.      */
  29.     protected $delegate false;
  30.     /**
  31.      * @param EngineInterface[] $engines
  32.      */
  33.     public function __construct(protected Environment $twig, protected Config $config, array $engines = [])
  34.     {
  35.         parent::__construct($engines);
  36.     }
  37.     /**
  38.      * {@inheritdoc}
  39.      */
  40.     public function exists($name): bool
  41.     {
  42.         if (!$this->delegate) {
  43.             return $this->twig->getLoader()->exists($name);
  44.         } else {
  45.             return parent::exists($name);
  46.         }
  47.     }
  48.     /**
  49.      * {@inheritdoc}
  50.      *
  51.      * @throws \Exception
  52.      */
  53.     public function render($name, array $parameters = []): string
  54.     {
  55.         if (!$this->delegate) {
  56.             return $this->twig->render($name$parameters);
  57.         } else {
  58.             return parent::render($name$parameters);
  59.         }
  60.     }
  61.     /**
  62.      * {@inheritdoc}
  63.      */
  64.     public function supports($name): bool
  65.     {
  66.         if (!$this->delegate) {
  67.             return true;
  68.         } else {
  69.             return parent::supports($name);
  70.         }
  71.     }
  72.     /**
  73.      * @param bool $delegate
  74.      */
  75.     public function setDelegate(bool $delegate)
  76.     {
  77.         $this->delegate $delegate;
  78.     }
  79.     /**
  80.      * @return bool $delegate
  81.      */
  82.     public function isDelegate()
  83.     {
  84.         return $this->delegate;
  85.     }
  86.     public function getTwigEnvironment(bool $sandboxed false): Environment
  87.     {
  88.         if ($sandboxed) {
  89.             /** @var SandboxExtension $sandboxExtension */
  90.             $sandboxExtension $this->twig->getExtension(SandboxExtension::class);
  91.             $sandboxExtension->enableSandbox();
  92.         }
  93.         return $this->twig;
  94.     }
  95.     public function disableSandboxExtensionFromTwigEnvironment(): void
  96.     {
  97.         /** @var SandboxExtension $sandboxExtension */
  98.         $sandboxExtension $this->twig->getExtension(SandboxExtension::class);
  99.         $sandboxExtension->disableSandbox();
  100.     }
  101.     /**
  102.      * @param string $view
  103.      * @param array $parameters
  104.      * @param Response|null $response
  105.      *
  106.      * @return Response
  107.      *
  108.      * @throws \Exception
  109.      */
  110.     public function renderResponse($view, array $parameters = [], Response $response null)
  111.     {
  112.         if (null === $response) {
  113.             $response = new Response();
  114.         }
  115.         $response->setContent($this->render($view$parameters));
  116.         return $response;
  117.     }
  118. }