/src/Symfony/Component/BrowserKit/CookieJar.php

https://github.com/Exercise/symfony · PHP · 204 lines · 103 code · 25 blank · 76 comment · 15 complexity · be89d7166e0844bd7fd7f5fed23850ec MD5 · raw file

  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\BrowserKit;
  11. /**
  12. * CookieJar.
  13. *
  14. * @author Fabien Potencier <fabien@symfony.com>
  15. *
  16. * @api
  17. */
  18. class CookieJar
  19. {
  20. protected $cookieJar = array();
  21. /**
  22. * Sets a cookie.
  23. *
  24. * @param Cookie $cookie A Cookie instance
  25. *
  26. * @api
  27. */
  28. public function set(Cookie $cookie)
  29. {
  30. $this->cookieJar[$cookie->getDomain()][$cookie->getPath()][$cookie->getName()] = $cookie;
  31. }
  32. /**
  33. * Gets a cookie by name.
  34. *
  35. * @param string $name The cookie name
  36. * @param string $path The cookie path
  37. * @param string $domain The cookie domain
  38. *
  39. * @return Cookie|null A Cookie instance or null if the cookie does not exist
  40. *
  41. * @api
  42. */
  43. public function get($name, $path = '/', $domain = null)
  44. {
  45. $this->flushExpiredCookies();
  46. return isset($this->cookieJar[$domain][$path][$name]) ? $this->cookieJar[$domain][$path][$name] : null;
  47. }
  48. /**
  49. * Removes a cookie by name.
  50. *
  51. * @param string $name The cookie name
  52. * @param string $path The cookie path
  53. * @param string $domain The cookie domain
  54. *
  55. * @api
  56. */
  57. public function expire($name, $path = '/', $domain = null)
  58. {
  59. if (null === $path) {
  60. $path = '/';
  61. }
  62. unset($this->cookieJar[$domain][$path][$name]);
  63. if (empty($this->cookieJar[$domain][$path])) {
  64. unset($this->cookieJar[$domain][$path]);
  65. if (empty($this->cookieJar[$domain])) {
  66. unset($this->cookieJar[$domain]);
  67. }
  68. }
  69. }
  70. /**
  71. * Removes all the cookies from the jar.
  72. *
  73. * @api
  74. */
  75. public function clear()
  76. {
  77. $this->cookieJar = array();
  78. }
  79. /**
  80. * Updates the cookie jar from a Response object.
  81. *
  82. * @param Response $response A Response object
  83. * @param string $uri The base URL
  84. */
  85. public function updateFromResponse(Response $response, $uri = null)
  86. {
  87. $cookies = array();
  88. foreach ($response->getHeader('Set-Cookie', false) as $cookie) {
  89. foreach (explode(',', $cookie) as $i => $part) {
  90. if (0 === $i || preg_match('/^(?P<token>\s*[0-9A-Za-z!#\$%\&\'\*\+\-\.^_`\|~]+)=/', $part)) {
  91. $cookies[] = ltrim($part);
  92. } else {
  93. $cookies[count($cookies) - 1] .= ','.$part;
  94. }
  95. }
  96. }
  97. foreach ($cookies as $cookie) {
  98. $this->set(Cookie::fromString($cookie, $uri));
  99. }
  100. }
  101. /**
  102. * Returns not yet expired cookies.
  103. *
  104. * @return array An array of cookies
  105. */
  106. public function all()
  107. {
  108. $this->flushExpiredCookies();
  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. return $flattenedCookies;
  118. }
  119. /**
  120. * Returns not yet expired cookie values for the given URI.
  121. *
  122. * @param string $uri A URI
  123. * @param Boolean $returnsRawValue Returns raw value or urldecoded value
  124. *
  125. * @return array An array of cookie values
  126. */
  127. public function allValues($uri, $returnsRawValue = false)
  128. {
  129. $this->flushExpiredCookies();
  130. $parts = array_replace(array('path' => '/'), parse_url($uri));
  131. $cookies = array();
  132. foreach ($this->cookieJar as $domain => $pathCookies) {
  133. if ($domain) {
  134. $domain = ltrim($domain, '.');
  135. if ($domain != substr($parts['host'], -strlen($domain))) {
  136. continue;
  137. }
  138. }
  139. foreach ($pathCookies as $path => $namedCookies) {
  140. if ($path != substr($parts['path'], 0, strlen($path))) {
  141. continue;
  142. }
  143. foreach ($namedCookies as $cookie) {
  144. if ($cookie->isSecure() && 'https' != $parts['scheme']) {
  145. continue;
  146. }
  147. $cookies[$cookie->getName()] = $returnsRawValue ? $cookie->getRawValue() : $cookie->getValue();
  148. }
  149. }
  150. }
  151. return $cookies;
  152. }
  153. /**
  154. * Returns not yet expired raw cookie values for the given URI.
  155. *
  156. * @param string $uri A URI
  157. *
  158. * @return array An array of cookie values
  159. */
  160. public function allRawValues($uri)
  161. {
  162. return $this->allValues($uri, true);
  163. }
  164. /**
  165. * Removes all expired cookies.
  166. */
  167. public function flushExpiredCookies()
  168. {
  169. foreach ($this->cookieJar as $domain => $pathCookies) {
  170. foreach ($pathCookies as $path => $namedCookies) {
  171. foreach ($namedCookies as $name => $cookie) {
  172. if ($cookie->isExpired()) {
  173. unset($this->cookieJar[$domain][$path][$name]);
  174. }
  175. }
  176. }
  177. }
  178. }
  179. }