[ Index ]

PHP Cross Reference of phpBB-3.3.14-deutsch

title

Body

[close]

/vendor/symfony/http-kernel/EventListener/ -> AbstractSessionListener.php (source)

   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\Component\HttpKernel\EventListener;
  13  
  14  use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15  use Symfony\Component\HttpFoundation\Session\Session;
  16  use Symfony\Component\HttpFoundation\Session\SessionInterface;
  17  use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
  18  use Symfony\Component\HttpKernel\Event\FinishRequestEvent;
  19  use Symfony\Component\HttpKernel\Event\GetResponseEvent;
  20  use Symfony\Component\HttpKernel\KernelEvents;
  21  
  22  /**
  23   * Sets the session in the request.
  24   *
  25   * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  26   */
  27  abstract class AbstractSessionListener implements EventSubscriberInterface
  28  {
  29      private $sessionUsageStack = [];
  30  
  31      public function onKernelRequest(GetResponseEvent $event)
  32      {
  33          if (!$event->isMasterRequest()) {
  34              return;
  35          }
  36  
  37          $request = $event->getRequest();
  38          $session = $this->getSession();
  39          $this->sessionUsageStack[] = $session instanceof Session ? $session->getUsageIndex() : null;
  40          if (null === $session || $request->hasSession()) {
  41              return;
  42          }
  43  
  44          $request->setSession($session);
  45      }
  46  
  47      public function onKernelResponse(FilterResponseEvent $event)
  48      {
  49          if (!$event->isMasterRequest()) {
  50              return;
  51          }
  52  
  53          if (!$session = $event->getRequest()->getSession()) {
  54              return;
  55          }
  56  
  57          if ($session instanceof Session ? $session->getUsageIndex() !== end($this->sessionUsageStack) : $session->isStarted()) {
  58              $event->getResponse()
  59                  ->setExpires(new \DateTime())
  60                  ->setPrivate()
  61                  ->setMaxAge(0)
  62                  ->headers->addCacheControlDirective('must-revalidate');
  63          }
  64      }
  65  
  66      /**
  67       * @internal
  68       */
  69      public function onFinishRequest(FinishRequestEvent $event)
  70      {
  71          if ($event->isMasterRequest()) {
  72              array_pop($this->sessionUsageStack);
  73          }
  74      }
  75  
  76      public static function getSubscribedEvents()
  77      {
  78          return [
  79              KernelEvents::REQUEST => ['onKernelRequest', 128],
  80              // low priority to come after regular response listeners, same as SaveSessionListener
  81              KernelEvents::RESPONSE => ['onKernelResponse', -1000],
  82              KernelEvents::FINISH_REQUEST => ['onFinishRequest'],
  83          ];
  84      }
  85  
  86      /**
  87       * Gets the session object.
  88       *
  89       * @return SessionInterface|null A SessionInterface instance or null if no session is available
  90       */
  91      abstract protected function getSession();
  92  }


Generated: Mon Nov 25 19:05:08 2024 Cross-referenced by PHPXref 0.7.1