- Modernized code a bit and added variables to blocks
This commit is contained in:
1 parent
722131d801
commit
778f84116b
12 files changed
+146
-52
No files matched your search
@@ -2,16 +2,21 @@
|
||||
|
||||
namespace Picea\ControlStructure;
|
||||
|
||||
use http\Exception\InvalidArgumentException;
|
||||
|
||||
class BlockToken implements ControlStructure {
|
||||
|
||||
public array $token = [ "arguments", "block", "endblock", "/block", "define", "slot", "endslot", "/slot", "using" ];
|
||||
public array $token = [ "arguments", "block", "endblock", "/block", "define", "slot", "endslot", "/slot", "using", "public", "private", "protected", "set" ];
|
||||
|
||||
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string
|
||||
{
|
||||
static $depth = 0;
|
||||
static $slotDepth = 0;
|
||||
static $slotDefinitions = [];
|
||||
static $slotName = [];
|
||||
$class= static::class;
|
||||
|
||||
# dump($depth, $token, $arguments, $slotDefinitions);
|
||||
# dump($slotDepth, $token, $arguments, $slotDefinitions);
|
||||
|
||||
switch($token) {
|
||||
case "block":
|
||||
@@ -26,8 +31,6 @@ class BlockToken implements ControlStructure {
|
||||
return "<?php echo \$___block->render(\$___class__template); unset(\$___block); ?>";
|
||||
|
||||
case "arguments":
|
||||
$class = static::class;
|
||||
|
||||
return <<<PHP
|
||||
<?php
|
||||
try {
|
||||
@@ -60,20 +63,21 @@ class BlockToken implements ControlStructure {
|
||||
list($name, $definition) = array_pad(explode(',', $arguments, 2), 2, "");
|
||||
|
||||
$loops = count($context->iterationStack ?? []) ? ",". implode(', ', array_filter(array_column($context->iterationStack, 'uid'), fn($e) => strpos($e, '[') === false)) : null;
|
||||
$slotDepth++;
|
||||
|
||||
if ($def->hasDefinitions() ) {
|
||||
$slotName = eval("return $name;");
|
||||
$def->currentSlot = $slotName;
|
||||
$def->setDefinitionVars($slotName, $definition);
|
||||
$slotName[] = eval("return $name;");
|
||||
$def->currentSlot = end($slotName);
|
||||
$def->setDefinitionVars(end($slotName), $definition);
|
||||
|
||||
$definition = $def->printDefinition($slotName);
|
||||
$definition = $def->printDefinition(end($slotName));
|
||||
|
||||
if ($definition) {
|
||||
$definition .= ",";
|
||||
}
|
||||
|
||||
return <<<PHP
|
||||
<?php \$this->printSlot($name, function($definition array \$___using = []) use (\$picea $loops) { extract(\$___using, \EXTR_SKIP); ?>
|
||||
<?php \$this->printSlot($name, function($definition array \$___using = []) use (\$picea, \$___class__template $loops) { extract(\$___using, \EXTR_SKIP); ?>
|
||||
PHP;
|
||||
}
|
||||
else {
|
||||
@@ -82,15 +86,17 @@ class BlockToken implements ControlStructure {
|
||||
}
|
||||
|
||||
return <<<PHP
|
||||
<?php (\$___block ?? \$this)->slotIsSet($name) || (\$___block ?? \$this)->setSlot($name, function($definition array \$___using = []) use (\$picea $loops) { extract(\$___using, \EXTR_SKIP); ?>
|
||||
<?php (\$___block ?? \$this)->slotIsSet($name) || (\$___block ?? \$this)->setSlot($name, function($definition array \$___using = []) use (\$picea, \$___class__template $loops) { extract(\$___using, \EXTR_SKIP); ?>
|
||||
PHP;
|
||||
}
|
||||
|
||||
case "/slot":
|
||||
case "endslot":
|
||||
$slotDepth--;
|
||||
$def = ( $slotDefinitions[$depth] ?? end($slotDefinitions) );
|
||||
|
||||
if ($def->hasDefinitions() ) {
|
||||
$def->currentSlot = array_pop($slotName);
|
||||
$definition = $def->getCurrentSlotDefinitionVars();
|
||||
|
||||
if ($definition) {
|
||||
@@ -105,9 +111,21 @@ class BlockToken implements ControlStructure {
|
||||
return "<?php }); ?>";
|
||||
}
|
||||
|
||||
case "public":
|
||||
case "private":
|
||||
case "protected":
|
||||
$arguments = rtrim($arguments, ';');
|
||||
return "<?php \$this->createProperty(\\$class::parseProperty('$token', fn($arguments) => func_get_arg(0))) ?>";
|
||||
|
||||
case "set":
|
||||
return "<?php \$___block->setPropertyValue({$arguments}); ?>";
|
||||
|
||||
# DEPRECATED ; should be removed, to be superseded by 'set' instead
|
||||
case "using":
|
||||
return "<?php \$___block->setUsing($arguments); ?>";
|
||||
}
|
||||
|
||||
throw new \LogicException("Unhandled token ? Should never have been here !");
|
||||
}
|
||||
|
||||
public function inlineHtml(? object $proxy, string $viewPath, ... $variables) {
|
||||
@@ -119,7 +137,6 @@ class BlockToken implements ControlStructure {
|
||||
#return
|
||||
}
|
||||
|
||||
|
||||
public static function parseArguments(Callable $method, array $arguments) : array
|
||||
{
|
||||
try{
|
||||
@@ -131,7 +148,6 @@ class BlockToken implements ControlStructure {
|
||||
|
||||
$parameters = [];
|
||||
|
||||
|
||||
foreach((new \ReflectionFunction($method))->getParameters() as $key => $value) {
|
||||
$name = $value->getName();
|
||||
|
||||
@@ -158,6 +174,26 @@ class BlockToken implements ControlStructure {
|
||||
return $parameters;
|
||||
}
|
||||
|
||||
public static function parseProperty(string $token, \Closure $method) : BlockToken\Property
|
||||
{
|
||||
foreach((new \ReflectionFunction($method))->getParameters() as $value) {
|
||||
$property = new BlockToken\Property(
|
||||
name: $value->getName(),
|
||||
scope: BlockToken\PropertyScopeEnum::from($token),
|
||||
setter: $method,
|
||||
);
|
||||
|
||||
if ($value->isDefaultValueAvailable()) {
|
||||
$property->value = $value->getDefaultValue();
|
||||
}
|
||||
|
||||
# For now, we only allow one declaration per token, might be revised at another moment...
|
||||
break;
|
||||
}
|
||||
|
||||
return $property ?? throw new \LogicException("No parameters found in property declaration.");
|
||||
}
|
||||
|
||||
public static function slotDefinitions() {
|
||||
return new class() {
|
||||
|
||||
@@ -230,6 +266,10 @@ class BlockToken implements ControlStructure {
|
||||
|
||||
public array $definition = [];
|
||||
|
||||
protected array $properties = [];
|
||||
|
||||
private array $definedPropertyValues = [];
|
||||
|
||||
public function __construct(string $viewPath, ...$arguments) {
|
||||
$this->viewPath = $viewPath;
|
||||
$this->arguments = $arguments;
|
||||
@@ -261,14 +301,51 @@ class BlockToken implements ControlStructure {
|
||||
return ! empty($this->slots[$name]);
|
||||
}
|
||||
|
||||
public function printSlot(string $name, Callable $default)
|
||||
public function printSlot(string $name, Callable $default) : mixed
|
||||
{
|
||||
return $this->slotIsSet($name) ? $this->slots[$name] : $default;
|
||||
}
|
||||
|
||||
public function setUsing(array $variables) {
|
||||
public function setUsing(array $variables) : void
|
||||
{
|
||||
$this->using = $variables;
|
||||
}
|
||||
|
||||
public function setPropertyValue(array $vars) : void
|
||||
{
|
||||
foreach($vars as $key => $value) {
|
||||
$this->definedPropertyValues[$key] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
public function createProperty(BlockToken\Property $property) : void
|
||||
{
|
||||
if (isset($this->properties[$property->name])) {
|
||||
throw new \InvalidArgumentException("Given property '{$property->name}' is already defined somewhere else in this block.");
|
||||
}
|
||||
|
||||
$this->properties[$property->name] = $property;
|
||||
|
||||
if (isset($this->definedPropertyValues[$property->name])) {
|
||||
if ($property->scope !== BlockToken\PropertyScopeEnum::Public) {
|
||||
throw new \InvalidArgumentException(
|
||||
sprintf("Trying to set property %s but it's scope (%s) makes it impossible", $property->name, $property->scope->name)
|
||||
);
|
||||
}
|
||||
|
||||
$this->__set($property->name, $this->definedPropertyValues[$property->name]);
|
||||
}
|
||||
}
|
||||
|
||||
public function __set(string $name, mixed $value) : void
|
||||
{
|
||||
$this->properties[$name]->value = call_user_func($this->properties[$name]->setter, $value);
|
||||
}
|
||||
|
||||
public function __get(string $name) : mixed
|
||||
{
|
||||
return $this->properties[$name]->value;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user