Skip to content

Commit

Permalink
Merge pull request #3 from smartondev/2-nocache-reset-fix
Browse files Browse the repository at this point in the history
fix: no-cache reset fix (private, public, noStore, mustRevalidate) #2
  • Loading branch information
kamarton authored Aug 21, 2024
2 parents d45d644 + 5980cca commit 8add935
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## dev

- fix: no-cache reset fix (private, public, noStore, mustRevalidate) #2

## 0.2.0

Expand Down
30 changes: 18 additions & 12 deletions src/CacheHeaderBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,7 @@ class CacheHeaderBuilder implements HttpHeaderInterface

public function noCache(): static
{
$this->reset()
->mustRevalidate()
->private()
->noStore();
$this->reset();
$this->noCache = true;
return $this;
}
Expand Down Expand Up @@ -257,6 +254,7 @@ public function withoutSharedMaxAge(): static

public function mustRevalidate(): static
{
$this->resetIfNoCache();
$this->mustRevalidate = true;
return $this;
}
Expand Down Expand Up @@ -300,6 +298,7 @@ public function withoutProxyRevalidate(): static

public function noStore(): static
{
$this->resetIfNoCache();
$this->noStore = true;
return $this;
}
Expand All @@ -324,6 +323,7 @@ public function withoutNoStore(): static

public function private(): static
{
$this->resetIfNoCache();
$this->private = true;
$this->public = false;
return $this;
Expand All @@ -349,6 +349,7 @@ public function withoutPrivate(): static

public function public(): static
{
$this->resetIfNoCache();
$this->public = true;
$this->private = false;
return $this;
Expand Down Expand Up @@ -588,6 +589,19 @@ public function toHeaders(): array
if (null !== $this->lastModified) {
$headers[self::LAST_MODIFIED_HEADER] = httpHeaderDate($this->lastModified);
}
if ($this->noCache) {
$cacheControl = [
'must-revalidate',
'no-store',
'private',
'no-cache',
];
sort($cacheControl);
$headers[self::CACHE_CONTROL_HEADER] = join(', ', $cacheControl);
$headers[self::PRAGMA_HEADER] = 'no-cache';
return $headers;
}

$cacheControl = [];
if ($this->mustRevalidate) {
$cacheControl[] = 'must-revalidate';
Expand All @@ -600,14 +614,6 @@ public function toHeaders(): array
if ($this->private) {
$cacheControl[] = 'private';
}

if ($this->noCache) {
$cacheControl[] = 'no-cache';
sort($cacheControl);
$headers[self::CACHE_CONTROL_HEADER] = join(', ', $cacheControl);
$headers[self::PRAGMA_HEADER] = 'no-cache';
return $headers;
}
// no-cache disables all other cache headers

if (null !== $this->expires) {
Expand Down
14 changes: 13 additions & 1 deletion tests/CacheHeaderBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,8 @@ public function testWithETag(): void
$this->assertSame(['etag' => '"123456"'], $headers);
}

public function testWithEmptyEtag() : void {
public function testWithEmptyEtag(): void
{
$builder = (new CacheHeaderBuilder())
->withETag('');
$this->assertNull($builder->getETag());
Expand Down Expand Up @@ -230,4 +231,15 @@ public function testIsNotEmpty(): void
$builder->reset();
$this->assertFalse($builder->isNotEmpty());
}

public function testNoCacheReset(): void
{
$builder = new CacheHeaderBuilder();
$builder->noCache();
$this->assertSame([], $builder->withReset()->toHeaders());
$this->assertSame(['cache-control' => 'private'], $builder->withPrivate()->toHeaders());
$this->assertSame(['cache-control' => 'public'], $builder->withPublic()->toHeaders());
$this->assertSame(['cache-control' => 'no-store'], $builder->withNoStore()->toHeaders());
$this->assertSame(['cache-control' => 'must-revalidate'], $builder->withMustRevalidate()->toHeaders());
}
}

0 comments on commit 8add935

Please sign in to comment.