ResponseHeaderBag.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. * @return void
  107. *
  108. * @api
  109. */
  110. public function setCookie(Cookie $cookie)
  111. {
  112. $this->cookies[$cookie->getDomain()][$cookie->getPath()][$cookie->getName()] = $cookie;
  113. }
  114. /**
  115. * Removes a cookie from the array, but does not unset it in the browser
  116. *
  117. * @param string $name
  118. * @param string $path
  119. * @param string $domain
  120. * @return void
  121. *
  122. * @api
  123. */
  124. public function removeCookie($name, $path = '/', $domain = null)
  125. {
  126. if (null === $path) {
  127. $path = '/';
  128. }
  129. unset($this->cookies[$domain][$path][$name]);
  130. if (empty($this->cookies[$domain][$path])) {
  131. unset($this->cookies[$domain][$path]);
  132. if (empty($this->cookies[$domain])) {
  133. unset($this->cookies[$domain]);
  134. }
  135. }
  136. }
  137. /**
  138. * Returns an array with all cookies
  139. *
  140. * @param string $format
  141. *
  142. * @throws \InvalidArgumentException When the $format is invalid
  143. *
  144. * @return array
  145. *
  146. * @api
  147. */
  148. public function getCookies($format = self::COOKIES_FLAT)
  149. {
  150. if (!in_array($format, array(self::COOKIES_FLAT, self::COOKIES_ARRAY))) {
  151. throw new \InvalidArgumentException(sprintf('Format "%s" invalid (%s).', $format, implode(', ', array(self::COOKIES_FLAT, self::COOKIES_ARRAY))));
  152. }
  153. if (self::COOKIES_ARRAY === $format) {
  154. return $this->cookies;
  155. }
  156. $flattenedCookies = array();
  157. foreach ($this->cookies as $path) {
  158. foreach ($path as $cookies) {
  159. foreach ($cookies as $cookie) {
  160. $flattenedCookies[] = $cookie;
  161. }
  162. }
  163. }
  164. return $flattenedCookies;
  165. }
  166. /**
  167. * Clears a cookie in the browser
  168. *
  169. * @param string $name
  170. * @param string $path
  171. * @param string $domain
  172. * @return void
  173. *
  174. * @api
  175. */
  176. public function clearCookie($name, $path = '/', $domain = null)
  177. {
  178. $this->setCookie(new Cookie($name, null, 1, $path, $domain));
  179. }
  180. /**
  181. * Returns the calculated value of the cache-control header.
  182. *
  183. * This considers several other headers and calculates or modifies the
  184. * cache-control header to a sensible, conservative value.
  185. *
  186. * @return string
  187. */
  188. protected function computeCacheControlValue()
  189. {
  190. if (!$this->cacheControl && !$this->has('ETag') && !$this->has('Last-Modified') && !$this->has('Expires')) {
  191. return 'no-cache';
  192. }
  193. if (!$this->cacheControl) {
  194. // conservative by default
  195. return 'private, must-revalidate';
  196. }
  197. $header = $this->getCacheControlHeader();
  198. if (isset($this->cacheControl['public']) || isset($this->cacheControl['private'])) {
  199. return $header;
  200. }
  201. // public if s-maxage is defined, private otherwise
  202. if (!isset($this->cacheControl['s-maxage'])) {
  203. return $header.', private';
  204. }
  205. return $header;
  206. }
  207. }