19 class ResponseHeaderBag
extends HeaderBag
35 if (!isset($this->headers[
'cache-control'])) {
36 $this->
set(
'Cache-Control',
'');
40 if (!isset($this->headers[
'date'])) {
53 foreach ($this->
all() as $name => $value) {
54 $headers[$this->headerNames[$name] ?? $name] = $value;
63 if (isset($this->headerNames[
'set-cookie'])) {
64 unset(
$headers[$this->headerNames[
'set-cookie']]);
75 $this->headerNames = [];
79 if (!isset($this->headers[
'cache-control'])) {
80 $this->
set(
'Cache-Control',
'');
83 if (!isset($this->headers[
'date'])) {
97 if (1 <= \func_num_args() &&
null !== $key = func_get_arg(0)) {
98 $key = strtr($key, self::UPPER, self::LOWER);
100 return 'set-cookie' !== $key ?
$headers[$key] ?? [] : array_map(
'strval', $this->
getCookies());
104 $headers[
'set-cookie'][] = (string) $cookie;
113 public function set($key, $values, $replace =
true)
115 $uniqueKey = strtr($key, self::UPPER, self::LOWER);
117 if (
'set-cookie' === $uniqueKey) {
121 foreach ((array) $values as $cookie) {
124 $this->headerNames[$uniqueKey] = $key;
129 $this->headerNames[$uniqueKey] = $key;
131 parent::set($key, $values, $replace);
134 if (\in_array($uniqueKey, [
'cache-control',
'etag',
'last-modified',
'expires'],
true) &&
'' !== $computed = $this->
computeCacheControlValue()) {
135 $this->headers[
'cache-control'] = [$computed];
136 $this->headerNames[
'cache-control'] =
'Cache-Control';
144 public function remove($key)
146 $uniqueKey = strtr($key, self::UPPER, self::LOWER);
147 unset($this->headerNames[$uniqueKey]);
149 if (
'set-cookie' === $uniqueKey) {
155 parent::remove($key);
157 if (
'cache-control' === $uniqueKey) {
158 $this->computedCacheControl = [];
161 if (
'date' === $uniqueKey) {
171 return \array_key_exists($key, $this->computedCacheControl);
179 return \array_key_exists($key, $this->computedCacheControl) ? $this->computedCacheControl[$key] :
null;
185 $this->headerNames[
'set-cookie'] =
'Set-Cookie';
197 if (
null === $path) {
201 unset($this->cookies[$domain][$path][$name]);
203 if (empty($this->cookies[$domain][$path])) {
204 unset($this->cookies[$domain][$path]);
206 if (empty($this->cookies[$domain])) {
207 unset($this->cookies[$domain]);
211 if (empty($this->cookies)) {
212 unset($this->headerNames[
'set-cookie']);
227 if (!\in_array($format, [self::COOKIES_FLAT, self::COOKIES_ARRAY])) {
228 throw new \InvalidArgumentException(sprintf(
'Format "%s" invalid (%s).', $format, implode(
', ', [self::COOKIES_FLAT, self::COOKIES_ARRAY])));
231 if (self::COOKIES_ARRAY === $format) {
235 $flattenedCookies = [];
236 foreach ($this->cookies as $path) {
239 $flattenedCookies[] = $cookie;
244 return $flattenedCookies;
257 public function clearCookie($name, $path =
'/', $domain =
null, $secure =
false, $httpOnly =
true)
259 $sameSite = \func_num_args() > 5 ? func_get_arg(5) :
null;
261 $this->
setCookie(
new Cookie($name,
null, 1, $path, $domain, $secure, $httpOnly,
false, $sameSite));
282 if (!$this->cacheControl) {
283 if ($this->
has(
'Last-Modified') || $this->
has(
'Expires')) {
284 return 'private, must-revalidate';
288 return 'no-cache, private';
292 if (isset($this->cacheControl[
'public']) || isset($this->cacheControl[
'private'])) {
297 if (!isset($this->cacheControl[
's-maxage'])) {
298 return $header.
', private';
304 private function initDate(): void
306 $now = \DateTime::createFromFormat(
'U', time());
307 $now->setTimezone(
new \DateTimeZone(
'UTC'));
308 $this->
set(
'Date', $now->format(
'D, d M Y H:i:s').
' GMT');