From 67600beae5a11f8558779e972677014c31f71697 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADt=20Kutn=C3=BD?= Date: Mon, 2 Jan 2017 17:48:28 +0100 Subject: [PATCH] Cache: create dependencies when closing macro - prevents invalid expiration from dependencies callback when caching time-expensive data --- src/Bridges/CacheLatte/CacheMacro.php | 22 +++++++++++++------ .../Bridges.Latte/CacheMacro.createCache.phpt | 9 +++++--- .../expected/CacheMacro.cache.inc.phtml | 3 +-- .../expected/CacheMacro.cache.phtml | 3 +-- 4 files changed, 23 insertions(+), 14 deletions(-) diff --git a/src/Bridges/CacheLatte/CacheMacro.php b/src/Bridges/CacheLatte/CacheMacro.php index f2d60412..97f7f912 100644 --- a/src/Bridges/CacheLatte/CacheMacro.php +++ b/src/Bridges/CacheLatte/CacheMacro.php @@ -69,7 +69,8 @@ public function nodeOpened(Latte\MacroNode $node) */ public function nodeClosed(Latte\MacroNode $node) { - $node->closingCode = 'global->cacheStack); if (!$_tmp instanceof stdClass) $_tmp->end(); } ?>'; + $node->closingCode = Latte\PhpWriter::using($node) + ->write('global->cacheStack, %node.array?); } ?>'); } @@ -112,19 +113,26 @@ public static function createCache(Nette\Caching\IStorage $cacheStorage, $key, & $cache = new Cache($cacheStorage, 'Nette.Templating.Cache'); if ($helper = $cache->start($key)) { + $parents[] = $helper; + } + return $helper; + } + + + public static function endCache(& $parents, array $args = NULL) + { + $helper = array_pop($parents); + if ($helper instanceof Nette\Caching\OutputHelper) { if (isset($args['dependencies'])) { $args += call_user_func($args['dependencies']); } if (isset($args['expire'])) { $args['expiration'] = $args['expire']; // back compatibility } - $helper->dependencies = [ - $cache::TAGS => isset($args['tags']) ? $args['tags'] : NULL, - $cache::EXPIRATION => isset($args['expiration']) ? $args['expiration'] : '+ 7 days', - ]; - $parents[] = $helper; + $helper->dependencies[Nette\Caching\Cache::TAGS] = isset($args['tags']) ? $args['tags'] : NULL; + $helper->dependencies[Nette\Caching\Cache::EXPIRATION] = isset($args['expiration']) ? $args['expiration'] : '+ 7 days'; + $helper->end(); } - return $helper; } } diff --git a/tests/Bridges.Latte/CacheMacro.createCache.phpt b/tests/Bridges.Latte/CacheMacro.createCache.phpt index 01dcfea3..500e54d0 100644 --- a/tests/Bridges.Latte/CacheMacro.createCache.phpt +++ b/tests/Bridges.Latte/CacheMacro.createCache.phpt @@ -15,8 +15,9 @@ require __DIR__ . '/../bootstrap.php'; test(function () { $parents = []; $dp = [Cache::TAGS => ['rum', 'cola']]; - $outputHelper = CacheMacro::createCache(new DevNullStorage(), 'test', $parents, $dp); + $outputHelper = CacheMacro::createCache(new DevNullStorage(), 'test', $parents); Assert::type(Nette\Caching\OutputHelper::class, $outputHelper); + CacheMacro::endCache($parents, $dp); Assert::same($dp + [Cache::EXPIRATION => '+ 7 days'], $outputHelper->dependencies); }); @@ -26,7 +27,8 @@ test(function () { $dpFallback = function () use ($dp) { return $dp; }; - $outputHelper = CacheMacro::createCache(new DevNullStorage(), 'test', $parents, ['dependencies' => $dpFallback]); + $outputHelper = CacheMacro::createCache(new DevNullStorage(), 'test', $parents); + CacheMacro::endCache($parents, ['dependencies' => $dpFallback]); Assert::same($dp + [Cache::EXPIRATION => '+ 7 days'], $outputHelper->dependencies); }); @@ -39,6 +41,7 @@ test(function () { $dpFallback = function () use ($dp) { return $dp; }; - $outputHelper = CacheMacro::createCache(new DevNullStorage(), 'test', $parents, ['dependencies' => $dpFallback]); + $outputHelper = CacheMacro::createCache(new DevNullStorage(), 'test', $parents); + CacheMacro::endCache($parents, ['dependencies' => $dpFallback]); Assert::same($dp, $outputHelper->dependencies); }); diff --git a/tests/Bridges.Latte/expected/CacheMacro.cache.inc.phtml b/tests/Bridges.Latte/expected/CacheMacro.cache.inc.phtml index 0754afd1..d71c9c39 100644 --- a/tests/Bridges.Latte/expected/CacheMacro.cache.inc.phtml +++ b/tests/Bridges.Latte/expected/CacheMacro.cache.inc.phtml @@ -11,8 +11,7 @@ class Template%a% extends Latte\Runtime\Template ?> global->cacheStack); - if (!$_tmp instanceof stdClass) $_tmp->end(); + Nette\Bridges\CacheLatte\CacheMacro::endCache($this->global->cacheStack); } %A% } diff --git a/tests/Bridges.Latte/expected/CacheMacro.cache.phtml b/tests/Bridges.Latte/expected/CacheMacro.cache.phtml index 287838b9..12289c74 100644 --- a/tests/Bridges.Latte/expected/CacheMacro.cache.phtml +++ b/tests/Bridges.Latte/expected/CacheMacro.cache.phtml @@ -21,8 +21,7 @@ Noncached content ?> global->cacheStack); - if (!$_tmp instanceof stdClass) $_tmp->end(); + Nette\Bridges\CacheLatte\CacheMacro::endCache($this->global->cacheStack, [$id, 'tags' => 'mytag']); } %A% }