Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions src/Bridges/CacheLatte/CacheMacro.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ public function nodeOpened(Latte\MacroNode $node)
*/
public function nodeClosed(Latte\MacroNode $node)
{
$node->closingCode = '<?php $_tmp = array_pop($this->global->cacheStack); if (!$_tmp instanceof stdClass) $_tmp->end(); } ?>';
$node->closingCode = Latte\PhpWriter::using($node)
->write('<?php Nette\Bridges\CacheLatte\CacheMacro::endCache($this->global->cacheStack, %node.array?); } ?>');
}


Expand Down Expand Up @@ -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;
}

}
9 changes: 6 additions & 3 deletions tests/Bridges.Latte/CacheMacro.createCache.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

Expand All @@ -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);
});

Expand All @@ -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);
});
3 changes: 1 addition & 2 deletions tests/Bridges.Latte/expected/CacheMacro.cache.inc.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ class Template%a% extends Latte\Runtime\Template
?> <?php echo %a% ?>

<?php
$_tmp = array_pop($this->global->cacheStack);
if (!$_tmp instanceof stdClass) $_tmp->end();
Nette\Bridges\CacheLatte\CacheMacro::endCache($this->global->cacheStack);
}
%A%
}
Expand Down
3 changes: 1 addition & 2 deletions tests/Bridges.Latte/expected/CacheMacro.cache.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ Noncached content
?>

<?php
$_tmp = array_pop($this->global->cacheStack);
if (!$_tmp instanceof stdClass) $_tmp->end();
Nette\Bridges\CacheLatte\CacheMacro::endCache($this->global->cacheStack, [$id, 'tags' => 'mytag']);
}
%A%
}
Expand Down