41187d4125d979b108f023bd00d795a44120836f
[yaffs-website] / 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\EventDispatcher\EventSubscriberInterface;
15 use Symfony\Component\HttpFoundation\Session\Session;
16 use Symfony\Component\HttpFoundation\Session\SessionInterface;
17 use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
18 use Symfony\Component\HttpKernel\Event\FinishRequestEvent;
19 use Symfony\Component\HttpKernel\Event\GetResponseEvent;
20 use Symfony\Component\HttpKernel\KernelEvents;
21
22 /**
23  * Sets the session in the request.
24  *
25  * @author Johannes M. Schmitt <schmittjoh@gmail.com>
26  */
27 abstract class AbstractSessionListener implements EventSubscriberInterface
28 {
29     private $sessionUsageStack = array();
30
31     public function onKernelRequest(GetResponseEvent $event)
32     {
33         if (!$event->isMasterRequest()) {
34             return;
35         }
36
37         $request = $event->getRequest();
38         $session = $this->getSession();
39         $this->sessionUsageStack[] = $session instanceof Session ? $session->getUsageIndex() : null;
40         if (null === $session || $request->hasSession()) {
41             return;
42         }
43
44         $request->setSession($session);
45     }
46
47     public function onKernelResponse(FilterResponseEvent $event)
48     {
49         if (!$event->isMasterRequest()) {
50             return;
51         }
52
53         if (!$session = $event->getRequest()->getSession()) {
54             return;
55         }
56
57         if ($session instanceof Session ? $session->getUsageIndex() !== end($this->sessionUsageStack) : $session->isStarted()) {
58             $event->getResponse()
59                 ->setPrivate()
60                 ->setMaxAge(0)
61                 ->headers->addCacheControlDirective('must-revalidate');
62         }
63     }
64
65     /**
66      * @internal
67      */
68     public function onFinishRequest(FinishRequestEvent $event)
69     {
70         if ($event->isMasterRequest()) {
71             array_pop($this->sessionUsageStack);
72         }
73     }
74
75     public static function getSubscribedEvents()
76     {
77         return array(
78             KernelEvents::REQUEST => array('onKernelRequest', 128),
79             // low priority to come after regular response listeners, same as SaveSessionListener
80             KernelEvents::RESPONSE => array('onKernelResponse', -1000),
81             KernelEvents::FINISH_REQUEST => array('onFinishRequest'),
82         );
83     }
84
85     /**
86      * Gets the session object.
87      *
88      * @return SessionInterface|null A SessionInterface instance or null if no session is available
89      */
90     abstract protected function getSession();
91 }