8000 [HttpFoundation] added support for must-revalidate and proxy-revalida… · symfony/symfony@440bba7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 440bba7

Browse files
committed
[HttpFoundation] added support for must-revalidate and proxy-revalidate cache control directives in Response#setCache()
1 parent c980c3f commit 440bba7

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

src/Symfony/Component/HttpFoundation/Response.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -888,7 +888,7 @@ public function setEtag($etag = null, $weak = false)
888888
/**
889889
* Sets the response's cache headers (validation and/or expiration).
890890
*
891-
* Available options are: etag, last_modified, max_age, s_maxage, private, and public.
891+
* Available options are: etag, last_modified, max_age, s_maxage, must_revalidate, proxy_revalidate, private, and public.
892892
*
893893
* @param array $options An array of cache options
894894
*
@@ -900,7 +900,7 @@ public function setEtag($etag = null, $weak = false)
900900
*/
901901
public function setCache(array $options)
902902
{
903-
if ($diff = array_diff(array_keys($options), array('etag', 'last_modified', 'max_age', 's_maxage', 'private', 'public'))) {
903+
if ($diff = array_diff(array_keys($options), array('etag', 'last_modified', 'max_age', 's_maxage', 'must_revalidate', 'proxy_revalidate', 'private', 'public'))) {
904904
throw new \InvalidArgumentException(sprintf('Response does not support the following options: "%s".', implode('", "', array_values($diff))));
905905
}
906906

@@ -920,6 +920,23 @@ public function setCache(array $options)
920920
$this->setSharedMaxAge($options['s_maxage']);
921921
}
922922

923+
if (isset($options['must_revalidate'])) {
924+
if ($options['must_revalidate']) {
925+
$this->headers->addCacheControlDirective('must-revalidate');
926+
} else {
927+
$this->headers->removeCacheControlDirective('must-revalidate');
928+
}
929+
}
930+
931+
if (isset($options['proxy_revalidate'])) {
932+
if ($options['proxy_revalidate']) {
933+
$this->setPublic(); // proxy-revalidate doesn't make any sense with private responses
934+
$this->headers->addCacheControlDirective('proxy-revalidate');
935+
} else {
936+
$this->headers->removeCacheControlDirective('proxy-revalidate');
937+
}
938+
}
939+
923940
if (isset($options['public'])) {
924941
if ($options['public']) {
925942
$this->setPublic();

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ public function testPrepareSetsPragmaOnHttp10Only()
495495
public function testSetCache()
496496
{
497497
$response = new Response();
498-
//array('etag', 'last_modified', 'max_age', 's_maxage', 'private', 'public')
498+
//array('etag', 'last_modified', 'max_age', 's_maxage', 'must_revalidate', 'proxy_revalidate', 'private', 'public')
499499
try {
500500
$response->setCache(array('wrong option' => 'value'));
501501
$this->fail('->setCache() throws an InvalidArgumentException if an option is not supported');
@@ -524,6 +524,19 @@ public function testSetCache()
524524
$this->assertTrue($response->headers->hasCacheControlDirective('public'));
525525
$this->assertFalse($response->headers->hasCacheControlDirective('private'));
526526

527+
$response->setCache(array('must_revalidate' => true));
528+
$this->assertTrue($response->headers->hasCacheControlDirective('must-revalidate'));
529+
$response->setCache(array('must_revalidate' => false));
530+
$this->assertFalse($response->headers->hasCacheControlDirective('must-revalidate'));
531+
532+
$response->setCache(array('proxy_revalidate' => true));
533+
$this->assertTrue($response->headers->hasCacheControlDirective('public'));
534+
$this->assertFalse($response->headers->hasCacheControlDirective('private'));
535+
$this->assertTrue($response->headers->hasCacheControlDirective('proxy-revalidate'));
536+
537+
$response->setCache(array('proxy_revalidate' => false));
538+
$this->assertFalse($response->headers->hasCacheControlDirective('proxy-revalidate'));
539+
527540
$response->setCache(array('public' => true));
528541
$this->assertTrue($response->headers->hasCacheControlDirective('public'));
529542
$this->assertFalse($response->headers->hasCacheControlDirective('private'));
@@ -767,6 +780,7 @@ public function testSettersAreChainable()
767780
'setSharedMaxAge' => 1,
768781
'setTtl' => 1,
769782
'setClientTtl' => 1,
783+
'setCache' => array(),
770784
);
771785

772786
foreach ($setters as $setter => $arg) {

0 commit comments

Comments
 (0)
0