PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/casoetan/ServerGroveLiveChat
PHP | 136 lines | 63 code | 19 blank | 54 comment | 11 complexity | c4f2ffe09c6c22c5128c7a7cd3667411 MD5 | raw file
Possible License(s): LGPL-2.1, LGPL-3.0, ISC, BSD-3-Clause
  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. * Constructor.
  21. *
  22. * @param array $headers An array of HTTP headers
  23. */
  24. public function __construct(array $parameters = array())
  25. {
  26. // this line is not necessary, but including it avoids any stupid
  27. // errors if we add code to the parent's constructor
  28. parent::__construct();
  29. $this->replace($parameters);
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function replace(array $headers = array())
  35. {
  36. parent::replace($headers);
  37. if (!isset($this->headers['cache-control'])) {
  38. $this->set('cache-control', '');
  39. }
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. public function set($key, $values, $replace = true)
  45. {
  46. parent::set($key, $values, $replace);
  47. // ensure the cache-control header has sensible defaults
  48. if ('cache-control' === strtr(strtolower($key), '_', '-')) {
  49. $computed = $this->computeCacheControlValue();
  50. $this->headers['cache-control'] = array($computed);
  51. $this->computedCacheControl = $this->parseCacheControl($computed);
  52. }
  53. }
  54. /**
  55. * {@inheritdoc}
  56. */
  57. public function remove($key)
  58. {
  59. parent::remove($key);
  60. if ('cache-control' === strtr(strtolower($key), '_', '-')) {
  61. $this->computedCacheControl = array();
  62. }
  63. }
  64. /**
  65. * {@inheritdoc}
  66. */
  67. public function hasCacheControlDirective($key)
  68. {
  69. return array_key_exists($key, $this->computedCacheControl);
  70. }
  71. /**
  72. * {@inheritdoc}
  73. */
  74. public function getCacheControlDirective($key)
  75. {
  76. return array_key_exists($key, $this->computedCacheControl) ? $this->computedCacheControl[$key] : null;
  77. }
  78. /**
  79. * Clears a cookie in the browser
  80. *
  81. * @param string $name
  82. * @param string $path
  83. * @param string $domain
  84. * @return void
  85. */
  86. public function clearCookie($name, $path = null, $domain = null)
  87. {
  88. $this->setCookie(new Cookie($name, null, time() - 86400, $path, $domain));
  89. }
  90. /**
  91. * Returns the calculated value of the cache-control header.
  92. *
  93. * This considers several other headers and calculates or modifies the
  94. * cache-control header to a sensible, conservative value.
  95. *
  96. * @return string
  97. */
  98. protected function computeCacheControlValue()
  99. {
  100. if (!$this->cacheControl && !$this->has('ETag') && !$this->has('Last-Modified') && !$this->has('Expires')) {
  101. return 'no-cache';
  102. }
  103. if (!$this->cacheControl) {
  104. // conservative by default
  105. return 'private, max-age=0, must-revalidate';
  106. }
  107. $header = $this->getCacheControlHeader();
  108. if (isset($this->cacheControl['public']) || isset($this->cacheControl['private'])) {
  109. return $header;
  110. }
  111. // public if s-maxage is defined, private otherwise
  112. if (!isset($this->cacheControl['s-maxage'])) {
  113. return $header.', private';
  114. }
  115. return $header;
  116. }
  117. }