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

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\Document\Editable\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\RequestEvent;
  22. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  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.  * @internal
  28.  */
  29. class BlockStateListener implements EventSubscriberInterfaceLoggerAwareInterface
  30. {
  31.     use LoggerAwareTrait;
  32.     use PimcoreContextAwareTrait;
  33.     /**
  34.      * @param BlockStateStack $blockStateStack
  35.      */
  36.     public function __construct(protected BlockStateStack $blockStateStack)
  37.     {
  38.     }
  39.     /**
  40.      * {@inheritdoc}
  41.      */
  42.     public static function getSubscribedEvents()
  43.     {
  44.         return [
  45.             KernelEvents::REQUEST => 'onKernelRequest',
  46.             KernelEvents::RESPONSE => 'onKernelResponse',
  47.         ];
  48.     }
  49.     /**
  50.      * @param RequestEvent $event
  51.      */
  52.     public function onKernelRequest(RequestEvent $event)
  53.     {
  54.         $request $event->getRequest();
  55.         if (!$this->matchesPimcoreContext($requestPimcoreContextResolver::CONTEXT_DEFAULT)) {
  56.             return;
  57.         }
  58.         if ($request->get('disableBlockClearing')) {
  59.             return;
  60.         }
  61.         // master request already has a state on the stack
  62.         if ($event->isMainRequest()) {
  63.             return;
  64.         }
  65.         // this is for $this->action() in templates when they are inside a block element
  66.         // adds a new, empty block state to the stack which is used in the sub-request
  67.         $this->blockStateStack->push();
  68.     }
  69.     /**
  70.      * @param ResponseEvent $event
  71.      */
  72.     public function onKernelResponse(ResponseEvent $event)
  73.     {
  74.         $request $event->getRequest();
  75.         if (!$this->matchesPimcoreContext($requestPimcoreContextResolver::CONTEXT_DEFAULT)) {
  76.             return;
  77.         }
  78.         if ($request->get('disableBlockClearing')) {
  79.             return;
  80.         }
  81.         if ($this->blockStateStack->count() > 1) {
  82.             // restore parent block data by removing sub-request block state
  83.             $this->blockStateStack->pop();
  84.         }
  85.     }
  86. }