菜谱项目

HeaderBag.php 8.5KB

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