class SessionTestSubscriber

Same name in other branches
  1. 9 core/modules/system/tests/modules/session_test/src/EventSubscriber/SessionTestSubscriber.php \Drupal\session_test\EventSubscriber\SessionTestSubscriber
  2. 8.9.x core/modules/system/tests/modules/session_test/src/EventSubscriber/SessionTestSubscriber.php \Drupal\session_test\EventSubscriber\SessionTestSubscriber
  3. 10 core/modules/system/tests/modules/session_test/src/EventSubscriber/SessionTestSubscriber.php \Drupal\session_test\EventSubscriber\SessionTestSubscriber

Defines a test session subscriber that checks whether the session is empty.

Hierarchy

  • class \Drupal\session_test\EventSubscriber\SessionTestSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of SessionTestSubscriber

1 string reference to 'SessionTestSubscriber'
session_test.services.yml in core/modules/system/tests/modules/session_test/session_test.services.yml
core/modules/system/tests/modules/session_test/session_test.services.yml
1 service uses SessionTestSubscriber
session_test.subscriber in core/modules/system/tests/modules/session_test/session_test.services.yml
Drupal\session_test\EventSubscriber\SessionTestSubscriber

File

core/modules/system/tests/modules/session_test/src/EventSubscriber/SessionTestSubscriber.php, line 15

Namespace

Drupal\session_test\EventSubscriber
View source
class SessionTestSubscriber implements EventSubscriberInterface {
    
    /**
     * Stores whether the session is empty at the beginning of the request.
     *
     * @var bool
     */
    protected $emptySession;
    
    /**
     * Set header for session testing.
     *
     * @param \Symfony\Component\HttpKernel\Event\RequestEvent $event
     *   The Event to process.
     */
    public function onKernelRequestSessionTest(RequestEvent $event) {
        $session = $event->getRequest()
            ->getSession();
        $this->emptySession = !($session && $session->start());
    }
    
    /**
     * Performs tasks for session_test module on kernel.response.
     *
     * @param \Symfony\Component\HttpKernel\Event\ResponseEvent $event
     *   The Event to process.
     */
    public function onKernelResponseSessionTest(ResponseEvent $event) {
        // Set header for session testing.
        $response = $event->getResponse();
        $response->headers
            ->set('X-Session-Empty', $this->emptySession ? '1' : '0');
    }
    
    /**
     * Registers the methods in this class that should be listeners.
     *
     * @return array
     *   An array of event listener definitions.
     */
    public static function getSubscribedEvents() : array {
        $events[KernelEvents::RESPONSE][] = [
            'onKernelResponseSessionTest',
        ];
        $events[KernelEvents::REQUEST][] = [
            'onKernelRequestSessionTest',
        ];
        return $events;
    }

}

Members

Title Sort descending Modifiers Object type Summary
SessionTestSubscriber::$emptySession protected property Stores whether the session is empty at the beginning of the request.
SessionTestSubscriber::getSubscribedEvents public static function Registers the methods in this class that should be listeners.
SessionTestSubscriber::onKernelRequestSessionTest public function Set header for session testing.
SessionTestSubscriber::onKernelResponseSessionTest public function Performs tasks for session_test module on kernel.response.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.