vendor/pimcore/pimcore/bundles/CoreBundle/EventListener/Frontend/GlobalTemplateVariablesListener.php line 98

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\Bundle\CoreBundle\EventListener\Frontend;
  15. use Pimcore\Bundle\CoreBundle\EventListener\Traits\PimcoreContextAwareTrait;
  16. use Pimcore\Http\Request\Resolver\DocumentResolver;
  17. use Pimcore\Http\Request\Resolver\EditmodeResolver;
  18. use Pimcore\Http\Request\Resolver\PimcoreContextResolver;
  19. use Psr\Log\LoggerAwareInterface;
  20. use Psr\Log\LoggerAwareTrait;
  21. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  22. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  23. use Symfony\Component\HttpKernel\Event\RequestEvent;
  24. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  25. use Symfony\Component\HttpKernel\KernelEvents;
  26. use Twig\Environment;
  27. /**
  28.  * @internal
  29.  */
  30. class GlobalTemplateVariablesListener implements EventSubscriberInterfaceLoggerAwareInterface
  31. {
  32.     use LoggerAwareTrait;
  33.     use PimcoreContextAwareTrait;
  34.     /**
  35.      * @var array
  36.      */
  37.     protected $globalsStack = [];
  38.     public function __construct(
  39.         protected DocumentResolver $documentResolver,
  40.         protected EditmodeResolver $editmodeResolver,
  41.         protected Environment $twig
  42.     ) {
  43.     }
  44.     /**
  45.      * {@inheritdoc}
  46.      */
  47.     public static function getSubscribedEvents()
  48.     {
  49.         return [
  50.             KernelEvents::CONTROLLER => ['onKernelController'15], // has to be after DocumentFallbackListener
  51.             KernelEvents::RESPONSE => 'onKernelResponse',
  52.             KernelEvents::REQUEST => ['onKernelRequest'700],
  53.         ];
  54.     }
  55.     public function onKernelRequest(RequestEvent $event)
  56.     {
  57.         // set the variables as soon as possible, so that we're not getting troubles in
  58.         // onKernelController() if the twig environment was already initialized before
  59.         // defining global variables is only possible before the twig environment was initialized
  60.         // however you can change the value of the variable at any time later on
  61.         if ($event->isMainRequest()) {
  62.             $this->twig->addGlobal('document'null);
  63.             $this->twig->addGlobal('editmode'false);
  64.         }
  65.     }
  66.     public function onKernelController(ControllerEvent $event)
  67.     {
  68.         $request $event->getRequest();
  69.         if (!$this->matchesPimcoreContext($requestPimcoreContextResolver::CONTEXT_DEFAULT)) {
  70.             return;
  71.         }
  72.         $globals $this->twig->getGlobals();
  73.         try {
  74.             // it could be the case that the Twig environment is already initialized at this point
  75.             // then it's not possible anymore to add globals
  76.             $this->twig->addGlobal('document'$this->documentResolver->getDocument($request));
  77.             $this->twig->addGlobal('editmode'$this->editmodeResolver->isEditmode($request));
  78.             array_push($this->globalsStack$globals);
  79.         } catch (\Exception $e) {
  80.             array_push($this->globalsStackfalse);
  81.         }
  82.     }
  83.     /**
  84.      * @param ResponseEvent $event
  85.      */
  86.     public function onKernelResponse(ResponseEvent $event)
  87.     {
  88.         if (count($this->globalsStack)) {
  89.             $globals array_pop($this->globalsStack);
  90.             if ($globals !== false) {
  91.                 $this->twig->addGlobal('document'$globals['document'] ?? null);
  92.                 $this->twig->addGlobal('editmode'$globals['editmode'] ?? null);
  93.             }
  94.         }
  95.     }
  96. }