- Small refractoring of code, merge tokens
- Completed block token
This commit is contained in:
1 parent
bf3816c3db
commit
ce4ef6351a
21 files changed
+370
-194
No files matched your search
@@ -4,14 +4,20 @@ namespace Picea\ControlStructure;
|
||||
|
||||
class BlockToken implements ControlStructure {
|
||||
|
||||
public array $token = [ "block", "arguments" ];
|
||||
public array $token = [ "arguments", "block", "endblock", "define", "slot", "endslot", "using" ];
|
||||
|
||||
public function parse(/*\Picae\Compiler\Context*/ &$context, ?string $arguments, string $token) {
|
||||
|
||||
static $slotDefinitions = [];
|
||||
|
||||
switch($token) {
|
||||
case "block":
|
||||
return "<?php echo \$___class__template->picea->inlineHtml(\$this, $arguments); ?>";
|
||||
|
||||
$slotDefinitions[] = $this->slotDefinitions();
|
||||
|
||||
return "<?php \$___block = \Picea\ControlStructure\BlockToken::instanciateBlock($arguments); ?>";
|
||||
|
||||
case "endblock":
|
||||
return "<?php echo \$___block->render(\$___class__template); unset(\$___block); ?>";
|
||||
|
||||
case "arguments":
|
||||
$class = static::class;
|
||||
|
||||
@@ -26,10 +32,73 @@ class BlockToken implements ControlStructure {
|
||||
throw new \Exception('A block awaiting arguments `$arguments` instead received `' . implode(', ', array_map('gettype', \$inlineVariables)) . '`');
|
||||
}
|
||||
?>
|
||||
PHP;
|
||||
}
|
||||
PHP;
|
||||
|
||||
case "define":
|
||||
list($name, $definition) = array_pad(explode(',', $arguments, 2), 2, "");
|
||||
|
||||
end($slotDefinitions)->setDefinition(eval("return $name;"), $definition);
|
||||
|
||||
return <<<PHP
|
||||
<?php \$this->defineSlot($name, function($definition) {}); ?>
|
||||
PHP;
|
||||
|
||||
case "slot":
|
||||
$def = end($slotDefinitions);
|
||||
|
||||
list($name, $definition) = array_pad(explode(',', $arguments, 2), 2, "");
|
||||
|
||||
if ($def->hasDefinitions() ) {
|
||||
$slotName = eval("return $name;");
|
||||
$def->currentSlot = $slotName;
|
||||
$def->setDefinitionVars($slotName, $definition);
|
||||
|
||||
$definition = $def->printDefinition($slotName);
|
||||
|
||||
return <<<PHP
|
||||
<?php \$this->printSlot($name, function($definition, array \$___using = []) { extract(\$___using, \EXTR_SKIP); ?>
|
||||
PHP;
|
||||
}
|
||||
else {
|
||||
return <<<PHP
|
||||
<?php \$___block->slotIsSet($name) || \$___block->setSlot($name, function($definition, array \$___using = []) { extract(\$___using, \EXTR_SKIP); ?>
|
||||
PHP;
|
||||
}
|
||||
|
||||
case "endslot":
|
||||
$def =end($slotDefinitions);
|
||||
|
||||
if ($def->hasDefinitions() ) {
|
||||
$definition = $def->getCurrentSlotDefinitionVars();
|
||||
|
||||
if ($definition) {
|
||||
$definition = "$definition,";
|
||||
}
|
||||
|
||||
return <<<PHP
|
||||
<?php })->call(\$this, $definition \$this->using); ?>
|
||||
PHP;
|
||||
}
|
||||
else {
|
||||
return "<?php }); ?>";
|
||||
}
|
||||
|
||||
case "using":
|
||||
return "<?php \$___block->setUsing($arguments); ?>";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public function inlineHtml(? object $proxy, string $viewPath, ... $variables) {
|
||||
return $this->renderHtml($viewPath, [ 'inlineVariables' => $variables ], $proxy);
|
||||
}
|
||||
|
||||
public static function parseSlotArguments(Callable $method) : array
|
||||
{
|
||||
#return
|
||||
}
|
||||
|
||||
|
||||
public static function parseArguments(Callable $method, array $arguments) : array
|
||||
{
|
||||
try{
|
||||
@@ -42,9 +111,128 @@ class BlockToken implements ControlStructure {
|
||||
$parameters = [];
|
||||
|
||||
foreach((new \ReflectionFunction($method))->getParameters() as $key => $value) {
|
||||
$parameters[ $value->getName() ] = $arguments[$key] ?? $value->getDefaultValue();
|
||||
if ( isset($arguments[$key]) ) {
|
||||
$parameters[ $value->getName() ] = $arguments[$key];
|
||||
}
|
||||
elseif ( $value->isDefaultValueAvailable() ) {
|
||||
$parameters[ $value->getName() ] = $value->getDefaultValue();
|
||||
}
|
||||
elseif ( $value->isVariadic() ) {
|
||||
$parameters[ $value->getName() ] = [];
|
||||
}
|
||||
}
|
||||
|
||||
return $parameters;
|
||||
}
|
||||
|
||||
public static function slotDefinitions() {
|
||||
return new class() {
|
||||
|
||||
public array $definitions = [];
|
||||
|
||||
public array $variables = [];
|
||||
|
||||
public bool $rendering = false;
|
||||
|
||||
public string $currentSlot;
|
||||
|
||||
public function setDefinition(string $name, string $definition) : self
|
||||
{
|
||||
$this->definitions[$name] = $definition;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setDefinitionVars(string $name, string $variables) : self
|
||||
{
|
||||
$this->variables[$name] = $variables;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getDefinitionVars(string $name) : string
|
||||
{
|
||||
return $this->variables[$name] ?? "";
|
||||
}
|
||||
|
||||
public function getCurrentSlotDefinitionVars() : string
|
||||
{
|
||||
return $this->getDefinitionVars($this->currentSlot);
|
||||
}
|
||||
|
||||
public function hasDefinitions() : bool
|
||||
{
|
||||
return count($this->definitions) > 0;
|
||||
}
|
||||
|
||||
public function printDefinition(string $name) : string
|
||||
{
|
||||
if ( ! isset($this->definitions[$name]) ) {
|
||||
throw new \Exception("Slot definition for `$name` was not found. Have you defined it in your block header ?");
|
||||
}
|
||||
|
||||
return $this->definitions[$name];
|
||||
}
|
||||
|
||||
public function render() : void
|
||||
{
|
||||
$this->rendering = true;
|
||||
}
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
public static function instanciateBlock(string $viewPath, ... $arguments)
|
||||
{
|
||||
return new class($viewPath, ...$arguments) {
|
||||
|
||||
public bool $rendering = false;
|
||||
|
||||
public string $viewPath;
|
||||
|
||||
public array $using = [];
|
||||
|
||||
public array $arguments = [];
|
||||
|
||||
public array $slots = [];
|
||||
|
||||
public array $definition = [];
|
||||
|
||||
public function __construct(string $viewPath, ...$arguments) {
|
||||
$this->viewPath = $viewPath;
|
||||
$this->arguments = $arguments;
|
||||
}
|
||||
|
||||
public function render(object $classTemplate) : string
|
||||
{
|
||||
$this->rendering = true;
|
||||
|
||||
return $classTemplate->picea->inlineHtml($this, $this->viewPath, ...$this->arguments);
|
||||
}
|
||||
|
||||
public function setSlot(string $name, Callable $method) : void
|
||||
{
|
||||
$this->slots[$name] = $method;
|
||||
}
|
||||
|
||||
public function defineSlot(string $name, Callable $method) : void
|
||||
{
|
||||
$this->definition[$name] = $method;
|
||||
}
|
||||
|
||||
public function slotIsSet(string $name) : bool
|
||||
{
|
||||
return ! empty($this->slots[$name]);
|
||||
}
|
||||
|
||||
public function printSlot(string $name, Callable $default)
|
||||
{
|
||||
return $this->slotIsSet($name) ? $this->slots[$name] : $default;
|
||||
}
|
||||
|
||||
public function setUsing(array $variables) {
|
||||
$this->using = $variables;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user