From 33573c6eb17bead47b42e53c8d0c863231c5857c Mon Sep 17 00:00:00 2001 From: Jaroslav Kuba Date: Sun, 28 May 2017 21:03:49 +0200 Subject: [PATCH] Added support for the immutable directive in the cache-control header --- .../Component/HttpFoundation/Response.php | 34 ++++++++++++++++++- .../HttpFoundation/Tests/ResponseTest.php | 22 ++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/HttpFoundation/Response.php b/src/Symfony/Component/HttpFoundation/Response.php index 3ea4cb6170452..8b237d7587af5 100644 --- a/src/Symfony/Component/HttpFoundation/Response.php +++ b/src/Symfony/Component/HttpFoundation/Response.php @@ -610,6 +610,34 @@ public function setPublic() return $this; } + /** + * Marks the response as "immutable". + * + * @param bool $immutable Enables or disables the immutable directive. + * + * @return $this + */ + public function setImmutable($immutable = true) + { + if ($immutable) { + $this->headers->addCacheControlDirective('immutable'); + } else { + $this->headers->removeCacheControlDirective('immutable'); + } + + return $this; + } + + /** + * Returns true if the response is marked as "immutable". + * + * @return bool Returns true if the response is marked as "immutable"; otherwise false. + */ + public function isImmutable() + { + return $this->headers->hasCacheControlDirective('immutable'); + } + /** * Returns true if the response must be revalidated by caches. * @@ -937,7 +965,7 @@ public function setEtag($etag = null, $weak = false) */ public function setCache(array $options) { - if ($diff = array_diff(array_keys($options), array('etag', 'last_modified', 'max_age', 's_maxage', 'private', 'public'))) { + if ($diff = array_diff(array_keys($options), array('etag', 'last_modified', 'max_age', 's_maxage', 'private', 'public', 'immutable'))) { throw new \InvalidArgumentException(sprintf('Response does not support the following options: "%s".', implode('", "', array_values($diff)))); } @@ -973,6 +1001,10 @@ public function setCache(array $options) } } + if (isset($options['immutable'])) { + $this->setImmutable((bool) $options['immutable']); + } + return $this; } diff --git a/src/Symfony/Component/HttpFoundation/Tests/ResponseTest.php b/src/Symfony/Component/HttpFoundation/Tests/ResponseTest.php index 62b8c652597ea..9c5b34febfa30 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/ResponseTest.php +++ b/src/Symfony/Component/HttpFoundation/Tests/ResponseTest.php @@ -610,6 +610,12 @@ public function testSetCache() $response->setCache(array('private' => false)); $this->assertTrue($response->headers->hasCacheControlDirective('public')); $this->assertFalse($response->headers->hasCacheControlDirective('private')); + + $response->setCache(array('immutable' => true)); + $this->assertTrue($response->headers->hasCacheControlDirective('immutable')); + + $response->setCache(array('immutable' => false)); + $this->assertFalse($response->headers->hasCacheControlDirective('immutable')); } public function testSendContent() @@ -631,6 +637,22 @@ public function testSetPublic() $this->assertFalse($response->headers->hasCacheControlDirective('private')); } + public function testSetImmutable() + { + $response = new Response(); + $response->setImmutable(); + + $this->assertTrue($response->headers->hasCacheControlDirective('immutable')); + } + + public function testIsImmutable() + { + $response = new Response(); + $response->setImmutable(); + + $this->assertTrue($response->isImmutable()); + } + public function testSetExpires() { $response = new Response(); 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