- 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:
1 parent
0e201b77f2
commit
404d1f4359
9 files changed
+143
-22
No files matched your search
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user