<?php
/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/
namespace Pimcore\Bundle\CoreBundle\EventListener\Frontend;
use Pimcore\Bundle\CoreBundle\EventListener\Traits\PimcoreContextAwareTrait;
use Pimcore\Document\Editable\Block\BlockStateStack;
use Pimcore\Http\Request\Resolver\PimcoreContextResolver;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
/**
* Handles block state for sub requests (saves parent state and restores it after request completes)
*
* @internal
*/
class BlockStateListener implements EventSubscriberInterface, LoggerAwareInterface
{
use LoggerAwareTrait;
use PimcoreContextAwareTrait;
/**
* @param BlockStateStack $blockStateStack
*/
public function __construct(protected BlockStateStack $blockStateStack)
{
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents(): array
{
return [
KernelEvents::REQUEST => 'onKernelRequest',
KernelEvents::RESPONSE => 'onKernelResponse',
];
}
/**
* @param RequestEvent $event
*/
public function onKernelRequest(RequestEvent $event)
{
$request = $event->getRequest();
if (!$this->matchesPimcoreContext($request, PimcoreContextResolver::CONTEXT_DEFAULT)) {
return;
}
if ($request->get('disableBlockClearing')) {
return;
}
// main request already has a state on the stack
if ($event->isMainRequest()) {
return;
}
// this is for $this->action() in templates when they are inside a block element
// adds a new, empty block state to the stack which is used in the sub-request
$this->blockStateStack->push();
}
/**
* @param ResponseEvent $event
*/
public function onKernelResponse(ResponseEvent $event)
{
$request = $event->getRequest();
if (!$this->matchesPimcoreContext($request, PimcoreContextResolver::CONTEXT_DEFAULT)) {
return;
}
if ($request->get('disableBlockClearing')) {
return;
}
if ($this->blockStateStack->count() > 1) {
// restore parent block data by removing sub-request block state
$this->blockStateStack->pop();
}
}
}