Complex classes like Runtime often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Runtime, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
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 | 9 | public static function debug($v, $f, $cx) |
|
62 | { |
||
63 | // Build array of reference for call_user_func_array |
||
64 | 9 | $P = func_get_args(); |
|
65 | 9 | $params = array(); |
|
66 | 9 | for ($i=2;$i<count($P);$i++) { |
|
|
|||
67 | 9 | $params[] = &$P[$i]; |
|
68 | } |
||
69 | 9 | $r = call_user_func_array((isset($cx['funcs'][$f]) ? $cx['funcs'][$f] : "{$cx['runtime']}::$f"), $params); |
|
70 | |||
71 | 8 | if ($cx['flags']['debug'] & static::DEBUG_TAGS) { |
|
72 | 5 | $ansi = $cx['flags']['debug'] & (static::DEBUG_TAGS_ANSI - static::DEBUG_TAGS); |
|
73 | 5 | $html = $cx['flags']['debug'] & (static::DEBUG_TAGS_HTML - static::DEBUG_TAGS); |
|
74 | 5 | $cs = ($html ? (($r !== '') ? '<!!--OK((-->' : '<!--MISSED((-->') : '') |
|
75 | 5 | . ($ansi ? (($r !== '') ? "\033[0;32m" : "\033[0;31m") : ''); |
|
76 | 5 | $ce = ($html ? '<!--))-->' : '') |
|
77 | 5 | . ($ansi ? "\033[0m" : ''); |
|
78 | 5 | switch ($f) { |
|
79 | 5 | case 'sec': |
|
80 | 3 | case 'wi': |
|
81 | 3 | if ($r == '') { |
|
82 | 3 | if ($ansi) { |
|
83 | 1 | $r = "\033[0;33mSKIPPED\033[0m"; |
|
84 | } |
||
85 | 3 | if ($html) { |
|
86 | 2 | $r = '<!--SKIPPED-->'; |
|
87 | } |
||
88 | } |
||
89 | 3 | return "$cs{{#{$v}}}$ce{$r}$cs{{/{$v}}}$ce"; |
|
90 | default: |
||
91 | 3 | return "$cs{{{$v}}}$ce"; |
|
92 | } |
||
93 | } else { |
||
94 | 3 | 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 | 18 | public static function err($cx, $err) |
|
107 | { |
||
108 | 18 | if ($cx['flags']['debug'] & static::DEBUG_ERROR_LOG) { |
|
109 | 5 | error_log($err); |
|
110 | 5 | return; |
|
111 | } |
||
112 | 13 | if ($cx['flags']['debug'] & static::DEBUG_ERROR_EXCEPTION) { |
|
113 | 9 | throw new \Exception($err); |
|
114 | } |
||
115 | 4 | } |
|
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 | 5 | public static function miss($cx, $v) |
|
124 | { |
||
125 | 5 | static::err($cx, "Runtime: $v does not exist"); |
|
126 | 4 | } |
|
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 | 388 | public static function v($cx, $in, $base, $path, $args = null) |
|
157 | { |
||
158 | 388 | $count = count($cx['scopes']); |
|
159 | 388 | $plen = count($path); |
|
160 | 388 | while ($base) { |
|
161 | 352 | $v = $base; |
|
162 | 352 | foreach ($path as $i => $name) { |
|
163 | 352 | if (is_array($v)) { |
|
164 | 350 | if (isset($v[$name])) { |
|
165 | 346 | $v = $v[$name]; |
|
166 | 346 | continue; |
|
167 | } |
||
168 | 31 | if (($i === $plen - 1) && ($name === 'length')) { |
|
169 | 1 | return count($v); |
|
170 | } |
||
171 | } |
||
172 | 37 | if (is_object($v)) { |
|
173 | 6 | if ($cx['flags']['prop'] && !($v instanceof \Closure) && isset($v->$name)) { |
|
174 | 4 | $v = $v->$name; |
|
175 | 4 | continue; |
|
176 | } |
||
177 | 5 | if ($cx['flags']['method'] && is_callable(array($v, $name))) { |
|
178 | try { |
||
179 | 4 | $v = $v->$name(); |
|
180 | 4 | continue; |
|
181 | } catch (\BadMethodCallException $e) {} |
||
182 | } |
||
183 | 3 | if ($v instanceof \ArrayAccess) { |
|
184 | if (isset($v[$name])) { |
||
185 | $v = $v[$name]; |
||
186 | continue; |
||
187 | } |
||
188 | } |
||
189 | } |
||
190 | 34 | if ($cx['flags']['mustlok']) { |
|
191 | 31 | unset($v); |
|
192 | 31 | break; |
|
193 | } |
||
194 | 3 | return null; |
|
195 | } |
||
196 | 351 | if (isset($v)) { |
|
197 | 340 | if ($v instanceof \Closure) { |
|
198 | 29 | if ($cx['flags']['mustlam'] || $cx['flags']['lambda']) { |
|
199 | 29 | if (!$cx['flags']['knohlp'] && !is_null($args)) { |
|
200 | 20 | $A = $args ? $args[0] : array(); |
|
201 | 20 | $A[] = array('hash' => is_array( $args ) ? $args[1] : null, '_this' => $in); |
|
202 | } else { |
||
203 | 10 | $A = array($in); |
|
204 | } |
||
205 | 29 | $v = call_user_func_array($v, $A); |
|
206 | } |
||
207 | } |
||
208 | 340 | return $v; |
|
209 | } |
||
210 | 31 | $count--; |
|
211 | 31 | switch ($count) { |
|
212 | case -1: |
||
213 | 30 | $base = $cx['sp_vars']['root']; |
|
214 | 30 | break; |
|
215 | case -2: |
||
216 | 29 | return null; |
|
217 | default: |
||
218 | 6 | $base = $cx['scopes'][$count]; |
|
219 | } |
||
220 | } |
||
221 | 40 | if ($args) { |
|
222 | 3 | static::err($cx, 'Can not find helper or lambda: "' . implode('.', $path) . '" !'); |
|
223 | } |
||
224 | 40 | } |
|
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 | 74 | public static function ifvar($cx, $v, $zero) |
|
247 | { |
||
248 | 74 | 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 | 37 | public static function isec($cx, $v) |
|
267 | { |
||
268 | 37 | 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 | 46 | public static function enc($cx, $var) |
|
285 | { |
||
286 | // Use full namespace classname for more specific code export/match in Exporter.php replaceSafeString. |
||
287 | 46 | if ($var instanceof \LightnCandy\SafeString) { |
|
288 | 1 | return (string)$var; |
|
289 | } |
||
290 | |||
291 | 46 | return htmlspecialchars(static::raw($cx, $var), ENT_QUOTES, 'UTF-8'); |
|
292 | } |
||
293 | |||
294 | /** |
||
295 | * For {{var}} , do html encode just like handlebars.js . |
||
296 | * |
||
297 | * @param array<string,array|string|integer> $cx render time context for lightncandy |
||
298 | * @param array<array|string|integer>|string|integer|null $var value to be htmlencoded |
||
299 | * |
||
300 | * @return string The htmlencoded value of the specified variable |
||
301 | * |
||
302 | * @expect 'a' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), 'a' |
||
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 | * @expect '`a'b' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), '`a\'b' |
||
306 | */ |
||
307 | 342 | public static function encq($cx, $var) |
|
308 | { |
||
309 | // Use full namespace classname for more specific code export/match in Exporter.php replaceSafeString. |
||
310 | 342 | if ($var instanceof \LightnCandy\SafeString) { |
|
311 | 3 | return (string)$var; |
|
312 | } |
||
313 | |||
314 | 339 | return str_replace(array('=', '`', '''), array('=', '`', '''), htmlspecialchars(static::raw($cx, $var), ENT_QUOTES, 'UTF-8')); |
|
315 | } |
||
316 | |||
317 | /** |
||
318 | * For {{#var}} or {{#each}} . |
||
319 | * |
||
320 | * @param array<string,array|string|integer> $cx render time context for lightncandy |
||
321 | * @param array<array|string|integer>|string|integer|null $v value for the section |
||
322 | * @param array<string>|null $bp block parameters |
||
323 | * @param array<array|string|integer>|string|integer|null $in input data with current scope |
||
324 | * @param boolean $each true when rendering #each |
||
325 | * @param Closure $cb callback function to render child context |
||
326 | * @param Closure|null $else callback function to render child context when {{else}} |
||
327 | * |
||
328 | * @return string The rendered string of the section |
||
329 | * |
||
330 | * @expect '' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'mustsec' => 0, 'lambda' => 0)), false, null, false, false, function () {return 'A';} |
||
331 | * @expect '' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'mustsec' => 0, 'lambda' => 0)), null, null, null, false, function () {return 'A';} |
||
332 | * @expect 'A' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'mustsec' => 0, 'lambda' => 0)), true, null, true, false, function () {return 'A';} |
||
333 | * @expect 'A' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'mustsec' => 0, 'lambda' => 0)), 0, null, 0, false, function () {return 'A';} |
||
334 | * @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=";} |
||
335 | * @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=";} |
||
336 | * @expect '' when input array('scopes' => array(), 'flags' => array('spvar' => 0, 'mustlam' => 0, 'lambda' => 0)), 'abc', null, 'abc', true, function ($c, $i) {return "-$i=";} |
||
337 | * @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=";} |
||
338 | * @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);} |
||
339 | * @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);} |
||
340 | * @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);} |
||
341 | * @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);} |
||
342 | * @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';} |
||
343 | * @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';} |
||
344 | * @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';} |
||
345 | * @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';} |
||
346 | * @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';} |
||
347 | * @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';} |
||
348 | * @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';} |
||
349 | * @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';} |
||
350 | * @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';} |
||
351 | * @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';} |
||
352 | * @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;} |
||
353 | * @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'];} |
||
354 | */ |
||
355 | 171 | public static function sec($cx, $v, $bp, $in, $each, $cb, $else = null) |
|
356 | { |
||
357 | 171 | $push = ($in !== $v) || $each; |
|
358 | |||
359 | 171 | $isAry = is_array($v) || ($v instanceof \ArrayObject); |
|
360 | 171 | $isTrav = $v instanceof \Traversable; |
|
361 | 171 | $loop = $each; |
|
362 | 171 | $keys = null; |
|
363 | 171 | $last = null; |
|
364 | 171 | $isObj = false; |
|
365 | |||
366 | 171 | if ($isAry && $else !== null && count($v) === 0) { |
|
367 | 3 | return $else($cx, $in); |
|
368 | } |
||
369 | |||
370 | // #var, detect input type is object or not |
||
371 | 169 | if (!$loop && $isAry) { |
|
372 | 66 | $keys = array_keys($v); |
|
373 | 66 | $loop = (count(array_diff_key($v, array_keys($keys))) == 0); |
|
374 | 66 | $isObj = !$loop; |
|
375 | } |
||
376 | |||
377 | 169 | if (($loop && $isAry) || $isTrav) { |
|
378 | 107 | if ($each && !$isTrav) { |
|
379 | // Detect input type is object or not when never done once |
||
380 | 51 | if ($keys == null) { |
|
381 | 51 | $keys = array_keys($v); |
|
382 | 51 | $isObj = (count(array_diff_key($v, array_keys($keys))) > 0); |
|
383 | } |
||
384 | } |
||
385 | 107 | $ret = array(); |
|
386 | 107 | if ($push) { |
|
387 | 105 | $cx['scopes'][] = $in; |
|
388 | } |
||
389 | 107 | $i = 0; |
|
390 | 107 | if ($cx['flags']['spvar']) { |
|
391 | 96 | $old_spvar = $cx['sp_vars']; |
|
392 | 96 | $cx['sp_vars'] = array_merge(array('root' => $old_spvar['root']), $old_spvar, array('_parent' => $old_spvar)); |
|
393 | 96 | if (!$isTrav) { |
|
394 | 95 | $last = count($keys) - 1; |
|
395 | } |
||
396 | } |
||
397 | |||
398 | 107 | $isSparceArray = $isObj && (count(array_filter(array_keys($v), 'is_string')) == 0); |
|
399 | 107 | foreach ($v as $index => $raw) { |
|
400 | 100 | if ($cx['flags']['spvar']) { |
|
401 | 90 | $cx['sp_vars']['first'] = ($i === 0); |
|
402 | 90 | $cx['sp_vars']['last'] = ($i == $last); |
|
403 | 90 | $cx['sp_vars']['key'] = $index; |
|
404 | 90 | $cx['sp_vars']['index'] = $isSparceArray ? $index : $i; |
|
405 | 90 | $i++; |
|
406 | } |
||
407 | 100 | if (isset($bp[0])) { |
|
408 | 4 | $raw = static::m($cx, $raw, array($bp[0] => $raw)); |
|
409 | } |
||
410 | 100 | if (isset($bp[1])) { |
|
411 | 3 | $raw = static::m($cx, $raw, array($bp[1] => $index)); |
|
412 | } |
||
413 | 100 | $ret[] = $cb($cx, $raw); |
|
414 | } |
||
415 | 106 | if ($cx['flags']['spvar']) { |
|
416 | 95 | if ($isObj) { |
|
417 | 17 | unset($cx['sp_vars']['key']); |
|
418 | } else { |
||
419 | 80 | unset($cx['sp_vars']['last']); |
|
420 | } |
||
421 | 95 | unset($cx['sp_vars']['index']); |
|
422 | 95 | unset($cx['sp_vars']['first']); |
|
423 | 95 | $cx['sp_vars'] = $old_spvar; |
|
424 | } |
||
425 | 106 | if ($push) { |
|
426 | 104 | array_pop($cx['scopes']); |
|
427 | } |
||
428 | 106 | return join('', $ret); |
|
429 | } |
||
430 | 66 | if ($each) { |
|
431 | 2 | if ($else !== null) { |
|
432 | 1 | $ret = $else($cx, $v); |
|
433 | 1 | return $ret; |
|
434 | } |
||
435 | 2 | return ''; |
|
436 | } |
||
437 | 65 | if ($isAry) { |
|
438 | 11 | if ($push) { |
|
439 | 10 | $cx['scopes'][] = $in; |
|
440 | } |
||
441 | 11 | $ret = $cb($cx, $v); |
|
442 | 11 | if ($push) { |
|
443 | 10 | array_pop($cx['scopes']); |
|
444 | } |
||
445 | 11 | return $ret; |
|
446 | } |
||
447 | |||
448 | 55 | if ($cx['flags']['mustsec']) { |
|
449 | 20 | return $v ? $cb($cx, $in) : ''; |
|
450 | } |
||
451 | |||
452 | 35 | if ($v === true) { |
|
453 | 9 | return $cb($cx, $in); |
|
454 | } |
||
455 | |||
456 | 28 | if (($v !== null) && ($v !== false)) { |
|
457 | 9 | return $cb($cx, $v); |
|
458 | } |
||
459 | |||
460 | 21 | if ($else !== null) { |
|
461 | 10 | $ret = $else($cx, $in); |
|
462 | 10 | return $ret; |
|
463 | } |
||
464 | |||
465 | 12 | return ''; |
|
466 | } |
||
467 | |||
468 | /** |
||
469 | * For {{#with}} . |
||
470 | * |
||
471 | * @param array<string,array|string|integer> $cx render time context for lightncandy |
||
472 | * @param array<array|string|integer>|string|integer|null $v value to be the new context |
||
473 | * @param array<array|string|integer>|string|integer|null $in input data with current scope |
||
474 | * @param array<string>|null $bp block parameters |
||
475 | * @param Closure $cb callback function to render child context |
||
476 | * @param Closure|null $else callback function to render child context when {{else}} |
||
477 | * |
||
478 | * @return string The rendered string of the token |
||
479 | * |
||
480 | * @expect '' when input array(), false, null, false, function () {return 'A';} |
||
481 | * @expect '' when input array(), null, null, null, function () {return 'A';} |
||
482 | * @expect '{"a":"b"}' when input array(), array('a'=>'b'), null, array('a'=>'c'), function ($c, $i) {return json_encode($i);} |
||
483 | * @expect '-b=' when input array(), 'b', null, array('a'=>'b'), function ($c, $i) {return "-$i=";} |
||
484 | */ |
||
485 | 30 | public static function wi($cx, $v, $bp, $in, $cb, $else = null) |
|
502 | |||
503 | /** |
||
504 | * Get merged context. |
||
505 | * |
||
506 | * @param array<string,array|string|integer> $cx render time context for lightncandy |
||
507 | * @param array<array|string|integer>|string|integer|null $a the context to be merged |
||
508 | * @param array<array|string|integer>|string|integer|null $b the new context to overwrite |
||
509 | * |
||
510 | * @return array<array|string|integer>|string|integer the merged context object |
||
511 | * |
||
512 | */ |
||
513 | 103 | public static function m($cx, $a, $b) |
|
531 | |||
532 | /** |
||
533 | * For {{> partial}} . |
||
534 | * |
||
535 | * @param array<string,array|string|integer> $cx render time context for lightncandy |
||
536 | * @param string $p partial name |
||
537 | * @param array<array|string|integer>|string|integer|null $v value to be the new context |
||
538 | * |
||
539 | * @return string The rendered string of the partial |
||
540 | * |
||
541 | */ |
||
542 | 100 | public static function p($cx, $p, $v, $pid, $sp = '') |
|
555 | |||
556 | /** |
||
557 | * For {{#* inlinepartial}} . |
||
558 | * |
||
559 | * @param array<string,array|string|integer> $cx render time context for lightncandy |
||
560 | * @param string $p partial name |
||
561 | * @param Closure $code the compiled partial code |
||
562 | * |
||
563 | */ |
||
564 | 9 | public static function in(&$cx, $p, $code) |
|
565 | { |
||
566 | 9 | $cx['partials'][$p] = $code; |
|
567 | 9 | } |
|
568 | |||
569 | /* For single custom helpers. |
||
570 | * |
||
571 | * @param array<string,array|string|integer> $cx render time context for lightncandy |
||
572 | * @param string $ch the name of custom helper to be executed |
||
573 | * @param array<array|string|integer>|string|integer|null $vars variables for the helper |
||
574 | * @param string $op the name of variable resolver. should be one of: 'raw', 'enc', or 'encq'. |
||
575 | * @param array<string,array|string|integer> $_this current rendering context for the helper |
||
576 | * |
||
577 | * @return string The rendered string of the token |
||
578 | */ |
||
579 | 121 | public static function hbch(&$cx, $ch, $vars, $op, &$_this) |
|
599 | |||
600 | /** |
||
601 | * For block custom helpers. |
||
602 | * |
||
603 | * @param array<string,array|string|integer> $cx render time context for lightncandy |
||
604 | * @param string $ch the name of custom helper to be executed |
||
605 | * @param array<array|string|integer>|string|integer|null $vars variables for the helper |
||
606 | * @param array<string,array|string|integer> $_this current rendering context for the helper |
||
607 | * @param boolean $inverted the logic will be inverted |
||
608 | * @param Closure|null $cb callback function to render child context |
||
609 | * @param Closure|null $else callback function to render child context when {{else}} |
||
610 | * |
||
611 | * @return string The rendered string of the token |
||
612 | */ |
||
613 | 57 | public static function hbbch(&$cx, $ch, $vars, &$_this, $inverted, $cb, $else = null) |
|
688 | |||
689 | /** |
||
690 | * Execute custom helper with prepared options |
||
691 | * |
||
692 | * @param array<string,array|string|integer> $cx render time context for lightncandy |
||
693 | * @param string $ch the name of custom helper to be executed |
||
694 | * @param array<array|string|integer>|string|integer|null $vars variables for the helper |
||
695 | * @param array<string,array|string|integer> $options the options object |
||
696 | * |
||
697 | * @return string The rendered string of the token |
||
698 | */ |
||
699 | 168 | public static function exch($cx, $ch, $vars, &$options) |
|
713 | } |
||
714 |
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: