SessionTestSubscriber.php in Drupal 8
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\FilterResponseEvent;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
class SessionTestSubscriber implements EventSubscriberInterface {
protected $emptySession;
public function onKernelRequestSessionTest(GetResponseEvent $event) {
$session = $event
->getRequest()
->getSession();
$this->emptySession = (int) (!($session && $session
->start()));
}
public function onKernelResponseSessionTest(FilterResponseEvent $event) {
$response = $event
->getResponse();
$response->headers
->set('X-Session-Empty', $this->emptySession);
}
public static function getSubscribedEvents() {
$events[KernelEvents::RESPONSE][] = [
'onKernelResponseSessionTest',
];
$events[KernelEvents::REQUEST][] = [
'onKernelRequestSessionTest',
];
return $events;
}
}