Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 16fbe3a

Browse filesBrowse files
committed
feature #22932 [HttpFoundation] Adds support for the immutable directive in the cache-control header (twoleds)
This PR was merged into the 3.4 branch. Discussion ---------- [HttpFoundation] Adds support for the immutable directive in the cache-control header | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #21425 | License | MIT | Doc PR | Added support for the immutable directive in the cache-control header, tries to resolve #21425. Commits ------- 33573c6 Added support for the immutable directive in the cache-control header
2 parents 30e817a + 33573c6 commit 16fbe3a
Copy full SHA for 16fbe3a

File tree

2 files changed

+55
-1
lines changed
Filter options

2 files changed

+55
-1
lines changed

‎src/Symfony/Component/HttpFoundation/Response.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpFoundation/Response.php
+33-1Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,34 @@ public function setPublic()
610610
return $this;
611611
}
612612

613+
/**
614+
* Marks the response as "immutable".
615+
*
616+
* @param bool $immutable Enables or disables the immutable directive.
617+
*
618+
* @return $this
619+
*/
620+
public function setImmutable($immutable = true)
621+
{
622+
if ($immutable) {
623+
$this->headers->addCacheControlDirective('immutable');
624+
} else {
625+
$this->headers->removeCacheControlDirective('immutable');
626+
}
627+
628+
return $this;
629+
}
630+
631+
/**
632+
* Returns true if the response is marked as "immutable".
633+
*
634+
* @return bool Returns true if the response is marked as "immutable"; otherwise false.
635+
*/
636+
public function isImmutable()
637+
{
638+
return $this->headers->hasCacheControlDirective('immutable');
639+
}
640+
613641
/**
614642
* Returns true if the response must be revalidated by caches.
615643
*
@@ -937,7 +965,7 @@ public function setEtag($etag = null, $weak = false)
937965
*/
938966
public function setCache(array $options)
939967
{
940-
if ($diff = array_diff(array_keys($options), array('etag', 'last_modified', 'max_age', 's_maxage', 'private', 'public'))) {
968+
if ($diff = array_diff(array_keys($options), array('etag', 'last_modified', 'max_age', 's_maxage', 'private', 'public', 'immutable'))) {
941969
throw new \InvalidArgumentException(sprintf('Response does not support the following options: "%s".', implode('", "', array_values($diff))));
942970
}
943971

@@ -973,6 +1001,10 @@ public function setCache(array $options)
9731001
}
9741002
}
9751003

1004+
if (isset($options['immutable'])) {
1005+
$this->setImmutable((bool) $options['immutable']);
1006+
}
1007+
9761008
return $this;
9771009
}
9781010

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpFoundation/Tests/ResponseTest.php
+22Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,12 @@ public function testSetCache()
610610
$response->setCache(array('private' => false));
611611
$this->assertTrue($response->headers->hasCacheControlDirective('public'));
612612
$this->assertFalse($response->headers->hasCacheControlDirective('private'));
613+
614+
$response->setCache(array('immutable' => true));
615+
$this->assertTrue($response->headers->hasCacheControlDirective('immutable'));
616+
617+
$response->setCache(array('immutable' => false));
618+
$this->assertFalse($response->headers->hasCacheControlDirective('immutable'));
613619
}
614620

615621
public function testSendContent()
@@ -631,6 +637,22 @@ public function testSetPublic()
631637
$this->assertFalse($response->headers->hasCacheControlDirective('private'));
632638
}
633639

640+
public function testSetImmutable()
641+
{
642+
$response = new Response();
643+
$response->setImmutable();
644+
645+
$this->assertTrue($response->headers->hasCacheControlDirective('immutable'));
646+
}
647+
648+
public function testIsImmutable()
649+
{
650+
$response = new Response();
651+
$response->setImmutable();
652+
653+
$this->assertTrue($response->isImmutable());
654+
}
655+
634656
public function testSetExpires()
635657
{
636658
$response = new Response();

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.