1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
|
<?php
/**
* This file is part of the Nette Framework (https://nette.org)
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
*/
namespace Nette\Bridges\CacheLatte;
use Nette;
use Nette\Caching\Cache;
use Latte;
/**
* Macro {cache} ... {/cache}
*/
class CacheMacro implements Latte\IMacro
{
use Nette\SmartObject;
/** @var bool */
private $used;
/**
* Initializes before template parsing.
* @return void
*/
public function initialize()
{
$this->used = FALSE;
}
/**
* Finishes template parsing.
* @return array(prolog, epilog)
*/
public function finalize()
{
if ($this->used) {
return ['Nette\Bridges\CacheLatte\CacheMacro::initRuntime($this);'];
}
}
/**
* New node is found.
* @return bool
*/
public function nodeOpened(Latte\MacroNode $node)
{
if ($node->modifiers) {
throw new Latte\CompileException('Modifiers are not allowed in ' . $node->getNotation());
}
$this->used = TRUE;
$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?)) { ?>',
Nette\Utils\Random::generate()
);
}
/**
* Node is closed.
* @return void
*/
public function nodeClosed(Latte\MacroNode $node)
{
$node->closingCode = '<?php $_tmp = array_pop($this->global->cacheStack); if (!$_tmp instanceof stdClass) $_tmp->end(); } ?>';
}
/********************* run-time helpers ****************d*g**/
/**
* @return void
*/
public static function initRuntime(Latte\Runtime\Template $template)
{
if (!empty($template->global->cacheStack)) {
$file = (new \ReflectionClass($template))->getFileName();
if (@is_file($file)) { // @ - may trigger error
end($template->global->cacheStack)->dependencies[Cache::FILES][] = $file;
}
}
}
/**
* Starts the output cache. Returns Nette\Caching\OutputHelper object if buffering was started.
* @param Nette\Caching\IStorage
* @param string
* @param Nette\Caching\OutputHelper[]
* @param array
* @return Nette\Caching\OutputHelper
*/
public static function createCache(Nette\Caching\IStorage $cacheStorage, $key, & $parents, array $args = NULL)
{
if ($args) {
if (array_key_exists('if', $args) && !$args['if']) {
return $parents[] = new \stdClass;
}
$key = array_merge([$key], array_intersect_key($args, range(0, count($args))));
}
if ($parents) {
end($parents)->dependencies[Cache::ITEMS][] = $key;
}
$cache = new Cache($cacheStorage, 'Nette.Templating.Cache');
if ($helper = $cache->start($key)) {
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;
}
return $helper;
}
}
|