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

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