-
Notifications
You must be signed in to change notification settings - Fork 690
Expand file tree
/
Copy pathTemplate.php
More file actions
419 lines (378 loc) · 13.3 KB
/
Template.php
File metadata and controls
419 lines (378 loc) · 13.3 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
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
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/
namespace craft\helpers;
use Craft;
use craft\base\ElementInterface;
use craft\db\Paginator;
use craft\web\twig\variables\Paginate;
use craft\web\View;
use Stringable;
use Twig\Environment;
use Twig\Error\RuntimeError;
use Twig\Extension\CoreExtension;
use Twig\Extension\SandboxExtension;
use Twig\Markup;
use Twig\Source;
use Twig\Template as TwigTemplate;
use Twig\TemplateWrapper;
use yii\base\BaseObject;
use yii\base\InvalidConfigException;
use yii\base\UnknownMethodException;
use yii\base\UnknownPropertyException;
use yii\db\Query;
use yii\db\QueryInterface;
/**
* Class Template
*
* @author Pixel & Tonic, Inc. <[email protected]>
* @since 3.0.0
*/
class Template
{
public const PROFILE_TYPE_TEMPLATE = 'template';
public const PROFILE_TYPE_BLOCK = 'block';
public const PROFILE_TYPE_MACRO = 'macro';
public const PROFILE_STAGE_BEGIN = 'begin';
public const PROFILE_STAGE_END = 'end';
/**
* @var bool Whether to enable profiling for this request
* @see _shouldProfile()
*/
private static bool $_shouldProfile;
/**
* @var array Counters for template elements being profiled
* @see beginProfile()
* @see endProfile()
*/
private static array $_profileCounters;
/**
* @var array Dynamically-defined fallback variables
* @see fallbackExists()
* @see fallback()
*/
private static array $_fallbacks = [];
/**
* Returns whether a fallback variable has been defined.
*
* @param string $name
* @return bool
* @since 4.4.0
* @deprecated in 5.9.15
*/
public static function fallbackExists(string $name): bool
{
return isset(self::$_fallbacks[$name]);
}
/**
* Provides dynamically-defined fallback variable’s value.
*
* @param string $name
* @throws UnknownPropertyException if `$name` isn’t defined as a fallback variable.
* @since 4.4.0
* @deprecated in 5.9.15
*/
public static function fallback(string $name): mixed
{
if (!static::fallbackExists($name)) {
throw new UnknownPropertyException("$name is not defined as a fallback template variable.");
}
return self::$_fallbacks[$name];
}
/**
* Returns the attribute value for a given array/object.
*
* @param Environment $env
* @param Source $source
* @param mixed $object The object or array from where to get the item
* @param mixed $item The item to get from the array or object
* @param array $arguments An array of arguments to pass if the item is an object method
* @param string $type The type of attribute (@see [[TwigTemplate]] constants)
* @param bool $isDefinedTest Whether this is only a defined check
* @param bool $ignoreStrictCheck Whether to ignore the strict attribute check or not
* @param bool $sandboxed Whether sandboxing is enabled
* @param int $lineno The template line where the attribute was called
* @return mixed The attribute value, or a Boolean when $isDefinedTest is true, or null when the attribute is not set and $ignoreStrictCheck is true
* @throws RuntimeError if the attribute does not exist and Twig is running in strict mode and $isDefinedTest is false
* @internal
*/
public static function attribute(
Environment $env,
Source $source,
mixed $object,
mixed $item,
array $arguments = [],
string $type = TwigTemplate::ANY_CALL,
bool $isDefinedTest = false,
bool $ignoreStrictCheck = false,
bool $sandboxed = false,
int $lineno = -1,
): mixed {
// Include this element in any active caches
if ($object instanceof ElementInterface) {
Craft::$app->getElements()->collectCacheInfoForElement($object);
}
if (
$type !== TwigTemplate::METHOD_CALL &&
$object instanceof BaseObject &&
$object->canGetProperty($item)
) {
if ($isDefinedTest) {
return true;
}
if ($sandboxed) {
$env->getExtension(SandboxExtension::class)->checkPropertyAllowed($object, $item, $lineno, $source);
}
return $object->$item;
}
// Convert any \Twig\Markup arguments back to strings (unless the class *extends* \Twig\Markup)
foreach ($arguments as $key => $value) {
if (is_object($value) && get_class($value) === Markup::class) {
$arguments[$key] = (string)$value;
}
}
try {
// workaround for https://github.com/twigphp/Twig/issues/4701
if ($type !== TwigTemplate::METHOD_CALL && $item instanceof Stringable) {
$item = (string) $item;
}
return CoreExtension::getAttribute(
$env,
$source,
$object,
$item,
$arguments,
$type,
$isDefinedTest,
$ignoreStrictCheck,
$sandboxed,
$lineno,
);
} catch (UnknownMethodException $e) {
// Copy twig_get_attribute()'s BadMethodCallException handling
if ($ignoreStrictCheck || !$env->isStrictVariables()) {
return null;
}
throw new RuntimeError($e->getMessage(), -1, $source);
}
}
/**
* Paginates a query.
*
* @param QueryInterface $query
* @return array
* @deprecated in 3.6.0. Use [[paginateQuery()]] instead.
*/
public static function paginateCriteria(QueryInterface $query): array
{
return static::paginateQuery($query);
}
/**
* Paginates a query.
*
* @param QueryInterface $query
* @return array
* @since 3.6.0
*/
public static function paginateQuery(QueryInterface $query): array
{
/** @var Query $query */
$paginator = new Paginator((clone $query)->limit(null), [
'currentPage' => Craft::$app->getRequest()->getPageNum(),
'pageSize' => $query->limit ?: 100,
]);
return [
Paginate::create($paginator),
$paginator->getPageResults(),
];
}
/**
* Returns a string wrapped in a \Twig\Markup object
*
* @param string $value
* @return Markup
*/
public static function raw(string $value): Markup
{
return new Markup($value, Craft::$app->charset);
}
/**
* Begins profiling a template element.
*
* @param string $type The type of template element being profiled ('template', 'block', or 'macro')
* @param string $name The name of the template element
* @since 3.3.0
*/
public static function beginProfile(string $type, string $name): void
{
if (!self::_shouldProfile()) {
return;
}
if (!isset(self::$_profileCounters[$type][$name])) {
$count = self::$_profileCounters[$type][$name] = 1;
} else {
$count = ++self::$_profileCounters[$type][$name];
}
Craft::beginProfile(self::_profileToken($type, $name, $count), 'Twig template');
}
/**
* Finishes profiling a template element.
*
* @param string $type The type of template element being profiled ('template', 'block', or 'macro')
* @param string $name The name of the template element
* @since 3.3.0
*/
public static function endProfile(string $type, string $name): void
{
if (!self::_shouldProfile()) {
return;
}
$count = self::$_profileCounters[$type][$name]--;
Craft::endProfile(self::_profileToken($type, $name, $count), 'Twig template');
}
/**
* Returns whether to profile the given template element.
*
* @return bool Whether to profile it
*/
private static function _shouldProfile(): bool
{
if (isset(self::$_shouldProfile)) {
return self::$_shouldProfile;
}
if (App::devMode()) {
return self::$_shouldProfile = true;
}
$user = Craft::$app->getUser()->getIdentity();
if (!$user) {
return false;
}
return self::$_shouldProfile = $user->admin && $user->getPreference('profileTemplates');
}
/**
* Returns the token name that should be used for a template profile.
*
* @param string $type
* @param string $name
* @param int $count
* @return string
*/
private static function _profileToken(string $type, string $name, int $count): string
{
return "render $type: $name" . ($count === 1 ? '' : " ($count)");
}
/**
* Registers a CSS file or a CSS code block.
*
* @param string $css the CSS file URL, or the content of the CSS code block to be registered
* @param array $options the HTML attributes for the `<link>`/`<style>` tag.
* @param string|null $key the key that identifies the CSS code block. If null, it will use
* `$css` as the key. If two CSS code blocks are registered with the same key, the latter
* will overwrite the former.
* @throws InvalidConfigException
* @since 3.5.6
*/
public static function css(string $css, array $options = [], ?string $key = null): void
{
// Is this a CSS file?
if (preg_match('/^[^\r\n]+\.css(\.gz)?$/i', $css) || UrlHelper::isAbsoluteUrl($css)) {
Craft::$app->getView()->registerCssFile($css, $options, $key);
} else {
Craft::$app->getView()->registerCss($css, $options, $key);
}
}
/**
* Registers a JS file or a JS code block.
*
* @param string $js the JS file URL, or the content of the JS code block to be registered
* @param array $options the HTML attributes for the `<script>` tag.
* @param string|null $key the key that identifies the JS code block. If null, it will use
* $css as the key. If two JS code blocks are registered with the same key, the latter
* will overwrite the former.
* @throws InvalidConfigException
* @since 3.5.6
*/
public static function js(string $js, array $options = [], ?string $key = null): void
{
// Is this a JS file?
if (preg_match('/^[^\r\n]+\.js(\.gz)?$/i', $js) || UrlHelper::isAbsoluteUrl($js)) {
Craft::$app->getView()->registerJsFile($js, $options, $key);
} else {
$position = $options['position'] ?? View::POS_READY;
Craft::$app->getView()->registerJs($js, $position, $key);
}
}
/**
* Attempts to resolve a compiled template file path and line number to its source template path and line number.
*
* @param string $path The compiled template path
* @param int|null $line The line number from the compiled template
* @return array|false The resolved template path and line number, or `false` if the path couldn’t be determined.
* If a template path could be determined but not the template line number, the line number will be null.
* @since 4.1.5
*/
public static function resolveTemplatePathAndLine(string $path, ?int $line)
{
if (!str_contains($path, 'compiled_templates')) {
return false;
}
$contents = file_get_contents($path);
if (!preg_match('/^class (\w+)/m', $contents, $match)) {
return false;
}
$class = $match[1];
if (!class_exists($class, false) || !is_subclass_of($class, TwigTemplate::class)) {
return false;
}
/** @var TwigTemplate $template */
$template = new $class(Craft::$app->getView()->getTwig());
$src = $template->getSourceContext();
$templatePath = $src->getPath() ?: null;
$templateLine = null;
if ($line !== null) {
foreach ($template->getDebugInfo() as $codeLine => $thisTemplateLine) {
if ($codeLine <= $line) {
$templateLine = $thisTemplateLine;
break;
}
}
}
return [$templatePath, $templateLine];
}
/**
* Filters the template from a context array.
*
* Used by the `dump()` function and `dd` tags.
*
* @param array $context
* @return array
* @since 4.4.0
*/
public static function contextWithoutTemplate(array $context): array
{
// Template check copied from twig_var_dump()
return array_filter($context, fn($value) => !$value instanceof TwigTemplate && !$value instanceof TemplateWrapper);
}
/**
* Preloads Single section entries as fallback values for [[fallbackValue()]]
*
* @param string[] $handles
* @since 4.4.0
*/
public static function preloadSingles(array $handles, ?array &$context = null): void
{
// Ignore handles that are defined Twig globals
$globals = Craft::$app->view->getTwig()->getGlobals();
$handles = array_diff($handles, array_keys($globals));
if (!empty($handles)) {
$singles = Craft::$app->getEntries()->getSingleEntriesByHandle($handles);
self::$_fallbacks += $singles;
if ($context !== null) {
$context += $singles;
}
}
}
}