|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Bundle\FrameworkBundle\EventListener; |
| 13 | + |
| 14 | +use Symfony\Component\Console\ConsoleEvents; |
| 15 | +use Symfony\Component\Console\Event\ConsoleErrorEvent; |
| 16 | +use Symfony\Component\Console\Exception\CommandNotFoundException; |
| 17 | +use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
| 18 | +use Symfony\Component\HttpFoundation\Session\SessionInterface; |
| 19 | +use Symfony\Component\HttpFoundation\Session\DisableableSessionProxy; |
| 20 | +use Symfony\Component\HttpKernel\Event\ControllerEvent; |
| 21 | +use Symfony\Component\HttpKernel\KernelEvents; |
| 22 | + |
| 23 | +/** |
| 24 | + * Disable session if stateless mode was specified |
| 25 | + * |
| 26 | + * @author Mathias Arlaud <mathias.arlaud@gmail.com> |
| 27 | + * |
| 28 | + * @internal |
| 29 | + */ |
| 30 | +final class DisableSessionSubscriber implements EventSubscriberInterface |
| 31 | +{ |
| 32 | + private $session; |
| 33 | + |
| 34 | + public function __construct(SessionInterface $session) |
| 35 | + { |
| 36 | + $this->session = $session; |
| 37 | + } |
| 38 | + |
| 39 | + public function onController(ControllerEvent $event): void |
| 40 | + { |
| 41 | + if ($this->session instanceof DisableableSessionProxy) { |
| 42 | + if ($event->getRequest()->attributes->get('_stateless', false)) { |
| 43 | + $this->session->disable(); |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + public static function getSubscribedEvents(): array |
| 49 | + { |
| 50 | + return [ |
| 51 | + KernelEvents::CONTROLLER => ['onController', 2048], |
| 52 | + ]; |
| 53 | + } |
| 54 | +} |
0 commit comments