vendor/symfony/http-foundation/ResponseHeaderBag.php line 83

  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.     public const COOKIES_FLAT 'flat';
  19.     public const COOKIES_ARRAY 'array';
  20.     public const DISPOSITION_ATTACHMENT 'attachment';
  21.     public const DISPOSITION_INLINE 'inline';
  22.     protected $computedCacheControl = [];
  23.     protected $cookies = [];
  24.     protected $headerNames = [];
  25.     public function __construct(array $headers = [])
  26.     {
  27.         parent::__construct($headers);
  28.         if (!isset($this->headers['cache-control'])) {
  29.             $this->set('Cache-Control''');
  30.         }
  31.         /* RFC2616 - 14.18 says all Responses need to have a Date */
  32.         if (!isset($this->headers['date'])) {
  33.             $this->initDate();
  34.         }
  35.     }
  36.     /**
  37.      * Returns the headers, with original capitalizations.
  38.      */
  39.     public function allPreserveCase(): array
  40.     {
  41.         $headers = [];
  42.         foreach ($this->all() as $name => $value) {
  43.             $headers[$this->headerNames[$name] ?? $name] = $value;
  44.         }
  45.         return $headers;
  46.     }
  47.     public function allPreserveCaseWithoutCookies()
  48.     {
  49.         $headers $this->allPreserveCase();
  50.         if (isset($this->headerNames['set-cookie'])) {
  51.             unset($headers[$this->headerNames['set-cookie']]);
  52.         }
  53.         return $headers;
  54.     }
  55.     public function replace(array $headers = [])
  56.     {
  57.         $this->headerNames = [];
  58.         parent::replace($headers);
  59.         if (!isset($this->headers['cache-control'])) {
  60.             $this->set('Cache-Control''');
  61.         }
  62.         if (!isset($this->headers['date'])) {
  63.             $this->initDate();
  64.         }
  65.     }
  66.     public function all(string $key null): array
  67.     {
  68.         $headers parent::all();
  69.         if (null !== $key) {
  70.             $key strtr($keyself::UPPERself::LOWER);
  71.             return 'set-cookie' !== $key $headers[$key] ?? [] : array_map('strval'$this->getCookies());
  72.         }
  73.         foreach ($this->getCookies() as $cookie) {
  74.             $headers['set-cookie'][] = (string) $cookie;
  75.         }
  76.         return $headers;
  77.     }
  78.     public function set(string $keystring|array|null $valuesbool $replace true)
  79.     {
  80.         $uniqueKey strtr($keyself::UPPERself::LOWER);
  81.         if ('set-cookie' === $uniqueKey) {
  82.             if ($replace) {
  83.                 $this->cookies = [];
  84.             }
  85.             foreach ((array) $values as $cookie) {
  86.                 $this->setCookie(Cookie::fromString($cookie));
  87.             }
  88.             $this->headerNames[$uniqueKey] = $key;
  89.             return;
  90.         }
  91.         $this->headerNames[$uniqueKey] = $key;
  92.         parent::set($key$values$replace);
  93.         // ensure the cache-control header has sensible defaults
  94.         if (\in_array($uniqueKey, ['cache-control''etag''last-modified''expires'], true) && '' !== $computed $this->computeCacheControlValue()) {
  95.             $this->headers['cache-control'] = [$computed];
  96.             $this->headerNames['cache-control'] = 'Cache-Control';
  97.             $this->computedCacheControl $this->parseCacheControl($computed);
  98.         }
  99.     }
  100.     public function remove(string $key)
  101.     {
  102.         $uniqueKey strtr($keyself::UPPERself::LOWER);
  103.         unset($this->headerNames[$uniqueKey]);
  104.         if ('set-cookie' === $uniqueKey) {
  105.             $this->cookies = [];
  106.             return;
  107.         }
  108.         parent::remove($key);
  109.         if ('cache-control' === $uniqueKey) {
  110.             $this->computedCacheControl = [];
  111.         }
  112.         if ('date' === $uniqueKey) {
  113.             $this->initDate();
  114.         }
  115.     }
  116.     public function hasCacheControlDirective(string $key): bool
  117.     {
  118.         return \array_key_exists($key$this->computedCacheControl);
  119.     }
  120.     public function getCacheControlDirective(string $key): bool|string|null
  121.     {
  122.         return $this->computedCacheControl[$key] ?? null;
  123.     }
  124.     public function setCookie(Cookie $cookie)
  125.     {
  126.         $this->cookies[$cookie->getDomain()][$cookie->getPath()][$cookie->getName()] = $cookie;
  127.         $this->headerNames['set-cookie'] = 'Set-Cookie';
  128.     }
  129.     /**
  130.      * Removes a cookie from the array, but does not unset it in the browser.
  131.      */
  132.     public function removeCookie(string $name, ?string $path '/'string $domain null)
  133.     {
  134.         $path ??= '/';
  135.         unset($this->cookies[$domain][$path][$name]);
  136.         if (empty($this->cookies[$domain][$path])) {
  137.             unset($this->cookies[$domain][$path]);
  138.             if (empty($this->cookies[$domain])) {
  139.                 unset($this->cookies[$domain]);
  140.             }
  141.         }
  142.         if (empty($this->cookies)) {
  143.             unset($this->headerNames['set-cookie']);
  144.         }
  145.     }
  146.     /**
  147.      * Returns an array with all cookies.
  148.      *
  149.      * @return Cookie[]
  150.      *
  151.      * @throws \InvalidArgumentException When the $format is invalid
  152.      */
  153.     public function getCookies(string $format self::COOKIES_FLAT): array
  154.     {
  155.         if (!\in_array($format, [self::COOKIES_FLATself::COOKIES_ARRAY])) {
  156.             throw new \InvalidArgumentException(sprintf('Format "%s" invalid (%s).'$formatimplode(', ', [self::COOKIES_FLATself::COOKIES_ARRAY])));
  157.         }
  158.         if (self::COOKIES_ARRAY === $format) {
  159.             return $this->cookies;
  160.         }
  161.         $flattenedCookies = [];
  162.         foreach ($this->cookies as $path) {
  163.             foreach ($path as $cookies) {
  164.                 foreach ($cookies as $cookie) {
  165.                     $flattenedCookies[] = $cookie;
  166.                 }
  167.             }
  168.         }
  169.         return $flattenedCookies;
  170.     }
  171.     /**
  172.      * Clears a cookie in the browser.
  173.      */
  174.     public function clearCookie(string $name, ?string $path '/'string $domain nullbool $secure falsebool $httpOnly truestring $sameSite null)
  175.     {
  176.         $this->setCookie(new Cookie($namenull1$path$domain$secure$httpOnlyfalse$sameSite));
  177.     }
  178.     /**
  179.      * @see HeaderUtils::makeDisposition()
  180.      */
  181.     public function makeDisposition(string $dispositionstring $filenamestring $filenameFallback '')
  182.     {
  183.         return HeaderUtils::makeDisposition($disposition$filename$filenameFallback);
  184.     }
  185.     /**
  186.      * Returns the calculated value of the cache-control header.
  187.      *
  188.      * This considers several other headers and calculates or modifies the
  189.      * cache-control header to a sensible, conservative value.
  190.      */
  191.     protected function computeCacheControlValue(): string
  192.     {
  193.         if (!$this->cacheControl) {
  194.             if ($this->has('Last-Modified') || $this->has('Expires')) {
  195.                 return 'private, must-revalidate'// allows for heuristic expiration (RFC 7234 Section 4.2.2) in the case of "Last-Modified"
  196.             }
  197.             // conservative by default
  198.             return 'no-cache, private';
  199.         }
  200.         $header $this->getCacheControlHeader();
  201.         if (isset($this->cacheControl['public']) || isset($this->cacheControl['private'])) {
  202.             return $header;
  203.         }
  204.         // public if s-maxage is defined, private otherwise
  205.         if (!isset($this->cacheControl['s-maxage'])) {
  206.             return $header.', private';
  207.         }
  208.         return $header;
  209.     }
  210.     private function initDate(): void
  211.     {
  212.         $this->set('Date'gmdate('D, d M Y H:i:s').' GMT');
  213.     }
  214. }