4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\HttpKernel\EventListener;
14 use Symfony\Component\HttpFoundation\Session\Session;
15 use Symfony\Component\HttpFoundation\Session\SessionInterface;
16 use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
17 use Symfony\Component\HttpKernel\Event\GetResponseEvent;
18 use Symfony\Component\HttpKernel\KernelEvents;
19 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
22 * Sets the session in the request.
24 * @author Johannes M. Schmitt <schmittjoh@gmail.com>
26 abstract class AbstractSessionListener implements EventSubscriberInterface
28 public function onKernelRequest(GetResponseEvent $event)
30 if (!$event->isMasterRequest()) {
34 $request = $event->getRequest();
35 $session = $this->getSession();
36 if (null === $session || $request->hasSession()) {
40 $request->setSession($session);
43 public function onKernelResponse(FilterResponseEvent $event)
45 if (!$event->isMasterRequest()) {
49 if (!$session = $event->getRequest()->getSession()) {
53 if ($session->isStarted() || ($session instanceof Session && $session->hasBeenStarted())) {
57 ->headers->addCacheControlDirective('must-revalidate');
61 public static function getSubscribedEvents()
64 KernelEvents::REQUEST => array('onKernelRequest', 128),
65 // low priority to come after regular response listeners, same as SaveSessionListener
66 KernelEvents::RESPONSE => array('onKernelResponse', -1000),
71 * Gets the session object.
73 * @return SessionInterface|null A SessionInterface instance or null if no session is available
75 abstract protected function getSession();