Skip to content

[HttpFoundation] Adds support for the immutable directive in the cache-control header #22932

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion src/Symfony/Component/HttpFoundation/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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))));
}

Expand Down Expand Up @@ -973,6 +1001,10 @@ public function setCache(array $options)
}
}

if (isset($options['immutable'])) {
$this->setImmutable((bool) $options['immutable']);
}

return $this;
}

Expand Down
22 changes: 22 additions & 0 deletions src/Symfony/Component/HttpFoundation/Tests/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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();
Expand Down