- First task of v2 done, tokens can now have unlimited options attached to it
This commit is contained in:
		
							parent
							
								
									afeb8789b5
								
							
						
					
					
						commit
						acd9f8c72e
					
				| @ -2,6 +2,7 @@ | |||||||
| 
 | 
 | ||||||
| namespace Picea; | namespace Picea; | ||||||
| 
 | 
 | ||||||
|  | use Picea\Exception\RegisterExtensionToken; | ||||||
| use Picea\Extension\FunctionExtension; | use Picea\Extension\FunctionExtension; | ||||||
| use Picea\Language\LanguageRegistration; | use Picea\Language\LanguageRegistration; | ||||||
| 
 | 
 | ||||||
| @ -47,18 +48,19 @@ class Compiler | |||||||
|             $matches[2] = trim($matches[2]); |             $matches[2] = trim($matches[2]); | ||||||
| 
 | 
 | ||||||
|             list($token, $arguments) = array_pad(array_filter(explode(' ', $matches[2], 2), 'strlen'), 2, null); |             list($token, $arguments) = array_pad(array_filter(explode(' ', $matches[2], 2), 'strlen'), 2, null); | ||||||
|              | 
 | ||||||
|             $token = strtolower(trim($token)); |             $token = strtolower(trim($token)); | ||||||
|  |             $tokenName = explode('.', $token, 2)[0]; | ||||||
|              |              | ||||||
|             # @TODO Refractor this parts to allows registration to the tag's name
 |             # @TODO Refractor this parts to allows registration to the tag's name
 | ||||||
|             if ( $this->tagList[$token] ?? false ) { |             if ( $this->tagList[$tokenName] ?? false ) { | ||||||
|                 return $this->tagList[$token]->parse($context, $arguments, $token); |                 return $this->tagList[$tokenName]->parse($context, $arguments, $token); | ||||||
|             } |             } | ||||||
|             elseif ( $this->extensionList[$token] ?? false ) { |             elseif ( $this->extensionList[$tokenName] ?? false ) { | ||||||
|                 return $this->extensionList[$token]->parse($context, $arguments, $token); |                 return $this->extensionList[$tokenName]->parse($context, $arguments, $token); | ||||||
|             } |             } | ||||||
|             else { |             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); |         }, $this->sourceCode); | ||||||
| @ -87,6 +89,10 @@ class Compiler | |||||||
|     public function registerControlStructure(ControlStructure\ControlStructure $controlStructureObject) : self |     public function registerControlStructure(ControlStructure\ControlStructure $controlStructureObject) : self | ||||||
|     { |     { | ||||||
|         foreach($controlStructureObject->tokens ?? (array) ( $controlStructureObject->token ?? [] ) as $token) { |         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; |             $this->tagList[strtolower($token)] = $controlStructureObject; | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
| @ -98,6 +104,10 @@ class Compiler | |||||||
|         $tokens = $extension->tokens ?? (array) ( $extension->token ?? [] ); |         $tokens = $extension->tokens ?? (array) ( $extension->token ?? [] ); | ||||||
| 
 | 
 | ||||||
|         foreach($tokens as $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; |             $this->extensionList[strtolower($token)] = $extension; | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
| @ -129,13 +139,15 @@ class Compiler | |||||||
| 
 | 
 | ||||||
|     public function getExtensionFromToken(string $name) : Extension\Extension |     public function getExtensionFromToken(string $name) : Extension\Extension | ||||||
|     { |     { | ||||||
|         if ( ! isset($this->extensionList[$name]) ) { |         $tokenName = explode('.', $name)[0]; | ||||||
|  | 
 | ||||||
|  |         if ( ! isset($this->extensionList[$tokenName]) ) { | ||||||
|             throw new \InvalidArgumentException(<<<MSG |             throw new \InvalidArgumentException(<<<MSG | ||||||
|                 Requested extension from token `$name` could not be found from loaded Picea compiler's extension. |                 Requested extension from token `$tokenName` could not be found from loaded Picea compiler's extension. | ||||||
|             MSG); |             MSG); | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         return $this->extensionList[$name]; |         return $this->extensionList[$tokenName]; | ||||||
|     } |     } | ||||||
|      |      | ||||||
|     public function exportFunctions() : void |     public function exportFunctions() : void | ||||||
|  | |||||||
| @ -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 |     public function pushFunction($name, Callable $callable) : void | ||||||
|     { |     { | ||||||
|         $this->functionStack[$name] = $callable; |         $this->functionStack[$name] = $callable; | ||||||
|  | |||||||
| @ -4,14 +4,20 @@ namespace Picea\ControlStructure; | |||||||
| 
 | 
 | ||||||
| class SectionToken implements 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) { |         switch($token) { | ||||||
|             case "section.prepend": |  | ||||||
|                 $context->sectionAction = "prepend"; |  | ||||||
|             case "section.append": |  | ||||||
|                 $context->sectionAction ??= "append"; |  | ||||||
|             case "section": |             case "section": | ||||||
|                 return $this->printSection($context, $arguments); |                 return $this->printSection($context, $arguments); | ||||||
| 
 | 
 | ||||||
|  | |||||||
							
								
								
									
										7
									
								
								src/Exception/RegisterExtensionToken.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								src/Exception/RegisterExtensionToken.php
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,7 @@ | |||||||
|  | <?php | ||||||
|  | 
 | ||||||
|  | namespace Picea\Exception; | ||||||
|  | 
 | ||||||
|  | use Picea\Picea; | ||||||
|  | 
 | ||||||
|  | class RegisterExtensionToken extends \Exception {} | ||||||
| @ -6,21 +6,22 @@ use Picea\Compiler\Context; | |||||||
| 
 | 
 | ||||||
| class JsonExtension implements Extension, FunctionExtension { | class JsonExtension implements Extension, FunctionExtension { | ||||||
| 
 | 
 | ||||||
|     public array $token = [ "json", "json.pretty", "json.html" ]; |     public array $token = [ "json" ]; | ||||||
|      |      | ||||||
|     public int $flags =  JSON_HEX_TAG | \JSON_HEX_QUOT | \JSON_THROW_ON_ERROR | \JSON_UNESCAPED_UNICODE; |     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) |     public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) | ||||||
|     { |     { | ||||||
|  |         $opt = $context->tokenOptions($token); | ||||||
|  | 
 | ||||||
|         $flag = $this->flags; |         $flag = $this->flags; | ||||||
| 
 | 
 | ||||||
|         switch ($token) { |         if ( in_array('pretty', $opt) ) { | ||||||
|             case "json.pretty": |             $flag |= \JSON_PRETTY_PRINT; | ||||||
|                 $flag |= \JSON_PRETTY_PRINT; |         } | ||||||
|                 break; |  | ||||||
| 
 | 
 | ||||||
|             case "json.html": |         if ( in_array('html', $opt) ) { | ||||||
|                 return "<?php echo htmlentities(json_encode($arguments, $flag), ENT_QUOTES, 'UTF-8') ?>"; |             return "<?php echo htmlentities(json_encode($arguments, $flag), ENT_QUOTES, 'UTF-8') ?>"; | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         $cls = static::class; |         $cls = static::class; | ||||||
|  | |||||||
| @ -7,7 +7,7 @@ use Picea\Event\Builder\ClassTemplateRenderSectionDone; | |||||||
| 
 | 
 | ||||||
| class LanguageExtension implements Extension, FunctionExtension { | class LanguageExtension implements Extension, FunctionExtension { | ||||||
| 
 | 
 | ||||||
|     public array $tokens = [ "lang", "lang.raw", "_", "_.raw", "language.set" ]; |     public array $tokens = [ "lang",  "_", "language" ]; | ||||||
| 
 | 
 | ||||||
|     public string $currentLanguage = ""; |     public string $currentLanguage = ""; | ||||||
| 
 | 
 | ||||||
| @ -27,8 +27,10 @@ class LanguageExtension implements Extension, FunctionExtension { | |||||||
| 
 | 
 | ||||||
|     public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) : string |     public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) : string | ||||||
|     { |     { | ||||||
|         switch($token) { |         $opt = $context->tokenOptions($token); | ||||||
|             case "language.set": | 
 | ||||||
|  |         switch(explode('.', $token)[0]) { | ||||||
|  |             case "language": | ||||||
|                 $cls = $this::class; |                 $cls = $this::class; | ||||||
| 
 | 
 | ||||||
|                 return <<<PHP |                 return <<<PHP | ||||||
| @ -47,16 +49,18 @@ class LanguageExtension implements Extension, FunctionExtension { | |||||||
|                 PHP; |                 PHP; | ||||||
| 
 | 
 | ||||||
|             case "lang": |             case "lang": | ||||||
|  |                 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) ?>"; |                 return "<?php echo htmlspecialchars(\$picea->compiler->getExtensionFromToken('$token')->absoluteLang($arguments), \ENT_QUOTES, ini_get('default_charset'), true) ?>"; | ||||||
| 
 | 
 | ||||||
|             case "lang.raw": |  | ||||||
|                 return "<?php echo \$picea->compiler->getExtensionFromToken('$token')->absoluteLang($arguments) ?>"; |  | ||||||
| 
 |  | ||||||
|             case "_": |             case "_": | ||||||
|                 return "<?php echo htmlspecialchars(\$picea->compiler->getExtensionFromToken('$token')->relativeLang($arguments), \ENT_QUOTES, ini_get('default_charset'), true) ?>"; |                 if ( in_array('raw', $opt) ) { | ||||||
|  |                     return "<?php echo \$picea->compiler->getExtensionFromToken('$token')->relativeLang($arguments) ?>"; | ||||||
|  |                 } | ||||||
| 
 | 
 | ||||||
|             case "_.raw": |                 return "<?php echo htmlspecialchars(\$picea->compiler->getExtensionFromToken('$token')->relativeLang($arguments), \ENT_QUOTES, ini_get('default_charset'), true) ?>"; | ||||||
|                 return "<?php echo \$picea->compiler->getExtensionFromToken('$token')->relativeLang($arguments) ?>"; |  | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         return ""; |         return ""; | ||||||
|  | |||||||
| @ -6,7 +6,7 @@ use Picea\Compiler\Context; | |||||||
| 
 | 
 | ||||||
| class PrintExtension implements Extension { | 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; |     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 |     public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) : string | ||||||
|     { |     { | ||||||
|  |         $opt = $context->tokenOptions($token); | ||||||
|  | 
 | ||||||
|         switch($token) { |         switch($token) { | ||||||
|             case 'echo': |             case 'echo': | ||||||
|             case 'print': |             case 'print': | ||||||
|  | |||||||
| @ -22,7 +22,7 @@ PATTERN; | |||||||
| 
 | 
 | ||||||
|     protected array $routesTarget; |     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 = "") { |     public function __construct(string $urlBase = "", string $assetToken = "") { | ||||||
|         $this->urlBase = trim($urlBase, "/"); |         $this->urlBase = trim($urlBase, "/"); | ||||||
| @ -31,6 +31,8 @@ PATTERN; | |||||||
| 
 | 
 | ||||||
|     public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) : ?string |     public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) : ?string | ||||||
|     { |     { | ||||||
|  |         $opt = $context->tokenOptions($token); | ||||||
|  | 
 | ||||||
|         switch($token) { |         switch($token) { | ||||||
|             case "asset": |             case "asset": | ||||||
|                 return "<?php echo \$picea->compiler->getExtensionFromToken('$token')->buildAssetUrl($arguments) ?>"; |                 return "<?php echo \$picea->compiler->getExtensionFromToken('$token')->buildAssetUrl($arguments) ?>"; | ||||||
| @ -39,18 +41,17 @@ PATTERN; | |||||||
|                 return "<?php echo \$picea->compiler->getExtensionFromToken('$token')->buildRouteUrl($arguments) ?>"; |                 return "<?php echo \$picea->compiler->getExtensionFromToken('$token')->buildRouteUrl($arguments) ?>"; | ||||||
| 
 | 
 | ||||||
|             case "url": |             case "url": | ||||||
|  |                 if ( in_array('parameters', $opt) ) { | ||||||
|  |                     return "<?php echo \$picea->compiler->getExtensionFromToken('$token')->setUrlParameters($arguments) ?>"; | ||||||
|  |                 } | ||||||
|  |                 elseif ( in_array('current', $opt) ) { | ||||||
|  |                     return "<?php echo \$picea->compiler->getExtensionFromToken('$token')->currentUrl($arguments) ?>"; | ||||||
|  |                 } | ||||||
|  | 
 | ||||||
|                 return "<?php echo \$picea->compiler->getExtensionFromToken('$token')->buildUrl($arguments) ?>"; |                 return "<?php echo \$picea->compiler->getExtensionFromToken('$token')->buildUrl($arguments) ?>"; | ||||||
| 
 | 
 | ||||||
|             case "url.current": |  | ||||||
|                 return "<?php echo \$picea->compiler->getExtensionFromToken('$token')->currentUrl($arguments) ?>"; |  | ||||||
| 
 |  | ||||||
|             case "url.parameters": |  | ||||||
|                 return "<?php echo \$picea->compiler->getExtensionFromToken('$token')->setUrlParameters($arguments) ?>"; |  | ||||||
| 
 |  | ||||||
|             case "slug": |             case "slug": | ||||||
|                 return "<?php echo \$picea->compiler->getExtensionFromToken('$token')->slug($arguments) ?>"; |                 return "<?php echo \$picea->compiler->getExtensionFromToken('$token')->slug($arguments) ?>"; | ||||||
| 
 |  | ||||||
|                 #return \Transliterator::createFromRules(':: Any-Latin;:: NFD;:: [:Nonspacing Mark:] Remove;:: NFC;:: [:Punctuation:] Remove;:: Lower();[:Separator:] > \'-\'')->transliterate( $arguments );
 |  | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         return null; |         return null; | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user