Skip to content

Commit 7cab46d

Browse files
committed
CacheMacro: wrapped in try ... finally
1 parent 1d4e43e commit 7cab46d

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

src/Bridges/CacheLatte/CacheMacro.php

+18-2
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function nodeOpened(Latte\MacroNode $node)
6060
$node->empty = false;
6161
$node->openingCode = Latte\PhpWriter::using($node)
6262
->write(
63-
'<?php if (Nette\Bridges\CacheLatte\CacheMacro::createCache($this->global->cacheStorage, %var, $this->global->cacheStack, %node.array?)) { ?>',
63+
'<?php if (Nette\Bridges\CacheLatte\CacheMacro::createCache($this->global->cacheStorage, %var, $this->global->cacheStack, %node.array?)) try { ?>',
6464
Nette\Utils\Random::generate()
6565
);
6666
}
@@ -73,7 +73,11 @@ public function nodeOpened(Latte\MacroNode $node)
7373
public function nodeClosed(Latte\MacroNode $node)
7474
{
7575
$node->closingCode = Latte\PhpWriter::using($node)
76-
->write('<?php Nette\Bridges\CacheLatte\CacheMacro::endCache($this->global->cacheStack, %node.array?); } ?>');
76+
->write('<?php
77+
Nette\Bridges\CacheLatte\CacheMacro::endCache($this->global->cacheStack, %node.array?);
78+
} catch (\Throwable $__e) {
79+
Nette\Bridges\CacheLatte\CacheMacro::rollback($this->global->cacheStack); throw $__e;
80+
} ?>');
7781
}
7882

7983

@@ -140,4 +144,16 @@ public static function endCache(array &$parents, array $args = null): void
140144
$helper->dependencies[Cache::EXPIRATION] = $args['expiration'] ?? '+ 7 days';
141145
$helper->end();
142146
}
147+
148+
149+
/**
150+
* @param Nette\Caching\OutputHelper[] $parents
151+
*/
152+
public static function rollback(array &$parents): void
153+
{
154+
$helper = array_pop($parents);
155+
if ($helper instanceof Nette\Caching\OutputHelper) {
156+
$helper->rollback();
157+
}
158+
}
143159
}
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
<?php
22
%A%
3-
if (Nette\Bridges\CacheLatte\CacheMacro::createCache($this->global->cacheStorage, '%[\w]+%', $this->global->cacheStack)) {
3+
if (Nette\Bridges\CacheLatte\CacheMacro::createCache($this->global->cacheStorage, '%[\w]+%', $this->global->cacheStack)) try {
44
echo ' ';
55
echo LR\Filters::escapeHtmlText(($this->filters->lower)($title)) /* line 4 */;
66
echo "\n";
77
Nette\Bridges\CacheLatte\CacheMacro::endCache($this->global->cacheStack);
88
}
9+
catch (\Throwable $__e) {
10+
Nette\Bridges\CacheLatte\CacheMacro::rollback($this->global->cacheStack);
11+
throw $__e;
12+
}
913
%A%

tests/Bridges.Latte/expected/CacheMacro.cache.phtml

+5-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
echo 'Noncached content
44
55
';
6-
if (Nette\Bridges\CacheLatte\CacheMacro::createCache($this->global->cacheStorage, '%[\w]+%', $this->global->cacheStack, [$id, 'tags' => 'mytag'])) {
6+
if (Nette\Bridges\CacheLatte\CacheMacro::createCache($this->global->cacheStorage, '%[\w]+%', $this->global->cacheStack, [$id, 'tags' => 'mytag'])) try {
77
echo '
88
<h1>';
99
echo LR\Filters::escapeHtmlText(($this->filters->upper)($title)) /* line 5 */;
@@ -15,6 +15,10 @@
1515
echo "\n";
1616
Nette\Bridges\CacheLatte\CacheMacro::endCache($this->global->cacheStack, [$id, 'tags' => 'mytag']);
1717
}
18+
catch (\Throwable $__e) {
19+
Nette\Bridges\CacheLatte\CacheMacro::rollback($this->global->cacheStack);
20+
throw $__e;
21+
}
1822
%A%
1923
}
2024

0 commit comments

Comments
 (0)