1 | <?php |
||
25 | final class CookieStorage implements StorageInterface, EventSubscriberInterface |
||
26 | { |
||
27 | /** |
||
28 | * @var ParameterBag |
||
29 | */ |
||
30 | private $requestCookies; |
||
31 | |||
32 | /** |
||
33 | * @var ParameterBag |
||
34 | */ |
||
35 | private $responseCookies; |
||
36 | |||
37 | public function __construct() |
||
38 | { |
||
39 | $this->requestCookies = new ParameterBag(); |
||
40 | $this->responseCookies = new ParameterBag(); |
||
41 | } |
||
42 | |||
43 | /** |
||
44 | * {@inheritdoc} |
||
45 | */ |
||
46 | public static function getSubscribedEvents() |
||
47 | { |
||
48 | return [ |
||
49 | KernelEvents::REQUEST => [['onKernelRequest', 1024]], |
||
50 | KernelEvents::RESPONSE => [['onKernelResponse', -1024]], |
||
51 | ]; |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * @param GetResponseEvent $event |
||
56 | */ |
||
57 | public function onKernelRequest(GetResponseEvent $event) |
||
58 | { |
||
59 | if (!$event->isMasterRequest()) { |
||
60 | return; |
||
61 | } |
||
62 | |||
63 | $this->requestCookies = new ParameterBag($event->getRequest()->cookies->all()); |
||
64 | $this->responseCookies = new ParameterBag(); |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * @param FilterResponseEvent $event |
||
69 | */ |
||
70 | public function onKernelResponse(FilterResponseEvent $event) |
||
71 | { |
||
72 | if (!$event->isMasterRequest()) { |
||
73 | return; |
||
74 | } |
||
75 | |||
76 | $response = $event->getResponse(); |
||
77 | foreach ($this->responseCookies as $name => $value) { |
||
78 | $response->headers->setCookie(new Cookie($name, $value)); |
||
79 | } |
||
80 | |||
81 | $this->requestCookies = new ParameterBag(); |
||
82 | $this->responseCookies = new ParameterBag(); |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * {@inheritdoc} |
||
87 | */ |
||
88 | public function has($name) |
||
92 | |||
93 | /** |
||
94 | * {@inheritdoc} |
||
95 | */ |
||
96 | public function get($name, $default = null) |
||
100 | |||
101 | /** |
||
102 | * {@inheritdoc} |
||
103 | */ |
||
104 | public function set($name, $value) |
||
108 | |||
109 | /** |
||
110 | * {@inheritdoc} |
||
111 | */ |
||
112 | public function remove($name) |
||
116 | |||
117 | /** |
||
118 | * {@inheritdoc} |
||
119 | */ |
||
120 | public function all() |
||
124 | } |
||
125 |