10000 [BrowserKit] first attempt at fixing CookieJar · proofek/symfony@6b9583e · GitHub
[go: up one dir, main page]

Skip to content

Commit 6b9583e

Browse files
committed
[BrowserKit] first attempt at fixing CookieJar
1 parent 83b24c5 commit 6b9583e

File tree

1 file changed

+48
-20
lines changed

1 file changed

+48
-20
lines changed

src/Symfony/Component/BrowserKit/CookieJar.php

Lines changed: 48 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -31,35 +31,47 @@ class CookieJar
3131
*/
3232
public function set(Cookie $cookie)
3333
{
34-
$this->cookieJar[$cookie->getName()] = $cookie;
34+
$this->cookieJar[$cookie->getDomain()][$cookie->getPath()][$cookie->getName()] = $cookie;
3535
}
3636

3737
/**
3838
* Gets a cookie by name.
3939
*
4040
* @param string $name The cookie name
41+
* @param string $path
42+
* @param string $domain
4143
*
4244
* @return Cookie|null A Cookie instance or null if the cookie does not exist
4345
*
4446
* @api
4547
*/
46-
public function get($name)
48+
public function get($name, $path = '/', $domain = '')
4749
{
4850
$this->flushExpiredCookies();
4951

50-
return isset($this->cookieJar[$name]) ? $this->cookieJar[$name] : null;
52+
return isset($this->cookieJar[$domain][$path][$name]) ? $this->cookieJar[$domain][$path][$name] : null;
5153
}
5254

5355
/**
5456
* Removes a cookie by name.
5557
*
5658
* @param string $name The cookie name
59+
* @param string $path
60+
* @param string $domain
5761
*
5862
* @api
5963
*/
60-
public function expire($name)
64+
public function expire($name, $path = '/', $domain = '')
6165
{
62-
unset($this->cookieJar[$name]);
66+
unset($this->cookieJar[$domain][$path][$name]);
67+
68+
if (empty($this->cookieJar[$domain][$path])) {
69+
unset($this->cookieJar[$domain][$path]);
70+
71+
if (empty($this->cookieJar[$domain])) {
72+
unset($this->cookieJar[$domain]);
73+
}
74+
}
6375
}
6476

6577
/**
@@ -94,7 +106,16 @@ public function all()
94106
{
95107
$this->flushExpiredCookies();
96108

97-
return $this->cookieJar;
109+
$flattenedCookies = array();
110+
foreach ($this->cookieJar as $path) {
111+
foreach ($path as $cookies) {
112+
foreach ($cookies as $cookie) {
113+
$flattenedCookies[] = $cookie;
114+
}
115+
}
116+
}
117+
118+
return $flattenedCookies;
98119
}
99120

100121
/**
@@ -112,23 +133,27 @@ public function allValues($uri, $returnsRawValue = false)
112133
$parts = array_replace(array('path' => '/'), parse_url($uri));
113134

114135
$cookies = array();
115-
foreach ($this->cookieJar as $cookie) {
116-
if ($cookie->getDomain()) {
117-
$domain = ltrim($cookie->getDomain(), '.');
136+
foreach ($this->cookieJar as $domain => $allCookies) {
137+
if ($domain) {
138+
$domain = ltrim($domain, '.');
118139
if ($domain != substr($parts['host'], -strlen($domain))) {
119140
continue;
120141
}
121142
}
122143

123-
if ($cookie->getPath() != substr($parts['path'], 0, strlen($cookie->getPath()))) {
124-
continue;
125-
}
144+
foreach ($allCookies as $path => $cookies) {
145+
if ($path != substr($parts['path'], 0, strlen($path))) {
146+
continue;
147+
}
126148

127-
if ($cookie->isSecure() && 'https' != $parts['scheme']) {
128-
continue;
129-
}
149+
foreach ($cookies as $name => $cookie) {
150+
if ($cookie->isSecure() && 'https' != $parts['scheme']) {
151+
continue;
152+
}
130153

131-
$cookies[$cookie->getName()] = $returnsRawValue ? $cookie->getRawValue() : $cookie->getValue();
154+
$cookies[$cookie->getName()] = $returnsRawValue ? $cookie->getRawValue() : $cookie->getValue();
155+
}
156+
}
132157
}
133158

134159
return $cookies;
@@ -151,10 +176,13 @@ public function allRawValues($uri)
151176
*/
152177
public function flushExpiredCookies()
153178
{
154-
$cookies = $this->cookieJar;
155-
foreach ($cookies as $name => $cookie) {
156-
if ($cookie->isExpired()) {
157-
unset($this->cookieJar[$name]);
179+
foreach ($this->cookieJar as $domain => $allCookies) {
180+
foreach ($allCookies as $path => $cookies) {
181+
foreach ($cookies as $name => $cookie) {
182+
if ($cookie->isExpired()) {
183+
unset($this->cookieJar[$domain][$path][$name]);
184+
}
185+
}
158186
}
159187
}
160188
}

0 commit comments

Comments
 (0)
0