-
Notifications
You must be signed in to change notification settings - Fork 645
/
Copy pathTemplateCaches.php
439 lines (384 loc) · 15.3 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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
<?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 Illuminate\Support\Collection;
use Throwable;
use yii\base\Component;
use yii\base\Exception;
use yii\caching\TagDependency;
use yii\web\AssetBundle;
/**
* 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 bool Whether global template caches should be enabled for this request
* @see _isTemplateCachingEnabled()
*/
private bool $_enabledGlobally;
/**
* @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($global) === false) {
return null;
}
$cacheKey = $this->_cacheKey($key, $global);
$data = Craft::$app->getCache()->get($cacheKey);
if ($data === false) {
return null;
}
[$body, $cacheInfo, $bufferedJs, $bufferedScripts, $bufferedCss, $bufferedJsFiles, $bufferedCssFiles, $bufferedHtml, $bufferedMetaTags, $bufferedAssetBundles] = array_pad($data, 10, null);
// If we're actively collecting element cache info, register this cache's tags and duration
$elementsService = Craft::$app->getElements();
if ($elementsService->getIsCollectingCacheInfo()) {
if (isset($cacheInfo['tags'])) {
$elementsService->collectCacheTags($cacheInfo['tags']);
$elementsService->setCacheExpiryDate(DateTimeHelper::toDateTime($cacheInfo['expiryDate']));
} else {
$elementsService->collectCacheTags($cacheInfo);
}
}
// Register JS, CSS and meta tags
if ($registerResources) {
$this->_registerResources(
$bufferedJs ?? [],
$bufferedScripts ?? [],
$bufferedCss ?? [],
$bufferedJsFiles ?? [],
$bufferedCssFiles ?? [],
$bufferedHtml ?? [],
$bufferedMetaTags ?? [],
$bufferedAssetBundles ?? [],
);
}
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()]], [[\craft\web\View::registerCssFile()]], and [[\craft\web\View::registerHtml()]]
* should be captured and included in the cache. If this is `true`, be sure to pass `$withResources = true`
* to [[endTemplateCache()]] as well.
* @param bool $global Whether the cache should be stored globally.
*/
public function startTemplateCache(bool $withResources = false, bool $global = false): void
{
// Make sure template caching is enabled
if ($this->_isTemplateCachingEnabled($global) === false) {
return;
}
Craft::$app->getElements()->startCollectingCacheInfo();
if ($withResources) {
$view = Craft::$app->getView();
$view->startJsBuffer();
$view->startScriptBuffer();
$view->startCssBuffer();
$view->startJsFileBuffer();
$view->startCssFileBuffer();
$view->startHtmlBuffer();
$view->startMetaTagBuffer();
$view->startAssetBundleBuffer();
}
}
/**
* 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 statement](https://www.php.net/manual/en/datetime.formats.php#datetime.formats.relative).
* @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()]], [[\craft\web\View::registerCssFile()]], and [[\craft\web\View::registerHtml()]]
* 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($global) === false) {
return;
}
[$dep, $maxDuration] = Craft::$app->getElements()->stopCollectingCacheInfo();
if ($withResources) {
$view = Craft::$app->getView();
$bufferedJs = $view->clearJsBuffer(false, false);
$bufferedScripts = $view->clearScriptBuffer();
$bufferedCss = $view->clearCssBuffer();
$bufferedJsFiles = $view->clearJsFileBuffer();
$bufferedCssFiles = $view->clearCssFileBuffer();
$bufferedHtml = $view->clearHtmlBuffer();
$bufferedMetaTags = $view->clearMetaTagBuffer();
$bufferedAssetBundles = $view->clearAssetBundleBuffer();
}
// 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.
$saveCache = !StringHelper::contains(stripslashes($body), 'assets/generate-transform');
if ($saveCache) {
if (!$dep) {
$dep = new TagDependency();
}
// Always add a `template` tag
$dep->tags[] = 'template';
if ($maxDuration) {
$expiryDate = DateTimeHelper::now()->modify("+$maxDuration seconds");
$cacheInfo = [
'tags' => $dep->tags,
'expiryDate' => DateTimeHelper::toIso8601($expiryDate),
];
} else {
$cacheInfo = $dep->tags;
}
$cacheValue = [$body, $cacheInfo];
}
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');
$bufferedMetaTags = $this->_parseSelfClosingTags($bufferedMetaTags);
$bufferedAssetBundles = Collection::make($bufferedAssetBundles)
->map(fn(AssetBundle $bundle, string $name) => [$name, $bundle->jsOptions['position'] ?? null])
->values()
->all();
if ($saveCache) {
array_push(
$cacheValue,
$bufferedJs,
$bufferedScripts,
$bufferedCss,
$bufferedJsFiles,
$bufferedCssFiles,
$bufferedHtml,
$bufferedMetaTags,
$bufferedAssetBundles,
);
}
// Re-register the JS and CSS
$this->_registerResources(
$bufferedJs,
$bufferedScripts,
$bufferedCss,
$bufferedJsFiles,
$bufferedCssFiles,
$bufferedHtml,
$bufferedMetaTags,
$bufferedAssetBundles,
);
}
if (!$saveCache) {
return;
}
$cacheKey = $this->_cacheKey($key, $global);
// Normalize duration/expiration into an integer duration
if ($duration !== null) {
$expiration = (new DateTime($duration));
}
if ($expiration !== null) {
$duration = DateTimeHelper::toDateTime($expiration)->getTimestamp() - DateTimeHelper::currentTimeStamp();
}
if ($duration <= 0) {
$duration = null;
}
if ($maxDuration) {
$duration = $duration ? min($duration, $maxDuration) : $maxDuration;
}
/** @phpstan-ignore-next-line */
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);
}
/**
* Parse each tag and return an array of its attributes
* where the key is the name of the attribute and the value is its value.
*
* @param array $tags
* @return array
*/
private function _parseSelfClosingTags(array $tags): array
{
return array_map(function($tag) {
return Html::parseTagAttributes($tag);
}, $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,
array $bufferedHtml,
array $bufferedMetaTags,
array $bufferedAssetBundles,
): 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);
}
foreach ($bufferedHtml as $pos => $tags) {
foreach ($tags as $key => $html) {
$view->registerHtml($html, $pos, $key);
}
}
foreach ($bufferedMetaTags as $key => $options) {
$view->registerMetaTag($options, $key);
}
foreach ($bufferedAssetBundles as [$name, $position]) {
$view->registerAssetBundle($name, $position);
}
}
/**
* Returns whether template caching is enabled, based on the 'enableTemplateCaching' config setting.
*
* @param bool $global Whether this is for a globally-scoped cache
* @return bool Whether template caching is enabled
*/
private function _isTemplateCachingEnabled(bool $global): bool
{
if (!isset($this->_enabled)) {
if (!Craft::$app->getConfig()->getGeneral()->enableTemplateCaching) {
$this->_enabled = $this->_enabledGlobally = false;
} else {
// Don't enable template caches for Live Preview/tokenized requests
$request = Craft::$app->getRequest();
if ($request->getIsPreview() || $request->getHadToken()) {
$this->_enabled = $this->_enabledGlobally = false;
} else {
$this->_enabled = !$request->getIsConsoleRequest();
$this->_enabledGlobally = true;
}
}
}
return $global ? $this->_enabledGlobally : $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.');
}
$isCpRequest = $request->getIsCpRequest();
if ($isCpRequest) {
$this->_path = 'cp:';
} else {
$this->_path = 'site:';
}
$this->_path .= $request->getPathInfo();
if (!Craft::$app->getDb()->getSupportsMb4()) {
$this->_path = StringHelper::encodeMb4($this->_path);
}
$pageNum = $request->getPageNum();
if ($pageNum !== 1) {
$pageTrigger = $isCpRequest ? 'p' : Craft::$app->getConfig()->getGeneral()->getPageTrigger();
$this->_path .= sprintf('/%s%s', $pageTrigger, $pageNum);
}
return $this->_path;
}
}