Overview

Namespaces

  • LightnCandy

Classes

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