1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: mmikkel |
5
|
|
|
* Date: 14/07/2018 |
6
|
|
|
* Time: 15:44 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace mmikkel\cacheflag\twigextensions; |
10
|
|
|
|
11
|
|
|
use mmikkel\cacheflag\CacheFlag; |
12
|
|
|
|
13
|
|
|
use Craft; |
14
|
|
|
use craft\helpers\StringHelper; |
15
|
|
|
|
16
|
|
|
use Twig\Compiler; |
17
|
|
|
use Twig\Node\Node; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class CacheFlagNode |
21
|
|
|
* @package mmikkel\cacheflag\twigextensions |
22
|
|
|
*/ |
23
|
|
|
class CacheFlagNode extends Node |
24
|
|
|
{ |
25
|
|
|
|
26
|
|
|
// Properties |
27
|
|
|
// ========================================================================= |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var int |
31
|
|
|
*/ |
32
|
|
|
private static $_cacheCount = 1; |
33
|
|
|
|
34
|
|
|
// Public Methods |
35
|
|
|
// ========================================================================= |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @inheritdoc |
39
|
|
|
*/ |
40
|
|
|
public function compile(Compiler $compiler) |
41
|
|
|
{ |
42
|
|
|
$n = self::$_cacheCount++; |
43
|
|
|
|
44
|
|
|
$flags = $this->hasNode('flags') ? $this->getNode('flags') : null; |
45
|
|
|
|
46
|
|
|
$conditions = $this->hasNode('conditions') ? $this->getNode('conditions') : null; |
47
|
|
|
$ignoreConditions = $this->hasNode('ignoreConditions') ? $this->getNode('ignoreConditions') : null; |
48
|
|
|
$key = $this->hasNode('key') ? $this->getNode('key') : null; |
49
|
|
|
$expiration = $this->hasNode('expiration') ? $this->getNode('expiration') : null; |
50
|
|
|
|
51
|
|
|
$durationNum = $this->getAttribute('durationNum'); |
52
|
|
|
$durationUnit = $this->getAttribute('durationUnit'); |
53
|
|
|
$global = $this->getAttribute('global') ? 'true' : 'false'; |
54
|
|
|
$elements = $this->getAttribute('elements') ? 'true' : 'false'; |
55
|
|
|
|
56
|
|
|
$compiler |
57
|
|
|
->addDebugInfo($this) |
58
|
|
|
->write('$cacheService = ' . CacheFlag::class . "::getInstance()->templateCaches;\n") |
59
|
|
|
->write('$request = ' . Craft::class . "::\$app->getRequest();\n") |
60
|
|
|
->write("\$ignoreCache{$n} = (\$request->getIsLivePreview() || \$request->getToken()"); |
61
|
|
|
|
62
|
|
|
if ($conditions) { |
63
|
|
|
$compiler |
64
|
|
|
->raw(' || !(') |
65
|
|
|
->subcompile($conditions) |
66
|
|
|
->raw(')'); |
67
|
|
|
} else if ($ignoreConditions) { |
68
|
|
|
$compiler |
69
|
|
|
->raw(' || (') |
70
|
|
|
->subcompile($ignoreConditions) |
71
|
|
|
->raw(')'); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$compiler |
75
|
|
|
->raw(");\n") |
76
|
|
|
->write("if (!\$ignoreCache{$n}) {\n") |
77
|
|
|
->indent() |
78
|
|
|
->write("\$cacheKey{$n} = "); |
79
|
|
|
|
80
|
|
|
if ($key) { |
81
|
|
|
$compiler->subcompile($key); |
82
|
|
|
} else { |
83
|
|
|
$compiler->raw('"' . StringHelper::randomString() . '"'); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$compiler |
87
|
|
|
->raw(";\n") |
88
|
|
|
->write("\$cacheBody{$n} = \$cacheService->getTemplateCache(\$cacheKey{$n}, "); |
89
|
|
|
|
90
|
|
|
if ($flags) { |
91
|
|
|
$compiler->subcompile($flags); |
92
|
|
|
} else { |
93
|
|
|
$compiler->raw('null'); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$compiler |
97
|
|
|
->raw(", {$elements}, {$global});\n") |
98
|
|
|
->outdent() |
99
|
|
|
->write("} else {\n") |
100
|
|
|
->indent() |
101
|
|
|
->write("\$cacheBody{$n} = null;\n") |
102
|
|
|
->outdent() |
103
|
|
|
->write("}\n") |
104
|
|
|
->write("if (\$cacheBody{$n} === null) {\n") |
105
|
|
|
->indent() |
106
|
|
|
->write("if (!\$ignoreCache{$n}) {\n") |
107
|
|
|
->indent() |
108
|
|
|
->write("\$cacheService->startTemplateCache($global);\n") |
109
|
|
|
->outdent() |
110
|
|
|
->write("}\n") |
111
|
|
|
->write("ob_start();\n") |
112
|
|
|
->subcompile($this->getNode('body')) |
113
|
|
|
->write("\$cacheBody{$n} = ob_get_clean();\n") |
114
|
|
|
->write("if (!\$ignoreCache{$n}) {\n") |
115
|
|
|
->indent() |
116
|
|
|
->write("\$cacheService->endTemplateCache(\$cacheKey{$n}, "); |
117
|
|
|
|
118
|
|
|
if ($flags) { |
119
|
|
|
$compiler->subcompile($flags); |
120
|
|
|
} else { |
121
|
|
|
$compiler->raw('null'); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
$compiler->raw(", {$global}, "); |
125
|
|
|
|
126
|
|
|
if ($durationNum) { |
127
|
|
|
// So silly that PHP doesn't support "+1 week" http://www.php.net/manual/en/datetime.formats.relative.php |
128
|
|
|
|
129
|
|
|
if ($durationUnit === 'week') { |
130
|
|
|
if ($durationNum == 1) { |
131
|
|
|
$durationNum = 7; |
132
|
|
|
$durationUnit = 'days'; |
133
|
|
|
} else { |
134
|
|
|
$durationUnit = 'weeks'; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
$compiler->raw("'+{$durationNum} {$durationUnit}'"); |
139
|
|
|
} else { |
140
|
|
|
$compiler->raw('null'); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
$compiler->raw(', '); |
144
|
|
|
|
145
|
|
|
if ($expiration) { |
146
|
|
|
$compiler->subcompile($expiration); |
147
|
|
|
} else { |
148
|
|
|
$compiler->raw('null'); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
$compiler |
152
|
|
|
->raw(", \$cacheBody{$n});\n") |
153
|
|
|
->outdent() |
154
|
|
|
->write("}\n") |
155
|
|
|
->outdent() |
156
|
|
|
->write("}\n") |
157
|
|
|
->write("echo \$cacheBody{$n};\n"); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
} |
161
|
|
|
|