PageRenderTime 59ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/symfony/src/Symfony/Component/HttpFoundation/HeaderBag.php

https://github.com/casoetan/ServerGroveLiveChat
PHP | 320 lines | 159 code | 40 blank | 121 comment | 18 complexity | 85409a7bab81905c5092eb8ce17183de MD5 | raw file
Possible License(s): LGPL-2.1, LGPL-3.0, ISC, BSD-3-Clause
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.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\HttpFoundation;
  11. /**
  12. * HeaderBag is a container for HTTP headers.
  13. *
  14. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  15. */
  16. class HeaderBag
  17. {
  18. protected $headers;
  19. protected $cookies;
  20. protected $cacheControl;
  21. /**
  22. * Constructor.
  23. *
  24. * @param array $headers An array of HTTP headers
  25. */
  26. public function __construct(array $headers = array())
  27. {
  28. $this->cacheControl = array();
  29. $this->cookies = array();
  30. $this->headers = array();
  31. foreach ($headers as $key => $values) {
  32. $this->set($key, $values);
  33. }
  34. }
  35. /**
  36. * Returns the headers.
  37. *
  38. * @return array An array of headers
  39. */
  40. public function all()
  41. {
  42. return $this->headers;
  43. }
  44. /**
  45. * Returns the parameter keys.
  46. *
  47. * @return array An array of parameter keys
  48. */
  49. public function keys()
  50. {
  51. return array_keys($this->headers);
  52. }
  53. /**
  54. * Replaces the current HTTP headers by a new set.
  55. *
  56. * @param array $headers An array of HTTP headers
  57. */
  58. public function replace(array $headers = array())
  59. {
  60. $this->headers = array();
  61. $this->add($headers);
  62. }
  63. /**
  64. * Adds new headers the current HTTP headers set.
  65. *
  66. * @param array $headers An array of HTTP headers
  67. */
  68. public function add(array $headers)
  69. {
  70. foreach ($headers as $key => $values) {
  71. $this->set($key, $values);
  72. }
  73. }
  74. /**
  75. * Returns a header value by name.
  76. *
  77. * @param string $key The header name
  78. * @param mixed $default The default value
  79. * @param Boolean $first Whether to return the first value or all header values
  80. *
  81. * @return string|array The first header value if $first is true, an array of values otherwise
  82. */
  83. public function get($key, $default = null, $first = true)
  84. {
  85. $key = strtr(strtolower($key), '_', '-');
  86. if (!array_key_exists($key, $this->headers)) {
  87. if (null === $default) {
  88. return $first ? null : array();
  89. } else {
  90. return $first ? $default : array($default);
  91. }
  92. }
  93. if ($first) {
  94. return count($this->headers[$key]) ? $this->headers[$key][0] : $default;
  95. } else {
  96. return $this->headers[$key];
  97. }
  98. }
  99. /**
  100. * Sets a header by name.
  101. *
  102. * @param string $key The key
  103. * @param string|array $values The value or an array of values
  104. * @param Boolean $replace Whether to replace the actual value of not (true by default)
  105. */
  106. public function set($key, $values, $replace = true)
  107. {
  108. $key = strtr(strtolower($key), '_', '-');
  109. if (!is_array($values)) {
  110. $values = array($values);
  111. }
  112. if (true === $replace || !isset($this->headers[$key])) {
  113. $this->headers[$key] = $values;
  114. } else {
  115. $this->headers[$key] = array_merge($this->headers[$key], $values);
  116. }
  117. if ('cache-control' === $key) {
  118. $this->cacheControl = $this->parseCacheControl($values[0]);
  119. }
  120. }
  121. /**
  122. * Returns true if the HTTP header is defined.
  123. *
  124. * @param string $key The HTTP header
  125. *
  126. * @return Boolean true if the parameter exists, false otherwise
  127. */
  128. public function has($key)
  129. {
  130. return array_key_exists(strtr(strtolower($key), '_', '-'), $this->headers);
  131. }
  132. /**
  133. * Returns true if the given HTTP header contains the given value.
  134. *
  135. * @param string $key The HTTP header name
  136. * @param string $value The HTTP value
  137. *
  138. * @return Boolean true if the value is contained in the header, false otherwise
  139. */
  140. public function contains($key, $value)
  141. {
  142. return in_array($value, $this->get($key, null, false));
  143. }
  144. /**
  145. * Removes a header.
  146. *
  147. * @param string $key The HTTP header name
  148. */
  149. public function remove($key)
  150. {
  151. $key = strtr(strtolower($key), '_', '-');
  152. unset($this->headers[$key]);
  153. if ('cache-control' === $key) {
  154. $this->cacheControl = array();
  155. }
  156. }
  157. /**
  158. * Sets a cookie.
  159. *
  160. * @param Cookie $cookie
  161. *
  162. * @throws \InvalidArgumentException When the cookie expire parameter is not valid
  163. *
  164. * @return void
  165. */
  166. public function setCookie(Cookie $cookie)
  167. {
  168. $this->cookies[$cookie->getName()] = $cookie;
  169. }
  170. /**
  171. * Removes a cookie from the array, but does not unset it in the browser
  172. *
  173. * @param string $name
  174. * @return void
  175. */
  176. public function removeCookie($name)
  177. {
  178. unset($this->cookies[$name]);
  179. }
  180. /**
  181. * Whether the array contains any cookie with this name
  182. *
  183. * @param string $name
  184. * @return Boolean
  185. */
  186. public function hasCookie($name)
  187. {
  188. return isset($this->cookies[$name]);
  189. }
  190. /**
  191. * Returns a cookie
  192. *
  193. * @param string $name
  194. * @return Cookie
  195. */
  196. public function getCookie($name)
  197. {
  198. if (!$this->hasCookie($name)) {
  199. throw new \InvalidArgumentException(sprintf('There is no cookie with name "%s".', $name));
  200. }
  201. return $this->cookies[$name];
  202. }
  203. /**
  204. * Returns an array with all cookies
  205. *
  206. * @return array
  207. */
  208. public function getCookies()
  209. {
  210. return $this->cookies;
  211. }
  212. /**
  213. * Returns the HTTP header value converted to a date.
  214. *
  215. * @param string $key The parameter key
  216. * @param \DateTime $default The default value
  217. *
  218. * @return \DateTime The filtered value
  219. */
  220. public function getDate($key, \DateTime $default = null)
  221. {
  222. if (null === $value = $this->get($key)) {
  223. return $default;
  224. }
  225. if (false === $date = \DateTime::createFromFormat(DATE_RFC2822, $value)) {
  226. throw new \RuntimeException(sprintf('The %s HTTP header is not parseable (%s).', $key, $value));
  227. }
  228. return $date;
  229. }
  230. public function addCacheControlDirective($key, $value = true)
  231. {
  232. $this->cacheControl[$key] = $value;
  233. $this->set('Cache-Control', $this->getCacheControlHeader());
  234. }
  235. public function hasCacheControlDirective($key)
  236. {
  237. return array_key_exists($key, $this->cacheControl);
  238. }
  239. public function getCacheControlDirective($key)
  240. {
  241. return array_key_exists($key, $this->cacheControl) ? $this->cacheControl[$key] : null;
  242. }
  243. public function removeCacheControlDirective($key)
  244. {
  245. unset($this->cacheControl[$key]);
  246. $this->set('Cache-Control', $this->getCacheControlHeader());
  247. }
  248. protected function getCacheControlHeader()
  249. {
  250. $parts = array();
  251. ksort($this->cacheControl);
  252. foreach ($this->cacheControl as $key => $value) {
  253. if (true === $value) {
  254. $parts[] = $key;
  255. } else {
  256. if (preg_match('#[^a-zA-Z0-9._-]#', $value)) {
  257. $value = '"'.$value.'"';
  258. }
  259. $parts[] = "$key=$value";
  260. }
  261. }
  262. return implode(', ', $parts);
  263. }
  264. /**
  265. * Parses a Cache-Control HTTP header.
  266. *
  267. * @param string $header The value of the Cache-Control HTTP header
  268. *
  269. * @return array An array representing the attribute values
  270. */
  271. protected function parseCacheControl($header)
  272. {
  273. $cacheControl = array();
  274. preg_match_all('#([a-zA-Z][a-zA-Z_-]*)\s*(?:=(?:"([^"]*)"|([^ \t",;]*)))?#', $header, $matches, PREG_SET_ORDER);
  275. foreach ($matches as $match) {
  276. $cacheControl[strtolower($match[1])] = isset($match[2]) && $match[2] ? $match[2] : (isset($match[3]) ? $match[3] : true);
  277. }
  278. return $cacheControl;
  279. }
  280. }