SessionTestSubscriber.php in Drupal 10
File
core/modules/system/tests/modules/session_test/src/EventSubscriber/SessionTestSubscriber.php
View source
<?php
namespace Drupal\session_test\EventSubscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\Event\RequestEvent;
class SessionTestSubscriber implements EventSubscriberInterface {
protected $emptySession;
public function onKernelRequestSessionTest(RequestEvent $event) {
$session = $event
->getRequest()
->getSession();
$this->emptySession = (int) (!($session && $session
->start()));
}
public function onKernelResponseSessionTest(ResponseEvent $event) {
$response = $event
->getResponse();
$response->headers
->set('X-Session-Empty', $this->emptySession);
}
public static function getSubscribedEvents() : array {
$events[KernelEvents::RESPONSE][] = [
'onKernelResponseSessionTest',
];
$events[KernelEvents::REQUEST][] = [
'onKernelRequestSessionTest',
];
return $events;
}
}