ResponseHeaderBag.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. protected $computedCacheControl = array();
  19. /**
  20. * Constructor.
  21. *
  22. * @param array $headers An array of HTTP headers
  23. */
  24. public function __construct(array $headers = array())
  25. {
  26. parent::__construct($headers);
  27. if (!isset($this->headers['cache-control'])) {
  28. $this->set('cache-control', '');
  29. }
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function __toString()
  35. {
  36. $cookies = '';
  37. foreach ($this->cookies as $cookie) {
  38. $cookies .= 'Set-Cookie: '.$cookie."\r\n";
  39. }
  40. return parent::__toString().$cookies;
  41. }
  42. /**
  43. * {@inheritdoc}
  44. */
  45. public function replace(array $headers = array())
  46. {
  47. parent::replace($headers);
  48. if (!isset($this->headers['cache-control'])) {
  49. $this->set('cache-control', '');
  50. }
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. public function set($key, $values, $replace = true)
  56. {
  57. parent::set($key, $values, $replace);
  58. // ensure the cache-control header has sensible defaults
  59. if (in_array(strtr(strtolower($key), '_', '-'), array('cache-control', 'etag', 'last-modified', 'expires'))) {
  60. $computed = $this->computeCacheControlValue();
  61. $this->headers['cache-control'] = array($computed);
  62. $this->computedCacheControl = $this->parseCacheControl($computed);
  63. }
  64. }
  65. /**
  66. * {@inheritdoc}
  67. */
  68. public function remove($key)
  69. {
  70. parent::remove($key);
  71. if ('cache-control' === strtr(strtolower($key), '_', '-')) {
  72. $this->computedCacheControl = array();
  73. }
  74. }
  75. /**
  76. * {@inheritdoc}
  77. */
  78. public function hasCacheControlDirective($key)
  79. {
  80. return array_key_exists($key, $this->computedCacheControl);
  81. }
  82. /**
  83. * {@inheritdoc}
  84. */
  85. public function getCacheControlDirective($key)
  86. {
  87. return array_key_exists($key, $this->computedCacheControl) ? $this->computedCacheControl[$key] : null;
  88. }
  89. /**
  90. * Clears a cookie in the browser
  91. *
  92. * @param string $name
  93. * @param string $path
  94. * @param string $domain
  95. * @return void
  96. */
  97. public function clearCookie($name, $path = null, $domain = null)
  98. {
  99. $this->setCookie(new Cookie($name, null, 1, $path, $domain));
  100. }
  101. /**
  102. * Returns the calculated value of the cache-control header.
  103. *
  104. * This considers several other headers and calculates or modifies the
  105. * cache-control header to a sensible, conservative value.
  106. *
  107. * @return string
  108. */
  109. protected function computeCacheControlValue()
  110. {
  111. if (!$this->cacheControl && !$this->has('ETag') && !$this->has('Last-Modified') && !$this->has('Expires')) {
  112. return 'no-cache';
  113. }
  114. if (!$this->cacheControl) {
  115. // conservative by default
  116. return 'private, must-revalidate';
  117. }
  118. $header = $this->getCacheControlHeader();
  119. if (isset($this->cacheControl['public']) || isset($this->cacheControl['private'])) {
  120. return $header;
  121. }
  122. // public if s-maxage is defined, private otherwise
  123. if (!isset($this->cacheControl['s-maxage'])) {
  124. return $header.', private';
  125. }
  126. return $header;
  127. }
  128. }