Compare commits

...

9 Commits
v2.x ... master

Author SHA1 Message Date
Dave Mc Nicoll e737e82d7e - Fixed a deprecated attribute not supported on properties.
@
2025-01-25 00:28:54 +00:00
Dave M. 830a643a56 - Fixed route generating (removed methods) 2024-11-21 19:15:04 -05:00
Dave Mc Nicoll b24bbbc150 - Fixed a bug where optional routes were badly parsed 2024-11-01 16:44:52 -04:00
Dave Mc Nicoll 3ffb69342b - Fixed a bug within FunctionToken control structure where multiple definition were runned if a view was loaded more than once 2024-11-01 16:11:31 -04:00
Dave M. fa815a506e - Added a Try/Catch token to the language (and DefaultRegistration also registers it automatically 2024-10-29 13:40:17 +00:00
Dave Mc Nicoll 35a7bd4cf7 - Block can now reference a 'this' object from it's {% using [ 'this' => new stdClass() ] %} token
- Fixed a bug with {% use %} which was buggy whenever two uses were declared and one of them had a curly braces notation in it.
2024-10-21 18:09:33 +00:00
Dave Mc Nicoll 7970679894 - Fixed a bug checking for incomplete data given to build an URL within the UrlExt. 2024-10-18 13:08:29 +00:00
Dave M. d22d26c9c8 - Fixed a bug that appeared deeper when rendering a block inside the slot of another block 2024-10-17 18:14:11 +00:00
Dave Mc Nicoll 934643214e - Fixed a part of Block's doc ; more work to be done on that part
- Fixed whitespaces from unprinted block parts
- Reworked a bit the UrlExtension route grabbing part
2024-10-16 20:30:27 +00:00
8 changed files with 137 additions and 86 deletions

View File

@ -96,9 +96,9 @@ You can do so by using `define` and `slot`.
```html ```html
{% arguments string $name, string $anchor, int $index = 0 %} {% arguments string $name, string $anchor, int $index = 0 %}
{% define slot %} {% define "attributes" %}
<a {% slot "attributes" %}href="{{ $anchor }}" tabindex="{{ $index }}"{% endslot %}>{{ $name }}"</a> <a {% slot "attributes" %}{% endslot %} href="{{ $anchor }}" tabindex="{{ $index }}"{% endslot %}>{{ $name }}"</a>
``` ```
**[HTML]** Would render the same as the `view` example : **[HTML]** Would render the same as the `view` example :

View File

@ -21,7 +21,7 @@ class Builder
$replace = [ $replace = [
'%NAMESPACE%' => $context->namespace, '%NAMESPACE%' => $context->namespace,
'%USE%' => ( $uses = $context->renderUses() ) ? "use $uses;" : false, '%USE%' => ( $uses = $context->renderUses() ) ? $uses : false,
'%CLASSNAME%' => $context->className, '%CLASSNAME%' => $context->className,
'%PATHNAME%' => $path($context->viewPath), '%PATHNAME%' => $path($context->viewPath),
'%FULLPATH%' => $path($context->filePath), '%FULLPATH%' => $path($context->filePath),

View File

@ -57,7 +57,7 @@ abstract class Context {
public function renderUses() : string public function renderUses() : string
{ {
return implode(",", $this->useStack ?? []); return implode(PHP_EOL, array_map(fn($use) => "use $use;", $this->useStack ?? []));
} }
public function renderFunctions() : string public function renderFunctions() : string

View File

@ -8,15 +8,20 @@ class BlockToken implements ControlStructure {
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 $slotDefinitions = []; static $slotDefinitions = [];
# dump($depth, $token, $arguments, $slotDefinitions);
switch($token) { switch($token) {
case "block": case "block":
$slotDefinitions[] = $this->slotDefinitions(); $slotDefinitions[] = $this->slotDefinitions();
$depth++;
return "<?php \$___block = \Picea\ControlStructure\BlockToken::instanciateBlock($arguments); ?>"; return "<?php \$___block = \Picea\ControlStructure\BlockToken::instanciateBlock($arguments); ?>";
case "endblock": case "endblock":
$depth--;
return "<?php echo \$___block->render(\$___class__template); unset(\$___block); ?>"; return "<?php echo \$___block->render(\$___class__template); unset(\$___block); ?>";
case "arguments": case "arguments":
@ -40,14 +45,14 @@ class BlockToken implements ControlStructure {
case "define": case "define":
list($name, $definition) = array_pad(explode(',', $arguments, 2), 2, ""); list($name, $definition) = array_pad(explode(',', $arguments, 2), 2, "");
end($slotDefinitions)->setDefinition(eval("return $name;"), $definition); ( $slotDefinitions[$depth] ?? end($slotDefinitions) )->setDefinition(eval("return $name;"), $definition);
return <<<PHP return <<<PHP
<?php \$this->defineSlot($name, function($definition) {}); ?> <?php \$this->defineSlot($name, function($definition) {}); ?>
PHP; PHP;
case "slot": case "slot":
$def = end($slotDefinitions); $def = ( $slotDefinitions[$depth] ?? end($slotDefinitions) );
list($name, $definition) = array_pad(explode(',', $arguments, 2), 2, ""); list($name, $definition) = array_pad(explode(',', $arguments, 2), 2, "");
@ -65,7 +70,7 @@ class BlockToken implements ControlStructure {
} }
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 $loops) { extract(\$___using, \EXTR_SKIP); ?>
PHP; PHP;
} }
else { else {
@ -74,12 +79,12 @@ class BlockToken implements ControlStructure {
} }
return <<<PHP return <<<PHP
<?php (\$___block ?? \$this)->slotIsSet($name) || \$___block->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 $loops) { extract(\$___using, \EXTR_SKIP); ?>
PHP; PHP;
} }
case "endslot": case "endslot":
$def =end($slotDefinitions); $def = ( $slotDefinitions[$depth] ?? end($slotDefinitions) );
if ($def->hasDefinitions() ) { if ($def->hasDefinitions() ) {
$definition = $def->getCurrentSlotDefinitionVars(); $definition = $def->getCurrentSlotDefinitionVars();
@ -89,7 +94,7 @@ class BlockToken implements ControlStructure {
} }
return <<<PHP return <<<PHP
<?php })->call(\$this, $definition array_merge(get_defined_vars(), \$this->using)); ?> <?php })->call(\$this, $definition array_merge(get_defined_vars(), \$this->using)); ?>
PHP; PHP;
} }
else { else {
@ -184,7 +189,7 @@ class BlockToken implements ControlStructure {
public function printDefinition(string $name) : string public function printDefinition(string $name) : string
{ {
if ( ! isset($this->definitions[$name]) ) { if ( ! isset($this->definitions[$name]) ) {
throw new \Exception("Slot definition for `$name` was not found. Have you defined it in your block header ?"); throw new \Exception("Slot definition for `$name` was not found. Have you defined it in your block header using something like '{% define \"$name\", ...\$arguments %}' ?");
} }
return $this->definitions[$name]; return $this->definitions[$name];
@ -194,7 +199,6 @@ class BlockToken implements ControlStructure {
{ {
$this->rendering = true; $this->rendering = true;
} }
}; };
} }
@ -223,7 +227,12 @@ class BlockToken implements ControlStructure {
{ {
$this->rendering = true; $this->rendering = true;
return $classTemplate->picea->inlineBlock($this, $this->viewPath, ...$this->arguments); if ($this->using['this'] ?? false) {
$thisProxy = $this->using['this'];
unset($this->using['this']);
}
return $classTemplate->picea->inlineBlock($thisProxy ?? $this, $this->viewPath, ...$this->arguments);
} }
public function setSlot(string $name, Callable $method) : void public function setSlot(string $name, Callable $method) : void

View File

@ -33,7 +33,9 @@ class FunctionToken implements ControlStructure {
protected function printFunction($context, ?string $arguments) : string protected function printFunction($context, ?string $arguments) : string
{ {
return "<?php function $arguments { ?>"; $name = trim(explode('(', $arguments, 2)[0]);
return "<?php if (! function_exists(__NAMESPACE__ . '\\$name')) { function $arguments { ?>";
} }
protected function printReturn($context, ?string $arguments) : string protected function printReturn($context, ?string $arguments) : string
@ -43,6 +45,6 @@ class FunctionToken implements ControlStructure {
protected function printEndFunction($context) : string protected function printEndFunction($context) : string
{ {
return "<?php } ?>"; return "<?php } } ?>";
} }
} }

View File

@ -0,0 +1,25 @@
<?php
namespace Picea\ControlStructure;
class TryCatchToken implements ControlStructure {
public array $token = [ "try", "catch", "finally", "endtry" ];
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string {
switch($token) {
case "try":
return "<?php try { ?>";
case "catch":
return "<?php } catch($arguments) { ?>";
case "finally":
return "<?php } finally { ?>";
case "endtry":
return "<?php } ?>";
}
}
}

View File

@ -30,7 +30,7 @@ PATTERN;
public array $tokens = [ "url" , "route", "asset", "slug" ]; public array $tokens = [ "url" , "route", "asset", "slug" ];
#[\Deprecated] ##[\Deprecated]
protected bool $enforceExistingArguments = true; protected bool $enforceExistingArguments = true;
public function __construct(string $urlBase = "", string $assetToken = "", array $appUrl = [], bool $forceSSL = false) { public function __construct(string $urlBase = "", string $assetToken = "", array $appUrl = [], bool $forceSSL = false) {
@ -120,7 +120,7 @@ PATTERN;
public function buildRouteUrl(string $name, array $parameters = [], bool $appendVersion = false) : string public function buildRouteUrl(string $name, array $parameters = [], bool $appendVersion = false) : string
{ {
if ( false !== ( $route = $this->routes[$name] ?? false ) ) { if ( false !== $route = $this->findRoute($name) ) {
return $this->buildUrl($this->prepareRoute($route, $parameters), $parameters, $appendVersion); return $this->buildUrl($this->prepareRoute($route, $parameters), $parameters, $appendVersion);
} }
@ -149,14 +149,28 @@ PATTERN;
public function registerRoute(string $name, string $route, string $class, string $method, array $routeMethods) : void public function registerRoute(string $name, string $route, string $class, string $method, array $routeMethods) : void
{ {
$this->routes[$name] = [ $this->routes[] = $array = [
'name' => $name,
'route' => $route, 'route' => $route,
'routeMethods' => $routeMethods, 'routeMethods' => array_map('strtoupper', $routeMethods),
'class' => $class, 'class' => $class,
'classMethod' => $method, 'classMethod' => $method,
]; ];
$this->eventExecute(UrlRegisterRouteEvent::class, $name, $this->routes[$name]); $this->eventExecute(UrlRegisterRouteEvent::class, $name, $array);
}
protected function findRoute(string $name, string $method = "*") : false|array
{
foreach($this->routes as $route) {
if ( $route['name'] === $name ) {
if ($method === '*' || in_array(strtoupper($method), $route['routeMethods']) ) {
return $route;
}
}
}
return false;
} }
/** /**
@ -275,7 +289,7 @@ PATTERN;
if ($default ?? false) { if ($default ?? false) {
$value = $default; $value = $default;
} }
elseif ( strpos($route, "[{$matches[0][0]}]") !== false && $this->enforceExistingArguments) { elseif ( $this->enforceExistingArguments && ! preg_match(sprintf("/\[\/?%s]/i", $item[0]), $route) ) {
throw new \RuntimeException(sprintf("Error while preparing route %s : could not match variable '%s' into given arguments ( %s ) from %s::%s", $route, $variable, json_encode($arguments), $routeParam['class'], $routeParam['classMethod'])); throw new \RuntimeException(sprintf("Error while preparing route %s : could not match variable '%s' into given arguments ( %s ) from %s::%s", $route, $variable, json_encode($arguments), $routeParam['class'], $routeParam['classMethod']));
} }
} }

View File

@ -32,6 +32,7 @@ class DefaultRegistrations implements LanguageRegistration
public function registerControlStructure(Compiler $compiler) : void public function registerControlStructure(Compiler $compiler) : void
{ {
$compiler->registerControlStructure(new \Picea\ControlStructure\TryCatchToken());
$compiler->registerControlStructure(new \Picea\ControlStructure\NamespaceToken()); $compiler->registerControlStructure(new \Picea\ControlStructure\NamespaceToken());
$compiler->registerControlStructure(new \Picea\ControlStructure\UseToken()); $compiler->registerControlStructure(new \Picea\ControlStructure\UseToken());
$compiler->registerControlStructure(new \Picea\ControlStructure\IfToken()); $compiler->registerControlStructure(new \Picea\ControlStructure\IfToken());