8000 [HttpFoundation] Add support for all core http control directives · symfony/symfony@1686e68 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1686e68

Browse files
committed
[HttpFoundation] Add support for all core http control directives
1 parent 9acb060 commit 1686e68

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

src/Symfony/Component/HttpFoundation/Response.php

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,24 @@ class Response
8282
const HTTP_NOT_EXTENDED = 510; // RFC2774
8383
const HTTP_NETWORK_AUTHENTICATION_REQUIRED = 511; // RFC6585
8484

85+
const HTTP_RESPONSE_CACHE_CONTROL_DIRECTIVES = [
86+
// Core
87+
'must_revalidate',
88+
'no_cache',
89+
'no_store',
90+
'no_transform',
91+
'public',
92+
'private',
93+
'proxy_revalidate',
94+
'max_age',
95+
's_maxage',
96+
// Extensions
97+
'immutable',
98+
// Other
99+
'last_modified',
100+
'etag',
101+
];
102+
85103
/**
86104
* @var ResponseHeaderBag
87105
*/
@@ -928,7 +946,7 @@ public function setEtag(string $etag = null, bool $weak = false): object
928946
*/
929947
public function setCache(array $options): object
930948
{
931-
if ($diff = array_diff(array_keys($options), ['etag', 'last_modified', 'max_age', 's_maxage', 'private', 'public', 'immutable'])) {
949+
if ($diff = array_diff(array_keys($options), static::HTTP_RESPONSE_CACHE_CONTROL_DIRECTIVES)) {
932950
throw new \InvalidArgumentException(sprintf('Response does not support the following options: "%s".', implode('", "', $diff)));
933951
}
934952

@@ -968,6 +986,16 @@ public function setCache(array $options): object
968986
$this->setImmutable((bool) $options['immutable']);
969987
}
970988

989+
foreach (['proxy_revalidate', 'must_revalidate', 'no_cache', 'no_store', 'no_transform'] as $directive) {
990+
if (isset($options[$directive])) {
991+
if ($options[$directive]) {
992+
$this->headers->addCacheControlDirective(str_replace('_', '-', $directive));
993+
} else {
994+
$this->headers->removeCacheControlDirective(str_replace('_', '-', $directive));
995+
}
996+
}
997+
}
998+
971999
return $this;
9721000
}
9731001

0 commit comments

Comments
 (0)
0