- Added a new {% block ... %} token

- Added a new {% php ... %} token
- Fixed a bug with an OR if foreach is empty
This commit is contained in:
Dave Mc Nicoll committed 2020-03-30 12:13:02 -04:00
1 parent 0e201b77f2
commit 404d1f4359
9 files changed
+143 -22

No files matched your search

+50
View File
@@ -0,0 +1,50 @@
<?php
namespace Picea\ControlStructure;
class BlockToken implements ControlStructure {
public array $token = [ "block", "arguments" ];
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) {
switch($token) {
case "block":
return "<?php echo \$___class__template->picea->inlineHtml(\$this, $arguments); ?>";
case "arguments":
$class = static::class;
return <<<PHP
<?php
try {
extract( \\$class::parseArguments(function($arguments) {}, \$inlineVariables), \EXTR_OVERWRITE);
unset(\$inlineVariables);
}
catch(\TypeError \$ex) {
throw new \Exception('A block awaiting arguments `$arguments` instead received `' . implode(', ', array_map('gettype', \$inlineVariables)) . '`');
}
?>
PHP;
}
}
public static function parseArguments(Callable $method, array $arguments) : array
{
try{
call_user_func_array($method, $arguments);
}
catch(\TypeError $ex) {
throw $ex;
}
$parameters = [];
foreach((new \ReflectionFunction($method))->getParameters() as $key => $value) {
$parameters[ $value->getName() ] = $arguments[$key] ?? $value->getDefaultValue();
}
return $parameters;
}
}
+10 -4
View File
@@ -7,14 +7,20 @@ class ForeachToken implements ControlStructure {
public string $token = "foreach";
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) {
$uid = "$".uniqid("foreach_");
$name = "$".uniqid("foreach_");
$count = count($context->iterationStack ?? []);
if ( $count > 0 ) {
$name .= "[" . $context->iterationStack[$count - 1]['uid'] . "]";
}
$context->iterationStack[] = [
'or' => false,
'uid' => $uid,
'uid' => $name,
'token' => 'endforeach',
];
return "<?php foreach ($arguments): {$uid} = 1; ?>";
return "<?php foreach ($arguments): $name = ( $name ?? 0 ) + 1; ; ?>";
}
}
+7 -1
View File
@@ -13,7 +13,13 @@ class OrToken implements ControlStructure {
$key = count( $context->iterationStack ) - 1;
$context->iterationStack[$key]['or'] = true;
return "<?php {$context->iterationStack[$key]['token']}; if( false === ({$context->iterationStack[$key]['uid']} ?? false) ): ?>";
$name = $context->iterationStack[$key]['uid'];
#if ($key !== 0) {
#}
return "<?php {$context->iterationStack[$key]['token']}; if( false === ($name ?? false) ): ?>";
}
}