vendor/pimcore/pimcore/bundles/CoreBundle/EventListener/Frontend/BlockStateListener.php line 62

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 Pimcore\Bundle\CoreBundle\EventListener\Frontend;
  15. use Pimcore\Bundle\CoreBundle\EventListener\Traits\PimcoreContextAwareTrait;
  16. use Pimcore\Document\Tag\Block\BlockStateStack;
  17. use Pimcore\Http\Request\Resolver\PimcoreContextResolver;
  18. use Psr\Log\LoggerAwareInterface;
  19. use Psr\Log\LoggerAwareTrait;
  20. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  21. use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
  22. use Symfony\Component\HttpKernel\Event\GetResponseEvent;
  23. use Symfony\Component\HttpKernel\KernelEvents;
  24. /**
  25.  * Handles block state for sub requests (saves parent state and restores it after request completes)
  26.  */
  27. class BlockStateListener implements EventSubscriberInterfaceLoggerAwareInterface
  28. {
  29.     use LoggerAwareTrait;
  30.     use PimcoreContextAwareTrait;
  31.     /**
  32.      * @var BlockStateStack
  33.      */
  34.     protected $blockStateStack;
  35.     /**
  36.      * @param \Pimcore\Document\Tag\Block\BlockStateStack $blockStateStack
  37.      */
  38.     public function __construct(BlockStateStack $blockStateStack)
  39.     {
  40.         $this->blockStateStack $blockStateStack;
  41.     }
  42.     /**
  43.      * @inheritDoc
  44.      */
  45.     public static function getSubscribedEvents()
  46.     {
  47.         return [
  48.             KernelEvents::REQUEST => 'onKernelRequest',
  49.             KernelEvents::RESPONSE => 'onKernelResponse'
  50.         ];
  51.     }
  52.     /**
  53.      * @param GetResponseEvent $event
  54.      */
  55.     public function onKernelRequest(GetResponseEvent $event)
  56.     {
  57.         $request $event->getRequest();
  58.         if (!$this->matchesPimcoreContext($requestPimcoreContextResolver::CONTEXT_DEFAULT)) {
  59.             return;
  60.         }
  61.         if ($request->get('disableBlockClearing')) {
  62.             return;
  63.         }
  64.         // master request already has a state on the stack
  65.         if ($event->isMasterRequest()) {
  66.             return;
  67.         }
  68.         // this is for $this->action() in templates when they are inside a block element
  69.         // adds a new, empty block state to the stack which is used in the sub-request
  70.         $this->blockStateStack->push();
  71.     }
  72.     /**
  73.      * @param FilterResponseEvent $event
  74.      */
  75.     public function onKernelResponse(FilterResponseEvent $event)
  76.     {
  77.         $request $event->getRequest();
  78.         if (!$this->matchesPimcoreContext($requestPimcoreContextResolver::CONTEXT_DEFAULT)) {
  79.             return;
  80.         }
  81.         if ($request->get('disableBlockClearing')) {
  82.             return;
  83.         }
  84.         if ($this->blockStateStack->count() > 1) {
  85.             // restore parent block data by removing sub-request block state
  86.             $this->blockStateStack->pop();
  87.         }
  88.     }
  89. }