PageRenderTime 28ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

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

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