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 |
||
| 42 | class Runtime extends Encoder |
||
| 43 | { |
||
| 44 | const DEBUG_ERROR_LOG = 1; |
||
| 45 | const DEBUG_ERROR_EXCEPTION = 2; |
||
| 46 | const DEBUG_TAGS = 4; |
||
| 47 | const DEBUG_TAGS_ANSI = 12; |
||
| 48 | const DEBUG_TAGS_HTML = 20; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Output debug info. |
||
| 52 | * |
||
| 53 | * @param string $v expression |
||
| 54 | * @param string $f runtime function name |
||
| 55 | * @param array<string,array|string|integer> $cx render time context for lightncandy |
||
| 56 | * |
||
| 57 | * @expect '{{123}}' when input '123', 'miss', array('flags' => array('debug' => Runtime::DEBUG_TAGS), 'runtime' => 'LightnCandy\\Runtime'), '' |
||
| 58 | * @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';} |
||
| 59 | */ |
||
| 60 | 9 | public static function debug($v, $f, $cx) { |
|
| 61 | // Build array of reference for call_user_func_array |
||
| 62 | 9 | $P = func_get_args(); |
|
| 63 | 9 | $params = array(); |
|
| 64 | 9 | for ($i=2;$i<count($P);$i++) { |
|
|
|
|||
| 65 | 9 | $params[] = &$P[$i]; |
|
| 66 | } |
||
| 67 | 9 | $r = call_user_func_array((isset($cx['funcs'][$f]) ? $cx['funcs'][$f] : "{$cx['runtime']}::$f"), $params); |
|
| 68 | |||
| 69 | 8 | if ($cx['flags']['debug'] & static::DEBUG_TAGS) { |
|
| 70 | 5 | $ansi = $cx['flags']['debug'] & (static::DEBUG_TAGS_ANSI - static::DEBUG_TAGS); |
|
| 71 | 5 | $html = $cx['flags']['debug'] & (static::DEBUG_TAGS_HTML - static::DEBUG_TAGS); |
|
| 72 | 5 | $cs = ($html ? (($r !== '') ? '<!!--OK((-->' : '<!--MISSED((-->') : '') |
|
| 73 | 5 | . ($ansi ? (($r !== '') ? "\033[0;32m" : "\033[0;31m") : ''); |
|
| 74 | 5 | $ce = ($html ? '<!--))-->' : '') |
|
| 75 | 5 | . ($ansi ? "\033[0m" : ''); |
|
| 76 | 5 | switch ($f) { |
|
| 77 | 5 | case 'sec': |
|
| 78 | 3 | case 'wi': |
|
| 79 | 3 | if ($r == '') { |
|
| 80 | 3 | if ($ansi) { |
|
| 81 | 1 | $r = "\033[0;33mSKIPPED\033[0m"; |
|
| 82 | } |
||
| 83 | 3 | if ($html) { |
|
| 84 | 2 | $r = '<!--SKIPPED-->'; |
|
| 85 | } |
||
| 86 | } |
||
| 87 | 3 | return "$cs{{#{$v}}}$ce{$r}$cs{{/{$v}}}$ce"; |
|
| 88 | default: |
||
| 89 | 3 | return "$cs{{{$v}}}$ce"; |
|
| 90 | } |
||
| 91 | } else { |
||
| 92 | 3 | return $r; |
|
| 93 | } |
||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Handle error by error_log or throw exception. |
||
| 98 | * |
||
| 99 | * @param array<string,array|string|integer> $cx render time context for lightncandy |
||
| 100 | * @param string $err error message |
||
| 101 | * |
||
| 102 | * @throws \Exception |
||
| 103 | */ |
||
| 104 | 18 | public static function err($cx, $err) { |
|
| 113 | |||
| 114 | /** |
||
| 115 | * Handle missing data error. |
||
| 116 | * |
||
| 117 | * @param array<string,array|string|integer> $cx render time context for lightncandy |
||
| 118 | * @param string $v expression |
||
| 119 | */ |
||
| 120 | 5 | public static function miss($cx, $v) { |
|
| 123 | |||
| 124 | /** |
||
| 125 | * For {{log}} . |
||
| 126 | * |
||
| 127 | * @param array<string,array|string|integer> $cx render time context for lightncandy |
||
| 128 | * @param string $v expression |
||
| 129 | */ |
||
| 130 | public static function lo($cx, $v) { |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Resursive lookup variable and helpers. This is slow and will only be used for instance property or method detection or lambdas. |
||
| 137 | * |
||
| 138 | * @param array<string,array|string|integer> $cx render time context for lightncandy |
||
| 139 | * @param array|string|boolean|integer|double|null $in current context |
||
| 140 | * @param array<array|string|integer> $base current variable context |
||
| 141 | * @param array<string|integer> $path array of names for path |
||
| 142 | * @param array|null $args extra arguments for lambda |
||
| 143 | * |
||
| 144 | * @return null|string Return the value or null when not found |
||
| 145 | * |
||
| 146 | * @expect null when input array('scopes' => array(), 'flags' => array('prop' => 0, 'method' => 0, 'mustlok' => 0)), null, 0, array('a', 'b') |
||
| 147 | * @expect 3 when input array('scopes' => array(), 'flags' => array('prop' => 0, 'method' => 0), 'mustlok' => 0), null, array('a' => array('b' => 3)), array('a', 'b') |
||
| 148 | * @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') |
||
| 149 | * @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') |
||
| 150 | */ |
||
| 151 | 388 | public static function v($cx, $in, $base, $path, $args = null) { |
|
| 152 | 388 | $count = count($cx['scopes']); |
|
| 153 | 388 | $plen = count($path); |
|
| 154 | 388 | while ($base) { |
|
| 155 | 352 | $v = $base; |
|
| 156 | 352 | foreach ($path as $i => $name) { |
|
| 157 | 352 | if (is_array($v)) { |
|
| 158 | 350 | if (isset($v[$name])) { |
|
| 159 | 346 | $v = $v[$name]; |
|
| 160 | 346 | continue; |
|
| 161 | } |
||
| 162 | 31 | if (($i === $plen - 1) && ($name === 'length')) { |
|
| 163 | 1 | return count($v); |
|
| 164 | } |
||
| 165 | } |
||
| 166 | 37 | if (is_object($v)) { |
|
| 167 | 6 | if ($cx['flags']['prop'] && !($v instanceof \Closure) && isset($v->$name)) { |
|
| 168 | 4 | $v = $v->$name; |
|
| 169 | 4 | continue; |
|
| 170 | } |
||
| 171 | 5 | if ($cx['flags']['method'] && is_callable(array($v, $name))) { |
|
| 172 | 4 | $v = $v->$name(); |
|
| 173 | 4 | continue; |
|
| 174 | } |
||
| 175 | } |
||
| 176 | 34 | if ($cx['flags']['mustlok']) { |
|
| 177 | 31 | unset($v); |
|
| 178 | 31 | break; |
|
| 179 | } |
||
| 180 | 3 | return null; |
|
| 181 | } |
||
| 182 | 351 | if (isset($v)) { |
|
| 183 | 340 | if ($v instanceof \Closure) { |
|
| 184 | 29 | if ($cx['flags']['mustlam'] || $cx['flags']['lambda']) { |
|
| 185 | 29 | if (!$cx['flags']['knohlp'] && ($args || ($args === 0))) { |
|
| 186 | 20 | $A = $args ? $args[0] : array(); |
|
| 187 | 20 | $A[] = array('hash' => $args[1], '_this' => $in); |
|
| 188 | } else { |
||
| 189 | 10 | $A = array($in); |
|
| 190 | } |
||
| 191 | 29 | $v = call_user_func_array($v, $A); |
|
| 192 | } |
||
| 193 | } |
||
| 194 | 340 | return $v; |
|
| 195 | } |
||
| 196 | 31 | $count--; |
|
| 197 | 31 | switch ($count) { |
|
| 198 | case -1: |
||
| 199 | 30 | $base = $cx['sp_vars']['root']; |
|
| 200 | 30 | break; |
|
| 201 | case -2: |
||
| 202 | 29 | return null; |
|
| 203 | default: |
||
| 204 | 6 | $base = $cx['scopes'][$count]; |
|
| 205 | } |
||
| 206 | } |
||
| 207 | 40 | if ($args) { |
|
| 208 | 3 | static::err($cx, 'Can not find helper or lambda: "' . implode('.', $path) . '" !'); |
|
| 209 | } |
||
| 210 | 40 | } |
|
| 211 | |||
| 212 | /** |
||
| 213 | * For {{#if}} . |
||
| 214 | * |
||
| 215 | * @param array<string,array|string|integer> $cx render time context for lightncandy |
||
| 216 | * @param array<array|string|integer>|string|integer|null $v value to be tested |
||
| 217 | * @param boolean $zero include zero as true |
||
| 218 | * |
||
| 219 | * @return boolean Return true when the value is not null nor false. |
||
| 220 | * |
||
| 221 | * @expect false when input array(), null, false |
||
| 222 | * @expect false when input array(), 0, false |
||
| 223 | * @expect true when input array(), 0, true |
||
| 224 | * @expect false when input array(), false, false |
||
| 225 | * @expect true when input array(), true, false |
||
| 226 | * @expect true when input array(), 1, false |
||
| 227 | * @expect false when input array(), '', false |
||
| 228 | * @expect false when input array(), array(), false |
||
| 229 | * @expect true when input array(), array(''), false |
||
| 230 | * @expect true when input array(), array(0), false |
||
| 231 | */ |
||
| 232 | 74 | public static function ifvar($cx, $v, $zero) { |
|
| 235 | |||
| 236 | /** |
||
| 237 | * For {{^var}} . |
||
| 238 | * |
||
| 239 | * @param array<string,array|string|integer> $cx render time context for lightncandy |
||
| 240 | * @param array<array|string|integer>|string|integer|null $v value to be tested |
||
| 241 | * |
||
| 242 | * @return boolean Return true when the value is not null nor false. |
||
| 243 | * |
||
| 244 | * @expect true when input array(), null |
||
| 245 | * @expect false when input array(), 0 |
||
| 246 | * @expect true when input array(), false |
||
| 247 | * @expect false when input array(), 'false' |
||
| 248 | * @expect true when input array(), array() |
||
| 249 | * @expect false when input array(), array('1') |
||
| 250 | */ |
||
| 251 | 37 | public static function isec($cx, $v) { |
|
| 254 | |||
| 255 | /** |
||
| 256 | * For {{var}} . |
||
| 257 | * |
||
| 258 | * @param array<string,array|string|integer> $cx render time context for lightncandy |
||
| 259 | * @param array<array|string|integer>|string|integer|null $var value to be htmlencoded |
||
| 260 | * |
||
| 261 | * @return string The htmlencoded value of the specified variable |
||
| 262 | * |
||
| 263 | * @expect 'a' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), 'a' |
||
| 264 | * @expect 'a&b' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), 'a&b' |
||
| 265 | * @expect 'a'b' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), 'a\'b' |
||
| 266 | * @expect 'a&b' when input null, new \LightnCandy\SafeString('a&b') |
||
| 267 | */ |
||
| 268 | 46 | public static function enc($cx, $var) { |
|
| 275 | |||
| 276 | /** |
||
| 277 | * For {{var}} , do html encode just like handlebars.js . |
||
| 278 | * |
||
| 279 | * @param array<string,array|string|integer> $cx render time context for lightncandy |
||
| 280 | * @param array<array|string|integer>|string|integer|null $var value to be htmlencoded |
||
| 281 | * |
||
| 282 | * @return string The htmlencoded value of the specified variable |
||
| 283 | * |
||
| 284 | * @expect 'a' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), 'a' |
||
| 285 | * @expect 'a&b' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), 'a&b' |
||
| 286 | * @expect 'a'b' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), 'a\'b' |
||
| 287 | * @expect '`a'b' when input array('flags' => array('mustlam' => 0, 'lambda' => 0)), '`a\'b' |
||
| 288 | */ |
||
| 289 | 341 | public static function encq($cx, $var) { |
|
| 296 | |||
| 297 | /** |
||
| 298 | * For {{#var}} or {{#each}} . |
||
| 299 | * |
||
| 300 | * @param array<string,array|string|integer> $cx render time context for lightncandy |
||
| 301 | * @param array<array|string|integer>|string|integer|null $v value for the section |
||
| 302 | * @param array<string>|null $bp block parameters |
||
| 303 | * @param array<array|string|integer>|string|integer|null $in input data with current scope |
||
| 304 | * @param boolean $each true when rendering #each |
||
| 305 | * @param Closure $cb callback function to render child context |
||
| 306 | * @param Closure|null $else callback function to render child context when {{else}} |
||
| 307 | * |
||
| 308 | * @return string The rendered string of the section |
||
| 309 | * |
||
| 310 | * @expect '' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'mustsec' => 0, 'lambda' => 0)), false, null, false, false, function () {return 'A';} |
||
| 311 | * @expect '' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'mustsec' => 0, 'lambda' => 0)), null, null, null, false, function () {return 'A';} |
||
| 312 | * @expect 'A' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'mustsec' => 0, 'lambda' => 0)), true, null, true, false, function () {return 'A';} |
||
| 313 | * @expect 'A' when input array('flags' => array('spvar' => 0, 'mustlam' => 0, 'mustsec' => 0, 'lambda' => 0)), 0, null, 0, false, function () {return 'A';} |
||
| 314 | * @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=";} |
||
| 315 | * @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=";} |
||
| 316 | * @expect '' when input array('scopes' => array(), 'flags' => array('spvar' => 0, 'mustlam' => 0, 'lambda' => 0)), 'abc', null, 'abc', true, function ($c, $i) {return "-$i=";} |
||
| 317 | * @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=";} |
||
| 318 | * @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);} |
||
| 319 | * @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);} |
||
| 320 | * @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);} |
||
| 321 | * @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);} |
||
| 322 | * @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';} |
||
| 323 | * @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';} |
||
| 324 | * @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';} |
||
| 325 | * @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';} |
||
| 326 | * @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';} |
||
| 327 | * @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';} |
||
| 328 | * @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';} |
||
| 329 | * @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';} |
||
| 330 | * @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';} |
||
| 331 | * @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';} |
||
| 332 | * @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;} |
||
| 333 | * @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'];} |
||
| 334 | */ |
||
| 335 | 170 | public static function sec($cx, $v, $bp, $in, $each, $cb, $else = null) { |
|
| 447 | |||
| 448 | /** |
||
| 449 | * For {{#with}} . |
||
| 450 | * |
||
| 451 | * @param array<string,array|string|integer> $cx render time context for lightncandy |
||
| 452 | * @param array<array|string|integer>|string|integer|null $v value to be the new context |
||
| 453 | * @param array<array|string|integer>|string|integer|null $in input data with current scope |
||
| 454 | * @param array<string>|null $bp block parameters |
||
| 455 | * @param Closure $cb callback function to render child context |
||
| 456 | * @param Closure|null $else callback function to render child context when {{else}} |
||
| 457 | * |
||
| 458 | * @return string The rendered string of the token |
||
| 459 | * |
||
| 460 | * @expect '' when input array(), false, null, false, function () {return 'A';} |
||
| 461 | * @expect '' when input array(), null, null, null, function () {return 'A';} |
||
| 462 | * @expect '{"a":"b"}' when input array(), array('a'=>'b'), null, array('a'=>'c'), function ($c, $i) {return json_encode($i);} |
||
| 463 | * @expect '-b=' when input array(), 'b', null, array('a'=>'b'), function ($c, $i) {return "-$i=";} |
||
| 464 | */ |
||
| 465 | 30 | public static function wi($cx, $v, $bp, $in, $cb, $else = null) { |
|
| 481 | |||
| 482 | /** |
||
| 483 | * Get merged context. |
||
| 484 | * |
||
| 485 | * @param array<string,array|string|integer> $cx render time context for lightncandy |
||
| 486 | * @param array<array|string|integer>|string|integer|null $a the context to be merged |
||
| 487 | * @param array<array|string|integer>|string|integer|null $b the new context to overwrite |
||
| 488 | * |
||
| 489 | * @return array<array|string|integer>|string|integer the merged context object |
||
| 490 | * |
||
| 491 | */ |
||
| 492 | 103 | public static function m($cx, $a, $b) { |
|
| 509 | |||
| 510 | /** |
||
| 511 | * For {{> partial}} . |
||
| 512 | * |
||
| 513 | * @param array<string,array|string|integer> $cx render time context for lightncandy |
||
| 514 | * @param string $p partial name |
||
| 515 | * @param array<array|string|integer>|string|integer|null $v value to be the new context |
||
| 516 | * |
||
| 517 | * @return string The rendered string of the partial |
||
| 518 | * |
||
| 519 | */ |
||
| 520 | 100 | public static function p($cx, $p, $v, $pid, $sp = '') { |
|
| 532 | |||
| 533 | /** |
||
| 534 | * For {{#* inlinepartial}} . |
||
| 535 | * |
||
| 536 | * @param array<string,array|string|integer> $cx render time context for lightncandy |
||
| 537 | * @param string $p partial name |
||
| 538 | * @param Closure $code the compiled partial code |
||
| 539 | * |
||
| 540 | */ |
||
| 541 | 7 | public static function in(&$cx, $p, $code) { |
|
| 544 | |||
| 545 | /* For single custom helpers. |
||
| 546 | * |
||
| 547 | * @param array<string,array|string|integer> $cx render time context for lightncandy |
||
| 548 | * @param string $ch the name of custom helper to be executed |
||
| 549 | * @param array<array|string|integer>|string|integer|null $vars variables for the helper |
||
| 550 | * @param string $op the name of variable resolver. should be one of: 'raw', 'enc', or 'encq'. |
||
| 551 | * @param array<string,array|string|integer> $_this current rendering context for the helper |
||
| 552 | * |
||
| 553 | * @return string The rendered string of the token |
||
| 554 | */ |
||
| 555 | 120 | public static function hbch($cx, $ch, $vars, $op, &$_this) { |
|
| 574 | |||
| 575 | /** |
||
| 576 | * For block custom helpers. |
||
| 577 | * |
||
| 578 | * @param array<string,array|string|integer> $cx render time context for lightncandy |
||
| 579 | * @param string $ch the name of custom helper to be executed |
||
| 580 | * @param array<array|string|integer>|string|integer|null $vars variables for the helper |
||
| 581 | * @param array<string,array|string|integer> $_this current rendering context for the helper |
||
| 582 | * @param boolean $inverted the logic will be inverted |
||
| 583 | * @param Closure|null $cb callback function to render child context |
||
| 584 | * @param Closure|null $else callback function to render child context when {{else}} |
||
| 585 | * |
||
| 586 | * @return string The rendered string of the token |
||
| 587 | */ |
||
| 588 | 57 | public static function hbbch($cx, $ch, $vars, &$_this, $inverted, $cb, $else = null) { |
|
| 589 | $options = array( |
||
| 590 | 57 | 'name' => $ch, |
|
| 591 | 57 | 'hash' => $vars[1], |
|
| 592 | 57 | 'contexts' => count($cx['scopes']) ? $cx['scopes'] : array(null), |
|
| 593 | 57 | 'fn.blockParams' => 0, |
|
| 594 | 57 | '_this' => &$_this, |
|
| 595 | ); |
||
| 596 | |||
| 597 | 57 | if ($cx['flags']['spvar']) { |
|
| 598 | 53 | $options['data'] = $cx['sp_vars']; |
|
| 599 | } |
||
| 600 | |||
| 601 | 57 | if (isset($vars[2])) { |
|
| 602 | 3 | $options['fn.blockParams'] = count($vars[2]); |
|
| 603 | } |
||
| 604 | |||
| 605 | // $invert the logic |
||
| 606 | 57 | if ($inverted) { |
|
| 607 | $tmp = $else; |
||
| 608 | $else = $cb; |
||
| 609 | $cb = $tmp; |
||
| 610 | } |
||
| 611 | |||
| 612 | $options['fn'] = function ($context = '_NO_INPUT_HERE_', $data = null) use ($cx, &$_this, $cb, $options, $vars) { |
||
| 613 | 46 | if ($cx['flags']['echo']) { |
|
| 614 | ob_start(); |
||
| 615 | } |
||
| 616 | 46 | if (isset($data['data'])) { |
|
| 617 | 6 | $old_spvar = $cx['sp_vars']; |
|
| 618 | 6 | $cx['sp_vars'] = array_merge(array('root' => $old_spvar['root']), $data['data'], array('_parent' => $old_spvar)); |
|
| 619 | } |
||
| 620 | 46 | $ex = false; |
|
| 621 | 46 | if (isset($data['blockParams']) && isset($vars[2])) { |
|
| 622 | 3 | $ex = array_combine($vars[2], array_slice($data['blockParams'], 0, count($vars[2]))); |
|
| 623 | 3 | array_unshift($cx['blparam'], $ex); |
|
| 624 | 44 | } else if (isset($cx['blparam'][0])) { |
|
| 625 | 1 | $ex = $cx['blparam'][0]; |
|
| 626 | } |
||
| 627 | 46 | if (($context === '_NO_INPUT_HERE_') || ($context === $_this)) { |
|
| 628 | 29 | $ret = $cb($cx, is_array($ex) ? static::m($cx, $_this, $ex) : $_this); |
|
| 629 | } else { |
||
| 630 | 20 | $cx['scopes'][] = $_this; |
|
| 631 | 20 | $ret = $cb($cx, is_array($ex) ? static::m($cx, $context, $ex) : $context); |
|
| 632 | 20 | array_pop($cx['scopes']); |
|
| 633 | } |
||
| 634 | 46 | if (isset($data['data'])) { |
|
| 635 | 6 | $cx['sp_vars'] = $old_spvar; |
|
| 636 | } |
||
| 637 | 46 | return $cx['flags']['echo'] ? ob_get_clean() : $ret; |
|
| 638 | }; |
||
| 639 | |||
| 640 | 57 | if ($else) { |
|
| 641 | $options['inverse'] = function ($context = '_NO_INPUT_HERE_') use ($cx, $_this, $else) { |
||
| 642 | 6 | if ($cx['flags']['echo']) { |
|
| 643 | ob_start(); |
||
| 644 | } |
||
| 645 | 6 | if ($context === '_NO_INPUT_HERE_') { |
|
| 646 | 1 | $ret = $else($cx, $_this); |
|
| 647 | } else { |
||
| 648 | 5 | $cx['scopes'][] = $_this; |
|
| 649 | 5 | $ret = $else($cx, $context); |
|
| 650 | 5 | array_pop($cx['scopes']); |
|
| 651 | } |
||
| 652 | 6 | return $cx['flags']['echo'] ? ob_get_clean() : $ret; |
|
| 653 | 10 | }; |
|
| 654 | } else { |
||
| 655 | $options['inverse'] = function () { |
||
| 656 | return ''; |
||
| 657 | }; |
||
| 658 | } |
||
| 659 | |||
| 660 | 57 | return static::exch($cx, $ch, $vars, $options); |
|
| 661 | } |
||
| 662 | |||
| 663 | /** |
||
| 664 | * Execute custom helper with prepared options |
||
| 665 | * |
||
| 666 | * @param array<string,array|string|integer> $cx render time context for lightncandy |
||
| 667 | * @param string $ch the name of custom helper to be executed |
||
| 668 | * @param array<array|string|integer>|string|integer|null $vars variables for the helper |
||
| 669 | * @param array<string,array|string|integer> $options the options object |
||
| 670 | * |
||
| 671 | * @return string The rendered string of the token |
||
| 672 | */ |
||
| 673 | 167 | public static function exch($cx, $ch, $vars, &$options) { |
|
| 691 | } |
||
| 692 |
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: