ResponseHeaderBag.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.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.potencier@symfony-project.com>
  15. */
  16. class ResponseHeaderBag extends HeaderBag
  17. {
  18. protected $computedCacheControl = array();
  19. /**
  20. * {@inheritdoc}
  21. */
  22. public function replace(array $headers = array())
  23. {
  24. parent::replace($headers);
  25. if (!isset($this->headers['cache-control'])) {
  26. $this->set('cache-control', '');
  27. }
  28. }
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public function set($key, $values, $replace = true)
  33. {
  34. parent::set($key, $values, $replace);
  35. // ensure the cache-control header has sensible defaults
  36. if ('cache-control' === strtr(strtolower($key), '_', '-')) {
  37. $computed = $this->computeCacheControlValue();
  38. $this->headers['cache-control'] = array($computed);
  39. $this->computedCacheControl = $this->parseCacheControl($computed);
  40. }
  41. }
  42. /**
  43. * {@inheritdoc}
  44. */
  45. public function remove($key)
  46. {
  47. parent::remove($key);
  48. if ('cache-control' === strtr(strtolower($key), '_', '-')) {
  49. $this->computedCacheControl = array();
  50. }
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. public function hasCacheControlDirective($key)
  56. {
  57. return array_key_exists($key, $this->computedCacheControl);
  58. }
  59. /**
  60. * {@inheritdoc}
  61. */
  62. public function getCacheControlDirective($key)
  63. {
  64. return array_key_exists($key, $this->computedCacheControl) ? $this->computedCacheControl[$key] : null;
  65. }
  66. /**
  67. * Clears a cookie in the browser
  68. *
  69. * @param string $name
  70. * @param string $path
  71. * @param string $domain
  72. * @return void
  73. */
  74. public function clearCookie($name, $path = null, $domain = null)
  75. {
  76. $this->setCookie(new Cookie($name, null, time() - 86400, $path, $domain));
  77. }
  78. /**
  79. * Returns the calculated value of the cache-control header.
  80. *
  81. * This considers several other headers and calculates or modifies the
  82. * cache-control header to a sensible, conservative value.
  83. *
  84. * @return string
  85. */
  86. protected function computeCacheControlValue()
  87. {
  88. if (!$this->cacheControl && !$this->has('ETag') && !$this->has('Last-Modified') && !$this->has('Expires')) {
  89. return 'no-cache';
  90. }
  91. if (!$this->cacheControl) {
  92. // conservative by default
  93. return 'private, max-age=0, must-revalidate';
  94. }
  95. $header = $this->getCacheControlHeader();
  96. if (isset($this->cacheControl['public']) || isset($this->cacheControl['private'])) {
  97. return $header;
  98. }
  99. // public if s-maxage is defined, private otherwise
  100. if (!isset($this->cacheControl['s-maxage'])) {
  101. return $header.', private';
  102. }
  103. return $header;
  104. }
  105. }