Skip to content

Commit

Permalink
Merge pull request #134 from jingu/save-donut-headers
Browse files Browse the repository at this point in the history
Add ResourceDonut header to save headers
  • Loading branch information
koriym committed Mar 26, 2024
2 parents 6cbbef9 + 858370e commit de00720
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 4 deletions.
7 changes: 5 additions & 2 deletions src/ResourceDonut.php
Expand Up @@ -22,8 +22,10 @@ final class ResourceDonut

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

/** @param array<string, string> $headers */
public function __construct(
private string $template,
private array $headers,
/** @readonly */
public int|null $ttl,
/** @readonly */
Expand All @@ -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;
}
Expand All @@ -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);
}
}
2 changes: 1 addition & 1 deletion src/ResourceStorageSaver.php
Expand Up @@ -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);
}

Expand Down
4 changes: 3 additions & 1 deletion tests/DonutCacheTest.php
Expand Up @@ -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']);
}
}
1 change: 1 addition & 0 deletions tests/DonutRepositoryTest.php
Expand Up @@ -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
Expand Down

0 comments on commit de00720

Please sign in to comment.