- 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; | ||||
| 
 | ||||
| use Picea\Exception\RegisterExtensionToken; | ||||
| use Picea\Extension\FunctionExtension; | ||||
| use Picea\Language\LanguageRegistration; | ||||
| 
 | ||||
| @ -49,16 +50,17 @@ class Compiler | ||||
|             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(<<<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); | ||||
|         } | ||||
| 
 | ||||
|         return $this->extensionList[$name]; | ||||
|         return $this->extensionList[$tokenName]; | ||||
|     } | ||||
|      | ||||
|     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 | ||||
|     { | ||||
|         $this->functionStack[$name] = $callable; | ||||
|  | ||||
| @ -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) { | ||||
|         switch($token) { | ||||
|             case "section.prepend": | ||||
|     public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) | ||||
|     { | ||||
|         $opt = $context->tokenOptions($token); | ||||
| 
 | ||||
|         if (in_array('prepend', $opt)) { | ||||
|             $context->sectionAction = "prepend"; | ||||
|             case "section.append": | ||||
|                 $context->sectionAction ??= "append"; | ||||
|         } | ||||
|         elseif (in_array('append', $opt)) { | ||||
|             $context->sectionAction = "append"; | ||||
|         } | ||||
| 
 | ||||
|         switch($token) { | ||||
|             case "section": | ||||
|                 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,20 +6,21 @@ use Picea\Compiler\Context; | ||||
| 
 | ||||
| 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 function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token) | ||||
|     { | ||||
|         $opt = $context->tokenOptions($token); | ||||
| 
 | ||||
|         $flag = $this->flags; | ||||
| 
 | ||||
|         switch ($token) { | ||||
|             case "json.pretty": | ||||
|         if ( in_array('pretty', $opt) ) { | ||||
|             $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') ?>"; | ||||
|         } | ||||
| 
 | ||||
|  | ||||
| @ -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 <<<PHP | ||||
| @ -47,18 +49,20 @@ class LanguageExtension implements Extension, FunctionExtension { | ||||
|                 PHP; | ||||
| 
 | ||||
|             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) ?>"; | ||||
| 
 | ||||
|             case "lang.raw": | ||||
|                 return "<?php echo \$picea->compiler->getExtensionFromToken('$token')->absoluteLang($arguments) ?>"; | ||||
| 
 | ||||
|             case "_": | ||||
|                 return "<?php echo htmlspecialchars(\$picea->compiler->getExtensionFromToken('$token')->relativeLang($arguments), \ENT_QUOTES, ini_get('default_charset'), true) ?>"; | ||||
| 
 | ||||
|             case "_.raw": | ||||
|                 if ( in_array('raw', $opt) ) { | ||||
|                     return "<?php echo \$picea->compiler->getExtensionFromToken('$token')->relativeLang($arguments) ?>"; | ||||
|                 } | ||||
| 
 | ||||
|                 return "<?php echo htmlspecialchars(\$picea->compiler->getExtensionFromToken('$token')->relativeLang($arguments), \ENT_QUOTES, ini_get('default_charset'), true) ?>"; | ||||
|         } | ||||
| 
 | ||||
|         return ""; | ||||
|     } | ||||
| 
 | ||||
|  | ||||
| @ -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': | ||||
|  | ||||
| @ -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 "<?php echo \$picea->compiler->getExtensionFromToken('$token')->buildAssetUrl($arguments) ?>"; | ||||
| @ -39,18 +41,17 @@ PATTERN; | ||||
|                 return "<?php echo \$picea->compiler->getExtensionFromToken('$token')->buildRouteUrl($arguments) ?>"; | ||||
| 
 | ||||
|             case "url": | ||||
|                 return "<?php echo \$picea->compiler->getExtensionFromToken('$token')->buildUrl($arguments) ?>"; | ||||
| 
 | ||||
|             case "url.current": | ||||
|                 return "<?php echo \$picea->compiler->getExtensionFromToken('$token')->currentUrl($arguments) ?>"; | ||||
| 
 | ||||
|             case "url.parameters": | ||||
|                 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) ?>"; | ||||
| 
 | ||||
|             case "slug": | ||||
|                 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; | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user