From 858370e715498db787f8cb97abab0c3d2eb0e74f Mon Sep 17 00:00:00 2001 From: Yoshitaka Jingu Date: Tue, 26 Mar 2024 11:06:39 +0900 Subject: [PATCH] Add ResourceDonut header to save headers --- src/ResourceDonut.php | 7 +++++-- src/ResourceStorageSaver.php | 2 +- tests/DonutCacheTest.php | 4 +++- tests/DonutRepositoryTest.php | 1 + 4 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/ResourceDonut.php b/src/ResourceDonut.php index b4de92e6..f095b841 100644 --- a/src/ResourceDonut.php +++ b/src/ResourceDonut.php @@ -22,8 +22,10 @@ final class ResourceDonut private const URI_REGEX = '/\[le:(.+)]/'; + /** @param array $headers */ public function __construct( private string $template, + private array $headers, /** @readonly */ public int|null $ttl, /** @readonly */ @@ -45,8 +47,9 @@ public function refresh(ResourceInterface $resource, ResourceObject $ro): Resour return (string) $ro->view; }, $this->template); - $etags->setSurrogateHeader($ro); + $ro->headers = $this->headers; $ro->view = $refreshView; + $etags->setSurrogateHeader($ro); return $ro; } @@ -72,6 +75,6 @@ public static function create(ResourceObject $ro, DonutRendererInterface $storag unset($maybeRequest); $donutTemplate = (string) $ro; - return new self($donutTemplate, $ttl, $isCacheble); + return new self($donutTemplate, $ro->headers, $ttl, $isCacheble); } } diff --git a/src/ResourceStorageSaver.php b/src/ResourceStorageSaver.php index a68f71e7..ac6eddbd 100644 --- a/src/ResourceStorageSaver.php +++ b/src/ResourceStorageSaver.php @@ -19,7 +19,7 @@ public function __invoke(string $key, mixed $value, CacheItemPoolInterface $pool assert($cacheItem instanceof CacheItem); $cacheItem->tag($tags); - if ($ttl) { + if ($ttl !== null && $ttl > 0) { $cacheItem->expiresAfter($ttl); } diff --git a/tests/DonutCacheTest.php b/tests/DonutCacheTest.php index c5ea134a..d565b8f8 100644 --- a/tests/DonutCacheTest.php +++ b/tests/DonutCacheTest.php @@ -30,10 +30,12 @@ protected function setUp(): void public function testGetState(): void { - $donut = new ResourceDonut('cmt=[le:page://self/html/comment]', null, true); + $headers = ['Content-Type' => 'application/xml;']; + $donut = new ResourceDonut('cmt=[le:page://self/html/comment]', $headers, null, true); $blog = $this->resource->get('page://self/html/blog-posting'); $ro = $donut->refresh($this->resource, $blog); $this->assertInstanceOf(ResourceObject::class, $ro); $this->assertSame('cmt=comment01', $ro->view); + $this->assertSame($headers['Content-Type'], $ro->headers['Content-Type']); } } diff --git a/tests/DonutRepositoryTest.php b/tests/DonutRepositoryTest.php index 09c1a172..1bce8869 100644 --- a/tests/DonutRepositoryTest.php +++ b/tests/DonutRepositoryTest.php @@ -116,6 +116,7 @@ public function testRefresh(): void assert($queryRepository->purge(new Uri('page://self/html/comment'))); $donutRo = $resource->get('page://self/html/blog-posting'); $this->assertSame('r', $donutRo->headers[Header::ETAG][-1]); + $this->assertStringContainsString('blog-posting-page', $donutRo->headers[Header::SURROGATE_KEY]); } public function testInvalidateTags(): void