-
Notifications
You must be signed in to change notification settings - Fork 619
/
TemplateCaches.php
315 lines (270 loc) · 10.8 KB
/
TemplateCaches.php
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/
namespace craft\services;
use Craft;
use craft\helpers\ArrayHelper;
use craft\helpers\DateTimeHelper;
use craft\helpers\Html;
use craft\helpers\StringHelper;
use DateTime;
use Throwable;
use yii\base\Component;
use yii\base\Exception;
/**
* Template Caches service.
*
* An instance of the service is available via [[\craft\base\ApplicationTrait::getTemplateCaches()|`Craft::$app->templateCaches`]].
*
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
* @since 3.0.0
*/
class TemplateCaches extends Component
{
/**
* @var bool Whether template caching should be enabled for this request
* @see _isTemplateCachingEnabled()
*/
private bool $_enabled;
/**
* @var string|null The current request's path
* @see _path()
*/
private ?string $_path = null;
/**
* Returns a cached template by its key.
*
* @param string $key The template cache key
* @param bool $global Whether the cache would have been stored globally.
* @param bool $registerResources Whether JS and CSS resources captured by the cache should be registered
* @return string|null
* @throws Exception if this is a console request and `false` is passed to `$global`
*/
public function getTemplateCache(string $key, bool $global, bool $registerResources = false): ?string
{
// Make sure template caching is enabled
if ($this->_isTemplateCachingEnabled() === false) {
return null;
}
$cacheKey = $this->_cacheKey($key, $global);
$data = Craft::$app->getCache()->get($cacheKey);
if ($data === false) {
return null;
}
[$body, $tags, $bufferedJs, $bufferedScripts, $bufferedCss, $bufferedJsFiles, $bufferedCssFiles] = array_pad($data, 7, null);
// If we're actively collecting element cache tags, add this cache's tags to the collection
Craft::$app->getElements()->collectCacheTags($tags);
// Register JS and CSS tags
if ($registerResources) {
$this->_registerResources(
$bufferedJs ?? [],
$bufferedScripts ?? [],
$bufferedCss ?? [],
$bufferedJsFiles ?? [],
$bufferedCssFiles ?? [],
);
}
return $body;
}
/**
* Starts a new template cache.
*
* @param bool $withResources Whether JS and CSS code registered with [[\craft\web\View::registerJs()]],
* [[\craft\web\View::registerScript()]], [[\craft\web\View::registerCss()]],
* [[\craft\web\View::registerJsFile()]], and [[\craft\web\View::registerCssFile()]] should be captured and
* included in the cache. If this is `true`, be sure to pass `$withResources = true` to [[endTemplateCache()]]
* as well.
*/
public function startTemplateCache(bool $withResources = false): void
{
// Make sure template caching is enabled
if ($this->_isTemplateCachingEnabled() === false) {
return;
}
Craft::$app->getElements()->startCollectingCacheTags();
if ($withResources) {
$view = Craft::$app->getView();
$view->startJsBuffer();
$view->startScriptBuffer();
$view->startCssBuffer();
$view->startJsFileBuffer();
$view->startCssFileBuffer();
}
}
/**
* Ends a template cache.
*
* @param string $key The template cache key.
* @param bool $global Whether the cache should be stored globally.
* @param string|null $duration How long the cache should be stored for. Should be a [relative time format](https://php.net/manual/en/datetime.formats.relative.php).
* @param mixed $expiration When the cache should expire.
* @param string $body The contents of the cache.
* @param bool $withResources Whether JS and CSS code registered with [[\craft\web\View::registerJs()]],
* [[\craft\web\View::registerScript()]], [[\craft\web\View::registerCss()]],
* [[\craft\web\View::registerJsFile()]], and [[\craft\web\View::registerCssFile()]] should be captured
* and included in the cache.
* @throws Exception if this is a console request and `false` is passed to `$global`
* @throws Throwable
*/
public function endTemplateCache(string $key, bool $global, ?string $duration, mixed $expiration, string $body, bool $withResources = false): void
{
// Make sure template caching is enabled
if ($this->_isTemplateCachingEnabled() === false) {
return;
}
$dep = Craft::$app->getElements()->stopCollectingCacheTags();
if ($withResources) {
$view = Craft::$app->getView();
$bufferedJs = $view->clearJsBuffer(false, false);
$bufferedScripts = $view->clearScriptBuffer();
$bufferedCss = $view->clearCssBuffer();
$bufferedJsFiles = $view->clearJsFileBuffer();
$bufferedCssFiles = $view->clearCssFileBuffer();
}
// If there are any transform generation URLs in the body, don't cache it.
// stripslashes($body) in case the URL has been JS-encoded or something.
if (StringHelper::contains(stripslashes($body), 'assets/generate-transform')) {
return;
}
// Always add a `template` tag
$dep->tags[] = 'template';
$cacheValue = [$body, $dep->tags];
if ($withResources) {
// Parse the JS/CSS code and tag attributes out of the <script> and <style> tags
$bufferedScripts = array_map(fn(array $tags) => $this->_parseInlineResourceTags($tags), $bufferedScripts);
$bufferedCss = $this->_parseInlineResourceTags($bufferedCss);
$bufferedJsFiles = array_map(fn(array $tags) => $this->_parseExternalResourceTags($tags, 'src'), $bufferedJsFiles);
$bufferedCssFiles = $this->_parseExternalResourceTags($bufferedCssFiles, 'href');
array_push($cacheValue, $bufferedJs, $bufferedScripts, $bufferedCss, $bufferedJsFiles, $bufferedCssFiles);
// Re-register the JS and CSS
$this->_registerResources($bufferedJs, $bufferedScripts, $bufferedCss, $bufferedJsFiles, $bufferedCssFiles);
}
$cacheKey = $this->_cacheKey($key, $global);
if ($duration !== null) {
$expiration = (new DateTime($duration));
}
if ($expiration !== null) {
$duration = DateTimeHelper::toDateTime($expiration)->getTimestamp() - time();
}
Craft::$app->getCache()->set($cacheKey, $cacheValue, $duration, $dep);
}
private function _parseInlineResourceTags(array $tags): array
{
return array_map(function($tag) {
$tag = Html::parseTag($tag);
return [$tag['children'][0]['value'], $tag['attributes']];
}, $tags);
}
private function _parseExternalResourceTags(array $tags, string $urlAttribute): array
{
return array_map(function($tag) use ($urlAttribute) {
[$tag, $condition] = Html::unwrapCondition($tag);
[$tag, $noscript] = Html::unwrapNoscript($tag);
$tag = Html::parseTag($tag);
$url = ArrayHelper::remove($tag['attributes'], $urlAttribute);
$options = $tag['attributes'];
if ($condition) {
$options['condition'] = $condition;
}
if ($noscript) {
$options['noscript'] = true;
}
return [$url, $options];
}, $tags);
}
private function _registerResources(
array $bufferedJs,
array $bufferedScripts,
array $bufferedCss,
array $bufferedJsFiles,
array $bufferedCssFiles,
): void {
$view = Craft::$app->getView();
foreach ($bufferedJs as $pos => $scripts) {
foreach ($scripts as $key => $js) {
$view->registerJs($js, $pos, $key);
}
}
foreach ($bufferedScripts as $pos => $tags) {
foreach ($tags as $key => [$script, $options]) {
$view->registerScript($script, $pos, $options, $key);
}
}
foreach ($bufferedCss as $key => [$css, $options]) {
$view->registerCss($css, $options, $key);
}
foreach ($bufferedJsFiles as $pos => $tags) {
foreach ($tags as $key => [$url, $options]) {
$options['position'] = $pos;
$view->registerJsFile($url, $options, $key);
}
}
foreach ($bufferedCssFiles as $key => [$url, $options]) {
$view->registerCssFile($url, $options, $key);
}
}
/**
* Returns whether template caching is enabled, based on the 'enableTemplateCaching' config setting.
*
* @return bool Whether template caching is enabled
*/
private function _isTemplateCachingEnabled(): bool
{
if (!isset($this->_enabled)) {
$this->_enabled = (
Craft::$app->getConfig()->getGeneral()->enableTemplateCaching &&
!Craft::$app->getRequest()->getIsConsoleRequest()
);
}
return $this->_enabled;
}
/**
* Defines a data cache key that should be used for a template cache.
*
* @param string $key
* @param bool $global
* @param int|null $siteId
* @return string
* @throws Exception if this is a console request and `false` is passed to `$global`
*/
private function _cacheKey(string $key, bool $global, ?int $siteId = null): string
{
$cacheKey = "template::$key::" . ($siteId ?? Craft::$app->getSites()->getCurrentSite()->id);
if (!$global) {
$cacheKey .= '::' . $this->_path();
}
return $cacheKey;
}
/**
* Returns the current request path, including a "site:" or "cp:" prefix.
*
* @return string
* @throws Exception if this is a console request
*/
private function _path(): string
{
if (isset($this->_path)) {
return $this->_path;
}
$request = Craft::$app->getRequest();
if ($request->getIsConsoleRequest()) {
throw new Exception('Not possible to determine the request path for console commands.');
}
if (Craft::$app->getRequest()->getIsCpRequest()) {
$this->_path = 'cp:';
} else {
$this->_path = 'site:';
}
$this->_path .= Craft::$app->getRequest()->getPathInfo();
if (Craft::$app->getDb()->getIsMysql()) {
$this->_path = StringHelper::encodeMb4($this->_path);
}
if (($pageNum = Craft::$app->getRequest()->getPageNum()) != 1) {
$this->_path .= '/' . Craft::$app->getConfig()->getGeneral()->getPageTrigger() . $pageNum;
}
return $this->_path;
}
}