1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Sylius package. |
5
|
|
|
* |
6
|
|
|
* (c) Paweł Jędrzejewski |
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 Sylius\Bundle\ResourceBundle\Storage; |
13
|
|
|
|
14
|
|
|
use Sylius\Component\Resource\Storage\StorageInterface; |
15
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
16
|
|
|
use Symfony\Component\HttpFoundation\Cookie; |
17
|
|
|
use Symfony\Component\HttpFoundation\ParameterBag; |
18
|
|
|
use Symfony\Component\HttpKernel\Event\FilterResponseEvent; |
19
|
|
|
use Symfony\Component\HttpKernel\Event\GetResponseEvent; |
20
|
|
|
use Symfony\Component\HttpKernel\KernelEvents; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @author Kamil Kokot <[email protected]> |
24
|
|
|
*/ |
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) |
89
|
|
|
{ |
90
|
|
|
return !in_array($this->get($name), ['', null], true); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* {@inheritdoc} |
95
|
|
|
*/ |
96
|
|
|
public function get($name, $default = null) |
97
|
|
|
{ |
98
|
|
|
return $this->responseCookies->get($name, $this->requestCookies->get($name, $default)); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* {@inheritdoc} |
103
|
|
|
*/ |
104
|
|
|
public function set($name, $value) |
105
|
|
|
{ |
106
|
|
|
$this->responseCookies->set($name, $value); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* {@inheritdoc} |
111
|
|
|
*/ |
112
|
|
|
public function remove($name) |
113
|
|
|
{ |
114
|
|
|
$this->set($name, null); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* {@inheritdoc} |
119
|
|
|
*/ |
120
|
|
|
public function all() |
121
|
|
|
{ |
122
|
|
|
return array_merge($this->responseCookies->all(), $this->requestCookies->all()); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|