Updating Media dependent modules to versions compatible with core Media.
[yaffs-website] / vendor / symfony / http-kernel / EventListener / AbstractSessionListener.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\HttpKernel\EventListener;
13
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;
20
21 /**
22  * Sets the session in the request.
23  *
24  * @author Johannes M. Schmitt <schmittjoh@gmail.com>
25  */
26 abstract class AbstractSessionListener implements EventSubscriberInterface
27 {
28     public function onKernelRequest(GetResponseEvent $event)
29     {
30         if (!$event->isMasterRequest()) {
31             return;
32         }
33
34         $request = $event->getRequest();
35         $session = $this->getSession();
36         if (null === $session || $request->hasSession()) {
37             return;
38         }
39
40         $request->setSession($session);
41     }
42
43     public function onKernelResponse(FilterResponseEvent $event)
44     {
45         if (!$event->isMasterRequest()) {
46             return;
47         }
48
49         if (!$session = $event->getRequest()->getSession()) {
50             return;
51         }
52
53         if ($session->isStarted() || ($session instanceof Session && $session->hasBeenStarted())) {
54             $event->getResponse()
55                 ->setPrivate()
56                 ->setMaxAge(0)
57                 ->headers->addCacheControlDirective('must-revalidate');
58         }
59     }
60
61     public static function getSubscribedEvents()
62     {
63         return array(
64             KernelEvents::REQUEST => array('onKernelRequest', 128),
65             // low priority to come after regular response listeners, same as SaveSessionListener
66             KernelEvents::RESPONSE => array('onKernelResponse', -1000),
67         );
68     }
69
70     /**
71      * Gets the session object.
72      *
73      * @return SessionInterface|null A SessionInterface instance or null if no session is available
74      */
75     abstract protected function getSession();
76 }