diff --git a/src/StaticCache.php b/src/StaticCache.php index 5bff051..b5a04db 100644 --- a/src/StaticCache.php +++ b/src/StaticCache.php @@ -225,15 +225,20 @@ private function addCacheHeadersToWebResponse(): void "public, s-maxage=$this->cacheDuration, max-age=0", ); - // Capture, remove any existing headers so we can prepare them + // Capture and remove any existing headers so we can prepare them $existingTagsFromHeader = Collection::make($headers->get(HeaderEnum::CACHE_TAG->value, first: false) ?? []); $headers->remove(HeaderEnum::CACHE_TAG->value); - $this->tags = $this->tags->push(...$existingTagsFromHeader); + $this->tags->push(...$existingTagsFromHeader); + $this->tags = $this->prepareTags(...$this->tags); - $this->prepareTags(...$this->tags) - ->each(fn(string $tag) => $headers->add( + Craft::warning(new PsrMessage('Adding cache tags to response', [ + 'tags' => $this->tags, + ])); + + $this->tags + ->each(fn(StaticCacheTag $tag) => $headers->add( HeaderEnum::CACHE_TAG->value, - $tag, + $tag->getValue(), )); } @@ -257,13 +262,13 @@ public function purgeTags(string|StaticCacheTag ...$tags): void } Craft::info(new PsrMessage('Purging tags', [ - 'tags' => $tags->all(), + 'tags' => $tags, ])); if ($isWebResponse) { - $tags->each(fn(string $tag) => $response->getHeaders()->add( + $tags->each(fn(StaticCacheTag $tag) => $response->getHeaders()->add( HeaderEnum::CACHE_PURGE_TAG->value, - $tag, + $tag->getValue(), )); return; @@ -306,12 +311,8 @@ private function isCacheable(): bool private function prepareTags(string|StaticCacheTag ...$tags): Collection { return Collection::make($tags) - ->map(function(string|StaticCacheTag $tag): string { - $tag = is_string($tag) ? StaticCacheTag::create($tag) : $tag; - - return $tag->getValue(); - }) - ->filter() - ->unique(); + ->map(fn(string|StaticCacheTag $tag) => is_string($tag) ? StaticCacheTag::create($tag) : $tag) + ->filter(fn(StaticCacheTag $tag) => (bool) $tag->getValue()) + ->unique(fn(StaticCacheTag $tag) => $tag->getValue()); } } diff --git a/src/StaticCacheTag.php b/src/StaticCacheTag.php index bda0739..0baf4f8 100644 --- a/src/StaticCacheTag.php +++ b/src/StaticCacheTag.php @@ -2,7 +2,7 @@ namespace craft\cloud; -class StaticCacheTag implements \Stringable +class StaticCacheTag implements \Stringable, \JsonSerializable { public readonly string $originalValue; private bool $minify = true; @@ -18,6 +18,14 @@ public static function create(string $value): self return new self($value); } + public function jsonSerialize(): false|string + { + return json_encode([ + 'value' => $this->getValue(), + 'originalValue' => $this->originalValue, + ]); + } + public function __toString(): string { return $this->getValue(); @@ -25,16 +33,17 @@ public function __toString(): string public function getValue(): string { - $this->removeInvalidCharacters(); + $clone = clone $this; + $clone->removeInvalidCharacters(); - if ($this->minify) { - return $this + if ($clone->value && $clone->minify) { + return self::create($clone->value) ->hash() ->withPrefix(Module::getInstance()->getConfig()->getShortEnvironmentId()) ->value; } - return $this->value; + return $clone->value; } public function withPrefix(string $prefix): self