Skip to content
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
12 changes: 12 additions & 0 deletions src/Symfony/Component/HttpKernel/Attribute/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,18 @@ public function __construct(
* It can be expressed in seconds or with a relative time format (1 day, 2 weeks, ...).
*/
public int|string|null $staleIfError = null,

/**
* Add the "no-store" Cache-Control directive when set to true.
*
* This directive indicates that no part of the response can be cached
* in any cache (not in a shared cache, nor in a private cache).
*
* Supersedes the "$public" and "$smaxage" values.
*
* @see https://datatracker.ietf.org/doc/html/rfc7234#section-5.2.2.3
*/
public ?bool $noStore = null,
) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,15 @@ public function onKernelResponse(ResponseEvent $event): void
if (false === $cache->public) {
$response->setPrivate();
}

if (true === $cache->noStore) {
$response->setPrivate();
$response->headers->addCacheControlDirective('no-store');
}

if (false === $cache->noStore) {
$response->headers->removeCacheControlDirective('no-store');
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,50 @@ public function testResponseIsPrivateIfConfigurationIsPublicFalse()
$this->assertTrue($this->response->headers->hasCacheControlDirective('private'));
}

public function testResponseIsPublicIfConfigurationIsPublicTrueNoStoreFalse()
{
$request = $this->createRequest(new Cache(public: true, noStore: false));

$this->listener->onKernelResponse($this->createEventMock($request, $this->response));

$this->assertTrue($this->response->headers->hasCacheControlDirective('public'));
$this->assertFalse($this->response->headers->hasCacheControlDirective('private'));
$this->assertFalse($this->response->headers->hasCacheControlDirective('no-store'));
}

public function testResponseIsPrivateIfConfigurationIsPublicTrueNoStoreTrue()
{
$request = $this->createRequest(new Cache(public: true, noStore: true));

$this->listener->onKernelResponse($this->createEventMock($request, $this->response));

$this->assertFalse($this->response->headers->hasCacheControlDirective('public'));
$this->assertTrue($this->response->headers->hasCacheControlDirective('private'));
$this->assertTrue($this->response->headers->hasCacheControlDirective('no-store'));
}

public function testResponseIsPrivateNoStoreIfConfigurationIsNoStoreTrue()
{
$request = $this->createRequest(new Cache(noStore: true));

$this->listener->onKernelResponse($this->createEventMock($request, $this->response));

$this->assertFalse($this->response->headers->hasCacheControlDirective('public'));
$this->assertTrue($this->response->headers->hasCacheControlDirective('private'));
$this->assertTrue($this->response->headers->hasCacheControlDirective('no-store'));
}

public function testResponseIsPrivateIfSharedMaxAgeSetAndNoStoreIsTrue()
{
$request = $this->createRequest(new Cache(smaxage: 1, noStore: true));

$this->listener->onKernelResponse($this->createEventMock($request, $this->response));

$this->assertFalse($this->response->headers->hasCacheControlDirective('public'));
$this->assertTrue($this->response->headers->hasCacheControlDirective('private'));
$this->assertTrue($this->response->headers->hasCacheControlDirective('no-store'));
}

public function testResponseVary()
{
$vary = ['foobar'];
Expand Down Expand Up @@ -132,6 +176,7 @@ public function testAttributeConfigurationsAreSetOnResponse()
$this->assertFalse($this->response->headers->hasCacheControlDirective('max-stale'));
$this->assertFalse($this->response->headers->hasCacheControlDirective('stale-while-revalidate'));
$this->assertFalse($this->response->headers->hasCacheControlDirective('stale-if-error'));
$this->assertFalse($this->response->headers->hasCacheControlDirective('no-store'));

$this->request->attributes->set('_cache', [new Cache(
expires: 'tomorrow',
Expand All @@ -140,6 +185,7 @@ public function testAttributeConfigurationsAreSetOnResponse()
maxStale: '5',
staleWhileRevalidate: '6',
staleIfError: '7',
noStore: true,
)]);

$this->listener->onKernelResponse($this->event);
Expand All @@ -149,6 +195,7 @@ public function testAttributeConfigurationsAreSetOnResponse()
$this->assertSame('5', $this->response->headers->getCacheControlDirective('max-stale'));
$this->assertSame('6', $this->response->headers->getCacheControlDirective('stale-while-revalidate'));
$this->assertSame('7', $this->response->headers->getCacheControlDirective('stale-if-error'));
$this->assertTrue($this->response->headers->hasCacheControlDirective('no-store'));
$this->assertInstanceOf(\DateTimeInterface::class, $this->response->getExpires());
}

Expand Down