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