1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Http\Message\CookieJar; |
4
|
|
|
|
5
|
|
|
use Http\Message\Cookie; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Common implementation of a Cookie Jar. |
9
|
|
|
* |
10
|
|
|
* @author Márk Sági-Kazár <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
trait CookieJarTemplate |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var \SplObjectStorage |
16
|
|
|
*/ |
17
|
|
|
protected $cookies; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* {@inheritdoc} |
21
|
|
|
*/ |
22
|
12 |
|
public function hasCookie(Cookie $cookie) |
23
|
|
|
{ |
24
|
12 |
|
return $this->cookies->contains($cookie); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* {@inheritdoc} |
29
|
|
|
*/ |
30
|
12 |
|
public function addCookie(Cookie $cookie) |
31
|
|
|
{ |
32
|
12 |
|
if (!$this->hasCookie($cookie)) { |
33
|
12 |
|
$cookies = $this->getMatchingCookies($cookie); |
34
|
|
|
|
35
|
12 |
|
foreach ($cookies as $matchingCookie) { |
36
|
3 |
|
if ($cookie->getValue() !== $matchingCookie->getValue() || $cookie->getExpires() > $matchingCookie->getExpires()) { |
37
|
3 |
|
$this->removeCookie($matchingCookie); |
38
|
|
|
|
39
|
3 |
|
continue; |
40
|
|
|
} |
41
|
12 |
|
} |
42
|
|
|
|
43
|
12 |
|
if ($cookie->hasValue()) { |
44
|
12 |
|
$this->cookies->attach($cookie); |
45
|
12 |
|
} |
46
|
12 |
|
} |
47
|
12 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritdoc} |
51
|
|
|
*/ |
52
|
6 |
|
public function removeCookie(Cookie $cookie) |
53
|
|
|
{ |
54
|
6 |
|
$this->cookies->detach($cookie); |
55
|
6 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritdoc} |
59
|
|
|
*/ |
60
|
1 |
|
public function getCookies() |
61
|
|
|
{ |
62
|
|
|
$match = function ($matchCookie) { |
|
|
|
|
63
|
1 |
|
return true; |
64
|
1 |
|
}; |
65
|
|
|
|
66
|
1 |
|
return $this->findMatchingCookies($match); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritdoc} |
71
|
|
|
*/ |
72
|
12 |
|
public function getMatchingCookies(Cookie $cookie) |
73
|
|
|
{ |
74
|
|
|
$match = function ($matchCookie) use ($cookie) { |
75
|
9 |
|
return $matchCookie->match($cookie); |
76
|
12 |
|
}; |
77
|
|
|
|
78
|
12 |
|
return $this->findMatchingCookies($match); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Finds matching cookies based on a callable. |
83
|
|
|
* |
84
|
|
|
* @param callable $match |
85
|
|
|
* |
86
|
|
|
* @return Cookie[] |
87
|
|
|
*/ |
88
|
12 |
|
protected function findMatchingCookies(callable $match) |
89
|
|
|
{ |
90
|
12 |
|
$cookies = []; |
91
|
|
|
|
92
|
12 |
|
foreach ($this->cookies as $cookie) { |
93
|
9 |
|
if ($match($cookie)) { |
94
|
6 |
|
$cookies[] = $cookie; |
95
|
6 |
|
} |
96
|
12 |
|
} |
97
|
|
|
|
98
|
12 |
|
return $cookies; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* {@inheritdoc} |
103
|
|
|
*/ |
104
|
6 |
|
public function hasCookies() |
105
|
|
|
{ |
106
|
6 |
|
return $this->cookies->count() > 0; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* {@inheritdoc} |
111
|
|
|
*/ |
112
|
1 |
|
public function setCookies(array $cookies) |
113
|
|
|
{ |
114
|
1 |
|
$this->clear(); |
115
|
1 |
|
$this->addCookies($cookies); |
116
|
1 |
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* {@inheritdoc} |
120
|
|
|
*/ |
121
|
5 |
|
public function addCookies(array $cookies) |
122
|
|
|
{ |
123
|
5 |
|
foreach ($cookies as $cookie) { |
124
|
5 |
|
$this->addCookie($cookie); |
125
|
5 |
|
} |
126
|
5 |
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* {@inheritdoc} |
130
|
|
|
*/ |
131
|
2 |
|
public function removeCookies(array $cookies) |
132
|
|
|
{ |
133
|
2 |
|
foreach ($cookies as $cookie) { |
134
|
2 |
|
$this->removeCookie($cookie); |
135
|
2 |
|
} |
136
|
2 |
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* {@inheritdoc} |
140
|
|
|
*/ |
141
|
|
|
public function removeMatchingCookies($name = null, $domain = null, $path = null) |
142
|
|
|
{ |
143
|
1 |
|
$match = function ($cookie) use ($name, $domain, $path) { |
144
|
1 |
|
$match = true; |
145
|
|
|
|
146
|
1 |
|
if (isset($name)) { |
147
|
1 |
|
$match = $match && ($cookie->getName() === $name); |
148
|
1 |
|
} |
149
|
|
|
|
150
|
1 |
|
if (isset($domain)) { |
151
|
1 |
|
$match = $match && $cookie->matchDomain($domain); |
152
|
1 |
|
} |
153
|
|
|
|
154
|
1 |
|
if (isset($path)) { |
155
|
1 |
|
$match = $match && $cookie->matchPath($path); |
156
|
1 |
|
} |
157
|
|
|
|
158
|
1 |
|
return $match; |
159
|
1 |
|
}; |
160
|
|
|
|
161
|
1 |
|
$cookies = $this->findMatchingCookies($match); |
162
|
|
|
|
163
|
1 |
|
$this->removeCookies($cookies); |
164
|
1 |
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* {@inheritdoc} |
168
|
|
|
*/ |
169
|
2 |
|
public function clear() |
170
|
|
|
{ |
171
|
2 |
|
$this->cookies = new \SplObjectStorage(); |
172
|
2 |
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* {@inheritdoc} |
176
|
|
|
*/ |
177
|
5 |
|
public function count() |
178
|
|
|
{ |
179
|
5 |
|
return $this->cookies->count(); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* {@inheritdoc} |
184
|
|
|
*/ |
185
|
1 |
|
public function getIterator() |
186
|
|
|
{ |
187
|
1 |
|
return clone $this->cookies; |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.