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

https://github.com/xbojer/gfw · PHP · 241 lines · 111 code · 31 blank · 99 comment · 17 complexity · 353a15e25b4ce7027e1a38ee7148a6c3 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\HttpFoundation;
  11. /**
  12. * ResponseHeaderBag is a container for Response HTTP headers.
  13. *
  14. * @author Fabien Potencier <fabien@symfony.com>
  15. *
  16. * @api
  17. */
  18. class ResponseHeaderBag extends HeaderBag
  19. {
  20. const COOKIES_FLAT = 'flat';
  21. const COOKIES_ARRAY = 'array';
  22. protected $computedCacheControl = array();
  23. protected $cookies = array();
  24. /**
  25. * Constructor.
  26. *
  27. * @param array $headers An array of HTTP headers
  28. *
  29. * @api
  30. */
  31. public function __construct(array $headers = array())
  32. {
  33. parent::__construct($headers);
  34. if (!isset($this->headers['cache-control'])) {
  35. $this->set('cache-control', '');
  36. }
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function __toString()
  42. {
  43. $cookies = '';
  44. foreach ($this->getCookies() as $cookie) {
  45. $cookies .= 'Set-Cookie: '.$cookie."\r\n";
  46. }
  47. return parent::__toString().$cookies;
  48. }
  49. /**
  50. * {@inheritdoc}
  51. *
  52. * @api
  53. */
  54. public function replace(array $headers = array())
  55. {
  56. parent::replace($headers);
  57. if (!isset($this->headers['cache-control'])) {
  58. $this->set('cache-control', '');
  59. }
  60. }
  61. /**
  62. * {@inheritdoc}
  63. *
  64. * @api
  65. */
  66. public function set($key, $values, $replace = true)
  67. {
  68. parent::set($key, $values, $replace);
  69. // ensure the cache-control header has sensible defaults
  70. if (in_array(strtr(strtolower($key), '_', '-'), array('cache-control', 'etag', 'last-modified', 'expires'))) {
  71. $computed = $this->computeCacheControlValue();
  72. $this->headers['cache-control'] = array($computed);
  73. $this->computedCacheControl = $this->parseCacheControl($computed);
  74. }
  75. }
  76. /**
  77. * {@inheritdoc}
  78. *
  79. * @api
  80. */
  81. public function remove($key)
  82. {
  83. parent::remove($key);
  84. if ('cache-control' === strtr(strtolower($key), '_', '-')) {
  85. $this->computedCacheControl = array();
  86. }
  87. }
  88. /**
  89. * {@inheritdoc}
  90. */
  91. public function hasCacheControlDirective($key)
  92. {
  93. return array_key_exists($key, $this->computedCacheControl);
  94. }
  95. /**
  96. * {@inheritdoc}
  97. */
  98. public function getCacheControlDirective($key)
  99. {
  100. return array_key_exists($key, $this->computedCacheControl) ? $this->computedCacheControl[$key] : null;
  101. }
  102. /**
  103. * Sets a cookie.
  104. *
  105. * @param Cookie $cookie
  106. *
  107. * @return void
  108. *
  109. * @api
  110. */
  111. public function setCookie(Cookie $cookie)
  112. {
  113. $this->cookies[$cookie->getDomain()][$cookie->getPath()][$cookie->getName()] = $cookie;
  114. }
  115. /**
  116. * Removes a cookie from the array, but does not unset it in the browser
  117. *
  118. * @param string $name
  119. * @param string $path
  120. * @param string $domain
  121. *
  122. * @return void
  123. *
  124. * @api
  125. */
  126. public function removeCookie($name, $path = '/', $domain = null)
  127. {
  128. if (null === $path) {
  129. $path = '/';
  130. }
  131. unset($this->cookies[$domain][$path][$name]);
  132. if (empty($this->cookies[$domain][$path])) {
  133. unset($this->cookies[$domain][$path]);
  134. if (empty($this->cookies[$domain])) {
  135. unset($this->cookies[$domain]);
  136. }
  137. }
  138. }
  139. /**
  140. * Returns an array with all cookies
  141. *
  142. * @param string $format
  143. *
  144. * @throws \InvalidArgumentException When the $format is invalid
  145. *
  146. * @return array
  147. *
  148. * @api
  149. */
  150. public function getCookies($format = self::COOKIES_FLAT)
  151. {
  152. if (!in_array($format, array(self::COOKIES_FLAT, self::COOKIES_ARRAY))) {
  153. throw new \InvalidArgumentException(sprintf('Format "%s" invalid (%s).', $format, implode(', ', array(self::COOKIES_FLAT, self::COOKIES_ARRAY))));
  154. }
  155. if (self::COOKIES_ARRAY === $format) {
  156. return $this->cookies;
  157. }
  158. $flattenedCookies = array();
  159. foreach ($this->cookies as $path) {
  160. foreach ($path as $cookies) {
  161. foreach ($cookies as $cookie) {
  162. $flattenedCookies[] = $cookie;
  163. }
  164. }
  165. }
  166. return $flattenedCookies;
  167. }
  168. /**
  169. * Clears a cookie in the browser
  170. *
  171. * @param string $name
  172. * @param string $path
  173. * @param string $domain
  174. *
  175. * @return void
  176. *
  177. * @api
  178. */
  179. public function clearCookie($name, $path = '/', $domain = null)
  180. {
  181. $this->setCookie(new Cookie($name, null, 1, $path, $domain));
  182. }
  183. /**
  184. * Returns the calculated value of the cache-control header.
  185. *
  186. * This considers several other headers and calculates or modifies the
  187. * cache-control header to a sensible, conservative value.
  188. *
  189. * @return string
  190. */
  191. protected function computeCacheControlValue()
  192. {
  193. if (!$this->cacheControl && !$this->has('ETag') && !$this->has('Last-Modified') && !$this->has('Expires')) {
  194. return 'no-cache';
  195. }
  196. if (!$this->cacheControl) {
  197. // conservative by default
  198. return 'private, must-revalidate';
  199. }
  200. $header = $this->getCacheControlHeader();
  201. if (isset($this->cacheControl['public']) || isset($this->cacheControl['private'])) {
  202. return $header;
  203. }
  204. // public if s-maxage is defined, private otherwise
  205. if (!isset($this->cacheControl['s-maxage'])) {
  206. return $header.', private';
  207. }
  208. return $header;
  209. }
  210. }