vendor/symfony/http-kernel/EventListener/AbstractSessionListener.php line 138

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 Psr\Container\ContainerInterface;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use Symfony\Component\HttpFoundation\Session\Session;
  14. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  15. use Symfony\Component\HttpKernel\Event\FinishRequestEvent;
  16. use Symfony\Component\HttpKernel\Event\RequestEvent;
  17. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  18. use Symfony\Component\HttpKernel\Exception\UnexpectedSessionUsageException;
  19. use Symfony\Component\HttpKernel\KernelEvents;
  20. /**
  21.  * Sets the session onto the request on the "kernel.request" event and saves
  22.  * it on the "kernel.response" event.
  23.  *
  24.  * In addition, if the session has been started it overrides the Cache-Control
  25.  * header in such a way that all caching is disabled in that case.
  26.  * If you have a scenario where caching responses with session information in
  27.  * them makes sense, you can disable this behaviour by setting the header
  28.  * AbstractSessionListener::NO_AUTO_CACHE_CONTROL_HEADER on the response.
  29.  *
  30.  * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  31.  * @author Tobias Schultze <http://tobion.de>
  32.  *
  33.  * @internal
  34.  */
  35. abstract class AbstractSessionListener implements EventSubscriberInterface
  36. {
  37.     public const NO_AUTO_CACHE_CONTROL_HEADER 'Symfony-Session-NoAutoCacheControl';
  38.     protected $container;
  39.     private $sessionUsageStack = [];
  40.     private $debug;
  41.     public function __construct(ContainerInterface $container nullbool $debug false)
  42.     {
  43.         $this->container $container;
  44.         $this->debug $debug;
  45.     }
  46.     public function onKernelRequest(RequestEvent $event)
  47.     {
  48.         if (!$event->isMasterRequest()) {
  49.             return;
  50.         }
  51.         $request $event->getRequest();
  52.         if (!$request->hasSession()) {
  53.             $sess null;
  54.             $request->setSessionFactory(function () use (&$sess) { return $sess ?? $sess $this->getSession(); });
  55.         }
  56.         $session $this->container && $this->container->has('initialized_session') ? $this->container->get('initialized_session') : null;
  57.         $this->sessionUsageStack[] = $session instanceof Session $session->getUsageIndex() : 0;
  58.     }
  59.     public function onKernelResponse(ResponseEvent $event)
  60.     {
  61.         if (!$event->isMasterRequest()) {
  62.             return;
  63.         }
  64.         $response $event->getResponse();
  65.         $autoCacheControl = !$response->headers->has(self::NO_AUTO_CACHE_CONTROL_HEADER);
  66.         // Always remove the internal header if present
  67.         $response->headers->remove(self::NO_AUTO_CACHE_CONTROL_HEADER);
  68.         if (!$session $this->container && $this->container->has('initialized_session') ? $this->container->get('initialized_session') : $event->getRequest()->getSession()) {
  69.             return;
  70.         }
  71.         if ($session->isStarted()) {
  72.             /*
  73.              * Saves the session, in case it is still open, before sending the response/headers.
  74.              *
  75.              * This ensures several things in case the developer did not save the session explicitly:
  76.              *
  77.              *  * If a session save handler without locking is used, it ensures the data is available
  78.              *    on the next request, e.g. after a redirect. PHPs auto-save at script end via
  79.              *    session_register_shutdown is executed after fastcgi_finish_request. So in this case
  80.              *    the data could be missing the next request because it might not be saved the moment
  81.              *    the new request is processed.
  82.              *  * A locking save handler (e.g. the native 'files') circumvents concurrency problems like
  83.              *    the one above. But by saving the session before long-running things in the terminate event,
  84.              *    we ensure the session is not blocked longer than needed.
  85.              *  * When regenerating the session ID no locking is involved in PHPs session design. See
  86.              *    https://bugs.php.net/61470 for a discussion. So in this case, the session must
  87.              *    be saved anyway before sending the headers with the new session ID. Otherwise session
  88.              *    data could get lost again for concurrent requests with the new ID. One result could be
  89.              *    that you get logged out after just logging in.
  90.              *
  91.              * This listener should be executed as one of the last listeners, so that previous listeners
  92.              * can still operate on the open session. This prevents the overhead of restarting it.
  93.              * Listeners after closing the session can still work with the session as usual because
  94.              * Symfonys session implementation starts the session on demand. So writing to it after
  95.              * it is saved will just restart it.
  96.              */
  97.             $session->save();
  98.         }
  99.         if ($session instanceof Session $session->getUsageIndex() === end($this->sessionUsageStack) : !$session->isStarted()) {
  100.             return;
  101.         }
  102.         if ($autoCacheControl) {
  103.             $response
  104.                 ->setExpires(new \DateTime())
  105.                 ->setPrivate()
  106.                 ->setMaxAge(0)
  107.                 ->headers->addCacheControlDirective('must-revalidate');
  108.         }
  109.         if (!$event->getRequest()->attributes->get('_stateless'false)) {
  110.             return;
  111.         }
  112.         if ($this->debug) {
  113.             throw new UnexpectedSessionUsageException('Session was used while the request was declared stateless.');
  114.         }
  115.         if ($this->container->has('logger')) {
  116.             $this->container->get('logger')->warning('Session was used while the request was declared stateless.');
  117.         }
  118.     }
  119.     public function onFinishRequest(FinishRequestEvent $event)
  120.     {
  121.         if ($event->isMasterRequest()) {
  122.             array_pop($this->sessionUsageStack);
  123.         }
  124.     }
  125.     public function onSessionUsage(): void
  126.     {
  127.         if (!$this->debug) {
  128.             return;
  129.         }
  130.         if ($this->container && $this->container->has('session_collector')) {
  131.             $this->container->get('session_collector')();
  132.         }
  133.         if (!$requestStack $this->container && $this->container->has('request_stack') ? $this->container->get('request_stack') : null) {
  134.             return;
  135.         }
  136.         $stateless false;
  137.         $clonedRequestStack = clone $requestStack;
  138.         while (null !== ($request $clonedRequestStack->pop()) && !$stateless) {
  139.             $stateless $request->attributes->get('_stateless');
  140.         }
  141.         if (!$stateless) {
  142.             return;
  143.         }
  144.         if (!$session $this->container && $this->container->has('initialized_session') ? $this->container->get('initialized_session') : $requestStack->getCurrentRequest()->getSession()) {
  145.             return;
  146.         }
  147.         if ($session->isStarted()) {
  148.             $session->save();
  149.         }
  150.         throw new UnexpectedSessionUsageException('Session was used while the request was declared stateless.');
  151.     }
  152.     public static function getSubscribedEvents(): array
  153.     {
  154.         return [
  155.             KernelEvents::REQUEST => ['onKernelRequest'128],
  156.             // low priority to come after regular response listeners, but higher than StreamedResponseListener
  157.             KernelEvents::RESPONSE => ['onKernelResponse', -1000],
  158.             KernelEvents::FINISH_REQUEST => ['onFinishRequest'],
  159.         ];
  160.     }
  161.     /**
  162.      * Gets the session object.
  163.      *
  164.      * @return SessionInterface|null A SessionInterface instance or null if no session is available
  165.      */
  166.     abstract protected function getSession();
  167. }