1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Spiral\Cookies; |
6
|
|
|
|
7
|
|
|
use Psr\Container\ContainerInterface; |
8
|
|
|
use Psr\Container\NotFoundExceptionInterface; |
9
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
10
|
|
|
use Spiral\Core\Attribute\Proxy; |
11
|
|
|
use Spiral\Core\Attribute\Scope; |
12
|
|
|
use Spiral\Core\Attribute\Singleton; |
13
|
|
|
use Spiral\Core\Exception\ScopeException; |
14
|
|
|
use Spiral\Framework\Spiral; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Cookies manages provides the ability to write and read cookies from the active request/response scope. |
18
|
|
|
*/ |
19
|
|
|
#[Singleton] |
20
|
|
|
#[Scope(Spiral::HttpRequest)] |
21
|
|
|
final class CookieManager |
22
|
|
|
{ |
23
|
8 |
|
public function __construct( |
24
|
|
|
#[Proxy] private readonly ContainerInterface $container, |
25
|
|
|
) { |
26
|
8 |
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @throws ScopeException |
30
|
|
|
*/ |
31
|
2 |
|
public function has(string $name): bool |
32
|
|
|
{ |
33
|
2 |
|
return \array_key_exists($name, $this->getRequest()->getCookieParams()); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @throws ScopeException |
38
|
|
|
*/ |
39
|
2 |
|
public function get(string $name, mixed $default = null): mixed |
40
|
|
|
{ |
41
|
2 |
|
return $this->getRequest()->getCookieParams()[$name] ?? $default; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Get all cookies. |
46
|
|
|
*/ |
47
|
1 |
|
public function getAll(): array |
48
|
|
|
{ |
49
|
1 |
|
return $this->getRequest()->getCookieParams(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Schedule new cookie. Cookie will be send while dispatching request. |
54
|
|
|
* |
55
|
|
|
* Domain, path, and secure values can be left in null state, in this case cookie manager will |
56
|
|
|
* populate them automatically. |
57
|
|
|
* |
58
|
|
|
* @link http://php.net/manual/en/function.setcookie.php |
59
|
|
|
* |
60
|
|
|
* @param string $name The name of the cookie. |
61
|
|
|
* @param string|null $value The value of the cookie. This value is stored on the clients |
62
|
|
|
* computer; do not store sensitive information. |
63
|
|
|
* @param int|null $lifetime Cookie lifetime. This value specified in seconds and declares period |
64
|
|
|
* of time in which cookie will expire relatively to current time() |
65
|
|
|
* value. |
66
|
|
|
* @param string|null $path The path on the server in which the cookie will be available on. |
67
|
|
|
* If set to '/', the cookie will be available within the entire |
68
|
|
|
* domain. |
69
|
|
|
* If set to '/foo/', the cookie will only be available within the |
70
|
|
|
* /foo/ |
71
|
|
|
* directory and all sub-directories such as /foo/bar/ of domain. The |
72
|
|
|
* default value is the current directory that the cookie is being set |
73
|
|
|
* in. |
74
|
|
|
* @param string|null $domain The domain that the cookie is available. To make the cookie |
75
|
|
|
* available |
76
|
|
|
* on all subdomains of example.com then you'd set it to |
77
|
|
|
* '.example.com'. |
78
|
|
|
* The . is not required but makes it compatible with more browsers. |
79
|
|
|
* Setting it to www.example.com will make the cookie only available in |
80
|
|
|
* the www subdomain. Refer to tail matching in the spec for details. |
81
|
|
|
* @param bool $secure Indicates that the cookie should only be transmitted over a secure |
82
|
|
|
* HTTPS connection from the client. When set to true, the cookie will |
83
|
|
|
* only be set if a secure connection exists. On the server-side, it's |
84
|
|
|
* on the programmer to send this kind of cookie only on secure |
85
|
|
|
* connection (e.g. with respect to $_SERVER["HTTPS"]). |
86
|
|
|
* @param bool $httpOnly When true the cookie will be made accessible only through the HTTP |
87
|
|
|
* protocol. This means that the cookie won't be accessible by |
88
|
|
|
* scripting |
89
|
|
|
* languages, such as JavaScript. This setting can effectively help to |
90
|
|
|
* reduce identity theft through XSS attacks (although it is not |
91
|
|
|
* supported by all browsers). |
92
|
|
|
* @param string|null $sameSite The value of the samesite element should be either None, Lax or Strict. If any of |
93
|
|
|
* the allowed options are not given, their default values are the same as the default |
94
|
|
|
* values of the explicit parameters. If the samesite element is omitted, no SameSite |
95
|
|
|
* cookie attribute is set. When Same-Site attribute is set to "None" it is required |
96
|
|
|
* to have "Secure" attribute enable. Otherwise it will be converted to "Lax". |
97
|
|
|
* @return $this |
98
|
|
|
* |
99
|
|
|
*/ |
100
|
1 |
|
public function set( |
101
|
|
|
string $name, |
102
|
|
|
?string $value = null, |
103
|
|
|
?int $lifetime = null, |
104
|
|
|
?string $path = null, |
105
|
|
|
?string $domain = null, |
106
|
|
|
?bool $secure = null, |
107
|
|
|
bool $httpOnly = true, |
108
|
|
|
?string $sameSite = null |
109
|
|
|
): self { |
110
|
1 |
|
$this->getCookieQueue()->set($name, $value, $lifetime, $path, $domain, $secure, $httpOnly, $sameSite); |
111
|
|
|
|
112
|
1 |
|
return $this; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Schedule new cookie instance to be send while dispatching request. |
117
|
|
|
* |
118
|
|
|
* @return $this |
119
|
|
|
*/ |
120
|
1 |
|
public function schedule(Cookie $cookie): self |
121
|
|
|
{ |
122
|
1 |
|
$this->getCookieQueue()->schedule($cookie); |
123
|
|
|
|
124
|
1 |
|
return $this; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Schedule cookie removal. |
129
|
|
|
* |
130
|
|
|
* @throws ScopeException |
131
|
|
|
*/ |
132
|
1 |
|
public function delete(string $name): void |
133
|
|
|
{ |
134
|
1 |
|
$this->getCookieQueue()->delete($name); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Cookies has to be send (specified via global scope). |
139
|
|
|
* |
140
|
|
|
* @return Cookie[] |
141
|
|
|
* |
142
|
|
|
* @throws ScopeException |
143
|
|
|
*/ |
144
|
1 |
|
public function getScheduled(): array |
145
|
|
|
{ |
146
|
1 |
|
return $this->getCookieQueue()->getScheduled(); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @throws ScopeException |
151
|
|
|
*/ |
152
|
5 |
|
private function getRequest(): ServerRequestInterface |
153
|
|
|
{ |
154
|
|
|
try { |
155
|
5 |
|
return $this->container->get(ServerRequestInterface::class); |
156
|
|
|
} catch (NotFoundExceptionInterface $e) { |
157
|
|
|
throw new ScopeException('Unable to receive active request', $e->getCode(), $e); |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @throws ScopeException |
163
|
|
|
*/ |
164
|
3 |
|
private function getCookieQueue(): CookieQueue |
165
|
|
|
{ |
166
|
|
|
try { |
167
|
3 |
|
return $this->container->get(CookieQueue::class); |
168
|
|
|
} catch (NotFoundExceptionInterface $e) { |
169
|
|
|
throw new ScopeException('Unable to receive cookie queue, invalid request scope', $e->getCode(), $e); |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|