Skip to content

Commit 402909f

Browse files
committed
feature #35748 [HttpFoundation] Add support for all core response http control directives (azjezz)
This PR was merged into the 5.1-dev branch. Discussion ---------- [HttpFoundation] Add support for all core response http control directives | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | Fix #35720 | License | MIT | Doc PR | N/A Add support for all core cache-control directives see : https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control Commits ------- 011cd38 [HttpFoundation] Add support for all core http control directives
2 parents 5517fbc + 011cd38 commit 402909f

File tree

3 files changed

+44
-6
lines changed

3 files changed

+44
-6
lines changed

src/Symfony/Component/HttpFoundation/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ CHANGELOG
1515
* made the Mime component an optional dependency
1616
* added `MarshallingSessionHandler`, `IdentityMarshaller`
1717
* made `Session` accept a callback to report when the session is being used
18+
* Add support for all core cache control directives
1819

1920
5.0.0
2021
-----

src/Symfony/Component/HttpFoundation/Response.php

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,24 @@ class Response
8585
const HTTP_NOT_EXTENDED = 510; // RFC2774
8686
const HTTP_NETWORK_AUTHENTICATION_REQUIRED = 511; // RFC6585
8787

88+
/**
89+
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control
90+
*/
91+
private const HTTP_RESPONSE_CACHE_CONTROL_DIRECTIVES = [
92+
'must_revalidate' => false,
93+
'no_cache' => false,
94+
'no_store' => false,
95+
'no_transform' => false,
96+
'public' => false,
97+
'private' => false,
98+
'proxy_revalidate' => false,
99+
'max_age' => true,
100+
's_maxage' => true,
101+
'immutable' => false,
102+
'last_modified' => true,
103+
'etag' => true,
104+
];
105+
88106
/**
89107
* @var ResponseHeaderBag
90108
*/
@@ -921,7 +939,7 @@ public function setEtag(string $etag = null, bool $weak = false): object
921939
/**
922940
* Sets the response's cache headers (validation and/or expiration).
923941
*
924-
* Available options are: etag, last_modified, max_age, s_maxage, private, public and immutable.
942+
* Available options are: must_revalidate, no_cache, no_store, no_transform, public, private, proxy_revalidate, max_age, s_maxage, immutable, last_modified and etag.
925943
*
926944
* @return $this
927945
*
@@ -931,7 +949,7 @@ public function setEtag(string $etag = null, bool $weak = false): object
931949
*/
932950
public function setCache(array $options): object
933951
{
934-
if ($diff = array_diff(array_keys($options), ['etag', 'last_modified', 'max_age', 's_maxage', 'private', 'public', 'immutable'])) {
952+
if ($diff = array_diff(array_keys($options), array_keys(static::HTTP_RESPONSE_CACHE_CONTROL_DIRECTIVES))) {
935953
throw new \InvalidArgumentException(sprintf('Response does not support the following options: "%s".', implode('", "', $diff)));
936954
}
937955

@@ -951,6 +969,16 @@ public function setCache(array $options): object
951969
$this->setSharedMaxAge($options['s_maxage']);
952970
}
953971

972+
foreach (self::HTTP_RESPONSE_CACHE_CONTROL_DIRECTIVES as $directive => $hasValue) {
973+
if (!$hasValue && isset($options[$directive])) {
974+
if ($options[$directive]) {
975+
$this->headers->addCacheControlDirective(str_replace('_', '-', $directive));
976+
} else {
977+
$this->headers->removeCacheControlDirective(str_replace('_', '-', $directive));
978+
}
979+
}
980+
}
981+
954982
if (isset($options['public'])) {
955983
if ($options['public']) {
956984
$this->setPublic();
@@ -967,10 +995,6 @@ public function setCache(array $options): object
967995
}
968996
}
969997

970-
if (isset($options['immutable'])) {
971-
$this->setImmutable((bool) $options['immutable']);
972-
}
973-
974998
return $this;
975999
}
9761000

src/Symfony/Component/HttpFoundation/Tests/ResponseTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,19 @@ public function testSetCache()
659659

660660
$response->setCache(['immutable' => false]);
661661
$this->assertFalse($response->headers->hasCacheControlDirective('immutable'));
662+
663+
$directives = ['proxy_revalidate', 'must_revalidate', 'no_cache', 'no_store', 'no_transform'];
664+
foreach ($directives as $directive) {
665+
$response->setCache([$directive => true]);
666+
667+
$this->assertTrue($response->headers->hasCacheControlDirective(str_replace('_', '-', $directive)));
668+
}
669+
670+
foreach ($directives as $directive) {
671+
$response->setCache([$directive => false]);
672+
673+
$this->assertFalse($response->headers->hasCacheControlDirective(str_replace('_', '-', $directive)));
674+
}
662675
}
663676

664677
public function testSendContent()

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy