Skip to content

Commit

Permalink
CacheMacro: wrapped in try ... finally
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Nov 16, 2020
1 parent 1d4e43e commit 7cab46d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
20 changes: 18 additions & 2 deletions src/Bridges/CacheLatte/CacheMacro.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function nodeOpened(Latte\MacroNode $node)
$node->empty = false;
$node->openingCode = Latte\PhpWriter::using($node)
->write(
'<?php if (Nette\Bridges\CacheLatte\CacheMacro::createCache($this->global->cacheStorage, %var, $this->global->cacheStack, %node.array?)) { ?>',
'<?php if (Nette\Bridges\CacheLatte\CacheMacro::createCache($this->global->cacheStorage, %var, $this->global->cacheStack, %node.array?)) try { ?>',
Nette\Utils\Random::generate()
);
}
Expand All @@ -73,7 +73,11 @@ public function nodeOpened(Latte\MacroNode $node)
public function nodeClosed(Latte\MacroNode $node)
{
$node->closingCode = Latte\PhpWriter::using($node)
->write('<?php Nette\Bridges\CacheLatte\CacheMacro::endCache($this->global->cacheStack, %node.array?); } ?>');
->write('<?php
Nette\Bridges\CacheLatte\CacheMacro::endCache($this->global->cacheStack, %node.array?);
} catch (\Throwable $__e) {
Nette\Bridges\CacheLatte\CacheMacro::rollback($this->global->cacheStack); throw $__e;
} ?>');
}


Expand Down Expand Up @@ -140,4 +144,16 @@ public static function endCache(array &$parents, array $args = null): void
$helper->dependencies[Cache::EXPIRATION] = $args['expiration'] ?? '+ 7 days';
$helper->end();
}


/**
* @param Nette\Caching\OutputHelper[] $parents
*/
public static function rollback(array &$parents): void
{
$helper = array_pop($parents);
if ($helper instanceof Nette\Caching\OutputHelper) {
$helper->rollback();
}
}
}
6 changes: 5 additions & 1 deletion tests/Bridges.Latte/expected/CacheMacro.cache.inc.phtml
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
<?php
%A%
if (Nette\Bridges\CacheLatte\CacheMacro::createCache($this->global->cacheStorage, '%[\w]+%', $this->global->cacheStack)) {
if (Nette\Bridges\CacheLatte\CacheMacro::createCache($this->global->cacheStorage, '%[\w]+%', $this->global->cacheStack)) try {
echo ' ';
echo LR\Filters::escapeHtmlText(($this->filters->lower)($title)) /* line 4 */;
echo "\n";
Nette\Bridges\CacheLatte\CacheMacro::endCache($this->global->cacheStack);
}
catch (\Throwable $__e) {
Nette\Bridges\CacheLatte\CacheMacro::rollback($this->global->cacheStack);
throw $__e;
}
%A%
6 changes: 5 additions & 1 deletion tests/Bridges.Latte/expected/CacheMacro.cache.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
echo 'Noncached content
';
if (Nette\Bridges\CacheLatte\CacheMacro::createCache($this->global->cacheStorage, '%[\w]+%', $this->global->cacheStack, [$id, 'tags' => 'mytag'])) {
if (Nette\Bridges\CacheLatte\CacheMacro::createCache($this->global->cacheStorage, '%[\w]+%', $this->global->cacheStack, [$id, 'tags' => 'mytag'])) try {
echo '
<h1>';
echo LR\Filters::escapeHtmlText(($this->filters->upper)($title)) /* line 5 */;
Expand All @@ -15,6 +15,10 @@
echo "\n";
Nette\Bridges\CacheLatte\CacheMacro::endCache($this->global->cacheStack, [$id, 'tags' => 'mytag']);
}
catch (\Throwable $__e) {
Nette\Bridges\CacheLatte\CacheMacro::rollback($this->global->cacheStack);
throw $__e;
}
%A%
}

Expand Down

0 comments on commit 7cab46d

Please sign in to comment.