@@ -37,6 +37,11 @@ public function set(Cookie $cookie)
37
37
/**
38
38
* Gets a cookie by name.
39
39
*
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
+ *
40
45
* @param string $name The cookie name
41
46
* @param string $path The cookie path
42
47
* @param string $domain The cookie domain
@@ -49,12 +54,27 @@ public function get($name, $path = '/', $domain = null)
49
54
{
50
55
$ this ->flushExpiredCookies ();
51
56
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 ;
53
69
}
54
70
55
71
/**
56
72
* Removes a cookie by name.
57
73
*
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
+ *
58
78
* @param string $name The cookie name
59
79
* @param string $path The cookie path
60
80
* @param string $domain The cookie domain
@@ -67,13 +87,23 @@ public function expire($name, $path = '/', $domain = null)
67
87
$ path = '/ ' ;
68
88
}
69
89
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
+ }
71
97
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 ] );
74
100
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
+ }
77
107
}
78
108
}
79
109
}
0 commit comments