-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathCookieJar.php
206 lines (176 loc) · 4.3 KB
/
CookieJar.php
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<?php
namespace Http\Message;
/**
* Cookie Jar holds a set of Cookies.
*
* @author Márk Sági-Kazár <[email protected]>
*/
final class CookieJar implements \Countable, \IteratorAggregate
{
/**
* @var \SplObjectStorage<Cookie, mixed>
*/
private $cookies;
public function __construct()
{
$this->cookies = new \SplObjectStorage();
}
/**
* Checks if there is a cookie.
*
* @return bool
*/
public function hasCookie(Cookie $cookie)
{
return $this->cookies->contains($cookie);
}
/**
* Adds a cookie.
*/
public function addCookie(Cookie $cookie)
{
if (!$this->hasCookie($cookie)) {
$cookies = $this->getMatchingCookies($cookie);
foreach ($cookies as $matchingCookie) {
if ($cookie->getValue() !== $matchingCookie->getValue() || $cookie->getMaxAge() > $matchingCookie->getMaxAge()) {
$this->removeCookie($matchingCookie);
continue;
}
}
if ($cookie->hasValue()) {
$this->cookies->attach($cookie);
}
}
}
/**
* Removes a cookie.
*/
public function removeCookie(Cookie $cookie)
{
$this->cookies->detach($cookie);
}
/**
* Returns the cookies.
*
* @return Cookie[]
*/
public function getCookies()
{
$match = function ($matchCookie) {
return true;
};
return $this->findMatchingCookies($match);
}
/**
* Returns all matching cookies.
*
* @return Cookie[]
*/
public function getMatchingCookies(Cookie $cookie)
{
$match = function ($matchCookie) use ($cookie) {
return $matchCookie->match($cookie);
};
return $this->findMatchingCookies($match);
}
/**
* Finds matching cookies based on a callable.
*
* @return Cookie[]
*/
private function findMatchingCookies(callable $match)
{
$cookies = [];
foreach ($this->cookies as $cookie) {
if ($match($cookie)) {
$cookies[] = $cookie;
}
}
return $cookies;
}
/**
* Checks if there are cookies.
*
* @return bool
*/
public function hasCookies()
{
return $this->cookies->count() > 0;
}
/**
* Sets the cookies and removes any previous one.
*
* @param Cookie[] $cookies
*/
public function setCookies(array $cookies)
{
$this->clear();
$this->addCookies($cookies);
}
/**
* Adds some cookies.
*
* @param Cookie[] $cookies
*/
public function addCookies(array $cookies)
{
foreach ($cookies as $cookie) {
$this->addCookie($cookie);
}
}
/**
* Removes some cookies.
*
* @param Cookie[] $cookies
*/
public function removeCookies(array $cookies)
{
foreach ($cookies as $cookie) {
$this->removeCookie($cookie);
}
}
/**
* Removes cookies which match the given parameters.
*
* Null means that parameter should not be matched
*
* @param string|null $name
* @param string|null $domain
* @param string|null $path
*/
public function removeMatchingCookies($name = null, $domain = null, $path = null)
{
$match = function ($cookie) use ($name, $domain, $path) {
$match = true;
if (isset($name)) {
$match = $match && ($cookie->getName() === $name);
}
if (isset($domain)) {
$match = $match && $cookie->matchDomain($domain);
}
if (isset($path)) {
$match = $match && $cookie->matchPath($path);
}
return $match;
};
$cookies = $this->findMatchingCookies($match);
$this->removeCookies($cookies);
}
/**
* Removes all cookies.
*/
public function clear()
{
$this->cookies = new \SplObjectStorage();
}
#[\ReturnTypeWillChange]
public function count()
{
return $this->cookies->count();
}
#[\ReturnTypeWillChange]
public function getIterator()
{
return clone $this->cookies;
}
}