Skip to content

Commit dde040d

Browse files
committed
Migrate runtime context to an object
1 parent 4136b99 commit dde040d

File tree

10 files changed

+142
-132
lines changed

10 files changed

+142
-132
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Changelog
22
All notable changes to this project will be documented in this file.
33

4-
## [0.9.0] Modern Cleanup - 2025-03-12
4+
## [0.9.0] Modern Cleanup - 2025-03-18
55
Initial release after forking from LightnCandy 1.2.6.
66

77
### Added

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
}
4040
},
4141
"scripts": {
42-
"analyze": "phpstan analyze src tests --level 1",
42+
"analyze": "phpstan analyze",
4343
"cs-fix": "php-cs-fixer fix -v",
4444
"test": "vendor/bin/phpunit tests"
4545
},

phpstan.neon

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
parameters:
2+
level: 2
3+
paths:
4+
- src
5+
- tests

src/Compiler.php

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public static function composePHPRender(Context $context, string $code): string
5555
$runtime = Runtime::class;
5656
$helperOptions = HelperOptions::class;
5757
$safeStringClass = SafeString::class;
58+
$runtimeContext = RuntimeContext::class;
5859
$helpers = Exporter::helpers($context);
5960
$partials = implode(",\n", $context->partialCode);
6061

@@ -63,17 +64,18 @@ public static function composePHPRender(Context $context, string $code): string
6364
use {$runtime} as LR;
6465
use {$safeStringClass};
6566
use {$helperOptions};
67+
use {$runtimeContext};
6668
return function (\$in = null, array \$options = []) {
6769
\$helpers = $helpers;
6870
\$partials = [$partials];
69-
\$cx = [
70-
'helpers' => isset(\$options['helpers']) ? array_merge(\$helpers, \$options['helpers']) : \$helpers,
71-
'partials' => isset(\$options['partials']) ? array_merge(\$partials, \$options['partials']) : \$partials,
72-
'scopes' => [],
73-
'sp_vars' => isset(\$options['data']) ? array_merge(['root' => \$in], \$options['data']) : ['root' => \$in],
74-
'blparam' => [],
75-
'partialid' => 0,
76-
];
71+
\$cx = new RuntimeContext(
72+
helpers: isset(\$options['helpers']) ? array_merge(\$helpers, \$options['helpers']) : \$helpers,
73+
partials: isset(\$options['partials']) ? array_merge(\$partials, \$options['partials']) : \$partials,
74+
scopes: [],
75+
spVars: isset(\$options['data']) ? array_merge(['root' => \$in], \$options['data']) : ['root' => \$in],
76+
blParam: [],
77+
partialId: 0,
78+
);
7779
{$context->ops['op_start']}'$code'{$context->ops['op_end']}
7880
};
7981
VAREND;
@@ -166,18 +168,18 @@ protected static function getVariableName(Context $context, array $var, ?array $
166168

167169
[$levels, $spvar, $var] = Expression::analyze($var);
168170
$exp = Expression::toString($levels, $spvar, $var);
169-
$base = $spvar ? "\$cx['sp_vars']" : '$in';
171+
$base = $spvar ? "\$cx->spVars" : '$in';
170172

171173
// change base when trace to parent
172174
if ($levels > 0) {
173175
if ($spvar) {
174176
$base .= str_repeat("['_parent']", $levels);
175177
} else {
176-
$base = "\$cx['scopes'][count(\$cx['scopes'])-$levels]";
178+
$base = "\$cx->scopes[count(\$cx->scopes)-$levels]";
177179
}
178180
}
179181

180-
if ((empty($var) || (count($var) == 0) || (($var[0] === null) && (count($var) == 1))) && ($lookup === null)) {
182+
if ((!$var || ($var[0] === null && count($var) == 1)) && $lookup === null) {
181183
return [$base, $exp];
182184
}
183185

@@ -295,7 +297,7 @@ public static function inline(Context $context, array $vars): string
295297
protected static function invertedSection(Context $context, array $vars): string
296298
{
297299
$v = static::getVariableName($context, $vars[0]);
298-
return "{$context->ops['cnd_start']}(" . static::getFuncName($context, 'isec', '^' . $v[1]) . "\$cx, {$v[0]})){$context->ops['cnd_then']}";
300+
return "{$context->ops['cnd_start']}(" . static::getFuncName($context, 'isec', '^' . $v[1]) . "{$v[0]})){$context->ops['cnd_then']}";
299301
}
300302

301303
/**
@@ -365,11 +367,11 @@ protected static function blockBegin(Context $context, array $vars): string
365367
case 'if':
366368
$includeZero = (isset($vars['includeZero'][1]) && $vars['includeZero'][1]) ? 'true' : 'false';
367369
return "{$context->ops['cnd_start']}(" . static::getFuncName($context, 'ifvar', $v[1])
368-
. "\$cx, {$v[0]}, {$includeZero})){$context->ops['cnd_then']}";
370+
. "{$v[0]}, {$includeZero})){$context->ops['cnd_then']}";
369371
case 'unless':
370372
$includeZero = (isset($vars['includeZero'][1]) && $vars['includeZero'][1]) ? 'true' : 'false';
371373
return "{$context->ops['cnd_start']}(!" . static::getFuncName($context, 'ifvar', $v[1])
372-
. "\$cx, {$v[0]}, {$includeZero})){$context->ops['cnd_then']}";
374+
. "{$v[0]}, {$includeZero})){$context->ops['cnd_then']}";
373375
case 'each':
374376
return static::section($context, $vars, true);
375377
case 'with':
@@ -495,7 +497,7 @@ protected static function compileLookup(Context $context, array &$vars, bool $ra
495497
$sep = $nosep ? '' : $context->ops['separator'];
496498
$ex = $nosep ? ', 1' : '';
497499

498-
return $sep . static::getFuncName($context, $raw ? 'raw' : 'encq', $v[1]) . "\$cx, {$v[0]}$ex){$sep}";
500+
return $sep . static::getFuncName($context, $raw ? 'raw' : 'encq', $v[1]) . "{$v[0]}$ex){$sep}";
499501
}
500502

501503
/**
@@ -508,7 +510,7 @@ protected static function compileLookup(Context $context, array &$vars, bool $ra
508510
protected static function compileOutput(Context $context, string $variable, string $expression, bool $raw): string
509511
{
510512
$sep = $context->ops['separator'];
511-
return $sep . static::getFuncName($context, $raw ? 'raw' : 'encq', $expression) . "\$cx, $variable)$sep";
513+
return $sep . static::getFuncName($context, $raw ? 'raw' : 'encq', $expression) . "$variable)$sep";
512514
}
513515

514516
/**

src/Encoder.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,22 @@ final class Encoder
1010
/**
1111
* Get the HTML encoded value of the specified variable.
1212
*
13-
* @param array<string,array|string|int> $cx render time context
1413
* @param array<array|string|int>|string|int|bool|null $var value to be htmlencoded
1514
*/
16-
public static function enc(array $cx, array|string|int|bool|null $var): string
15+
public static function enc(array|string|int|bool|null $var): string
1716
{
18-
return htmlspecialchars(Runtime::raw($cx, $var), ENT_QUOTES, 'UTF-8');
17+
return htmlspecialchars(Runtime::raw($var), ENT_QUOTES, 'UTF-8');
1918
}
2019

2120
/**
2221
* Runtime method for {{var}}, and deal with single quote the same as Handlebars.js.
2322
*
24-
* @param array<string,array|string|int> $cx render time context
2523
* @param array<array|string|int>|string|int|bool|null $var value to be htmlencoded
2624
*
2725
* @return string The htmlencoded value of the specified variable
2826
*/
29-
public static function encq(array $cx, array|string|int|bool|null $var)
27+
public static function encq(array|string|int|bool|null $var)
3028
{
31-
return str_replace(['=', '`', '&#039;'], ['&#x3D;', '&#x60;', '&#x27;'], self::enc($cx, $var));
29+
return str_replace(['=', '`', '&#039;'], ['&#x3D;', '&#x60;', '&#x27;'], self::enc($var));
3230
}
3331
}

0 commit comments

Comments
 (0)