4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\BrowserKit;
17 * @author Fabien Potencier <fabien@symfony.com>
21 protected $cookieJar = array();
26 * @param Cookie $cookie A Cookie instance
28 public function set(Cookie $cookie)
30 $this->cookieJar[$cookie->getDomain()][$cookie->getPath()][$cookie->getName()] = $cookie;
34 * Gets a cookie by name.
36 * You should never use an empty domain, but if you do so,
37 * this method returns the first cookie for the given name/path
38 * (this behavior ensures a BC behavior with previous versions of
41 * @param string $name The cookie name
42 * @param string $path The cookie path
43 * @param string $domain The cookie domain
45 * @return Cookie|null A Cookie instance or null if the cookie does not exist
47 public function get($name, $path = '/', $domain = null)
49 $this->flushExpiredCookies();
51 if (!empty($domain)) {
52 foreach ($this->cookieJar as $cookieDomain => $pathCookies) {
54 $cookieDomain = '.'.ltrim($cookieDomain, '.');
55 if ($cookieDomain != substr('.'.$domain, -strlen($cookieDomain))) {
60 foreach ($pathCookies as $cookiePath => $namedCookies) {
61 if ($cookiePath != substr($path, 0, strlen($cookiePath))) {
64 if (isset($namedCookies[$name])) {
65 return $namedCookies[$name];
73 // avoid relying on this behavior that is mainly here for BC reasons
74 foreach ($this->cookieJar as $cookies) {
75 if (isset($cookies[$path][$name])) {
76 return $cookies[$path][$name];
82 * Removes a cookie by name.
84 * You should never use an empty domain, but if you do so,
85 * all cookies for the given name/path expire (this behavior
86 * ensures a BC behavior with previous versions of Symfony).
88 * @param string $name The cookie name
89 * @param string $path The cookie path
90 * @param string $domain The cookie domain
92 public function expire($name, $path = '/', $domain = null)
99 // an empty domain means any domain
100 // this should never happen but it allows for a better BC
101 $domains = array_keys($this->cookieJar);
103 $domains = array($domain);
106 foreach ($domains as $domain) {
107 unset($this->cookieJar[$domain][$path][$name]);
109 if (empty($this->cookieJar[$domain][$path])) {
110 unset($this->cookieJar[$domain][$path]);
112 if (empty($this->cookieJar[$domain])) {
113 unset($this->cookieJar[$domain]);
120 * Removes all the cookies from the jar.
122 public function clear()
124 $this->cookieJar = array();
128 * Updates the cookie jar from a response Set-Cookie headers.
130 * @param array $setCookies Set-Cookie headers from an HTTP response
131 * @param string $uri The base URL
133 public function updateFromSetCookie(array $setCookies, $uri = null)
137 foreach ($setCookies as $cookie) {
138 foreach (explode(',', $cookie) as $i => $part) {
139 if (0 === $i || preg_match('/^(?P<token>\s*[0-9A-Za-z!#\$%\&\'\*\+\-\.^_`\|~]+)=/', $part)) {
140 $cookies[] = ltrim($part);
142 $cookies[count($cookies) - 1] .= ','.$part;
147 foreach ($cookies as $cookie) {
149 $this->set(Cookie::fromString($cookie, $uri));
150 } catch (\InvalidArgumentException $e) {
151 // invalid cookies are just ignored
157 * Updates the cookie jar from a Response object.
159 * @param Response $response A Response object
160 * @param string $uri The base URL
162 public function updateFromResponse(Response $response, $uri = null)
164 $this->updateFromSetCookie($response->getHeader('Set-Cookie', false), $uri);
168 * Returns not yet expired cookies.
170 * @return Cookie[] An array of cookies
172 public function all()
174 $this->flushExpiredCookies();
176 $flattenedCookies = array();
177 foreach ($this->cookieJar as $path) {
178 foreach ($path as $cookies) {
179 foreach ($cookies as $cookie) {
180 $flattenedCookies[] = $cookie;
185 return $flattenedCookies;
189 * Returns not yet expired cookie values for the given URI.
191 * @param string $uri A URI
192 * @param bool $returnsRawValue Returns raw value or urldecoded value
194 * @return array An array of cookie values
196 public function allValues($uri, $returnsRawValue = false)
198 $this->flushExpiredCookies();
200 $parts = array_replace(array('path' => '/'), parse_url($uri));
202 foreach ($this->cookieJar as $domain => $pathCookies) {
204 $domain = '.'.ltrim($domain, '.');
205 if ($domain != substr('.'.$parts['host'], -strlen($domain))) {
210 foreach ($pathCookies as $path => $namedCookies) {
211 if ($path != substr($parts['path'], 0, strlen($path))) {
215 foreach ($namedCookies as $cookie) {
216 if ($cookie->isSecure() && 'https' != $parts['scheme']) {
220 $cookies[$cookie->getName()] = $returnsRawValue ? $cookie->getRawValue() : $cookie->getValue();
229 * Returns not yet expired raw cookie values for the given URI.
231 * @param string $uri A URI
233 * @return array An array of cookie values
235 public function allRawValues($uri)
237 return $this->allValues($uri, true);
241 * Removes all expired cookies.
243 public function flushExpiredCookies()
245 foreach ($this->cookieJar as $domain => $pathCookies) {
246 foreach ($pathCookies as $path => $namedCookies) {
247 foreach ($namedCookies as $name => $cookie) {
248 if ($cookie->isExpired()) {
249 unset($this->cookieJar[$domain][$path][$name]);