diff --git a/src/Cache/CachePublic.php b/src/Cache/CachePublic.php index 8c2fd5e..3d25e7b 100644 --- a/src/Cache/CachePublic.php +++ b/src/Cache/CachePublic.php @@ -9,7 +9,7 @@ { public function tokenize(Tokenizer $tokenizer): Tokenizer { - if (!$tokenizer->public) { + if ($tokenizer->private) { return $tokenizer; } diff --git a/src/Cache/Control/Tokenizer.php b/src/Cache/Control/Tokenizer.php index 9025c6c..e856250 100644 --- a/src/Cache/Control/Tokenizer.php +++ b/src/Cache/Control/Tokenizer.php @@ -40,7 +40,12 @@ public function __construct( /** * @var bool indicates that any caches of any kind (public or shared) should not store this response. */ - public bool $public = true, + public bool $public = false, + + /** + * @var bool indicates that a public cache should not cache this + */ + public bool $private = false, /** * @var bool indicates that the response will not be updated while it's fresh. @@ -68,6 +73,7 @@ public function with( bool|null $proxyRevalidate = null, bool|null $noStore = null, bool|null $public = null, + bool|null $private = null, bool|null $immutable = null, int|null|false $staleWhileRevalidating = false, int|null|false $staleIfError = false, @@ -80,6 +86,7 @@ public function with( proxyRevalidate: $this->withBool($this->proxyRevalidate, $proxyRevalidate), noStore: $this->withBool($this->noStore, $noStore), public: $this->withBool($this->public, $public), + private: $this->withBool($this->private, $private), immutable: $this->withBool($this->immutable, $immutable), staleWhileRevalidating: $this->withInt($this->staleWhileRevalidating, $staleWhileRevalidating), staleIfError: $this->withInt($this->staleIfError, $staleIfError), @@ -99,7 +106,8 @@ private function withBool(bool $original, bool|null $var): bool public function render(): string { $header = [ - $this->public ? 'public' : 'private', + ...$this->header($this->private, 'private'), + ...$this->header($this->public, 'public'), ...$this->header($this->maxAge, "max-age=$this->maxAge"), ...$this->header($this->sMaxAge, "s-maxage=$this->sMaxAge"), ...$this->header($this->noCache, "no-cache"), diff --git a/src/Cache/NeverCache.php b/src/Cache/NeverCache.php index 5c56951..37cd108 100644 --- a/src/Cache/NeverCache.php +++ b/src/Cache/NeverCache.php @@ -18,9 +18,10 @@ public function tokenize(Tokenizer $tokenizer): Tokenizer proxyRevalidate: false, noStore: true, public: false, + private: true, immutable: false, staleWhileRevalidating: null, - staleIfError: null + staleIfError: null, ); } } diff --git a/src/Cache/UserSpecific.php b/src/Cache/UserSpecific.php index 3995144..41ce524 100644 --- a/src/Cache/UserSpecific.php +++ b/src/Cache/UserSpecific.php @@ -12,6 +12,6 @@ { public function tokenize(Tokenizer $tokenizer): Tokenizer { - return $tokenizer->with(public: false); + return $tokenizer->with(public: false, private: true); } } diff --git a/tests/CacheTest.php b/tests/CacheTest.php index 50a04b5..89f7ffb 100644 --- a/tests/CacheTest.php +++ b/tests/CacheTest.php @@ -21,13 +21,15 @@ function render(Tokenizer $tokenizer, AbstractCache ...$directives): string it('can describe a simple public cache', function () { $tokenizer = new Tokenizer(); - expect(render($tokenizer))->toBe('public'); + $directives[] = new CachePublic(); + expect(render($tokenizer, ...$directives))->toBe('public'); }); it('can handle max age rules', function () { $tokenizer = new Tokenizer(); $directives[] = new NeverChanges(); + $directives[] = new CachePublic(); expect(render($tokenizer, ...$directives))->toBe("public max-age=604800 immutable"); $directives[] = new MaxAge(3000); @@ -54,6 +56,7 @@ function render(Tokenizer $tokenizer, AbstractCache ...$directives): string $tokenizer = new Tokenizer(); $directives[] = new NeverChanges(); + $directives[] = new CachePublic(); expect(render($tokenizer, ...$directives))->toBe("public max-age=604800 immutable"); $directives[] = new Revalidate(RevalidationEnum::AfterError, 300); expect(render($tokenizer, ...$directives))->toBe("public stale-if-error=300"); @@ -96,3 +99,9 @@ function render(Tokenizer $tokenizer, AbstractCache ...$directives): string $directives[] = new CachePublic(); expect(render($tokenizer, ...$directives))->toBe("private"); }); + +it('renders nothing when empty', function() { + $tokenizer = new Tokenizer(); + + expect(render($tokenizer))->toBe(''); +});