Skip to content

Commit 25b8b84

Browse files
committed
[BrowserKit] fixed BC break done recently
1 parent 372a76a commit 25b8b84

File tree

1 file changed

+36
-6
lines changed

1 file changed

+36
-6
lines changed

src/Symfony/Component/BrowserKit/CookieJar.php

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ public function set(Cookie $cookie)
3737
/**
3838
* Gets a cookie by name.
3939
*
40+
* You should never use an empty domain, but if you do so,
41+
* this method returns the first cookie for the given name/path
42+
* (this behavior ensures a BC behavior with previous versions of
43+
* Symfony).
44+
*
4045
* @param string $name The cookie name
4146
* @param string $path The cookie path
4247
* @param string $domain The cookie domain
@@ -49,12 +54,27 @@ public function get($name, $path = '/', $domain = null)
4954
{
5055
$this->flushExpiredCookies();
5156

52-
return isset($this->cookieJar[$domain][$path][$name]) ? $this->cookieJar[$domain][$path][$name] : null;
57+
if (!empty($domain)) {
58+
return isset($this->cookieJar[$domain][$path][$name]) ? $this->cookieJar[$domain][$path][$name] : null;
59+
}
60+
61+
// avoid relying on this behavior that is mainly here for BC reasons
62+
foreach ($this->cookieJar as $domain => $cookies) {
63+
if (isset($cookies[$path][$name])) {
64+
return $cookies[$path][$name];
65+
}
66+
}
67+
68+
return null;
5369
}
5470

5571
/**
5672
* Removes a cookie by name.
5773
*
74+
* You should never use an empty domain, but if you do so,
75+
* all cookies for the given name/path expire (this behavior
76+
* ensures a BC behavior with previous versions of Symfony).
77+
*
5878
* @param string $name The cookie name
5979
* @param string $path The cookie path
6080
* @param string $domain The cookie domain
@@ -67,13 +87,23 @@ public function expire($name, $path = '/', $domain = null)
6787
$path = '/';
6888
}
6989

70-
unset($this->cookieJar[$domain][$path][$name]);
90+
if (empty($domain)) {
91+
// an empty domain means any domain
92+
// this should never happen but it allows for a better BC
93+
$domains = array_keys($this->cookieJar);
94+
} else {
95+
$domains = array($domain);
96+
}
7197

72-
if (empty($this->cookieJar[$domain][$path])) {
73-
unset($this->cookieJar[$domain][$path]);
98+
foreach ($domains as $domain) {
99+
unset($this->cookieJar[$domain][$path][$name]);
74100

75-
if (empty($this->cookieJar[$domain])) {
76-
unset($this->cookieJar[$domain]);
101+
if (empty($this->cookieJar[$domain][$path])) {
102+
unset($this->cookieJar[$domain][$path]);
103+
104+
if (empty($this->cookieJar[$domain])) {
105+
unset($this->cookieJar[$domain]);
106+
}
77107
}
78108
}
79109
}

0 commit comments

Comments
 (0)