Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / vendor / symfony / http-kernel / EventListener / AbstractTestSessionListener.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\Cookie;
16 use Symfony\Component\HttpFoundation\Session\Session;
17 use Symfony\Component\HttpFoundation\Session\SessionInterface;
18 use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
19 use Symfony\Component\HttpKernel\Event\GetResponseEvent;
20 use Symfony\Component\HttpKernel\KernelEvents;
21
22 /**
23  * TestSessionListener.
24  *
25  * Saves session in test environment.
26  *
27  * @author Bulat Shakirzyanov <mallluhuct@gmail.com>
28  * @author Fabien Potencier <fabien@symfony.com>
29  */
30 abstract class AbstractTestSessionListener implements EventSubscriberInterface
31 {
32     private $sessionId;
33
34     public function onKernelRequest(GetResponseEvent $event)
35     {
36         if (!$event->isMasterRequest()) {
37             return;
38         }
39
40         // bootstrap the session
41         $session = $this->getSession();
42         if (!$session) {
43             return;
44         }
45
46         $cookies = $event->getRequest()->cookies;
47
48         if ($cookies->has($session->getName())) {
49             $this->sessionId = $cookies->get($session->getName());
50             $session->setId($this->sessionId);
51         }
52     }
53
54     /**
55      * Checks if session was initialized and saves if current request is master
56      * Runs on 'kernel.response' in test environment.
57      */
58     public function onKernelResponse(FilterResponseEvent $event)
59     {
60         if (!$event->isMasterRequest()) {
61             return;
62         }
63
64         if (!$session = $event->getRequest()->getSession()) {
65             return;
66         }
67
68         if ($wasStarted = $session->isStarted()) {
69             $session->save();
70         }
71
72         if ($session instanceof Session ? !$session->isEmpty() || (null !== $this->sessionId && $session->getId() !== $this->sessionId) : $wasStarted) {
73             $params = session_get_cookie_params();
74
75             foreach ($event->getResponse()->headers->getCookies() as $cookie) {
76                 if ($session->getName() === $cookie->getName() && $params['path'] === $cookie->getPath() && $params['domain'] == $cookie->getDomain()) {
77                     return;
78                 }
79             }
80
81             $event->getResponse()->headers->setCookie(new Cookie($session->getName(), $session->getId(), 0 === $params['lifetime'] ? 0 : time() + $params['lifetime'], $params['path'], $params['domain'], $params['secure'], $params['httponly']));
82             $this->sessionId = $session->getId();
83         }
84     }
85
86     public static function getSubscribedEvents()
87     {
88         return array(
89             KernelEvents::REQUEST => array('onKernelRequest', 192),
90             KernelEvents::RESPONSE => array('onKernelResponse', -128),
91         );
92     }
93
94     /**
95      * Gets the session object.
96      *
97      * @return SessionInterface|null A SessionInterface instance or null if no session is available
98      */
99     abstract protected function getSession();
100 }