[ Index ]

PHP Cross Reference of phpBB-3.3.11-deutsch

title

Body

[close]

/vendor/symfony/http-kernel/EventListener/ -> AbstractTestSessionListener.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\Cookie;
  16  use Symfony\Component\HttpFoundation\Session\Session;
  17  use Symfony\Component\HttpFoundation\Session\SessionInterface;
  18  use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
  19  use Symfony\Component\HttpKernel\Event\GetResponseEvent;
  20  use Symfony\Component\HttpKernel\KernelEvents;
  21  
  22  /**
  23   * TestSessionListener.
  24   *
  25   * Saves session in test environment.
  26   *
  27   * @author Bulat Shakirzyanov <mallluhuct@gmail.com>
  28   * @author Fabien Potencier <fabien@symfony.com>
  29   */
  30  abstract class AbstractTestSessionListener implements EventSubscriberInterface
  31  {
  32      private $sessionId;
  33  
  34      public function onKernelRequest(GetResponseEvent $event)
  35      {
  36          if (!$event->isMasterRequest()) {
  37              return;
  38          }
  39  
  40          // bootstrap the session
  41          $session = $this->getSession();
  42          if (!$session) {
  43              return;
  44          }
  45  
  46          $cookies = $event->getRequest()->cookies;
  47  
  48          if ($cookies->has($session->getName())) {
  49              $this->sessionId = $cookies->get($session->getName());
  50              $session->setId($this->sessionId);
  51          }
  52      }
  53  
  54      /**
  55       * Checks if session was initialized and saves if current request is master
  56       * Runs on 'kernel.response' in test environment.
  57       */
  58      public function onKernelResponse(FilterResponseEvent $event)
  59      {
  60          if (!$event->isMasterRequest()) {
  61              return;
  62          }
  63  
  64          if (!$session = $event->getRequest()->getSession()) {
  65              return;
  66          }
  67  
  68          if ($wasStarted = $session->isStarted()) {
  69              $session->save();
  70          }
  71  
  72          if ($session instanceof Session ? !$session->isEmpty() || (null !== $this->sessionId && $session->getId() !== $this->sessionId) : $wasStarted) {
  73              $params = session_get_cookie_params();
  74  
  75              foreach ($event->getResponse()->headers->getCookies() as $cookie) {
  76                  if ($session->getName() === $cookie->getName() && $params['path'] === $cookie->getPath() && $params['domain'] == $cookie->getDomain()) {
  77                      return;
  78                  }
  79              }
  80  
  81              $event->getResponse()->headers->setCookie(new Cookie($session->getName(), $session->getId(), 0 === $params['lifetime'] ? 0 : time() + $params['lifetime'], $params['path'], $params['domain'], $params['secure'], $params['httponly']));
  82              $this->sessionId = $session->getId();
  83          }
  84      }
  85  
  86      public static function getSubscribedEvents()
  87      {
  88          return [
  89              KernelEvents::REQUEST => ['onKernelRequest', 192],
  90              KernelEvents::RESPONSE => ['onKernelResponse', -128],
  91          ];
  92      }
  93  
  94      /**
  95       * Gets the session object.
  96       *
  97       * @return SessionInterface|null A SessionInterface instance or null if no session is available
  98       */
  99      abstract protected function getSession();
 100  }


Generated: Sat Nov 4 14:26:03 2023 Cross-referenced by PHPXref 0.7.1