Compare commits

..

No commits in common. "master" and "tokens_v2" have entirely different histories.

51 changed files with 289 additions and 611 deletions

View File

@ -6,7 +6,7 @@
"authors": [
{
"name": "Dave Mc Nicoll",
"email": "info@mcnd.ca"
"email": "mcndave@gmail.com"
}
],
"autoload": {
@ -16,14 +16,5 @@
},
"require": {
"psr/http-message": "^1.0"
},
"extra": {
"lean": {
"autoload": {
"definitions": [
"meta/definitions.php"
]
}
}
}
}
}

View File

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

View File

@ -10,7 +10,7 @@ use function DI\autowire, DI\create, DI\get;
use Laminas\Diactoros\Response\HtmlResponse;
use Picea\{ Picea, Caching\Cache, Caching\Opcache, Compiler, Compiler\Context, Compiler\BaseContext, FileFetcher, Language\DefaultRegistrations, Method\Request };
use Picea\Extension\{ LanguageHandlerInterface, LanguageExtension, TitleExtension, NumberExtension, UrlExtension };
use Picea\Extension\{ LanguageHandler, LanguageExtension, TitleExtension, MoneyExtension, UrlExtension };
use Picea\Ui\{ Method, Ui };
return [
@ -26,7 +26,7 @@ return [
return new Compiler(new class(array_merge([
$c->get(LanguageExtension::class),
$c->get(TitleExtension::class),
$c->get(NumberExtension::class),
$c->get(MoneyExtension::class),
$c->get(UrlExtension::class),
$c->get(Method\Form::class),
$c->get(Method\Pagination::class),
@ -49,10 +49,10 @@ return [
Method\Pagination::class => autowire(Method\Pagination::class),
LanguageExtension::class => create(LanguageExtension::class)->constructor(get(LanguageHandlerInterface::class)),
LanguageExtension::class => create(LanguageExtension::class)->constructor(get(LanguageHandler::class)),
LanguageHandlerInterface::class => function($c) {
return new class( $c->get(Tell\I18n::class) ) implements LanguageHandlerInterface {
LanguageHandler::class => function($c) {
return new class( $c->get(Tell\I18n::class) ) implements LanguageHandler {
public Tell\I18n $tell;
public function __construct(Tell\I18n $tell) {
@ -68,7 +68,7 @@ return [
TitleExtension::class => autowire(TitleExtension::class),
NumberExtension::class => autowire(NumberExtension::class),
MoneyExtension::class => autowire(MoneyExtension::class),
UrlExtension::class => create(UrlExtension::class)->constructor(get(Context::class), getenv("URL_BASE"), get('git.commit')),

View File

@ -1,59 +0,0 @@
<?php
use function DI\autowire, DI\create, DI\get;
use Picea\{
Caching\Cache,
Caching\Opcache,
Compiler,
Compiler\Context,
Compiler\BaseContext,
FileFetcher,
Method\Request
};
use Picea\Extension\{ LanguageHandlerInterface, LanguageExtension, TitleExtension, NumberExtension, UrlExtension };
return [
Picea\Picea::class => function($c) {
return new Picea\Picea($c->get(Context::class), $c->get(Cache::class), $c->get(Compiler::class), null, $c->get(FileFetcher::class), null, getenv("DEBUG"));
},
Context::class => function($c) {
return new BaseContext($c->get(Lean\Lean::class)->getPiceaContext());
},
Compiler::class => autowire(Compiler::class),
Request::class => autowire(Request::class),
LanguageExtension::class => create(LanguageExtension::class)->constructor(get(LanguageHandlerInterface::class)),
# LanguageHandlerInterface::class => autowire(\Lean\LanguageHandler::class),
# LanguageRegistration::class => create(\Lean\PiceaDefaultRegistration::class)->constructor(get('picea.extensions'), [], [], get(Ui::class), null),
'picea.extensions' => function(\Psr\Container\ContainerInterface $c) {
return array_merge([
$c->get(LanguageExtension::class),
$c->get(TitleExtension::class),
$c->get(NumberExtension::class),
$c->get(UrlExtension::class),
$c->get(Request::class),
], class_exists(\Taxus\Picea\Extension::class) ? [ $c->get(\Taxus\Picea\Extension::class) ] : [],
array_map(fn($class) => $c->get($class), $c->get(Lean\Lean::class)->getPiceaExtensions())
);
},
TitleExtension::class => autowire(TitleExtension::class),
NumberExtension::class => autowire(NumberExtension::class),
UrlExtension::class => create(UrlExtension::class)->constructor(getenv("URL_BASE"), get('git.commit'), explode(',', getenv('APP_URL')), (bool) getenv('FORCE_SSL')),
Cache::class => create(Opcache::class)->constructor(getenv("CACHE_PATH"), get(Context::class)),
FileFetcher::class => function($c) {
return new FileFetcher($c->get(Lean\Lean::class)->getViewPaths());
},
];

View File

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

View File

@ -50,21 +50,15 @@ if (! class_exists("%NAMESPACE%\%CLASSNAME%", false) ) {
$__event->eventExecute(\Picea\Event\Builder\ClassTemplateOutputing::class, $variablesList);
try {
( function($___class__template, $___global_variables, $___variables, $__event, $picea) {
extract($___global_variables);
extract($___variables, \EXTR_OVERWRITE);
?>%CONTENT%<?php
} )->call($this->thisProxy ?? new class() {}, $this, $this->variableList, $variablesList, $__event, $this->picea);
} catch (\Throwable $error) {
throw $this->errorHandler($error);
}
( function($___class__template, $___global_variables, $___variables, $__event, $picea) {
extract($___global_variables);
extract($___variables, \EXTR_OVERWRITE);
?>%CONTENT%<?php
} )->call($this->thisProxy ?? new class() {}, $this, $this->variableList, $variablesList, $__event, $this->picea);
$__event->eventExecute(\Picea\Event\Builder\ClassTemplateOutputDone::class, $variablesList);
try { %PARENT_OUTPUT% } catch (\Throwable $error) {
throw $this->errorHandler($error);
}
%PARENT_OUTPUT%
$this->depth--;
@ -144,32 +138,6 @@ if (! class_exists("%NAMESPACE%\%CLASSNAME%", false) ) {
$this->output($variablesList);
return ob_get_clean();
}
public static function isTemplateError(\Throwable $error) : bool
{
$class = substr(static::class, strripos(static::class, '\\') + 1) . ".php";
return substr($error->getFile(), -strlen($class)) === $class;
}
protected function errorHandler(\Throwable $error) : \Throwable
{
$class = substr(static::class, strrpos(static::class, '\\') + 1) . ".php";
#dump( __NAMESPACE__, basename($error->getFile()), $class );
$basename = basename($error->getFile());
#dump(is_subclass_of(sprintf("\\%s\\%s", __NAMESPACE__, substr($basename,0 , strrpos($basename, '.'))), static::class, true));
if (str_ends_with($error->getFile(), $class)) {
$error = new \Picea\Exception\RenderHtmlException($this, $error->getMessage(), $error->getCode(), $error);
}
#elseif (is_subclass_of(sprintf("\\%s\\%s", __NAMESPACE__, substr($basename,0 , strrpos($basename, '.'))), static::class, true)) {
# $error = new \Picea\Exception\RenderHtmlException($this, $error->getMessage(), $error->getCode(), $error);
#}
return $error;
}
}
}

View File

@ -12,9 +12,9 @@ class Opcache implements Cache {
protected array $compiled = [];
protected null|Context $context;
protected ? Context $context;
public function __construct(?string $cachePath = null, null|Context $context = null)
public function __construct(?string $cachePath = null, ? Context $context)
{
$this->cachePath = $cachePath ?? sys_get_temp_dir();
$this->context = $context;
@ -34,7 +34,7 @@ class Opcache implements Cache {
$compiledContext = $this->compiled[$viewPath];
$fullName = isset($compiledContext['namespace']) ? "\\{$compiledContext['namespace']}\\{$compiledContext['classname']}" : ($compiledContext['classname'] ?? $viewPath);
$fullName = isset($compiledContext['namespace']) ? "\\{$compiledContext['namespace']}\\{$compiledContext['classname']}" : $compiledContext['classname'];
return new $fullName(...$arguments);
}

View File

@ -2,17 +2,12 @@
namespace Picea;
use Picea\Event\Compiler\CompileTokenExtension;
use Picea\Event\Compiler\CompileTokenExtensionEvent;
use Picea\Event\Compiler\CompileTokenTagEvent;
use Picea\Exception\RegisterExtensionToken;
use Picea\Extension\FunctionExtension;
use Picea\Language\LanguageRegistration;
class Compiler
{
use EventTrait;
protected string $sourceCode = "";
protected array $syntaxObjectList = [];
@ -25,10 +20,11 @@ class Compiler
public array $extensionList = [];
public function __construct(
public LanguageRegistration $languageRegistration
)
public ? LanguageRegistration $languageRegistration;
public function __construct(?LanguageRegistration $languageRegistration = null)
{
$this->languageRegistration = $languageRegistration;
$this->languageRegistration->registerAll($this);
}
@ -54,18 +50,14 @@ class Compiler
list($token, $arguments) = array_pad(array_filter(explode(' ', $matches[2], 2), 'strlen'), 2, null);
$token = strtolower(trim($token));
$tokenName = $context->tokenName($token);
$tokenOptions = $context->tokenOptions($token);
$tokenName = explode('.', $token, 2)[0];
# @TODO Refractor this parts to allows registration to the tag's name
if ( $this->tagList[$tokenName] ?? false ) {
$this->eventExecute(CompileTokenTagEvent::class, $context, $arguments, $tokenName, $tokenOptions);
return $this->tagList[$tokenName]->parse($context, $arguments, $tokenName, $tokenOptions);
return $this->tagList[$tokenName]->parse($context, $arguments, $token);
}
elseif ( $this->extensionList[$tokenName] ?? false ) {
$this->eventExecute(CompileTokenExtensionEvent::class, $context, $arguments, $tokenName, $tokenOptions);
return $this->extensionList[$tokenName]->parse($context, $arguments, $tokenName, $tokenOptions);
return $this->extensionList[$tokenName]->parse($context, $arguments, $token);
}
else {
throw new \LogicException("Impossible to find token `$tokenName` declared in `{$matches[2]}`. Perhapse you forgot to add a custom token to Picea's engine ?");
@ -142,7 +134,7 @@ class Compiler
public function __toString() : string
{
return "???";
return "WHATAFAK";
}
public function getExtensionFromToken(string $name) : Extension\Extension
@ -157,7 +149,7 @@ class Compiler
return $this->extensionList[$tokenName];
}
public function exportFunctions() : void
{
static $caching = [];

View File

@ -20,16 +20,12 @@ abstract class Context {
public string $filePath = "";
public string $classPath = "";
public array $switchStack = [];
public array $iterationStack = [];
public array $iterateStack = [];
public array $useStack = [];
public array $sections = [];
public int $functions = 0;
public array $functionStack = [];
@ -57,7 +53,7 @@ abstract class Context {
public function renderUses() : string
{
return implode(PHP_EOL, array_map(fn($use) => "use $use;", $this->useStack ?? []));
return implode(",", $this->useStack ?? []);
}
public function renderFunctions() : string
@ -92,14 +88,10 @@ abstract class Context {
}
public function tokenName(string $token) : string
{
return explode('.', $token)[0];
}
public function tokenOptions(string $token, bool $export = false) : array|string
{
$options = array_slice(explode('.', strtolower($token)), 1);
$options = explode('.', strtolower($token));
array_shift($options);
return $export ? var_export($options, true) : $options;
}

View File

@ -4,8 +4,7 @@ namespace Picea\ControlStructure;
abstract class AbstractLoop 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) {
switch($token) {
case "while":
case "foreach":

View File

@ -6,24 +6,18 @@ class BlockToken implements ControlStructure {
public array $token = [ "arguments", "block", "endblock", "define", "slot", "endslot", "using" ];
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string
{
static $depth = 0;
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) {
static $slotDefinitions = [];
# dump($depth, $token, $arguments, $slotDefinitions);
switch($token) {
case "block":
$slotDefinitions[] = $this->slotDefinitions();
$depth++;
return "<?php \$___block = \Picea\ControlStructure\BlockToken::instanciateBlock($arguments); ?>";
case "endblock":
$depth--;
return "<?php echo \$___block->render(\$___class__template); unset(\$___block); ?>";
case "arguments":
$class = static::class;
@ -41,19 +35,19 @@ class BlockToken implements ControlStructure {
}
/*%EXCEPTION_LINE_BASE%*/?>
PHP;
case "define":
list($name, $definition) = array_pad(explode(',', $arguments, 2), 2, "");
( $slotDefinitions[$depth] ?? end($slotDefinitions) )->setDefinition(eval("return $name;"), $definition);
end($slotDefinitions)->setDefinition(eval("return $name;"), $definition);
return <<<PHP
<?php \$this->defineSlot($name, function($definition) {}); ?>
<?php \$this->defineSlot($name, function($definition) {}); ?>
PHP;
case "slot":
$def = ( $slotDefinitions[$depth] ?? end($slotDefinitions) );
$def = end($slotDefinitions);
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;
@ -62,7 +56,7 @@ class BlockToken implements ControlStructure {
$slotName = eval("return $name;");
$def->currentSlot = $slotName;
$def->setDefinitionVars($slotName, $definition);
$definition = $def->printDefinition($slotName);
if ($definition) {
@ -70,53 +64,53 @@ class BlockToken implements ControlStructure {
}
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;
}
else {
if ($definition) {
$definition .= ",";
}
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->setSlot($name, function($definition array \$___using = []) use (\$picea $loops) { extract(\$___using, \EXTR_SKIP); ?>
PHP;
}
case "endslot":
$def = ( $slotDefinitions[$depth] ?? end($slotDefinitions) );
$def =end($slotDefinitions);
if ($def->hasDefinitions() ) {
$definition = $def->getCurrentSlotDefinitionVars();
if ($definition) {
$definition .= ",";
}
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;
}
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
#return
}
public static function parseArguments(Callable $method, array $arguments) : array
{
try{
@ -124,10 +118,10 @@ class BlockToken implements ControlStructure {
}
catch(\TypeError $ex) {
throw $ex;
}
}
$parameters = [];
foreach((new \ReflectionFunction($method))->getParameters() as $key => $value) {
if ( isset($arguments[$key]) ) {
$parameters[ $value->getName() ] = $arguments[$key];
@ -142,119 +136,115 @@ class BlockToken implements ControlStructure {
$parameters[ $value->getName() ] = null;
}
}
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
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 using something like '{% define \"$name\", ...\$arguments %}' ?");
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)
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;
if ($this->using['this'] ?? false) {
$thisProxy = $this->using['this'];
unset($this->using['this']);
}
return $classTemplate->picea->inlineBlock($thisProxy ?? $this, $this->viewPath, ...$this->arguments);
return $classTemplate->picea->inlineBlock($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;
}

View File

@ -6,8 +6,7 @@ class BreakToken implements ControlStructure {
public string $token = "break";
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) {
return "<?php break; ?>";
}

View File

@ -6,8 +6,7 @@ class CaseToken implements ControlStructure {
public string $token = "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) {
$output = "";
if ( $context->switchStack ) {

View File

@ -6,7 +6,7 @@ class ContinueToken implements ControlStructure {
public string $token = "continue";
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) {
return "<?php continue; ?>";
}

View File

@ -6,7 +6,7 @@ class DefaultToken implements ControlStructure {
public string $token = "default";
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) {
$output = "";
if ( $context->switchStack ) {

View File

@ -6,7 +6,7 @@ class EchoToken implements ControlStructure {
public array $token = [ "echo", "echo.raw" ];
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) {
switch($token) {
case "echo":
return "<?php \\Picea\\ControlStructure\\EchoToken::echoSafe($arguments) ?>";

View File

@ -6,7 +6,7 @@ class ForToken implements ControlStructure {
public array $token = [ "for", "endfor" ];
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) {
switch($token) {
case "for":
$uid = "$".uniqid("for_");

View File

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

View File

@ -6,7 +6,7 @@ class IfToken implements ControlStructure {
public array $token = [ "if", "else", "elseif", "endif" ];
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) {
switch($token) {
case "if":
return "<?php if ($arguments): ?>";

View File

@ -6,7 +6,7 @@ class NamespaceToken implements ControlStructure {
public string $token = "namespace";
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) {
$context->namespace = $arguments;
return "";
}

View File

@ -6,7 +6,7 @@ class OrToken implements ControlStructure {
public string $token = "or";
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) {
if ( empty($context->iterationStack) ) {
throw new \LogicException("Token `or` was used outside of iterator. Make sure your `for`, `foreach`, `while`, `do/until` declaration are properly made.");
}

View File

@ -2,26 +2,19 @@
namespace Picea\ControlStructure;
enum PrintActionEnum : string {
case prepend = "prepend";
case default = "default";
case append = "append";
# case both = "both";
}
class SectionToken implements ControlStructure {
public array $token = [ "section", "endsection" ];
protected PrintActionEnum $action = PrintActionEnum::default;
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)
{
if (in_array('prepend', $options)) {
$this->action = PrintActionEnum::prepend;
$opt = $context->tokenOptions($token);
if (in_array('prepend', $opt)) {
$context->sectionAction = "prepend";
}
elseif (in_array('append', $options)) {
$this->action = PrintActionEnum::append;
elseif (in_array('append', $opt)) {
$context->sectionAction = "append";
}
switch($token) {
@ -33,13 +26,11 @@ class SectionToken implements ControlStructure {
throw new \RuntimeException("A section closing tag {% endsection %} was found without an opening {% section %} tag");
}
$this->action = PrintActionEnum::default;
return $this->printEndSection($context);
}
}
protected function printSection($context, ? string $arguments) : string
protected function printSection($context, ?string $arguments) : string
{
list($name, $options) = array_pad(explode(',', $arguments, 2), 2, null);
@ -56,7 +47,9 @@ class SectionToken implements ControlStructure {
'options' => $options,
];
$action = $options['action'] ?? $this->action->value;
$action = $options['action'] ?? $context->sectionAction ?? "default";
unset($context->sectionAction);
if (! in_array($action, ['prepend', 'append', 'default'])) {
throw new \RuntimeException("An unsupported action `$action` was given as an option of a {% section %} tag");
@ -64,13 +57,13 @@ class SectionToken implements ControlStructure {
$order = $options['order'] ?? "count(\$___class__template->sectionList[$name]['$action'])";
return "<?php \$___class__template->sectionList[$name] ??= [ 'prepend' => [], 'append' => [], 'default' => [] ];" .
"\$___class__template->sectionList[$name]['$action'][] = [ ".
"'order' => $order, " .
"'callback' => function() use (\$picea, \$___class__template, \$___global_variables, \$___variables, \$__event) {" .
"extract(\$___global_variables); extract(\$___variables, \EXTR_OVERWRITE);" .
"\$___class__template->sectionStack[] = $name;" .
"\$__event->eventExecute(\Picea\Event\Builder\ClassTemplateRenderSection::class, $name);?>";
return "<?php \$___class__template->sectionList[$name] ??= [ 'prepend' => [], 'append' => [], 'default' => [] ];".
"\$___class__template->sectionList[$name]['$action'][] = [
'order' => $order,
'callback' => function() use (\$picea, \$___class__template, \$___global_variables, \$___variables, \$__event) {".
"extract(\$___global_variables); extract(\$___variables, \EXTR_OVERWRITE);
\$___class__template->sectionStack[] = $name;
\$__event->eventExecute(\Picea\Event\Builder\ClassTemplateRenderSection::class, $name);?>";
}
protected function printEndSection($context) : string
@ -78,10 +71,13 @@ class SectionToken implements ControlStructure {
$section = array_pop($context->sections);
$build = $context->extendFrom ? "!empty(\$___class__template->sectionStack) && \$___class__template->renderSection({$section['name']}, false);" : "\$___class__template->renderSection({$section['name']}, false);";
return "<?php \$__event->eventExecute(\Picea\Event\Builder\ClassTemplateRenderSectionDone::class, {$section['name']});" .
"array_pop(\$___class__template->sectionStack); }];" .
$build .
"?>";
return <<<PHP
<?php
\$__event->eventExecute(\Picea\Event\Builder\ClassTemplateRenderSectionDone::class, {$section['name']});
array_pop(\$___class__template->sectionStack); }];
$build
?>
PHP;
}
}

View File

@ -6,7 +6,7 @@ class SwitchToken implements ControlStructure {
public array $token = [ "switch", "case", "endswitch" ];
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) {
switch($token) {
case "switch":
$context->switchStack[] = true;

View File

@ -1,25 +0,0 @@
<?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

@ -6,7 +6,7 @@ class UseToken implements ControlStructure {
public string $token = "use";
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) {
$context->useStack[] = $arguments;
return "";
}

View File

@ -6,7 +6,7 @@ class ViewToken implements ControlStructure {
public string $token = "view";
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) {
# The way this is ordered, if you provide a second arguments, being an array of variables, get_defined_vars() will not be pushed inside the view
return "<?php echo \$___class__template->picea->inlineHtml(\$this, $arguments, get_defined_vars()); ?>";
}

View File

@ -5,7 +5,7 @@ namespace Picea\ControlStructure;
class WhileToken extends AbstractLoop {
public array $token = [ "do", "while", "endwhile", ];
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) {
switch($token) {
case "do":
$context->iterationStack[] = [

View File

@ -1,10 +0,0 @@
<?php
namespace Picea\Event\Compiler;
use Picea\Compiler\Context;
interface CompileTokenExtensionEvent
{
public function execute(Context $context, ?string $arguments, string $token, array $options = []);
}

View File

@ -1,10 +0,0 @@
<?php
namespace Picea\Event\Compiler;
use Picea\Compiler\Context;
interface CompileTokenTagEvent
{
public function execute(Context &$context, ?string $arguments, string $token, array $options = []);
}

View File

@ -1,8 +0,0 @@
<?php
namespace Picea\Event\Extension;
interface BuildAssetUrl
{
public function execute(string $uri, array $parameters = [], bool $appendVersion = true) : mixed;
}

View File

@ -1,8 +0,0 @@
<?php
namespace Picea\Event\Extension;
interface UrlBuildAssetEvent
{
public function execute(string $uri, array $parameters = [], bool $appendVersion) : void;
}

View File

@ -1,8 +0,0 @@
<?php
namespace Picea\Event\Extension;
interface UrlBuildRouteEvent
{
public function execute(string $uri, array $parameters = [], bool $appendVersion) : void;
}

View File

@ -1,8 +0,0 @@
<?php
namespace Picea\Event\Extension;
interface UrlBuildUrlEvent
{
public function execute(string $uri, array $parameters = [], bool $appendVersion) : void;
}

View File

@ -1,8 +0,0 @@
<?php
namespace Picea\Event\Extension;
interface UrlRegisterRouteEvent
{
public function execute(string $name, array $route) : void;
}

View File

@ -4,26 +4,26 @@ namespace Picea;
trait EventTrait
{
protected $eventTraitMethod = "execute";
protected $_eventTraitMethod = "execute";
public array $eventTraitList = [];
public array $_eventList = [];
protected array $eventTraitReturnList = [];
protected array $_returnList = [];
public function eventRegister(object $event) : void
{
$this->eventTraitList[] = $event;
$this->_eventList[] = $event;
}
public function eventFromType(string $type) : array
{
return array_filter($this->eventTraitList, fn($ev) => $ev instanceof $type);
return array_filter($this->_eventList, fn($ev) => $ev instanceof $type);
}
public function eventExecute(string $type, ...$arguments) : void
{
foreach($this->eventFromType($type) as $event) {
$this->eventTraitReturnList[$event::class][] = call_user_func_array([ $event, $this->eventTraitMethod ], $arguments);
$this->_returnList[$event::class][] = call_user_func_array([ $event, $this->_eventTraitMethod ], $arguments);
}
}
}

View File

@ -1,7 +0,0 @@
<?php
namespace Picea\Exception;
use Picea\Picea;
class ParseExtensionException extends \Exception {}

View File

@ -10,20 +10,33 @@ use Picea\Picea;
*/
class RenderHtmlException extends \Exception
{
protected Picea $picea;
public function __construct(object $template, string $message, int $code, \Throwable $previous)
public function __construct(object $compiledObject, Picea $picea, string $message, int $code, \Throwable $previous)
{
parent::__construct($message, $code, $previous);
$this->defineError($previous, $template);
$this->picea = $picea;
$this->defineError($previous, $compiledObject);
}
protected function defineError(\Throwable $previous, object $template) : void
protected function defineError(\Throwable $previous, object $compiledObject) : void
{
/*$loadedTemplates = array_flip($this->picea->loadedTemplateFile);*/
$loadedTemplates = array_flip($this->picea->loadedTemplateFile);
$this->file = $template::getParam('view');
$this->line = $template::getSourceLineFromException($previous->getLine());
foreach($previous->getTrace() as $trace) {
if ( isset($trace['file'], $loadedTemplates[$trace['file']]) ) {
$class = $loadedTemplates[ $trace['file'] ];
$content = include($trace['file']);
$this->file = $content['view'];
$this->line = $class::getSourceLineFromException($trace['line']);
return;
}
}
}
protected function getTemplateFile(string $filePath) : ? array

View File

@ -1,33 +0,0 @@
<?php
namespace Picea\Extension;
use Picea\Compiler\Context;
class BatchExtension implements Extension, FunctionExtension {
public string $token = "batch";
public function exportFunctions(): array
{
return [
"batch" => [ $this, 'batch' ]
];
}
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string
{
return "<?php echo batch($arguments) ?>";
}
public function batch(iterable $array, int $size, mixed $default = null) : \Generator
{
$split = ceil( count($array) / $size );
for($i = 0; $i < $split; $i++) {
$slice = array_slice(is_array($array) ? $array : iterator_to_array($array, true), $i * $size, $size, true);
$pad = ( count($slice) !== $size ) && ( $default !== null );
yield $pad ? array_pad($slice, $size, $default) : $slice;
}
}
}

View File

@ -3,5 +3,5 @@
namespace Picea\Extension;
interface Extension {
public function parse(\Picea\Compiler\Context &$context, string $sourceCode, string $token, array $options = []) : string;
public function parse(\Picea\Compiler\Context &$context, string $sourceCode, string $token);
}

View File

@ -10,15 +10,17 @@ class JsonExtension implements Extension, FunctionExtension {
public int $flags = JSON_HEX_TAG | \JSON_HEX_QUOT | \JSON_THROW_ON_ERROR | \JSON_UNESCAPED_UNICODE;
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)
{
$opt = $context->tokenOptions($token);
$flag = $this->flags;
if ( in_array('pretty', $options) ) {
if ( in_array('pretty', $opt) ) {
$flag |= \JSON_PRETTY_PRINT;
}
if ( in_array('html', $options) ) {
if ( in_array('html', $opt) ) {
return "<?php echo htmlentities(json_encode($arguments, $flag), ENT_QUOTES, 'UTF-8') ?>";
}

View File

@ -11,9 +11,9 @@ class LanguageExtension implements Extension, FunctionExtension {
public string $currentLanguage = "";
protected LanguageHandlerInterface $languageHandler;
protected LanguageHandler $languageHandler;
public function __construct(LanguageHandlerInterface $handler) {
public function __construct(LanguageHandler $handler) {
$this->languageHandler = $handler;
}
@ -25,8 +25,10 @@ class LanguageExtension implements Extension, FunctionExtension {
];
}
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) : string
{
$opt = $context->tokenOptions($token);
switch(explode('.', $token)[0]) {
case "language":
$cls = $this::class;
@ -47,14 +49,14 @@ class LanguageExtension implements Extension, FunctionExtension {
PHP;
case "lang":
if ( in_array('raw', $options) ) {
if ( in_array('raw', $opt) ) {
return "<?php echo \$picea->compiler->getExtensionFromToken('$token')->absoluteLang($arguments) ?>";
}
return "<?php echo htmlspecialchars(\$picea->compiler->getExtensionFromToken('$token')->absoluteLang($arguments), \ENT_QUOTES, ini_get('default_charset'), true) ?>";
case "_":
if ( in_array('raw', $options) ) {
if ( in_array('raw', $opt) ) {
return "<?php echo \$picea->compiler->getExtensionFromToken('$token')->relativeLang($arguments) ?>";
}

View File

@ -2,7 +2,7 @@
namespace Picea\Extension;
interface LanguageHandlerInterface
interface LanguageHandler
{
public function languageFromKey(string $key, array $variables = []); #: array|string;
}

View File

@ -0,0 +1,42 @@
<?php
namespace Picea\Extension;
use Picea\Compiler\Context;
class MoneyExtension implements Extension, FunctionExtension {
public string $token = "money";
public string $title = "";
public string $locale;
public \NumberFormatter $formatter;
public function __construct() {
$this->locale = explode('.', \Locale::getDefault())[0];
$this->formatter = new \NumberFormatter($this->locale, \NumberFormatter::CURRENCY);
}
public function exportFunctions(): array
{
return [
"money" => [ $this, 'money' ]
];
}
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) {
return "<?php echo money($arguments) ?>";
}
public function money(float $money, ? string $currency = null) /* : string|false */
{
$this->formatter->setTextAttribute(\NumberFormatter::CURRENCY_CODE, 'CAD');
$this->formatter->setPattern( str_replace('¤#','¤ #', $this->formatter->getPattern() ) );
return $this->formatter->formatCurrency($money, $currency ?? $this->formatter->getTextAttribute(\NumberFormatter::CURRENCY_CODE));
}
}

View File

@ -1,66 +0,0 @@
<?php
namespace Picea\Extension;
use Picea\Compiler\Context;
class NumberExtension implements Extension, FunctionExtension {
public string $locale;
public \NumberFormatter $formatter;
public function __construct(
public string|array $token = [ "money", "format", "format_zero" ],
public string $title = "",
) {
$this->locale = explode('.', \Locale::getDefault())[0];
$this->formatter = new \NumberFormatter($this->locale, \NumberFormatter::CURRENCY);
}
public function exportFunctions(): array
{
return [
"money" => [ $this, 'money' ],
"format" => [ $this, 'format' ],
"format_zero" => [ $this, 'formatZero' ],
];
}
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string
{
switch($token) {
case "format":
return "<?php echo format($arguments) ?>";
case "format_zero":
return "<?php echo format_zero($arguments) ?>";
case "money":
return "<?php echo money($arguments) ?>";
}
return "";
}
public function format(int|float $num, int $decimals = 0, null|string $decimalSeparator = ".", null|string $thousandsSeparator = ",") : string
{
return number_format($num, $decimals, $decimalSeparator, $thousandsSeparator);
}
public function formatZero(int $num, int $length) : string
{
return sprintf("%0{$length}d", $num);
}
public function money(float $money, ? string $currency = null) /* : string|false */
{
$this->formatter->setTextAttribute(\NumberFormatter::CURRENCY_CODE, 'CAD');
$this->formatter->setPattern( str_replace('¤#','¤ #', $this->formatter->getPattern() ) );
return $this->formatter->formatCurrency($money, $currency ?? $this->formatter->getTextAttribute(\NumberFormatter::CURRENCY_CODE));
}
}

View File

@ -6,8 +6,7 @@ class PhpExtension implements Extension {
public array $token = [ "php" ];
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) {
return "<?php $arguments ?>";
}
}

View File

@ -6,9 +6,9 @@ use Picea\Compiler\Context;
class PrintExtension implements Extension {
public array $token = [ "echo", "print", "printf", "no_html" ];
public array $token = [ "echo", "print", "printf" ];
public int $flag = \ENT_QUOTES | \ENT_DISALLOWED | \ENT_SUBSTITUTE;
public int $flag = \ENT_QUOTES;
public string $encoding;
@ -18,27 +18,24 @@ class PrintExtension implements Extension {
$this->encoding = ini_get("default_charset");
}
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) : string
{
$opt = $context->tokenOptions($token);
switch($token) {
case 'echo':
case 'print':
if (in_array('raw', $options)) {
return "<?php echo $arguments ?>";
}
return "<?php echo htmlspecialchars((string) $arguments, {$this->flag}, '{$this->encoding}', " . ($this->doubleEncode ? "true" : "false") . ") ?>";
case 'printf':
if (in_array('raw', $options)) {
return "<?php printf((string) $arguments) ?>";
}
case 'echo.raw':
case "print.raw":
return "<?php echo $arguments ?>";
case 'printf':
return "<?php echo htmlspecialchars(sprintf((string) $arguments), {$this->flag}, '{$this->encoding}', " . ($this->doubleEncode ? "true" : "false") . ") ?>";
case 'no_html':
return "<?php echo nl2br(strip_tags((string) $arguments)) ?>";
case 'printf.raw':
return "<?php printf((string) $arguments) ?>";
}
}

View File

@ -15,12 +15,14 @@ class TitleExtension implements Extension, FunctionExtension {
];
}
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string
{
return "<?php ".
"if ( null !== \$title = title($arguments) ) {".
"echo \$title;".
"} ?>";
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) {
return <<<PHP
<?php
if ( null !== \$title = title($arguments) ) {
echo \$title;
}
?>
PHP;
}
public function handleTitle(? string $set = null, ...$arguments) : ? string

View File

@ -2,11 +2,9 @@
namespace Picea\Extension;
use Picea\EventTrait;
use Picea\Event\Extension\{ UrlBuildAssetEvent, UrlBuildUrlEvent, UrlBuildRouteEvent, UrlRegisterRouteEvent };
use Picea\Compiler\Context;
class UrlExtension implements Extension, FunctionExtension {
use EventTrait;
public const URLIZE_PATTERN_URL = <<<PATTERN
~(?<!href=['"])https?://[\w/._\-&?]*(?!</a>)(?=[^\w/._\-&])~s
@ -18,30 +16,23 @@ PATTERN;
protected string $urlBase;
protected array $appUrl;
protected string $assetToken;
protected array $routes;
protected array $routesTarget;
protected bool $forceSSL = false;
public array $tokens = [ "url" , "route", "asset", "slug" ];
##[\Deprecated]
protected bool $enforceExistingArguments = true;
public function __construct(string $urlBase = "", string $assetToken = "", array $appUrl = [], bool $forceSSL = false) {
public function __construct(string $urlBase = "", string $assetToken = "") {
$this->urlBase = trim($urlBase, "/");
$this->assetToken = $assetToken;
$this->appUrl = $appUrl;
$this->forceSSL = $forceSSL;
}
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) : ?string
{
$opt = $context->tokenOptions($token);
switch($token) {
case "asset":
return "<?php echo \$picea->compiler->getExtensionFromToken('$token')->buildAssetUrl($arguments) ?>";
@ -50,10 +41,10 @@ PATTERN;
return "<?php echo \$picea->compiler->getExtensionFromToken('$token')->buildRouteUrl($arguments) ?>";
case "url":
if ( in_array('parameters', $options) ) {
if ( in_array('parameters', $opt) ) {
return "<?php echo \$picea->compiler->getExtensionFromToken('$token')->setUrlParameters($arguments) ?>";
}
elseif ( in_array('current', $options) ) {
elseif ( in_array('current', $opt) ) {
return "<?php echo \$picea->compiler->getExtensionFromToken('$token')->currentUrl($arguments) ?>";
}
@ -63,7 +54,7 @@ PATTERN;
return "<?php echo \$picea->compiler->getExtensionFromToken('$token')->slug($arguments) ?>";
}
throw new \Exception("Unknown token given $token in UrlExtension.");
return null;
}
public function exportFunctions(): array
@ -106,22 +97,18 @@ PATTERN;
public function buildUrl(string $uri = "", array $parameters = [], bool $appendVersion = false) : string
{
$this->eventExecute(UrlBuildUrlEvent::class, $uri, $parameters, $appendVersion);
return $this->setUrlParameters($this->url() . "/" . ltrim($uri, "/"), $appendVersion ? array_replace([ 'v' => $this->assetToken ], $parameters) : $parameters);
}
public function buildAssetUrl(string $uri, array $parameters = [], bool $appendVersion = true) : string
{
$this->eventExecute(UrlBuildAssetEvent::class, $uri, $parameters, $appendVersion);
return $this->buildUrl($uri, $parameters, $appendVersion);
}
public function buildRouteUrl(string $name, array $parameters = [], bool $appendVersion = false) : string
{
if ( false !== $route = $this->findRoute($name) ) {
return $this->buildUrl($this->prepareRoute($route, $parameters), $parameters, $appendVersion);
if ( false !== ( $route = $this->routes[$name] ?? false ) ) {
return $this->buildUrl($this->prepareRoute($route['route'], $parameters), $parameters, $appendVersion);
}
$routeList = json_encode($this->routes, \JSON_PRETTY_PRINT);
@ -149,28 +136,12 @@ PATTERN;
public function registerRoute(string $name, string $route, string $class, string $method, array $routeMethods) : void
{
$this->routes[] = $array = [
'name' => $name,
$this->routes[$name] = [
'route' => $route,
'routeMethods' => array_map('strtoupper', $routeMethods),
'routeMethods' => $routeMethods,
'class' => $class,
'classMethod' => $method,
];
$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;
}
/**
@ -197,7 +168,7 @@ PATTERN;
protected function scheme() : string
{
return ( $this->forceSSL || $this->isHttps() ? "https" : "http" ) . "://";
return ( $this->isHttps() ? "https" : "http" ) . "://";
}
protected function base() : string
@ -216,25 +187,7 @@ PATTERN;
protected function domain() : string
{
if ( ! empty($_SERVER['HTTP_X_FORWARDED_PROTO']) || ! empty($_SERVER['HTTP_X_FORWARDED_SSL']) ) {
$port = "";
}
else {
$port = $this->isDefaultPort() ? "" : ":" . $_SERVER['SERVER_PORT'];
}
return strtolower($this->appUrl($port) ?: $_SERVER['HTTP_HOST']);
}
protected function appUrl(string $port) : string
{
$domain = strtolower($_SERVER['HTTP_HOST']);
if (in_array($domain, $this->appUrl)) {
return $domain . $port;
}
return $this->appUrl[0] . $port;
return strtolower($_SERVER['HTTP_HOST']);
}
protected function isDefaultPort() : bool
@ -259,42 +212,32 @@ PATTERN;
|| ( "off" !== ( strtolower($_SERVER['HTTPS'] ?? $_SERVER['HTTP_X_FORWARDED_SSL'] ?? $_SERVER['X-Forwarded-Ssl'] ?? "off")) );
}
protected function prepareRoute(array $routeParam, array &$arguments)
protected function prepareRoute(string $route, array &$arguments)
{
$route = $routeParam['route'];
if ( preg_match_all('~{(.*?)}~si', $route, $matches, PREG_SET_ORDER) ) {
$search = [];
foreach($matches as $item) {
$default = null;
$variable = $item[1];
# Handles default
if (strpos($variable, "=") !== false) {
if (strpos($item[1], "=") !== false) {
list($variable, $default) = explode('=', $item[1]);
}
# Handles type
if (strpos($variable, ":") !== false) {
elseif (strpos($item[1], ":") !== false) {
list($variable, ) = explode(':', $item[1]);
}
else {
$variable = $item[1];
}
if ( array_key_exists($variable, $arguments) ) {
$value = $arguments[ $variable ];
unset($arguments[ $variable ]);
}
else {
if ($default ?? false) {
$value = $default;
}
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']));
}
$value = $default ?? "";
}
$search[$item[0]] = $value ?? "";
$search[ $item[0] ] = $value;
}
$route = str_replace(array_keys($search), array_values($search), $route);

View File

@ -6,11 +6,18 @@ use Picea\Compiler;
class DefaultRegistrations implements LanguageRegistration
{
public function __construct(
protected array $extensions,
protected array $syntaxes,
protected array $controlStructures,
) { }
protected array $extensions;
protected array $syntaxes;
protected array $controlStructures;
public function __construct(array $extensions = [], array $syntaxes = [], array $controlStructure = [])
{
$this->extensions = $extensions;
$this->syntaxes = $syntaxes;
$this->controlStructures = $controlStructure;
}
public function registerAll(Compiler $compiler) : void
{
@ -32,7 +39,6 @@ class DefaultRegistrations implements LanguageRegistration
public function registerControlStructure(Compiler $compiler) : void
{
$compiler->registerControlStructure(new \Picea\ControlStructure\TryCatchToken());
$compiler->registerControlStructure(new \Picea\ControlStructure\NamespaceToken());
$compiler->registerControlStructure(new \Picea\ControlStructure\UseToken());
$compiler->registerControlStructure(new \Picea\ControlStructure\IfToken());
@ -61,7 +67,6 @@ class DefaultRegistrations implements LanguageRegistration
$compiler->registerExtension(new \Picea\Extension\PhpExtension());
$compiler->registerExtension(new \Picea\Extension\PrintExtension());
$compiler->registerExtension(new \Picea\Extension\JsonExtension());
$compiler->registerExtension(new \Picea\Extension\BatchExtension());
foreach($this->extensions ?? [] as $extension) {
$compiler->registerExtension($extension);

View File

@ -24,7 +24,7 @@ class Request implements Extension, FunctionExtension {
$this->request = $request;
}
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) : string { }
public function exportFunctions(): array
{

View File

@ -43,7 +43,7 @@ class Picea implements LanguageRegistration
){
$this->cache = $cache ?? new Caching\Memory("");
$this->context = $context ?? new Compiler\BaseContext();
$this->languageRegistration = $languageRegistration ?? new Language\DefaultRegistrations([], [], []);
$this->languageRegistration = $languageRegistration ?? new Language\DefaultRegistrations();
$this->builderTemplatePath = $builderTemplatePath ?? dirname(__FILE__) . static::DEFAULT_BUILDER_TEMPLATE;
$this->compiler = $compiler ?? $this->instanciateCompiler();
$this->fileFetcher = $fileFetcher ?? $this->instanciateFileFetcher();
@ -71,15 +71,15 @@ class Picea implements LanguageRegistration
return call_user_func($object);
}
catch(\Throwable $ex) {
#if (! $ex instanceof Exception\RenderHtmlException ) {
# throw new Exception\RenderHtmlException($object, $this, "An error occurred trying to render HTML view `$viewPath` : " . $ex->getMessage(), 911, $ex);
#}
#else {
throw $ex;
#}
if (! $ex instanceof Exception\RenderHtmlException ) {
throw new Exception\RenderHtmlException($object, $this, "An error occurred trying to render HTML view `$viewPath` : " . $ex->getMessage(), 911, $ex);
}
else {
throw $ex;
}
}
return null;
exit();
}
/**
@ -180,7 +180,6 @@ class Picea implements LanguageRegistration
{
if ( $this->debug || ! $this->cache->compiled( $context->cacheFilename() ) ) {
$context = $this->compileContext($context);
$this->cache->save($context);
}