vendor/symfony/http-kernel/EventListener/AbstractSessionListener.php line47

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\HttpKernel\EventListener;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use Symfony\Component\HttpFoundation\Session\Session;
  13. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  14. use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
  15. use Symfony\Component\HttpKernel\Event\FinishRequestEvent;
  16. use Symfony\Component\HttpKernel\Event\GetResponseEvent;
  17. use Symfony\Component\HttpKernel\KernelEvents;
  18. /**
  19.  * Sets the session in the request.
  20.  *
  21.  * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  22.  */
  23. abstract class AbstractSessionListener implements EventSubscriberInterface
  24. {
  25.     private $sessionUsageStack = [];
  26.     public function onKernelRequest(GetResponseEvent $event)
  27.     {
  28.         if (!$event->isMasterRequest()) {
  29.             return;
  30.         }
  31.         $request $event->getRequest();
  32.         $session $this->getSession();
  33.         $this->sessionUsageStack[] = $session instanceof Session $session->getUsageIndex() : null;
  34.         if (null === $session || $request->hasSession()) {
  35.             return;
  36.         }
  37.         $request->setSession($session);
  38.     }
  39.     public function onKernelResponse(FilterResponseEvent $event)
  40.     {
  41.         if (!$event->isMasterRequest()) {
  42.             return;
  43.         }
  44.         if (!$session $event->getRequest()->getSession()) {
  45.             return;
  46.         }
  47.         if ($session instanceof Session $session->getUsageIndex() !== end($this->sessionUsageStack) : $session->isStarted()) {
  48.             $event->getResponse()
  49.                 ->setPrivate()
  50.                 ->setMaxAge(0)
  51.                 ->headers->addCacheControlDirective('must-revalidate');
  52.         }
  53.     }
  54.     /**
  55.      * @internal
  56.      */
  57.     public function onFinishRequest(FinishRequestEvent $event)
  58.     {
  59.         if ($event->isMasterRequest()) {
  60.             array_pop($this->sessionUsageStack);
  61.         }
  62.     }
  63.     public static function getSubscribedEvents()
  64.     {
  65.         return [
  66.             KernelEvents::REQUEST => ['onKernelRequest'128],
  67.             // low priority to come after regular response listeners, same as SaveSessionListener
  68.             KernelEvents::RESPONSE => ['onKernelResponse', -1000],
  69.             KernelEvents::FINISH_REQUEST => ['onFinishRequest'],
  70.         ];
  71.     }
  72.     /**
  73.      * Gets the session object.
  74.      *
  75.      * @return SessionInterface|null A SessionInterface instance or null if no session is available
  76.      */
  77.     abstract protected function getSession();
  78. }