From 7cab46d8d436e26a874715e6fff13b74d29dbf8c Mon Sep 17 00:00:00 2001 From: David Grudl Date: Mon, 16 Nov 2020 00:13:12 +0100 Subject: [PATCH] CacheMacro: wrapped in try ... finally --- src/Bridges/CacheLatte/CacheMacro.php | 20 +++++++++++++++++-- .../expected/CacheMacro.cache.inc.phtml | 6 +++++- .../expected/CacheMacro.cache.phtml | 6 +++++- 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/Bridges/CacheLatte/CacheMacro.php b/src/Bridges/CacheLatte/CacheMacro.php index 5bcae692..353bde0b 100644 --- a/src/Bridges/CacheLatte/CacheMacro.php +++ b/src/Bridges/CacheLatte/CacheMacro.php @@ -60,7 +60,7 @@ public function nodeOpened(Latte\MacroNode $node) $node->empty = false; $node->openingCode = Latte\PhpWriter::using($node) ->write( - 'global->cacheStorage, %var, $this->global->cacheStack, %node.array?)) { ?>', + 'global->cacheStorage, %var, $this->global->cacheStack, %node.array?)) try { ?>', Nette\Utils\Random::generate() ); } @@ -73,7 +73,11 @@ public function nodeOpened(Latte\MacroNode $node) public function nodeClosed(Latte\MacroNode $node) { $node->closingCode = Latte\PhpWriter::using($node) - ->write('global->cacheStack, %node.array?); } ?>'); + ->write('global->cacheStack, %node.array?); + } catch (\Throwable $__e) { + Nette\Bridges\CacheLatte\CacheMacro::rollback($this->global->cacheStack); throw $__e; + } ?>'); } @@ -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(); + } + } } diff --git a/tests/Bridges.Latte/expected/CacheMacro.cache.inc.phtml b/tests/Bridges.Latte/expected/CacheMacro.cache.inc.phtml index 6118fa78..2c2f930a 100644 --- a/tests/Bridges.Latte/expected/CacheMacro.cache.inc.phtml +++ b/tests/Bridges.Latte/expected/CacheMacro.cache.inc.phtml @@ -1,9 +1,13 @@ 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% diff --git a/tests/Bridges.Latte/expected/CacheMacro.cache.phtml b/tests/Bridges.Latte/expected/CacheMacro.cache.phtml index a040ed79..7eaa0294 100644 --- a/tests/Bridges.Latte/expected/CacheMacro.cache.phtml +++ b/tests/Bridges.Latte/expected/CacheMacro.cache.phtml @@ -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 '

'; echo LR\Filters::escapeHtmlText(($this->filters->upper)($title)) /* line 5 */; @@ -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% }