Skip to content

Commit 695fd9a

Browse files
committed
Added support for the immutable directive in the cache-control header
1 parent e992eae commit 695fd9a

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

src/Symfony/Component/HttpFoundation/Response.php

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,34 @@ public function setPublic()
620620
return $this;
621621
}
622622

623+
/**
624+
* Marks the response as "immutable".
625+
*
626+
* @param bool $immutable Enables or disables the immutable directive.
627+
*
628+
* @return $this
629+
*/
630+
public function setImmutable($immutable = true)
631+
{
632+
if ($immutable) {
633+
$this->headers->addCacheControlDirective('immutable');
634+
} else {
635+
$this->headers->removeCacheControlDirective('immutable');
636+
}
637+
638+
return $this;
639+
}
640+
641+
/**
642+
* Returns true if the response is marked as "immutable".
643+
*
644+
* @return bool Returns true if the response is marked as "immutable"; otherwise false.
645+
*/
646+
public function isImmutable()
647+
{
648+
return $this->headers->hasCacheControlDirective('immutable');
649+
}
650+
623651
/**
624652
* Returns true if the response must be revalidated by caches.
625653
*
@@ -956,7 +984,7 @@ public function setEtag($etag = null, $weak = false)
956984
*/
957985
public function setCache(array $options)
958986
{
959-
if ($diff = array_diff(array_keys($options), array('etag', 'last_modified', 'max_age', 's_maxage', 'private', 'public'))) {
987+
if ($diff = array_diff(array_keys($options), array('etag', 'last_modified', 'max_age', 's_maxage', 'private', 'public', 'immutable'))) {
960988
throw new \InvalidArgumentException(sprintf('Response does not support the following options: "%s".', implode('", "', array_values($diff))));
961989
}
962990

@@ -992,6 +1020,10 @@ public function setCache(array $options)
9921020
}
9931021
}
9941022

1023+
if (isset($options['immutable'])) {
1024+
$this->setImmutable((bool)$options['immutable']);
1025+
}
1026+
9951027
return $this;
9961028
}
9971029

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

Lines 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)