-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathRuntime.php
More file actions
446 lines (393 loc) · 13.6 KB
/
Runtime.php
File metadata and controls
446 lines (393 loc) · 13.6 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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
<?php
namespace DevTheorem\Handlebars;
/**
* @internal
*/
final class Runtime
{
/**
* Output debug info.
*/
public static function debug(string $expression, string $runtimeFn, mixed ...$rest): mixed
{
$runtime = self::class;
return call_user_func_array("$runtime::$runtimeFn", $rest);
}
/**
* Throw exception for missing expression. Only used in strict mode.
*/
public static function miss(string $v): void
{
throw new \Exception("Runtime: $v does not exist");
}
/**
* For {{log}}.
* @param array<mixed> $v
*/
public static function lo(array $v): string
{
error_log(var_export($v[0], true));
return '';
}
/**
* For {{#if}} and {{#unless}}.
*
* @param array<array<mixed>|string|int>|string|\Stringable|int|float|bool|null $v value to be tested
* @param bool $zero include zero as true
*
* @return bool Return true when the value is not null nor false.
*/
public static function ifvar(mixed $v, bool $zero = false): bool
{
return $v !== null
&& $v !== false
&& ($zero || ($v !== 0 && $v !== 0.0))
&& $v !== ''
&& (!$v instanceof \Stringable || (string) $v !== '')
&& (!is_array($v) || count($v) > 0);
}
/**
* For {{^var}} .
*
* @param array<array<mixed>|string|int>|string|int|bool|null $v value to be tested
*
* @return bool Return true when the value is null or false or empty
*/
public static function isec(mixed $v): bool
{
return $v === null || $v === false || (is_array($v) && count($v) === 0);
}
/**
* HTML encode {{var}} just like handlebars.js
*
* @param array<array<mixed>|string|int>|string|SafeString|int|null $var value to be htmlencoded
*/
public static function encq($var): string
{
if ($var instanceof SafeString) {
return (string) $var;
}
return Handlebars::escapeExpression(static::raw($var));
}
/**
* Get string value
*
* @param array<array<mixed>|string|int>|string|int|bool|null $v value to be output
* @param int $ex 1 to return untouched value, default is 0
*
* @return array<array<mixed>|string|int>|string|null The raw value of the specified variable
*/
public static function raw(array|string|int|bool|null $v, int $ex = 0): string|array|null
{
if ($ex) {
return $v;
}
if ($v === true) {
return 'true';
}
if ($v === false) {
return 'false';
}
if (is_array($v)) {
if (count(array_diff_key($v, array_keys(array_keys($v)))) > 0) {
return '[object Object]';
} else {
$ret = '';
foreach ($v as $vv) {
$ret .= static::raw($vv) . ',';
}
return substr($ret, 0, -1);
}
}
return "$v";
}
/**
* For {{#var}} or {{#each}} .
*
* @param array<array<mixed>|string|int>|string|int|bool|null|\Traversable<string, mixed> $v value for the section
* @param array<string>|null $bp block parameters
* @param array<array<mixed>|string|int>|string|int|null $in input data with current scope
* @param bool $each true when rendering #each
* @param \Closure $cb callback function to render child context
* @param \Closure|null $else callback function to render child context when {{else}}
*/
public static function sec(RuntimeContext $cx, mixed $v, ?array $bp, mixed $in, bool $each, \Closure $cb, ?\Closure $else = null): string
{
$push = $in !== $v || $each;
$isAry = is_array($v) || ($v instanceof \ArrayObject);
$isTrav = $v instanceof \Traversable;
$loop = $each;
$keys = null;
$last = null;
$isObj = false;
if ($isAry && $else !== null && count($v) === 0) {
return $else($cx, $in);
}
// #var, detect input type is object or not
if (!$loop && $isAry) {
$keys = array_keys($v);
$loop = (count(array_diff_key($v, array_keys($keys))) == 0);
$isObj = !$loop;
}
if (($loop && $isAry) || $isTrav) {
if ($each && !$isTrav) {
// Detect input type is object or not when never done once
if ($keys == null) {
$keys = array_keys($v);
$isObj = (count(array_diff_key($v, array_keys($keys))) > 0);
}
}
$ret = [];
$cx = clone $cx;
if ($push) {
$cx->scopes[] = $in;
}
$i = 0;
$oldSpvar = $cx->spVars ?? [];
$cx->spVars = array_merge(['root' => $oldSpvar['root'] ?? null], $oldSpvar, ['_parent' => $oldSpvar]);
if (!$isTrav) {
$last = count($keys) - 1;
}
$isSparceArray = $isObj && (count(array_filter(array_keys($v), 'is_string')) == 0);
foreach ($v as $index => $raw) {
$cx->spVars['first'] = ($i === 0);
$cx->spVars['last'] = ($i == $last);
$cx->spVars['key'] = $index;
$cx->spVars['index'] = $isSparceArray ? $index : $i;
$i++;
if (isset($bp[0])) {
$raw = static::merge($raw, [$bp[0] => $raw]);
}
if (isset($bp[1])) {
$raw = static::merge($raw, [$bp[1] => $index]);
}
$ret[] = $cb($cx, $raw);
}
if ($isObj) {
unset($cx->spVars['key']);
} else {
unset($cx->spVars['last']);
}
unset($cx->spVars['index'], $cx->spVars['first']);
if ($push) {
array_pop($cx->scopes);
}
return join('', $ret);
}
if ($each) {
return ($else !== null) ? $else($cx, $in) : '';
}
if ($isAry) {
if ($push) {
$cx->scopes[] = $in;
}
$ret = $cb($cx, $v);
if ($push) {
array_pop($cx->scopes);
}
return $ret;
}
if ($v === true) {
return $cb($cx, $in);
}
if ($v !== null && $v !== false) {
return $cb($cx, $v);
}
if ($else !== null) {
return $else($cx, $in);
}
return '';
}
/**
* For {{#with}} .
*
* @param array<array<mixed>|string|int>|string|int|bool|null $v value to be the new context
* @param array<string>|null $bp block parameters
* @param array<array<mixed>|string|int>|\stdClass|null $in input data with current scope
* @param \Closure $cb callback function to render child context
* @param \Closure|null $else callback function to render child context when {{else}}
*/
public static function wi(RuntimeContext $cx, mixed $v, ?array $bp, array|\stdClass|null $in, \Closure $cb, ?\Closure $else = null): string
{
if (isset($bp[0])) {
$v = static::merge($v, [$bp[0] => $v]);
}
if ($v === false || $v === null || (is_array($v) && count($v) === 0)) {
return $else ? $else($cx, $in) : '';
}
if ($v === $in) {
$ret = $cb($cx, $v);
} else {
$cx->scopes[] = $in;
$ret = $cb($cx, $v);
array_pop($cx->scopes);
}
return $ret;
}
/**
* Get merged context.
*
* @param array<array<mixed>|string|int>|object|string|int|null $a the context to be merged
* @param array<array<mixed>|string|int>|string|int|null $b the new context to overwrite
*
* @return array<array<mixed>|string|int>|string|int the merged context object
*/
public static function merge(mixed $a, mixed $b): mixed
{
if (is_array($b)) {
if ($a === null) {
return $b;
} elseif (is_array($a)) {
return array_merge($a, $b);
} else {
if (!is_object($a)) {
$a = new StringObject($a);
}
foreach ($b as $i => $v) {
$a->$i = $v;
}
}
}
return $a;
}
/**
* For {{> partial}} .
*
* @param string $p partial name
* @param array<array<mixed>|string|int>|string|int|null $v value to be the new context
*/
public static function p(RuntimeContext $cx, string $p, $v, int $pid, string $sp): string
{
$pp = ($p === '@partial-block') ? $p . ($pid > 0 ? $pid : $cx->partialId) : $p;
if (!isset($cx->partials[$pp])) {
throw new \Exception("Runtime: the partial $p could not be found");
}
$cx = clone $cx;
$cx->partialId = ($p === '@partial-block') ? ($pid > 0 ? $pid : ($cx->partialId > 0 ? $cx->partialId - 1 : 0)) : $pid;
return $cx->partials[$pp]($cx, static::merge($v[0][0], $v[1]), $sp);
}
/**
* For {{#* inlinepartial}} .
*
* @param string $p partial name
* @param \Closure $code the compiled partial code
*/
public static function in(RuntimeContext $cx, string $p, \Closure $code): void
{
$cx->partials[$p] = $code;
}
/**
* For single custom helpers.
*
* @param string $ch the name of custom helper to be executed
* @param array<array<mixed>|string|int> $vars variables for the helper
* @param array<string,array<mixed>|string|int> $_this current rendering context for the helper
*/
public static function hbch(RuntimeContext $cx, string $ch, array $vars, mixed &$_this): mixed
{
if (isset($cx->blParam[0][$ch])) {
return $cx->blParam[0][$ch];
}
$options = new HelperOptions(
name: $ch,
hash: $vars[1],
fn: function () { return ''; },
inverse: function () { return ''; },
blockParams: 0,
scope: $_this,
data: $cx->spVars,
);
return static::exch($cx, $ch, $vars, $options);
}
/**
* For block custom helpers.
*
* @param string $ch the name of custom helper to be executed
* @param array<array<mixed>|string|int> $vars variables for the helper
* @param array<string,array<mixed>|string|int> $_this current rendering context for the helper
* @param bool $inverted the logic will be inverted
* @param \Closure|null $cb callback function to render child context
* @param \Closure|null $else callback function to render child context when {{else}}
*/
public static function hbbch(RuntimeContext $cx, string $ch, array $vars, mixed &$_this, bool $inverted, ?\Closure $cb, ?\Closure $else = null): mixed
{
$blockParams = 0;
$data = &$cx->spVars;
if (isset($vars[2])) {
$blockParams = count($vars[2]);
}
// invert the logic
if ($inverted) {
$tmp = $else;
$else = $cb;
$cb = $tmp;
}
$fn = function ($context = null, $data = null) use ($cx, $_this, $cb, $vars) {
$cx = clone $cx;
$old_spvar = $cx->spVars;
if (isset($data['data'])) {
$cx->spVars = array_merge(['root' => $old_spvar['root']], $data['data'], ['_parent' => $old_spvar]);
}
$ex = false;
if (isset($data['blockParams'], $vars[2])) {
$ex = array_combine($vars[2], array_slice($data['blockParams'], 0, count($vars[2])));
array_unshift($cx->blParam, $ex);
} elseif (isset($cx->blParam[0])) {
$ex = $cx->blParam[0];
}
if ($context === null) {
$ret = $cb($cx, is_array($ex) ? static::merge($_this, $ex) : $_this);
} else {
$cx->scopes[] = $_this;
$ret = $cb($cx, is_array($ex) ? static::merge($context, $ex) : $context);
array_pop($cx->scopes);
}
if (isset($data['data'])) {
$cx->spVars = $old_spvar;
}
return $ret;
};
if ($else) {
$inverse = function ($context = null) use ($cx, $_this, $else) {
if ($context === null) {
$ret = $else($cx, $_this);
} else {
$cx->scopes[] = $_this;
$ret = $else($cx, $context);
array_pop($cx->scopes);
}
return $ret;
};
} else {
$inverse = function () {
return '';
};
}
$options = new HelperOptions(
name: $ch,
hash: $vars[1],
fn: $fn,
inverse: $inverse,
blockParams: $blockParams,
scope: $_this,
data: $data,
);
return static::exch($cx, $ch, $vars, $options);
}
/**
* Execute custom helper with prepared options
*
* @param string $ch the name of custom helper to be executed
* @param array<array<mixed>|string|int> $vars variables for the helper
*/
public static function exch(RuntimeContext $cx, string $ch, array $vars, HelperOptions $options): mixed
{
$args = $vars[0];
$args[] = $options;
try {
return ($cx->helpers[$ch])(...$args);
} catch (\Throwable $e) {
throw new \Exception("Runtime: call custom helper '$ch' error: " . $e->getMessage());
}
}
}