[HttpFoundation] Add support for all core http control directives
This commit is contained in:
parent
2130465899
commit
011cd38974
@ -15,6 +15,7 @@ CHANGELOG
|
|||||||
* made the Mime component an optional dependency
|
* made the Mime component an optional dependency
|
||||||
* added `MarshallingSessionHandler`, `IdentityMarshaller`
|
* added `MarshallingSessionHandler`, `IdentityMarshaller`
|
||||||
* made `Session` accept a callback to report when the session is being used
|
* made `Session` accept a callback to report when the session is being used
|
||||||
|
* Add support for all core cache control directives
|
||||||
|
|
||||||
5.0.0
|
5.0.0
|
||||||
-----
|
-----
|
||||||
|
@ -85,6 +85,24 @@ class Response
|
|||||||
const HTTP_NOT_EXTENDED = 510; // RFC2774
|
const HTTP_NOT_EXTENDED = 510; // RFC2774
|
||||||
const HTTP_NETWORK_AUTHENTICATION_REQUIRED = 511; // RFC6585
|
const HTTP_NETWORK_AUTHENTICATION_REQUIRED = 511; // RFC6585
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control
|
||||||
|
*/
|
||||||
|
private const HTTP_RESPONSE_CACHE_CONTROL_DIRECTIVES = [
|
||||||
|
'must_revalidate' => false,
|
||||||
|
'no_cache' => false,
|
||||||
|
'no_store' => false,
|
||||||
|
'no_transform' => false,
|
||||||
|
'public' => false,
|
||||||
|
'private' => false,
|
||||||
|
'proxy_revalidate' => false,
|
||||||
|
'max_age' => true,
|
||||||
|
's_maxage' => true,
|
||||||
|
'immutable' => false,
|
||||||
|
'last_modified' => true,
|
||||||
|
'etag' => true,
|
||||||
|
];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var ResponseHeaderBag
|
* @var ResponseHeaderBag
|
||||||
*/
|
*/
|
||||||
@ -921,7 +939,7 @@ class Response
|
|||||||
/**
|
/**
|
||||||
* Sets the response's cache headers (validation and/or expiration).
|
* Sets the response's cache headers (validation and/or expiration).
|
||||||
*
|
*
|
||||||
* Available options are: etag, last_modified, max_age, s_maxage, private, public and immutable.
|
* Available options are: must_revalidate, no_cache, no_store, no_transform, public, private, proxy_revalidate, max_age, s_maxage, immutable, last_modified and etag.
|
||||||
*
|
*
|
||||||
* @return $this
|
* @return $this
|
||||||
*
|
*
|
||||||
@ -931,7 +949,7 @@ class Response
|
|||||||
*/
|
*/
|
||||||
public function setCache(array $options): object
|
public function setCache(array $options): object
|
||||||
{
|
{
|
||||||
if ($diff = array_diff(array_keys($options), ['etag', 'last_modified', 'max_age', 's_maxage', 'private', 'public', 'immutable'])) {
|
if ($diff = array_diff(array_keys($options), array_keys(static::HTTP_RESPONSE_CACHE_CONTROL_DIRECTIVES))) {
|
||||||
throw new \InvalidArgumentException(sprintf('Response does not support the following options: "%s".', implode('", "', $diff)));
|
throw new \InvalidArgumentException(sprintf('Response does not support the following options: "%s".', implode('", "', $diff)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -951,6 +969,16 @@ class Response
|
|||||||
$this->setSharedMaxAge($options['s_maxage']);
|
$this->setSharedMaxAge($options['s_maxage']);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
foreach (self::HTTP_RESPONSE_CACHE_CONTROL_DIRECTIVES as $directive => $hasValue) {
|
||||||
|
if (!$hasValue && isset($options[$directive])) {
|
||||||
|
if ($options[$directive]) {
|
||||||
|
$this->headers->addCacheControlDirective(str_replace('_', '-', $directive));
|
||||||
|
} else {
|
||||||
|
$this->headers->removeCacheControlDirective(str_replace('_', '-', $directive));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (isset($options['public'])) {
|
if (isset($options['public'])) {
|
||||||
if ($options['public']) {
|
if ($options['public']) {
|
||||||
$this->setPublic();
|
$this->setPublic();
|
||||||
@ -967,10 +995,6 @@ class Response
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isset($options['immutable'])) {
|
|
||||||
$this->setImmutable((bool) $options['immutable']);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -659,6 +659,19 @@ class ResponseTest extends ResponseTestCase
|
|||||||
|
|
||||||
$response->setCache(['immutable' => false]);
|
$response->setCache(['immutable' => false]);
|
||||||
$this->assertFalse($response->headers->hasCacheControlDirective('immutable'));
|
$this->assertFalse($response->headers->hasCacheControlDirective('immutable'));
|
||||||
|
|
||||||
|
$directives = ['proxy_revalidate', 'must_revalidate', 'no_cache', 'no_store', 'no_transform'];
|
||||||
|
foreach ($directives as $directive) {
|
||||||
|
$response->setCache([$directive => true]);
|
||||||
|
|
||||||
|
$this->assertTrue($response->headers->hasCacheControlDirective(str_replace('_', '-', $directive)));
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($directives as $directive) {
|
||||||
|
$response->setCache([$directive => false]);
|
||||||
|
|
||||||
|
$this->assertFalse($response->headers->hasCacheControlDirective(str_replace('_', '-', $directive)));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testSendContent()
|
public function testSendContent()
|
||||||
|
Reference in New Issue
Block a user