Skip to content

Commit de00720

Browse files
authored
Merge pull request #134 from jingu/save-donut-headers
Add ResourceDonut header to save headers
2 parents 6cbbef9 + 858370e commit de00720

4 files changed

Lines changed: 10 additions & 4 deletions

File tree

src/ResourceDonut.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@ final class ResourceDonut
2222

2323
private const URI_REGEX = '/\[le:(.+)]/';
2424

25+
/** @param array<string, string> $headers */
2526
public function __construct(
2627
private string $template,
28+
private array $headers,
2729
/** @readonly */
2830
public int|null $ttl,
2931
/** @readonly */
@@ -45,8 +47,9 @@ public function refresh(ResourceInterface $resource, ResourceObject $ro): Resour
4547
return (string) $ro->view;
4648
}, $this->template);
4749

48-
$etags->setSurrogateHeader($ro);
50+
$ro->headers = $this->headers;
4951
$ro->view = $refreshView;
52+
$etags->setSurrogateHeader($ro);
5053

5154
return $ro;
5255
}
@@ -72,6 +75,6 @@ public static function create(ResourceObject $ro, DonutRendererInterface $storag
7275
unset($maybeRequest);
7376
$donutTemplate = (string) $ro;
7477

75-
return new self($donutTemplate, $ttl, $isCacheble);
78+
return new self($donutTemplate, $ro->headers, $ttl, $isCacheble);
7679
}
7780
}

src/ResourceStorageSaver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public function __invoke(string $key, mixed $value, CacheItemPoolInterface $pool
1919
assert($cacheItem instanceof CacheItem);
2020
$cacheItem->tag($tags);
2121

22-
if ($ttl) {
22+
if ($ttl !== null && $ttl > 0) {
2323
$cacheItem->expiresAfter($ttl);
2424
}
2525

tests/DonutCacheTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,12 @@ protected function setUp(): void
3030

3131
public function testGetState(): void
3232
{
33-
$donut = new ResourceDonut('cmt=[le:page://self/html/comment]', null, true);
33+
$headers = ['Content-Type' => 'application/xml;'];
34+
$donut = new ResourceDonut('cmt=[le:page://self/html/comment]', $headers, null, true);
3435
$blog = $this->resource->get('page://self/html/blog-posting');
3536
$ro = $donut->refresh($this->resource, $blog);
3637
$this->assertInstanceOf(ResourceObject::class, $ro);
3738
$this->assertSame('cmt=comment01', $ro->view);
39+
$this->assertSame($headers['Content-Type'], $ro->headers['Content-Type']);
3840
}
3941
}

tests/DonutRepositoryTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ public function testRefresh(): void
116116
assert($queryRepository->purge(new Uri('page://self/html/comment')));
117117
$donutRo = $resource->get('page://self/html/blog-posting');
118118
$this->assertSame('r', $donutRo->headers[Header::ETAG][-1]);
119+
$this->assertStringContainsString('blog-posting-page', $donutRo->headers[Header::SURROGATE_KEY]);
119120
}
120121

121122
public function testInvalidateTags(): void

0 commit comments

Comments
 (0)