From acd9f8c72e434a3a6d5b2076ca7829451c657d5b Mon Sep 17 00:00:00 2001 From: Dave Mc Nicoll Date: Thu, 2 Feb 2023 16:46:46 +0000 Subject: [PATCH] - First task of v2 done, tokens can now have unlimited options attached to it --- src/Compiler.php | 30 +++++++++++++++++------- src/Compiler/Context.php | 8 +++++++ src/ControlStructure/SectionToken.php | 18 +++++++++----- src/Exception/RegisterExtensionToken.php | 7 ++++++ src/Extension/JsonExtension.php | 15 ++++++------ src/Extension/LanguageExtension.php | 22 ++++++++++------- src/Extension/PrintExtension.php | 4 +++- src/Extension/UrlExtension.php | 19 ++++++++------- 8 files changed, 82 insertions(+), 41 deletions(-) create mode 100644 src/Exception/RegisterExtensionToken.php diff --git a/src/Compiler.php b/src/Compiler.php index edb8fff..d5e310a 100644 --- a/src/Compiler.php +++ b/src/Compiler.php @@ -2,6 +2,7 @@ namespace Picea; +use Picea\Exception\RegisterExtensionToken; use Picea\Extension\FunctionExtension; use Picea\Language\LanguageRegistration; @@ -47,18 +48,19 @@ class Compiler $matches[2] = trim($matches[2]); list($token, $arguments) = array_pad(array_filter(explode(' ', $matches[2], 2), 'strlen'), 2, null); - + $token = strtolower(trim($token)); + $tokenName = explode('.', $token, 2)[0]; # @TODO Refractor this parts to allows registration to the tag's name - if ( $this->tagList[$token] ?? false ) { - return $this->tagList[$token]->parse($context, $arguments, $token); + if ( $this->tagList[$tokenName] ?? false ) { + return $this->tagList[$tokenName]->parse($context, $arguments, $token); } - elseif ( $this->extensionList[$token] ?? false ) { - return $this->extensionList[$token]->parse($context, $arguments, $token); + elseif ( $this->extensionList[$tokenName] ?? false ) { + return $this->extensionList[$tokenName]->parse($context, $arguments, $token); } else { - throw new \LogicException("Impossible to find token `$token` declared in `{$matches[2]}`. Perhapse you forgot to add a custom token to Picea's engine ?"); + throw new \LogicException("Impossible to find token `$tokenName` declared in `{$matches[2]}`. Perhapse you forgot to add a custom token to Picea's engine ?"); } }, $this->sourceCode); @@ -87,6 +89,10 @@ class Compiler public function registerControlStructure(ControlStructure\ControlStructure $controlStructureObject) : self { foreach($controlStructureObject->tokens ?? (array) ( $controlStructureObject->token ?? [] ) as $token) { + if ( strpos($token, '.') !== false ) { + throw new RegisterExtensionToken(sprintf("Could not register token '%s' from control structure '%s' in this version. Options are now delt by extensions themselve instead.", $token, $controlStructureObject::class)); + } + $this->tagList[strtolower($token)] = $controlStructureObject; } @@ -98,6 +104,10 @@ class Compiler $tokens = $extension->tokens ?? (array) ( $extension->token ?? [] ); foreach($tokens as $token) { + if ( strpos($token, '.') !== false ) { + throw new RegisterExtensionToken(sprintf("Could not register token '%s' from extension '%s' in this version. Options are now delt by extensions themselve instead.", $token, $extension::class)); + } + $this->extensionList[strtolower($token)] = $extension; } @@ -129,13 +139,15 @@ class Compiler public function getExtensionFromToken(string $name) : Extension\Extension { - if ( ! isset($this->extensionList[$name]) ) { + $tokenName = explode('.', $name)[0]; + + if ( ! isset($this->extensionList[$tokenName]) ) { throw new \InvalidArgumentException(<<extensionList[$name]; + return $this->extensionList[$tokenName]; } public function exportFunctions() : void diff --git a/src/Compiler/Context.php b/src/Compiler/Context.php index 1fbe3e3..9955055 100644 --- a/src/Compiler/Context.php +++ b/src/Compiler/Context.php @@ -88,6 +88,14 @@ abstract class Context { } + public function tokenOptions(string $token, bool $export = false) : array|string + { + $options = explode('.', strtolower($token)); + array_shift($options); + + return $export ? var_export($options, true) : $options; + } + public function pushFunction($name, Callable $callable) : void { $this->functionStack[$name] = $callable; diff --git a/src/ControlStructure/SectionToken.php b/src/ControlStructure/SectionToken.php index b930345..e7c13d5 100644 --- a/src/ControlStructure/SectionToken.php +++ b/src/ControlStructure/SectionToken.php @@ -4,14 +4,20 @@ namespace Picea\ControlStructure; class SectionToken implements ControlStructure { - public array $token = [ "section", "section.prepend", "section.append", "endsection" ]; + public array $token = [ "section", "endsection" ]; + + public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) + { + $opt = $context->tokenOptions($token); + + if (in_array('prepend', $opt)) { + $context->sectionAction = "prepend"; + } + elseif (in_array('append', $opt)) { + $context->sectionAction = "append"; + } - public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) { switch($token) { - case "section.prepend": - $context->sectionAction = "prepend"; - case "section.append": - $context->sectionAction ??= "append"; case "section": return $this->printSection($context, $arguments); diff --git a/src/Exception/RegisterExtensionToken.php b/src/Exception/RegisterExtensionToken.php new file mode 100644 index 0000000..0df8960 --- /dev/null +++ b/src/Exception/RegisterExtensionToken.php @@ -0,0 +1,7 @@ +tokenOptions($token); + $flag = $this->flags; - switch ($token) { - case "json.pretty": - $flag |= \JSON_PRETTY_PRINT; - break; + if ( in_array('pretty', $opt) ) { + $flag |= \JSON_PRETTY_PRINT; + } - case "json.html": - return ""; + if ( in_array('html', $opt) ) { + return ""; } $cls = static::class; diff --git a/src/Extension/LanguageExtension.php b/src/Extension/LanguageExtension.php index fbbb7c4..48100f3 100644 --- a/src/Extension/LanguageExtension.php +++ b/src/Extension/LanguageExtension.php @@ -7,7 +7,7 @@ use Picea\Event\Builder\ClassTemplateRenderSectionDone; class LanguageExtension implements Extension, FunctionExtension { - public array $tokens = [ "lang", "lang.raw", "_", "_.raw", "language.set" ]; + public array $tokens = [ "lang", "_", "language" ]; public string $currentLanguage = ""; @@ -27,8 +27,10 @@ class LanguageExtension implements Extension, FunctionExtension { public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) : string { - switch($token) { - case "language.set": + $opt = $context->tokenOptions($token); + + switch(explode('.', $token)[0]) { + case "language": $cls = $this::class; return <<compiler->getExtensionFromToken('$token')->absoluteLang($arguments) ?>"; + } + return "compiler->getExtensionFromToken('$token')->absoluteLang($arguments), \ENT_QUOTES, ini_get('default_charset'), true) ?>"; - case "lang.raw": - return "compiler->getExtensionFromToken('$token')->absoluteLang($arguments) ?>"; - case "_": - return "compiler->getExtensionFromToken('$token')->relativeLang($arguments), \ENT_QUOTES, ini_get('default_charset'), true) ?>"; + if ( in_array('raw', $opt) ) { + return "compiler->getExtensionFromToken('$token')->relativeLang($arguments) ?>"; + } - case "_.raw": - return "compiler->getExtensionFromToken('$token')->relativeLang($arguments) ?>"; + return "compiler->getExtensionFromToken('$token')->relativeLang($arguments), \ENT_QUOTES, ini_get('default_charset'), true) ?>"; } return ""; diff --git a/src/Extension/PrintExtension.php b/src/Extension/PrintExtension.php index a9baf2e..a85f4fc 100644 --- a/src/Extension/PrintExtension.php +++ b/src/Extension/PrintExtension.php @@ -6,7 +6,7 @@ use Picea\Compiler\Context; class PrintExtension implements Extension { - public array $token = [ "echo", "echo.raw", "print", "print.raw", "printf", "printf.raw" ]; + public array $token = [ "echo", "print", "printf" ]; public int $flag = \ENT_QUOTES; @@ -20,6 +20,8 @@ class PrintExtension implements Extension { public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) : string { + $opt = $context->tokenOptions($token); + switch($token) { case 'echo': case 'print': diff --git a/src/Extension/UrlExtension.php b/src/Extension/UrlExtension.php index d9cbc7d..d38d960 100644 --- a/src/Extension/UrlExtension.php +++ b/src/Extension/UrlExtension.php @@ -22,7 +22,7 @@ PATTERN; protected array $routesTarget; - public array $tokens = [ "url" , "route", "route.cacheless", "asset", "url.current", "url.parameters", "slug" ]; + public array $tokens = [ "url" , "route", "asset", "slug" ]; public function __construct(string $urlBase = "", string $assetToken = "") { $this->urlBase = trim($urlBase, "/"); @@ -31,6 +31,8 @@ PATTERN; public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) : ?string { + $opt = $context->tokenOptions($token); + switch($token) { case "asset": return "compiler->getExtensionFromToken('$token')->buildAssetUrl($arguments) ?>"; @@ -39,18 +41,17 @@ PATTERN; return "compiler->getExtensionFromToken('$token')->buildRouteUrl($arguments) ?>"; case "url": + if ( in_array('parameters', $opt) ) { + return "compiler->getExtensionFromToken('$token')->setUrlParameters($arguments) ?>"; + } + elseif ( in_array('current', $opt) ) { + return "compiler->getExtensionFromToken('$token')->currentUrl($arguments) ?>"; + } + return "compiler->getExtensionFromToken('$token')->buildUrl($arguments) ?>"; - case "url.current": - return "compiler->getExtensionFromToken('$token')->currentUrl($arguments) ?>"; - - case "url.parameters": - return "compiler->getExtensionFromToken('$token')->setUrlParameters($arguments) ?>"; - case "slug": return "compiler->getExtensionFromToken('$token')->slug($arguments) ?>"; - - #return \Transliterator::createFromRules(':: Any-Latin;:: NFD;:: [:Nonspacing Mark:] Remove;:: NFC;:: [:Punctuation:] Remove;:: Lower();[:Separator:] > \'-\'')->transliterate( $arguments ); } return null;