1: <?php
2: /*
3:
4: MIT License
5: Copyright 2013-2019 Zordius Chen. All Rights Reserved.
6: Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
7: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
9:
10: Origin: https://github.com/zordius/lightncandy
11: */
12:
13: /**
14: * file to support LightnCandy compiled PHP runtime
15: *
16: * @package LightnCandy
17: * @author Zordius <zordius@gmail.com>
18: */
19:
20: namespace LightnCandy;
21:
22: /**
23: * LightnCandy class for Object property access on a string.
24: */
25: class StringObject
26: {
27: protected $string;
28:
29: public function __construct($string)
30: {
31: $this->string = $string;
32: }
33:
34: public function __toString()
35: {
36: return strval($this->string);
37: }
38: }
39:
40: /**
41: * LightnCandy class for compiled PHP runtime.
42: */
43: class Runtime extends Encoder
44: {
45: const DEBUG_ERROR_LOG = 1;
46: const DEBUG_ERROR_EXCEPTION = 2;
47: const DEBUG_TAGS = 4;
48: const DEBUG_TAGS_ANSI = 12;
49: const DEBUG_TAGS_HTML = 20;
50:
51: /**
52: * Output debug info.
53: *
54: * @param string $v expression
55: * @param string $f runtime function name
56: * @param array<string,array|string|integer> $cx render time context for lightncandy
57: *
58: * @expect '{{123}}' when input '123', 'miss', array('flags' => array('debug' => Runtime::DEBUG_TAGS), 'runtime' => 'LightnCandy\\Runtime'), ''
59: * @expect '<!--MISSED((-->{{#123}}<!--))--><!--SKIPPED--><!--MISSED((-->{{/123}}<!--))-->' when input '123', 'wi', array('flags' => array('debug' => Runtime::DEBUG_TAGS_HTML), 'runtime' => 'LightnCandy\\Runtime'), false, null, false, function () {return 'A';}
60: */
61: public static function debug($v, $f, $cx)
62: {
63: // Build array of reference for call_user_func_array
64: $P = func_get_args();
65: $params = array();
66: for ($i=2;$i<count($P);$i++) {
67: $params[] = &$P[$i];
68: }
69: $r = call_user_func_array((isset($cx['funcs'][$f]) ? $cx['funcs'][$f] : "{$cx['runtime']}::$f"), $params);
70:
71: if ($cx['flags']['debug'] & static::DEBUG_TAGS) {
72: $ansi = $cx['flags']['debug'] & (static::DEBUG_TAGS_ANSI - static::DEBUG_TAGS);
73: $html = $cx['flags']['debug'] & (static::DEBUG_TAGS_HTML - static::DEBUG_TAGS);
74: $cs = ($html ? (($r !== '') ? '<!!--OK((-->' : '<!--MISSED((-->') : '')
75: . ($ansi ? (($r !== '') ? "\033[0;32m" : "\033[0;31m") : '');
76: $ce = ($html ? '<!--))-->' : '')
77: . ($ansi ? "\033[0m" : '');
78: switch ($f) {
79: case 'sec':
80: case 'wi':
81: if ($r == '') {
82: if ($ansi) {
83: $r = "\033[0;33mSKIPPED\033[0m";
84: }
85: if ($html) {
86: $r = '<!--SKIPPED-->';
87: }
88: }
89: return "$cs{{#{$v}}}$ce{$r}$cs{{/{$v}}}$ce";
90: default:
91: return "$cs{{{$v}}}$ce";
92: }
93: } else {
94: return $r;
95: }
96: }
97:
98: /**
99: * Handle error by error_log or throw exception.
100: *
101: * @param array<string,array|string|integer> $cx render time context for lightncandy
102: * @param string $err error message
103: *
104: * @throws \Exception
105: */
106: public static function err($cx, $err)
107: {
108: if ($cx['flags']['debug'] & static::DEBUG_ERROR_LOG) {
109: error_log($err);
110: return;
111: }
112: if ($cx['flags']['debug'] & static::DEBUG_ERROR_EXCEPTION) {
113: throw new \Exception($err);
114: }
115: }
116:
117: /**
118: * Handle missing data error.
119: *
120: * @param array<string,array|string|integer> $cx render time context for lightncandy
121: * @param string $v expression
122: */
123: public static function miss($cx, $v)
124: {
125: static::err($cx, "Runtime: $v does not exist");
126: }
127:
128: /**
129: * For {{log}} .
130: *
131: * @param array<string,array|string|integer> $cx render time context for lightncandy
132: * @param string $v expression
133: */
134: public static function lo($cx, $v)
135: {
136: error_log(var_export($v[0], true));
137: return '';
138: }
139:
140: /**
141: * Resursive lookup variable and helpers. This is slow and will only be used for instance property or method detection or lambdas.
142: *
143: * @param array<string,array|string|integer> $cx render time context for lightncandy
144: * @param array|string|boolean|integer|double|null $in current context
145: * @param array<array|string|integer> $base current variable context
146: * @param array<string|integer> $path array of names for path
147: * @param array|null $args extra arguments for lambda
148: *
149: * @return null|string Return the value or null when not found
150: *
151: * @expect null when input array('scopes' => array(), 'flags' => array('prop' => 0, 'method' => 0, 'mustlok' => 0)), null, 0, array('a', 'b')
152: * @expect 3 when input array('scopes' => array(), 'flags' => array('prop' => 0, 'method' => 0), 'mustlok' => 0), null, array('a' => array('b' => 3)), array('a', 'b')
153: * @expect null when input array('scopes' => array(), 'flags' => array('prop' => 0, 'method' => 0, 'mustlok' => 0)), null, (Object) array('a' => array('b' => 3)), array('a', 'b')
154: * @expect 3 when input array('scopes' => array(), 'flags' => array('prop' => 1, 'method' => 0, 'mustlok' => 0)), null, (Object) array('a' => array('b' => 3)), array('a', 'b')
155: */
156: public static function v($cx, $in, $base, $path, $args = null)
157: {
158: $count = count($cx['scopes']);
159: $plen = count($path);
160: while ($base) {
161: $v = $base;
162: foreach ($path as $i => $name) {
163: if (is_array($v)) {
164: if (isset($v[$name])) {
165: $v = $v[$name];
166: continue;
167: }
168: if (($i === $plen - 1) && ($name === 'length')) {
169: return count($v);
170: }
171: }
172: if (is_object($v)) {
173: if ($cx['flags']['prop'] && !($v instanceof \Closure) && isset($v->$name)) {
174: $v = $v->$name;
175: continue;
176: }
177: if ($cx['flags']['method'] && is_callable(array($v, $name))) {
178: try {
179: $v = $v->$name();
180: continue;
181: } catch (\BadMethodCallException $e) {}
182: }
183: if ($v instanceof \ArrayAccess) {
184: if (isset($v[$name])) {
185: $v = $v[$name];
186: continue;
187: }
188: }
189: }
190: if ($cx['flags']['mustlok']) {
191: unset($v);
192: break;
193: }
194: return null;
195: }
196: if (isset($v)) {
197: if ($v instanceof \Closure) {
198: if ($cx['flags']['mustlam'] || $cx['flags']['lambda']) {
199: if (!$cx['flags']['knohlp'] && ($args || ($args === 0))) {
200: $A = $args ? $args[0] : array();
201: $A[] = array('hash' => $args[1], '_this' => $in);
202: } else {
203: $A = array($in);
204: }
205: $v = call_user_func_array($v, $A);
206: }
207: }
208: return $v;
209: }
210: $count--;
211: switch ($count) {
212: case -1:
213: $base = $cx['sp_vars']['root'];
214: break;
215: case -2:
216: return null;
217: default:
218: $base = $cx['scopes'][$count];
219: }
220: }
221: if ($args) {
222: static::err($cx, 'Can not find helper or lambda: "' . implode('.', $path) . '" !');
223: }
224: }
225:
226: /**
227: * For {{#if}} .
228: *
229: * @param array<string,array|string|integer> $cx render time context for lightncandy
230: * @param array<array|string|integer>|string|integer|null $v value to be tested
231: * @param boolean $zero include zero as true
232: *
233: * @return boolean Return true when the value is not null nor false.
234: *
235: * @expect false when input array(), null, false
236: * @expect false when input array(), 0, false
237: * @expect true when input array(), 0, true
238: * @expect false when input array(), false, false
239: * @expect true when input array(), true, false
240: * @expect true when input array(), 1, false
241: * @expect false when input array(), '', false
242: * @expect false when input array(), array(), false
243: * @expect true when input array(), array(''), false
244: * @expect true when input array(), array(0), false
245: */
246: public static function ifvar($cx, $v, $zero)
247: {
248: return ($v !== null) && ($v !== false) && ($zero || ($v !== 0) && ($v !== 0.0)) && ($v !== '') && (is_array($v) ? (count($v) > 0) : true);
249: }
250:
251: /**
252: * For {{^var}} .
253: *
254: * @param array<string,array|string|integer> $cx render time context for lightncandy
255: * @param array<array|string|integer>|string|integer|null $v value to be tested
256: *
257: * @return boolean Return true when the value is not null nor false.
258: *
259: * @expect true when input array(), null
260: * @expect false when input array(), 0
261: * @expect true when input array(), false
262: * @expect false when input array(), 'false'
263: * @expect true when input array(), array()
264: * @expect false when input array(), array('1')
265: */
266: public static function isec($cx, $v)
267: {
268: return ($v === null) || ($v === false) || (is_array($v) && (count($v) === 0));
269: }
270:
271: /**
272: * For {{var}} .
273: *
274: * @param array<string,array|string|integer> $cx render time context for lightncandy
275: * @param array<array|string|integer>|string|integer|null $var value to be htmlencoded
276: *
277: * @return string The htmlencoded value of the specified variable
278: *
279: * @expect 'a' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), 'a'
280: * @expect 'a&b' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), 'a&b'
281: * @expect 'a'b' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), 'a\'b'
282: * @expect 'a&b' when input null, new \LightnCandy\SafeString('a&b')
283: */
284: public static function enc($cx, $var)
285: {
286: if ($var instanceof SafeString) {
287: return (string)$var;
288: }
289:
290: return htmlspecialchars(static::raw($cx, $var), ENT_QUOTES, 'UTF-8');
291: }
292:
293: /**
294: * For {{var}} , do html encode just like handlebars.js .
295: *
296: * @param array<string,array|string|integer> $cx render time context for lightncandy
297: * @param array<array|string|integer>|string|integer|null $var value to be htmlencoded
298: *
299: * @return string The htmlencoded value of the specified variable
300: *
301: * @expect 'a' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), 'a'
302: * @expect 'a&b' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), 'a&b'
303: * @expect 'a'b' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), 'a\'b'
304: * @expect '`a'b' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), '`a\'b'
305: */
306: public static function encq($cx, $var)
307: {
308: if ($var instanceof SafeString) {
309: return (string)$var;
310: }
311:
312: return str_replace(array('=', '`', '''), array('=', '`', '''), htmlspecialchars(static::raw($cx, $var), ENT_QUOTES, 'UTF-8'));
313: }
314:
315: /**
316: * For {{#var}} or {{#each}} .
317: *
318: * @param array<string,array|string|integer> $cx render time context for lightncandy
319: * @param array<array|string|integer>|string|integer|null $v value for the section
320: * @param array<string>|null $bp block parameters
321: * @param array<array|string|integer>|string|integer|null $in input data with current scope
322: * @param boolean $each true when rendering #each
323: * @param Closure $cb callback function to render child context
324: * @param Closure|null $else callback function to render child context when {{else}}
325: *
326: * @return string The rendered string of the section
327: *
328: * @expect '' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'mustsec' => 0, 'lambda' => 0)), false, null, false, false, function () {return 'A';}
329: * @expect '' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'mustsec' => 0, 'lambda' => 0)), null, null, null, false, function () {return 'A';}
330: * @expect 'A' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'mustsec' => 0, 'lambda' => 0)), true, null, true, false, function () {return 'A';}
331: * @expect 'A' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'mustsec' => 0, 'lambda' => 0)), 0, null, 0, false, function () {return 'A';}
332: * @expect '-a=' when input array('scopes' => array(), 'flags' => array('spvar' => 0, 'mustlam' => 0, 'lambda' => 0)), array('a'), null, array('a'), false, function ($c, $i) {return "-$i=";}
333: * @expect '-a=-b=' when input array('scopes' => array(), 'flags' => array('spvar' => 0, 'mustlam' => 0, 'lambda' => 0)), array('a','b'), null, array('a','b'), false, function ($c, $i) {return "-$i=";}
334: * @expect '' when input array('scopes' => array(), 'flags' => array('spvar' => 0, 'mustlam' => 0, 'lambda' => 0)), 'abc', null, 'abc', true, function ($c, $i) {return "-$i=";}
335: * @expect '-b=' when input array('scopes' => array(), 'flags' => array('spvar' => 0, 'mustlam' => 0, 'lambda' => 0)), array('a' => 'b'), null, array('a' => 'b'), true, function ($c, $i) {return "-$i=";}
336: * @expect 'b' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'mustsec' => 0, 'lambda' => 0)), 'b', null, 'b', false, function ($c, $i) {return print_r($i, true);}
337: * @expect '1' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'mustsec' => 0, 'lambda' => 0)), 1, null, 1, false, function ($c, $i) {return print_r($i, true);}
338: * @expect '0' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'mustsec' => 0, 'lambda' => 0)), 0, null, 0, false, function ($c, $i) {return print_r($i, true);}
339: * @expect '{"b":"c"}' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'lambda' => 0)), array('b' => 'c'), null, array('b' => 'c'), false, function ($c, $i) {return json_encode($i);}
340: * @expect 'inv' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'lambda' => 0)), array(), null, 0, true, function ($c, $i) {return 'cb';}, function ($c, $i) {return 'inv';}
341: * @expect 'inv' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'lambda' => 0)), array(), null, 0, false, function ($c, $i) {return 'cb';}, function ($c, $i) {return 'inv';}
342: * @expect 'inv' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'lambda' => 0)), false, null, 0, true, function ($c, $i) {return 'cb';}, function ($c, $i) {return 'inv';}
343: * @expect 'inv' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'mustsec' => 0, 'lambda' => 0)), false, null, 0, false, function ($c, $i) {return 'cb';}, function ($c, $i) {return 'inv';}
344: * @expect 'inv' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'lambda' => 0)), '', null, 0, true, function ($c, $i) {return 'cb';}, function ($c, $i) {return 'inv';}
345: * @expect 'cb' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'mustsec' => 0, 'lambda' => 0)), '', null, 0, false, function ($c, $i) {return 'cb';}, function ($c, $i) {return 'inv';}
346: * @expect 'inv' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'lambda' => 0)), 0, null, 0, true, function ($c, $i) {return 'cb';}, function ($c, $i) {return 'inv';}
347: * @expect 'cb' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'mustsec' => 0, 'lambda' => 0)), 0, null, 0, false, function ($c, $i) {return 'cb';}, function ($c, $i) {return 'inv';}
348: * @expect 'inv' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'lambda' => 0)), new stdClass, null, 0, true, function ($c, $i) {return 'cb';}, function ($c, $i) {return 'inv';}
349: * @expect 'cb' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'mustsec' => 0, 'lambda' => 0)), new stdClass, null, 0, false, function ($c, $i) {return 'cb';}, function ($c, $i) {return 'inv';}
350: * @expect '268' when input array('scopes' => array(), 'flags' => array('spvar' => 1, 'mustlam' => 0, 'lambda' => 0), 'sp_vars'=>array('root' => 0)), array(1,3,4), null, 0, false, function ($c, $i) {return $i * 2;}
351: * @expect '038' when input array('scopes' => array(), 'flags' => array('spvar' => 1, 'mustlam' => 0, 'lambda' => 0), 'sp_vars'=>array('root' => 0)), array(1,3,'a'=>4), null, 0, true, function ($c, $i) {return $i * $c['sp_vars']['index'];}
352: */
353: public static function sec($cx, $v, $bp, $in, $each, $cb, $else = null)
354: {
355: $push = ($in !== $v) || $each;
356:
357: $isAry = is_array($v) || ($v instanceof \ArrayObject);
358: $isTrav = $v instanceof \Traversable;
359: $loop = $each;
360: $keys = null;
361: $last = null;
362: $isObj = false;
363:
364: if ($isAry && $else !== null && count($v) === 0) {
365: return $else($cx, $in);
366: }
367:
368: // #var, detect input type is object or not
369: if (!$loop && $isAry) {
370: $keys = array_keys($v);
371: $loop = (count(array_diff_key($v, array_keys($keys))) == 0);
372: $isObj = !$loop;
373: }
374:
375: if (($loop && $isAry) || $isTrav) {
376: if ($each && !$isTrav) {
377: // Detect input type is object or not when never done once
378: if ($keys == null) {
379: $keys = array_keys($v);
380: $isObj = (count(array_diff_key($v, array_keys($keys))) > 0);
381: }
382: }
383: $ret = array();
384: if ($push) {
385: $cx['scopes'][] = $in;
386: }
387: $i = 0;
388: if ($cx['flags']['spvar']) {
389: $old_spvar = $cx['sp_vars'];
390: $cx['sp_vars'] = array_merge(array('root' => $old_spvar['root']), $old_spvar, array('_parent' => $old_spvar));
391: if (!$isTrav) {
392: $last = count($keys) - 1;
393: }
394: }
395:
396: $isSparceArray = $isObj && (count(array_filter(array_keys($v), 'is_string')) == 0);
397: foreach ($v as $index => $raw) {
398: if ($cx['flags']['spvar']) {
399: $cx['sp_vars']['first'] = ($i === 0);
400: $cx['sp_vars']['last'] = ($i == $last);
401: $cx['sp_vars']['key'] = $index;
402: $cx['sp_vars']['index'] = $isSparceArray ? $index : $i;
403: $i++;
404: }
405: if (isset($bp[0])) {
406: $raw = static::m($cx, $raw, array($bp[0] => $raw));
407: }
408: if (isset($bp[1])) {
409: $raw = static::m($cx, $raw, array($bp[1] => $index));
410: }
411: $ret[] = $cb($cx, $raw);
412: }
413: if ($cx['flags']['spvar']) {
414: if ($isObj) {
415: unset($cx['sp_vars']['key']);
416: } else {
417: unset($cx['sp_vars']['last']);
418: }
419: unset($cx['sp_vars']['index']);
420: unset($cx['sp_vars']['first']);
421: $cx['sp_vars'] = $old_spvar;
422: }
423: if ($push) {
424: array_pop($cx['scopes']);
425: }
426: return join('', $ret);
427: }
428: if ($each) {
429: if ($else !== null) {
430: $ret = $else($cx, $v);
431: return $ret;
432: }
433: return '';
434: }
435: if ($isAry) {
436: if ($push) {
437: $cx['scopes'][] = $in;
438: }
439: $ret = $cb($cx, $v);
440: if ($push) {
441: array_pop($cx['scopes']);
442: }
443: return $ret;
444: }
445:
446: if ($cx['flags']['mustsec']) {
447: return $v ? $cb($cx, $in) : '';
448: }
449:
450: if ($v === true) {
451: return $cb($cx, $in);
452: }
453:
454: if (($v !== null) && ($v !== false)) {
455: return $cb($cx, $v);
456: }
457:
458: if ($else !== null) {
459: $ret = $else($cx, $in);
460: return $ret;
461: }
462:
463: return '';
464: }
465:
466: /**
467: * For {{#with}} .
468: *
469: * @param array<string,array|string|integer> $cx render time context for lightncandy
470: * @param array<array|string|integer>|string|integer|null $v value to be the new context
471: * @param array<array|string|integer>|string|integer|null $in input data with current scope
472: * @param array<string>|null $bp block parameters
473: * @param Closure $cb callback function to render child context
474: * @param Closure|null $else callback function to render child context when {{else}}
475: *
476: * @return string The rendered string of the token
477: *
478: * @expect '' when input array(), false, null, false, function () {return 'A';}
479: * @expect '' when input array(), null, null, null, function () {return 'A';}
480: * @expect '{"a":"b"}' when input array(), array('a'=>'b'), null, array('a'=>'c'), function ($c, $i) {return json_encode($i);}
481: * @expect '-b=' when input array(), 'b', null, array('a'=>'b'), function ($c, $i) {return "-$i=";}
482: */
483: public static function wi($cx, $v, $bp, $in, $cb, $else = null)
484: {
485: if (isset($bp[0])) {
486: $v = static::m($cx, $v, array($bp[0] => $v));
487: }
488: if (($v === false) || ($v === null) || (is_array($v) && (count($v) === 0))) {
489: return $else ? $else($cx, $in) : '';
490: }
491: if ($v === $in) {
492: $ret = $cb($cx, $v);
493: } else {
494: $cx['scopes'][] = $in;
495: $ret = $cb($cx, $v);
496: array_pop($cx['scopes']);
497: }
498: return $ret;
499: }
500:
501: /**
502: * Get merged context.
503: *
504: * @param array<string,array|string|integer> $cx render time context for lightncandy
505: * @param array<array|string|integer>|string|integer|null $a the context to be merged
506: * @param array<array|string|integer>|string|integer|null $b the new context to overwrite
507: *
508: * @return array<array|string|integer>|string|integer the merged context object
509: *
510: */
511: public static function m($cx, $a, $b)
512: {
513: if (is_array($b)) {
514: if ($a === null) {
515: return $b;
516: } elseif (is_array($a)) {
517: return array_merge($a, $b);
518: } elseif ($cx['flags']['method'] || $cx['flags']['prop']) {
519: if (!is_object($a)) {
520: $a = new StringObject($a);
521: }
522: foreach ($b as $i => $v) {
523: $a->$i = $v;
524: }
525: }
526: }
527: return $a;
528: }
529:
530: /**
531: * For {{> partial}} .
532: *
533: * @param array<string,array|string|integer> $cx render time context for lightncandy
534: * @param string $p partial name
535: * @param array<array|string|integer>|string|integer|null $v value to be the new context
536: *
537: * @return string The rendered string of the partial
538: *
539: */
540: public static function p($cx, $p, $v, $pid, $sp = '')
541: {
542: $pp = ($p === '@partial-block') ? "$p" . ($pid > 0 ? $pid : $cx['partialid']) : $p;
543:
544: if (!isset($cx['partials'][$pp])) {
545: static::err($cx, "Can not find partial named as '$p' !!");
546: return '';
547: }
548:
549: $cx['partialid'] = ($p === '@partial-block') ? (($pid > 0) ? $pid : (($cx['partialid'] > 0) ? $cx['partialid'] - 1 : 0)) : $pid;
550:
551: return call_user_func($cx['partials'][$pp], $cx, static::m($cx, $v[0][0], $v[1]), $sp);
552: }
553:
554: /**
555: * For {{#* inlinepartial}} .
556: *
557: * @param array<string,array|string|integer> $cx render time context for lightncandy
558: * @param string $p partial name
559: * @param Closure $code the compiled partial code
560: *
561: */
562: public static function in(&$cx, $p, $code)
563: {
564: $cx['partials'][$p] = $code;
565: }
566:
567: /* For single custom helpers.
568: *
569: * @param array<string,array|string|integer> $cx render time context for lightncandy
570: * @param string $ch the name of custom helper to be executed
571: * @param array<array|string|integer>|string|integer|null $vars variables for the helper
572: * @param string $op the name of variable resolver. should be one of: 'raw', 'enc', or 'encq'.
573: * @param array<string,array|string|integer> $_this current rendering context for the helper
574: *
575: * @return string The rendered string of the token
576: */
577: public static function hbch(&$cx, $ch, $vars, $op, &$_this)
578: {
579: if (isset($cx['blparam'][0][$ch])) {
580: return $cx['blparam'][0][$ch];
581: }
582:
583: $options = array(
584: 'name' => $ch,
585: 'hash' => $vars[1],
586: 'contexts' => count($cx['scopes']) ? $cx['scopes'] : array(null),
587: 'fn.blockParams' => 0,
588: '_this' => &$_this
589: );
590:
591: if ($cx['flags']['spvar']) {
592: $options['data'] = &$cx['sp_vars'];
593: }
594:
595: return static::exch($cx, $ch, $vars, $options);
596: }
597:
598: /**
599: * For block custom helpers.
600: *
601: * @param array<string,array|string|integer> $cx render time context for lightncandy
602: * @param string $ch the name of custom helper to be executed
603: * @param array<array|string|integer>|string|integer|null $vars variables for the helper
604: * @param array<string,array|string|integer> $_this current rendering context for the helper
605: * @param boolean $inverted the logic will be inverted
606: * @param Closure|null $cb callback function to render child context
607: * @param Closure|null $else callback function to render child context when {{else}}
608: *
609: * @return string The rendered string of the token
610: */
611: public static function hbbch(&$cx, $ch, $vars, &$_this, $inverted, $cb, $else = null)
612: {
613: $options = array(
614: 'name' => $ch,
615: 'hash' => $vars[1],
616: 'contexts' => count($cx['scopes']) ? $cx['scopes'] : array(null),
617: 'fn.blockParams' => 0,
618: '_this' => &$_this,
619: );
620:
621: if ($cx['flags']['spvar']) {
622: $options['data'] = &$cx['sp_vars'];
623: }
624:
625: if (isset($vars[2])) {
626: $options['fn.blockParams'] = count($vars[2]);
627: }
628:
629: // $invert the logic
630: if ($inverted) {
631: $tmp = $else;
632: $else = $cb;
633: $cb = $tmp;
634: }
635:
636: $options['fn'] = function ($context = '_NO_INPUT_HERE_', $data = null) use ($cx, &$_this, $cb, $options, $vars) {
637: if ($cx['flags']['echo']) {
638: ob_start();
639: }
640: if (isset($data['data'])) {
641: $old_spvar = $cx['sp_vars'];
642: $cx['sp_vars'] = array_merge(array('root' => $old_spvar['root']), $data['data'], array('_parent' => $old_spvar));
643: }
644: $ex = false;
645: if (isset($data['blockParams']) && isset($vars[2])) {
646: $ex = array_combine($vars[2], array_slice($data['blockParams'], 0, count($vars[2])));
647: array_unshift($cx['blparam'], $ex);
648: } elseif (isset($cx['blparam'][0])) {
649: $ex = $cx['blparam'][0];
650: }
651: if (($context === '_NO_INPUT_HERE_') || ($context === $_this)) {
652: $ret = $cb($cx, is_array($ex) ? static::m($cx, $_this, $ex) : $_this);
653: } else {
654: $cx['scopes'][] = $_this;
655: $ret = $cb($cx, is_array($ex) ? static::m($cx, $context, $ex) : $context);
656: array_pop($cx['scopes']);
657: }
658: if (isset($data['data'])) {
659: $cx['sp_vars'] = $old_spvar;
660: }
661: return $cx['flags']['echo'] ? ob_get_clean() : $ret;
662: };
663:
664: if ($else) {
665: $options['inverse'] = function ($context = '_NO_INPUT_HERE_') use ($cx, $_this, $else) {
666: if ($cx['flags']['echo']) {
667: ob_start();
668: }
669: if ($context === '_NO_INPUT_HERE_') {
670: $ret = $else($cx, $_this);
671: } else {
672: $cx['scopes'][] = $_this;
673: $ret = $else($cx, $context);
674: array_pop($cx['scopes']);
675: }
676: return $cx['flags']['echo'] ? ob_get_clean() : $ret;
677: };
678: } else {
679: $options['inverse'] = function () {
680: return '';
681: };
682: }
683:
684: return static::exch($cx, $ch, $vars, $options);
685: }
686:
687: /**
688: * Execute custom helper with prepared options
689: *
690: * @param array<string,array|string|integer> $cx render time context for lightncandy
691: * @param string $ch the name of custom helper to be executed
692: * @param array<array|string|integer>|string|integer|null $vars variables for the helper
693: * @param array<string,array|string|integer> $options the options object
694: *
695: * @return string The rendered string of the token
696: */
697: public static function exch($cx, $ch, $vars, &$options)
698: {
699: $args = $vars[0];
700: $args[] = &$options;
701: $r = true;
702:
703: try {
704: $r = call_user_func_array($cx['helpers'][$ch], $args);
705: } catch (\Exception $E) {
706: static::err($cx, "Runtime: call custom helper '$ch' error: " . $E->getMessage());
707: }
708:
709: return $r;
710: }
711: }
712: