-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCookiesHub.php
More file actions
140 lines (123 loc) · 3.65 KB
/
CookiesHub.php
File metadata and controls
140 lines (123 loc) · 3.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php
declare(strict_types=1);
namespace Lit\Middleware\Cookie;
use Dflydev\FigCookies\Cookies;
use Dflydev\FigCookies\SetCookie;
use Dflydev\FigCookies\SetCookies;
use Lit\Nimo\Middlewares\AbstractMiddleware;
use Psr\Http\Message\ResponseInterface;
class CookiesHub extends AbstractMiddleware
{
/**
* @var Cookies
*/
protected $requestCookies;
/**
* @var SetCookies
*/
protected $responseCookies;
/**
* @return Cookies
*/
public function getRequestCookies()
{
return $this->requestCookies;
}
/**
* @return SetCookies
*/
public function getResponseCookies()
{
return $this->responseCookies;
}
public function setResponseCookies(
array $cookies,
$domain = null,
$path = null,
$expires = null,
$maxAge = null,
$httpOnly = null,
$secure = null
) {
foreach ($cookies as $key => $value) {
$this->setResponseCookie($key, $value, $domain, $path, $expires, $maxAge, $httpOnly, $secure);
}
return $this;
}
public function getRequestCookie($name, $default = null)
{
$cookie = $this->requestCookies->get($name);
return $cookie ? $cookie->getValue() : $default;
}
/**
* @param string $name
* @param mixed $value
* @param ?string $domain
* @param ?string $path
* @param ?string $expires
* @param ?string $maxAge
* @param ?bool $httpOnly
* @param ?bool $secure
*/
public function setResponseCookie(
string $name,
$value,
$domain = null,
$path = null,
$expires = null,
$maxAge = null,
$httpOnly = null,
$secure = null
) {
if (!$value instanceof SetCookie) {
if (is_array($value)) {
$value = self::arrayToSetCookie($name, $value);
} elseif (count($args = func_get_args()) > 2) {
$value = self::arrayToSetCookie($name, get_defined_vars());
} elseif (is_string($value)) {
$value = SetCookie::create($name, $value);
}
}
$this->responseCookies = $this->responseCookies->with($value);
}
public function unsetResponseCookie($name)
{
$this->responseCookies = $this->responseCookies->without($name);
return $this;
}
protected function main(): ResponseInterface
{
$this->attachToRequest();
$this->requestCookies = Cookies::fromRequest($this->request);
$this->responseCookies = new SetCookies();
$response = $this->delegate();
$cookies = SetCookies::fromResponse($response);
foreach ($this->responseCookies->getAll() as $setCookie) {
$cookies = $cookies->with($setCookie);
}
return $cookies->renderIntoSetCookieHeader($response);
}
protected static function arrayToSetCookie(string $name, array $arr): SetCookie
{
$cookie = SetCookie::create($name, $arr['value']);
if (isset($arr['domain'])) {
$cookie = $cookie->withDomain($arr['domain']);
}
if (isset($arr['expires'])) {
$cookie = $cookie->withExpires($arr['expires']);
}
if (isset($arr['httpOnly'])) {
$cookie = $cookie->withHttpOnly($arr['httpOnly']);
}
if (isset($arr['maxAge'])) {
$cookie = $cookie->withMaxAge($arr['maxAge']);
}
if (isset($arr['path'])) {
$cookie = $cookie->withPath($arr['path']);
}
if (isset($arr['secure'])) {
$cookie = $cookie->withSecure($arr['secure']);
}
return $cookie;
}
}