You are here

abstract class TestSessionListener in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/symfony/http-kernel/EventListener/TestSessionListener.php \Symfony\Component\HttpKernel\EventListener\TestSessionListener

TestSessionListener.

Saves session in test environment.

@author Bulat Shakirzyanov <mallluhuct@gmail.com> @author Fabien Potencier <fabien@symfony.com>

Hierarchy

Expanded class hierarchy of TestSessionListener

File

vendor/symfony/http-kernel/EventListener/TestSessionListener.php, line 28

Namespace

Symfony\Component\HttpKernel\EventListener
View source
abstract class TestSessionListener implements EventSubscriberInterface {
  public function onKernelRequest(GetResponseEvent $event) {
    if (!$event
      ->isMasterRequest()) {
      return;
    }

    // bootstrap the session
    $session = $this
      ->getSession();
    if (!$session) {
      return;
    }
    $cookies = $event
      ->getRequest()->cookies;
    if ($cookies
      ->has($session
      ->getName())) {
      $session
        ->setId($cookies
        ->get($session
        ->getName()));
    }
  }

  /**
   * Checks if session was initialized and saves if current request is master
   * Runs on 'kernel.response' in test environment.
   *
   * @param FilterResponseEvent $event
   */
  public function onKernelResponse(FilterResponseEvent $event) {
    if (!$event
      ->isMasterRequest()) {
      return;
    }
    $session = $event
      ->getRequest()
      ->getSession();
    if ($session && $session
      ->isStarted()) {
      $session
        ->save();
      $params = session_get_cookie_params();
      $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']));
    }
  }
  public static function getSubscribedEvents() {
    return array(
      KernelEvents::REQUEST => array(
        'onKernelRequest',
        192,
      ),
      KernelEvents::RESPONSE => array(
        'onKernelResponse',
        -128,
      ),
    );
  }

  /**
   * Gets the session object.
   *
   * @return SessionInterface|null A SessionInterface instance or null if no session is available
   */
  protected abstract function getSession();

}

Members

Namesort descending Modifiers Type Description Overrides
TestSessionListener::getSession abstract protected function Gets the session object.
TestSessionListener::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to. Overrides EventSubscriberInterface::getSubscribedEvents
TestSessionListener::onKernelRequest public function
TestSessionListener::onKernelResponse public function Checks if session was initialized and saves if current request is master Runs on 'kernel.response' in test environment.