ResponseHeaderBag.php 5.5 KB

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