HeaderBag.php 7.6 KB

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