-
-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathCacheMacro.php
More file actions
168 lines (135 loc) · 3.66 KB
/
CacheMacro.php
File metadata and controls
168 lines (135 loc) · 3.66 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
/**
* This file is part of the Nette Framework (https://nette.org)
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
*/
declare(strict_types=1);
namespace Nette\Bridges\CacheLatte;
use Latte;
use Nette;
use Nette\Caching\Cache;
/**
* Macro {cache} ... {/cache} for Latte v2
*/
final class CacheMacro implements Latte\IMacro
{
use Nette\SmartObject;
private bool $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?)) /* line %var */ try { ?>',
Nette\Utils\Random::generate(),
$node->startLine,
);
}
/**
* Node is closed.
* @return void
*/
public function nodeClosed(Latte\MacroNode $node)
{
$node->closingCode = Latte\PhpWriter::using($node)
->write(
'<?php
Nette\Bridges\CacheLatte\CacheMacro::endCache($this->global->cacheStack, %node.array?) /* line %var */;
} catch (\Throwable $ʟ_e) {
Nette\Bridges\CacheLatte\CacheMacro::rollback($this->global->cacheStack); throw $ʟ_e;
} ?>',
$node->startLine,
);
}
/********************* run-time helpers ****************d*g**/
public static function initRuntime(Latte\Runtime\Template $template): void
{
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.
*/
public static function createCache(
Nette\Caching\Storage $cacheStorage,
string $key,
?array &$parents,
?array $args = null,
): Nette\Caching\OutputHelper|\stdClass|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->capture($key)) {
$parents[] = $helper;
}
return $helper;
}
/**
* Ends the output cache.
* @param Nette\Caching\OutputHelper[] $parents
*/
public static function endCache(array &$parents, ?array $args = null): void
{
$helper = array_pop($parents);
if (!$helper instanceof Nette\Caching\OutputHelper) {
return;
}
if (isset($args['dependencies'])) {
$args += $args['dependencies']();
}
if (isset($args['expire'])) {
$args['expiration'] = $args['expire']; // back compatibility
}
$helper->dependencies[Cache::Tags] = $args['tags'] ?? null;
$helper->dependencies[Cache::Expire] = $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();
}
}
}