- Modernized code a bit and added variables to blocks
This commit is contained in:
parent
722131d801
commit
778f84116b
@ -20,7 +20,10 @@ return [
|
|||||||
},
|
},
|
||||||
|
|
||||||
Context::class => function($c) {
|
Context::class => function($c) {
|
||||||
return new BaseContext($c->get(Lean\Lean::class)->getPiceaContext());
|
return new BaseContext(
|
||||||
|
defaultNamespace: $c->get(Lean\Lean::class)->getPiceaContext(),
|
||||||
|
declare: getenv('PICEA_DECLARE') ?? 'declare(strict_types=1)'
|
||||||
|
);
|
||||||
},
|
},
|
||||||
|
|
||||||
Compiler::class => autowire(Compiler::class),
|
Compiler::class => autowire(Compiler::class),
|
||||||
|
|||||||
@ -22,6 +22,7 @@ class Builder
|
|||||||
$replace = [
|
$replace = [
|
||||||
'%NAMESPACE%' => $context->namespace,
|
'%NAMESPACE%' => $context->namespace,
|
||||||
'%USE%' => ( $uses = $context->renderUses() ) ? $uses : false,
|
'%USE%' => ( $uses = $context->renderUses() ) ? $uses : false,
|
||||||
|
'%DECLARE%' => $context->declare ? rtrim($context->declare, ';') . ';' : "",
|
||||||
'%CLASSNAME%' => $context->className,
|
'%CLASSNAME%' => $context->className,
|
||||||
'%PATHNAME%' => $path($context->viewPath),
|
'%PATHNAME%' => $path($context->viewPath),
|
||||||
'%FULLPATH%' => $path($context->filePath),
|
'%FULLPATH%' => $path($context->filePath),
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
<?php
|
<?php %DECLARE%
|
||||||
|
|
||||||
namespace %NAMESPACE%;
|
namespace %NAMESPACE%;
|
||||||
|
|
||||||
|
|||||||
@ -4,7 +4,25 @@ namespace Picea\Compiler;
|
|||||||
|
|
||||||
class BaseContext extends Context {
|
class BaseContext extends Context {
|
||||||
|
|
||||||
public function __construct(?string $defaultNamespace = null) {
|
public function __construct(
|
||||||
|
?string $defaultNamespace = null,
|
||||||
|
public string $namespace = "",
|
||||||
|
public string $extendFrom = "",
|
||||||
|
public string $className = "",
|
||||||
|
public string $compiledSource = "",
|
||||||
|
public ? string $sectionAction = null,
|
||||||
|
public string $viewPath = "",
|
||||||
|
public string $filePath = "",
|
||||||
|
public string $classPath = "",
|
||||||
|
public string $declare = "declare(strict_types=1);",
|
||||||
|
public array $switchStack = [],
|
||||||
|
public array $iterationStack = [],
|
||||||
|
public array $useStack = [],
|
||||||
|
public array $sections = [],
|
||||||
|
public int $functions = 0,
|
||||||
|
public array $functionStack = [],
|
||||||
|
public array $hooks = []
|
||||||
|
) {
|
||||||
if ( $defaultNamespace !== null ) {
|
if ( $defaultNamespace !== null ) {
|
||||||
$this->namespace = $defaultNamespace;
|
$this->namespace = $defaultNamespace;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -6,36 +6,6 @@ use Picea\Compiler;
|
|||||||
|
|
||||||
abstract class Context {
|
abstract class Context {
|
||||||
|
|
||||||
public string $namespace = "";
|
|
||||||
|
|
||||||
public string $extendFrom = "";
|
|
||||||
|
|
||||||
public string $className = "";
|
|
||||||
|
|
||||||
public string $compiledSource = "";
|
|
||||||
|
|
||||||
public ? string $sectionAction = null;
|
|
||||||
|
|
||||||
public string $viewPath = "";
|
|
||||||
|
|
||||||
public string $filePath = "";
|
|
||||||
|
|
||||||
public string $classPath = "";
|
|
||||||
|
|
||||||
public array $switchStack = [];
|
|
||||||
|
|
||||||
public array $iterationStack = [];
|
|
||||||
|
|
||||||
public array $useStack = [];
|
|
||||||
|
|
||||||
public array $sections = [];
|
|
||||||
|
|
||||||
public int $functions = 0;
|
|
||||||
|
|
||||||
public array $functionStack = [];
|
|
||||||
|
|
||||||
public array $hooks = [];
|
|
||||||
|
|
||||||
public Compiler $compiler;
|
public Compiler $compiler;
|
||||||
|
|
||||||
public function hook(string $path, Callable $callback) : self
|
public function hook(string $path, Callable $callback) : self
|
||||||
|
|||||||
@ -2,16 +2,21 @@
|
|||||||
|
|
||||||
namespace Picea\ControlStructure;
|
namespace Picea\ControlStructure;
|
||||||
|
|
||||||
|
use http\Exception\InvalidArgumentException;
|
||||||
|
|
||||||
class BlockToken implements ControlStructure {
|
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
|
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string
|
||||||
{
|
{
|
||||||
static $depth = 0;
|
static $depth = 0;
|
||||||
|
static $slotDepth = 0;
|
||||||
static $slotDefinitions = [];
|
static $slotDefinitions = [];
|
||||||
|
static $slotName = [];
|
||||||
|
$class= static::class;
|
||||||
|
|
||||||
# dump($depth, $token, $arguments, $slotDefinitions);
|
# dump($slotDepth, $token, $arguments, $slotDefinitions);
|
||||||
|
|
||||||
switch($token) {
|
switch($token) {
|
||||||
case "block":
|
case "block":
|
||||||
@ -26,8 +31,6 @@ class BlockToken implements ControlStructure {
|
|||||||
return "<?php echo \$___block->render(\$___class__template); unset(\$___block); ?>";
|
return "<?php echo \$___block->render(\$___class__template); unset(\$___block); ?>";
|
||||||
|
|
||||||
case "arguments":
|
case "arguments":
|
||||||
$class = static::class;
|
|
||||||
|
|
||||||
return <<<PHP
|
return <<<PHP
|
||||||
<?php
|
<?php
|
||||||
try {
|
try {
|
||||||
@ -60,20 +63,21 @@ class BlockToken implements ControlStructure {
|
|||||||
list($name, $definition) = array_pad(explode(',', $arguments, 2), 2, "");
|
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;
|
$loops = count($context->iterationStack ?? []) ? ",". implode(', ', array_filter(array_column($context->iterationStack, 'uid'), fn($e) => strpos($e, '[') === false)) : null;
|
||||||
|
$slotDepth++;
|
||||||
|
|
||||||
if ($def->hasDefinitions() ) {
|
if ($def->hasDefinitions() ) {
|
||||||
$slotName = eval("return $name;");
|
$slotName[] = eval("return $name;");
|
||||||
$def->currentSlot = $slotName;
|
$def->currentSlot = end($slotName);
|
||||||
$def->setDefinitionVars($slotName, $definition);
|
$def->setDefinitionVars(end($slotName), $definition);
|
||||||
|
|
||||||
$definition = $def->printDefinition($slotName);
|
$definition = $def->printDefinition(end($slotName));
|
||||||
|
|
||||||
if ($definition) {
|
if ($definition) {
|
||||||
$definition .= ",";
|
$definition .= ",";
|
||||||
}
|
}
|
||||||
|
|
||||||
return <<<PHP
|
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;
|
PHP;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@ -82,15 +86,17 @@ class BlockToken implements ControlStructure {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return <<<PHP
|
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;
|
PHP;
|
||||||
}
|
}
|
||||||
|
|
||||||
case "/slot":
|
case "/slot":
|
||||||
case "endslot":
|
case "endslot":
|
||||||
|
$slotDepth--;
|
||||||
$def = ( $slotDefinitions[$depth] ?? end($slotDefinitions) );
|
$def = ( $slotDefinitions[$depth] ?? end($slotDefinitions) );
|
||||||
|
|
||||||
if ($def->hasDefinitions() ) {
|
if ($def->hasDefinitions() ) {
|
||||||
|
$def->currentSlot = array_pop($slotName);
|
||||||
$definition = $def->getCurrentSlotDefinitionVars();
|
$definition = $def->getCurrentSlotDefinitionVars();
|
||||||
|
|
||||||
if ($definition) {
|
if ($definition) {
|
||||||
@ -105,9 +111,21 @@ class BlockToken implements ControlStructure {
|
|||||||
return "<?php }); ?>";
|
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":
|
case "using":
|
||||||
return "<?php \$___block->setUsing($arguments); ?>";
|
return "<?php \$___block->setUsing($arguments); ?>";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
throw new \LogicException("Unhandled token ? Should never have been here !");
|
||||||
}
|
}
|
||||||
|
|
||||||
public function inlineHtml(? object $proxy, string $viewPath, ... $variables) {
|
public function inlineHtml(? object $proxy, string $viewPath, ... $variables) {
|
||||||
@ -119,7 +137,6 @@ class BlockToken implements ControlStructure {
|
|||||||
#return
|
#return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static function parseArguments(Callable $method, array $arguments) : array
|
public static function parseArguments(Callable $method, array $arguments) : array
|
||||||
{
|
{
|
||||||
try{
|
try{
|
||||||
@ -131,7 +148,6 @@ class BlockToken implements ControlStructure {
|
|||||||
|
|
||||||
$parameters = [];
|
$parameters = [];
|
||||||
|
|
||||||
|
|
||||||
foreach((new \ReflectionFunction($method))->getParameters() as $key => $value) {
|
foreach((new \ReflectionFunction($method))->getParameters() as $key => $value) {
|
||||||
$name = $value->getName();
|
$name = $value->getName();
|
||||||
|
|
||||||
@ -158,6 +174,26 @@ class BlockToken implements ControlStructure {
|
|||||||
return $parameters;
|
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() {
|
public static function slotDefinitions() {
|
||||||
return new class() {
|
return new class() {
|
||||||
|
|
||||||
@ -230,6 +266,10 @@ class BlockToken implements ControlStructure {
|
|||||||
|
|
||||||
public array $definition = [];
|
public array $definition = [];
|
||||||
|
|
||||||
|
protected array $properties = [];
|
||||||
|
|
||||||
|
private array $definedPropertyValues = [];
|
||||||
|
|
||||||
public function __construct(string $viewPath, ...$arguments) {
|
public function __construct(string $viewPath, ...$arguments) {
|
||||||
$this->viewPath = $viewPath;
|
$this->viewPath = $viewPath;
|
||||||
$this->arguments = $arguments;
|
$this->arguments = $arguments;
|
||||||
@ -261,14 +301,51 @@ class BlockToken implements ControlStructure {
|
|||||||
return ! empty($this->slots[$name]);
|
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;
|
return $this->slotIsSet($name) ? $this->slots[$name] : $default;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setUsing(array $variables) {
|
public function setUsing(array $variables) : void
|
||||||
|
{
|
||||||
$this->using = $variables;
|
$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;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
14
src/ControlStructure/BlockToken/Property.php
Normal file
14
src/ControlStructure/BlockToken/Property.php
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Picea\ControlStructure\BlockToken;
|
||||||
|
|
||||||
|
class Property
|
||||||
|
{
|
||||||
|
public mixed $value;
|
||||||
|
|
||||||
|
public function __construct(
|
||||||
|
public readonly string $name,
|
||||||
|
public readonly PropertyScopeEnum $scope,
|
||||||
|
public readonly \Closure $setter,
|
||||||
|
) {}
|
||||||
|
}
|
||||||
10
src/ControlStructure/BlockToken/PropertyScopeEnum.php
Normal file
10
src/ControlStructure/BlockToken/PropertyScopeEnum.php
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Picea\ControlStructure\BlockToken;
|
||||||
|
|
||||||
|
enum PropertyScopeEnum: string
|
||||||
|
{
|
||||||
|
case Private = "private";
|
||||||
|
case Protected = "protected";
|
||||||
|
case Public = "public";
|
||||||
|
}
|
||||||
@ -4,7 +4,7 @@ namespace Picea\ControlStructure;
|
|||||||
|
|
||||||
class BreakToken implements ControlStructure {
|
class BreakToken implements ControlStructure {
|
||||||
|
|
||||||
public string $token = "break";
|
public array $token = [ "break", "/case" ];
|
||||||
|
|
||||||
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string
|
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string
|
||||||
{
|
{
|
||||||
|
|||||||
@ -4,7 +4,7 @@ namespace Picea\ControlStructure;
|
|||||||
|
|
||||||
class SwitchToken implements ControlStructure {
|
class SwitchToken implements ControlStructure {
|
||||||
|
|
||||||
public array $token = [ "switch", "case", "endswitch" ];
|
public array $token = [ "switch", "case", "endcase", "endswitch", "/switch" ];
|
||||||
|
|
||||||
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string {
|
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string {
|
||||||
switch($token) {
|
switch($token) {
|
||||||
|
|||||||
@ -23,7 +23,7 @@ class TitleExtension implements Extension, FunctionExtension {
|
|||||||
"} ?>";
|
"} ?>";
|
||||||
}
|
}
|
||||||
|
|
||||||
public function handleTitle(? string $set = null, ...$arguments) : ? string
|
public function handleTitle(null|\Stringable|string $set = null, ...$arguments) : ? string
|
||||||
{
|
{
|
||||||
if ( $set === null ) {
|
if ( $set === null ) {
|
||||||
return $this->title;
|
return $this->title;
|
||||||
@ -31,6 +31,7 @@ class TitleExtension implements Extension, FunctionExtension {
|
|||||||
else {
|
else {
|
||||||
# Fixed a bug where template inheritance was rewriting title
|
# Fixed a bug where template inheritance was rewriting title
|
||||||
if ( empty($this->title) ) {
|
if ( empty($this->title) ) {
|
||||||
|
$set = (string) $set;
|
||||||
$this->title = $arguments ? sprintf($set, ...$arguments) : $set;
|
$this->title = $arguments ? sprintf($set, ...$arguments) : $set;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -138,9 +138,9 @@ PATTERN;
|
|||||||
}
|
}
|
||||||
|
|
||||||
# src: https://stackoverflow.com/a/14550919
|
# src: https://stackoverflow.com/a/14550919
|
||||||
public static function slug(string $text, string $separator = '-') : string
|
public static function slug(\Stringable|string $text, string $separator = '-') : string
|
||||||
{
|
{
|
||||||
$clean = iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $text);
|
$clean = iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', (string) $text);
|
||||||
$clean = preg_replace("/[^a-zA-Z0-9\/_| -]/", '', $clean);
|
$clean = preg_replace("/[^a-zA-Z0-9\/_| -]/", '', $clean);
|
||||||
|
|
||||||
return preg_replace("/[\/_| -]+/", $separator, strtolower(trim($clean, '-')));
|
return preg_replace("/[\/_| -]+/", $separator, strtolower(trim($clean, '-')));
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user