Skip to content

Commit

Permalink
handle etags
Browse files Browse the repository at this point in the history
  • Loading branch information
withinboredom committed Jan 1, 2024
1 parent a7a1f74 commit 0f8df7d
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Cache/Control/GeneratesEtagInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@

interface GeneratesEtagInterface
{
public function getEtagComponents(): array;
public function getEtagComponents(array $context): array;
}
2 changes: 1 addition & 1 deletion src/Hooks/Api/Invoker.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public function process(ServerRequestInterface $request, ResponseInterface $resp
}

if ($component instanceof GeneratesEtagInterface) {
$response = $response->withHeader("ETag", $etag = "W\\" . md5(implode($component->getEtagComponents())));
$response = $response->withHeader("ETag", $etag = "W\\" . md5(implode('', $component->getEtagComponents($arguments))));
if(($_SERVER['HTTP_IF_NONE_MATCH'] ?? null) && $etag === $_SERVER['HTTP_IF_NONE_MATCH']) {
return $response->withStatus(304)->withHeader('Cache-Control', $tokenizer->render());
}
Expand Down
8 changes: 8 additions & 0 deletions src/Hooks/Html/Renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ public function process(ServerRequestInterface $request, ResponseInterface $resp
$root = $this->container->make($this->root);
$rendered = $this->container->call($root->render(...));
$rendered = $this->compiler->compile($rendered);

if(!empty($this->compiler->etagDescription)) {
$response = $response->withHeader('ETag', $etag = "W\\".md5(implode("", $this->compiler->etagDescription)));
if(($_SERVER['HTTP_IF_NONE_MATCH'] ?? null) && $etag === $_SERVER['HTTP_IF_NONE_MATCH']) {
return $response->withStatus(304)->withHeader('Cache-Control', $this->compiler->tokenizer->render());
}
}

return $response->withBody($this->psr17Factory->createStream($rendered))->withHeader('Cache-Control', $this->compiler->tokenizer->render());
}
}
8 changes: 8 additions & 0 deletions src/Template/Parser/StreamingCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Bottledcode\SwytchFramework\Template\Parser;

use Bottledcode\SwytchFramework\Cache\AbstractCache;
use Bottledcode\SwytchFramework\Cache\Control\GeneratesEtagInterface;
use Bottledcode\SwytchFramework\Cache\Control\Tokenizer;
use Bottledcode\SwytchFramework\Template\Functional\DataProvider;
use Bottledcode\SwytchFramework\Template\Functional\RewritingTag;
Expand Down Expand Up @@ -47,6 +48,8 @@ class StreamingCompiler
private int $fragmentLength = 0;
private bool $shallow = false;

public array $etagDescription = [];

public function __construct(
public FactoryInterface $factory,
private EscaperInterface $blobber,
Expand Down Expand Up @@ -792,6 +795,11 @@ private function renderComponent(Document $document): Document
}

$rendering = $component->renderToString($this->attributes);

if($component->rawComponent instanceof GeneratesEtagInterface) {
$this->etagDescription = array_merge_recursive($this->etagDescription, $component->rawComponent->getEtagComponents($this->attributes));
}

$rendering = $this->blobber->makeBlobs($rendering);
// stupid hack to get around the fact that we are lexigraphically parsing the document instead
// of creating a tree.
Expand Down

0 comments on commit 0f8df7d

Please sign in to comment.