@@ -31,35 +31,47 @@ class CookieJar
31
31
*/
32
32
public function set (Cookie $ cookie )
33
33
{
34
- $ this ->cookieJar [$ cookie ->getName ()] = $ cookie ;
34
+ $ this ->cookieJar [$ cookie ->getDomain ()][ $ cookie -> getPath ()][ $ cookie -> getName ()] = $ cookie ;
35
35
}
36
36
37
37
/**
38
38
* Gets a cookie by name.
39
39
*
40
40
* @param string $name The cookie name
41
+ * @param string $path
42
+ * @param string $domain
41
43
*
42
44
* @return Cookie|null A Cookie instance or null if the cookie does not exist
43
45
*
44
46
* @api
45
47
*/
46
- public function get ($ name )
48
+ public function get ($ name, $ path = ' / ' , $ domain = '' )
47
49
{
48
50
$ this ->flushExpiredCookies ();
49
51
50
- return isset ($ this ->cookieJar [$ name ]) ? $ this ->cookieJar [$ name ] : null ;
52
+ return isset ($ this ->cookieJar [$ domain ][ $ path ][ $ name ]) ? $ this ->cookieJar [ $ domain ][ $ path ] [$ name ] : null ;
51
53
}
52
54
53
55
/**
54
56
* Removes a cookie by name.
55
57
*
56
58
* @param string $name The cookie name
59
+ * @param string $path
60
+ * @param string $domain
57
61
*
58
62
* @api
59
63
*/
60
- public function expire ($ name )
64
+ public function expire ($ name, $ path = ' / ' , $ domain = '' )
61
65
{
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
+ }
63
75
}
64
76
65
77
/**
@@ -94,7 +106,16 @@ public function all()
94
106
{
95
107
$ this ->flushExpiredCookies ();
96
108
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 ;
98
119
}
99
120
100
121
/**
@@ -112,23 +133,27 @@ public function allValues($uri, $returnsRawValue = false)
112
133
$ parts = array_replace (array ('path ' => '/ ' ), parse_url ($ uri ));
113
134
114
135
$ 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 , '. ' );
118
139
if ($ domain != substr ($ parts ['host ' ], -strlen ($ domain ))) {
119
140
continue ;
120
141
}
121
142
}
122
143
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
+ }
126
148
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
+ }
130
153
131
- $ cookies [$ cookie ->getName ()] = $ returnsRawValue ? $ cookie ->getRawValue () : $ cookie ->getValue ();
154
+ $ cookies [$ cookie ->getName ()] = $ returnsRawValue ? $ cookie ->getRawValue () : $ cookie ->getValue ();
155
+ }
156
+ }
132
157
}
133
158
134
159
return $ cookies ;
@@ -151,10 +176,13 @@ public function allRawValues($uri)
151
176
*/
152
177
public function flushExpiredCookies ()
153
178
{
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
+ }
158
186
}
159
187
}
160
188
}
0 commit comments