src/EventListener/GlobalTemplateVariablesListener.php line 104

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 Enterprise License (PEL)
  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 PEL
  13.  */
  14. namespace App\EventListener;
  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\ResponseEvent;
  24. use Symfony\Component\HttpKernel\KernelEvents;
  25. use Twig\Environment;
  26. /**
  27.  * @internal
  28.  */
  29. final class GlobalTemplateVariablesListener implements EventSubscriberInterfaceLoggerAwareInterface
  30. {
  31.     use LoggerAwareTrait;
  32.     use PimcoreContextAwareTrait;
  33.     /**
  34.      * @var DocumentResolver
  35.      */
  36.     protected $documentResolver;
  37.     /**
  38.      * @var EditmodeResolver
  39.      */
  40.     protected $editmodeResolver;
  41.     /**
  42.      * @var Environment
  43.      */
  44.     protected $twig;
  45.     /**
  46.      * @var array
  47.      */
  48.     protected $globalsStack = [];
  49.     public function __construct(
  50.         DocumentResolver $documentResolver,
  51.         EditmodeResolver $editmodeResolver,
  52.         Environment $twig
  53.     ) {
  54.         $this->documentResolver $documentResolver;
  55.         $this->editmodeResolver $editmodeResolver;
  56.         $this->twig $twig;
  57.     }
  58.     /**
  59.      * {@inheritdoc}
  60.      */
  61.     public static function getSubscribedEvents()
  62.     {
  63.         return [
  64.             KernelEvents::CONTROLLER => ['onKernelController'15], // has to be after DocumentFallbackListener
  65.             KernelEvents::RESPONSE => 'onKernelResponse',
  66.         ];
  67.     }
  68.     public function onKernelController(ControllerEvent $event)
  69.     {
  70.         $request $event->getRequest();
  71.         $globals $this->twig->getGlobals();
  72.         array_push($this->globalsStack$globals);
  73.         $document $this->documentResolver->getDocument($request);
  74. //        $editmode = $this->editmodeResolver->isEditmode($request);
  75. //        $this->twig->addGlobal('document', $document);
  76. //        $this->twig->addGlobal('editmode', $editmode);
  77.         $this->twig->addGlobal('staticFolder''/static/build');
  78.         $this->twig->addGlobal('staticDebugMode'false);
  79.         if($document) {
  80.             $this->twig->addGlobal('siteConfig'$document->getProperty('siteConfig'));
  81.         }
  82.         if (!$this->matchesPimcoreContext($requestPimcoreContextResolver::CONTEXT_DEFAULT)) {
  83.             return;
  84.         }
  85.     }
  86.     /**
  87.      * @param ResponseEvent $event
  88.      */
  89.     public function onKernelResponse(ResponseEvent $event)
  90.     {
  91.         if (count($this->globalsStack)) {
  92.             $globals array_pop($this->globalsStack);
  93.             $this->twig->addGlobal('document'$globals['document'] ?? null);
  94.             $this->twig->addGlobal('editmode'$globals['editmode'] ?? null);
  95.             $this->twig->addGlobal('staticFolder'$globals['staticFolder'] ?? null );
  96.             $this->twig->addGlobal('staticDebugMode'false );
  97.         }
  98.     }
  99. }