Compare commits
	
		
			11 Commits
		
	
	
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
|  | 0c0ba39072 | ||
|  | ef95576d74 | ||
|  | e737e82d7e | ||
| 830a643a56 | |||
|  | b24bbbc150 | ||
|  | 3ffb69342b | ||
| fa815a506e | |||
|  | 35a7bd4cf7 | ||
|  | 7970679894 | ||
| d22d26c9c8 | |||
|  | 934643214e | 
| @ -96,9 +96,9 @@ You can do so by using `define` and `slot`. | ||||
| ```html | ||||
| {% arguments string $name, string $anchor, int $index = 0 %} | ||||
| 
 | ||||
| {% define slot %} | ||||
| {% define "attributes" %} | ||||
| 
 | ||||
| <a {% slot "attributes" %}href="{{ $anchor }}" tabindex="{{ $index }}"{% endslot %}>{{ $name }}"</a> | ||||
| <a {% slot "attributes" %}{% endslot %} href="{{ $anchor }}" tabindex="{{ $index }}"{% endslot %}>{{ $name }}"</a> | ||||
| ``` | ||||
| 
 | ||||
| **[HTML]** Would render the same as the `view` example : | ||||
|  | ||||
| @ -21,7 +21,7 @@ class Builder | ||||
| 
 | ||||
|         $replace = [ | ||||
|             '%NAMESPACE%' => $context->namespace, | ||||
|             '%USE%' => ( $uses = $context->renderUses() ) ? "use $uses;" : false, | ||||
|             '%USE%' => ( $uses = $context->renderUses() ) ? $uses : false, | ||||
|             '%CLASSNAME%' => $context->className, | ||||
|             '%PATHNAME%' => $path($context->viewPath), | ||||
|             '%FULLPATH%' => $path($context->filePath), | ||||
|  | ||||
| @ -57,7 +57,7 @@ abstract class Context { | ||||
| 
 | ||||
|     public function renderUses() : string | ||||
|     { | ||||
|         return implode(",", $this->useStack ?? []); | ||||
|         return implode(PHP_EOL, array_map(fn($use) => "use $use;", $this->useStack ?? [])); | ||||
|     } | ||||
| 
 | ||||
|     public function renderFunctions() : string | ||||
|  | ||||
| @ -4,45 +4,43 @@ namespace Picea\ControlStructure; | ||||
| 
 | ||||
| abstract class AbstractLoop implements ControlStructure { | ||||
| 
 | ||||
|     public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string | ||||
|     protected function openLoop(\Picea\Compiler\Context &$context, ?string $arguments, string $token,) : string | ||||
|     { | ||||
|         switch($token) { | ||||
|             case "while": | ||||
|             case "foreach": | ||||
|                 $name = "$".uniqid("{$token}_"); | ||||
|                  | ||||
|                 $stack = array_filter($context->iterationStack ?? [], function($item) { | ||||
|                     return ! $item['or']; | ||||
|                 }); | ||||
|                  | ||||
|                 $count = count($stack); | ||||
|         $name = "$".uniqid("{$token}_"); | ||||
| 
 | ||||
|                 if ( $count > 0 ) { | ||||
|                     $name .= "[" . end($stack)['uid'] . "]"; | ||||
|                 } | ||||
|         $stack = array_filter($context->iterationStack ?? [], function($item) { | ||||
|             return ! $item['or']; | ||||
|         }); | ||||
| 
 | ||||
|                 $context->iterationStack[] = [ | ||||
|                     'or' => false, | ||||
|                     'uid' => $name, | ||||
|                     'token' => "end{$token}", | ||||
|                 ]; | ||||
|         $count = count($stack); | ||||
| 
 | ||||
|                 return "<?php $token ($arguments): $name = ( $name ?? 0 ) + 1; ?>"; | ||||
| 
 | ||||
|             case "endwhile": | ||||
|             case "endforeach": | ||||
|                 $last = end($context->iterationStack); | ||||
| 
 | ||||
|                 if ( $last['or'] === false ) { | ||||
|                     $output = "<?php $token; ?>"; | ||||
|                 } | ||||
|                 else { | ||||
|                     $output = "<?php endif; if ( isset({$last['uid']}) ) unset({$last['uid']}); ?>"; | ||||
|                 } | ||||
| 
 | ||||
|                 array_pop($context->iterationStack); | ||||
|                  | ||||
|                 return $output; | ||||
|         if ( $count > 0 ) { | ||||
|             $name .= "[" . end($stack)['uid'] . "]"; | ||||
|         } | ||||
| 
 | ||||
|         $context->iterationStack[] = [ | ||||
|             'or' => false, | ||||
|             'uid' => $name, | ||||
|             'token' => "end{$token}", | ||||
|         ]; | ||||
| 
 | ||||
|         return "<?php $token ($arguments): \$__loop_index = $name = ( $name ?? 0 ) + 1; ?>"; | ||||
|     } | ||||
| 
 | ||||
|     protected function closeLoop(\Picea\Compiler\Context &$context, ?string $arguments, string $token,) : string | ||||
|     { | ||||
|         $last = end($context->iterationStack); | ||||
| 
 | ||||
|         if ( $last['or'] === false ) { | ||||
|             $output = "<?php $token; ?>"; | ||||
|         } | ||||
|         else { | ||||
|             $output = "<?php endif; if ( isset({$last['uid']}) ) unset({$last['uid']}); ?>"; | ||||
|         } | ||||
| 
 | ||||
|         array_pop($context->iterationStack); | ||||
| 
 | ||||
|         return $output; | ||||
|     } | ||||
| } | ||||
|   | ||||
| @ -4,21 +4,27 @@ namespace Picea\ControlStructure; | ||||
| 
 | ||||
| class BlockToken implements ControlStructure { | ||||
| 
 | ||||
|     public array $token = [ "arguments", "block", "endblock", "define", "slot", "endslot", "using" ]; | ||||
|     public array $token = [ "arguments", "block", "endblock", "/block", "define", "slot", "endslot", "/slot", "using" ]; | ||||
| 
 | ||||
|     public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string | ||||
|     { | ||||
|         static $depth = 0; | ||||
|         static $slotDefinitions = []; | ||||
| 
 | ||||
|         # dump($depth, $token, $arguments, $slotDefinitions);
 | ||||
| 
 | ||||
|         switch($token) { | ||||
|             case "block": | ||||
|                 $slotDefinitions[] = $this->slotDefinitions(); | ||||
|                  | ||||
|                 $depth++; | ||||
| 
 | ||||
|                 return "<?php \$___block = \Picea\ControlStructure\BlockToken::instanciateBlock($arguments); ?>"; | ||||
|              | ||||
| 
 | ||||
|             case "/block": | ||||
|             case "endblock": | ||||
|                 $depth--; | ||||
|                 return "<?php echo \$___block->render(\$___class__template); unset(\$___block); ?>"; | ||||
|              | ||||
| 
 | ||||
|             case "arguments": | ||||
|                 $class = static::class; | ||||
| 
 | ||||
| @ -36,19 +42,19 @@ class BlockToken implements ControlStructure { | ||||
|                         } | ||||
|                     /*%EXCEPTION_LINE_BASE%*/?>
 | ||||
|                 PHP; | ||||
|              | ||||
| 
 | ||||
|             case "define": | ||||
|                 list($name, $definition) = array_pad(explode(',', $arguments, 2), 2, ""); | ||||
|                  | ||||
|                 end($slotDefinitions)->setDefinition(eval("return $name;"), $definition); | ||||
|                  | ||||
| 
 | ||||
|                 ( $slotDefinitions[$depth] ?? end($slotDefinitions) )->setDefinition(eval("return $name;"), $definition); | ||||
| 
 | ||||
|                 return <<<PHP | ||||
|                     <?php \$this->defineSlot($name, function($definition) {}); ?>
 | ||||
|                 <?php \$this->defineSlot($name, function($definition) {}); ?>
 | ||||
|                 PHP; | ||||
|                      | ||||
| 
 | ||||
|             case "slot": | ||||
|                 $def = end($slotDefinitions); | ||||
|                  | ||||
|                 $def = ( $slotDefinitions[$depth] ?? 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; | ||||
| @ -57,7 +63,7 @@ class BlockToken implements ControlStructure { | ||||
|                     $slotName = eval("return $name;"); | ||||
|                     $def->currentSlot = $slotName; | ||||
|                     $def->setDefinitionVars($slotName, $definition); | ||||
|                      | ||||
| 
 | ||||
|                     $definition = $def->printDefinition($slotName); | ||||
| 
 | ||||
|                     if ($definition) { | ||||
| @ -65,53 +71,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->setSlot($name, function($definition array \$___using = []) use (\$picea $loops) { extract(\$___using, \EXTR_SKIP); ?>
 | ||||
|                     PHP; | ||||
|                 } | ||||
|                      | ||||
|             case "endslot": | ||||
|                 $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 (\$___block ?? \$this)->slotIsSet($name) || (\$___block ?? \$this)->setSlot($name, function($definition array \$___using = []) use (\$picea $loops) { extract(\$___using, \EXTR_SKIP); ?>
 | ||||
|                     PHP; | ||||
|                 } | ||||
| 
 | ||||
|             case "/slot": | ||||
|             case "endslot": | ||||
|                 $def = ( $slotDefinitions[$depth] ?? 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; | ||||
|                 } | ||||
|                 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{ | ||||
| @ -119,10 +125,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]; | ||||
| @ -137,115 +143,119 @@ 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 ?"); | ||||
|                     throw new \Exception("Slot definition for `$name` was not found. Have you defined it in your block header using something like '{% define \"$name\", ...\$arguments %}' ?");
 | ||||
|                 } | ||||
|                  | ||||
| 
 | ||||
|                 return $this->definitions[$name]; | ||||
|             } | ||||
|              | ||||
| 
 | ||||
|             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; | ||||
| 
 | ||||
|                 return $classTemplate->picea->inlineBlock($this, $this->viewPath, ...$this->arguments); | ||||
|                 if ($this->using['this'] ?? false) { | ||||
|                     $thisProxy = $this->using['this']; | ||||
|                     unset($this->using['this']); | ||||
|                 } | ||||
|                  | ||||
|                 return $classTemplate->picea->inlineBlock($thisProxy ?? $this, $this->viewPath, ...$this->arguments); | ||||
|             } | ||||
|              | ||||
| 
 | ||||
|             public function setSlot(string $name, Callable $method) : void | ||||
|             { | ||||
|                 $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; | ||||
|             } | ||||
|  | ||||
| @ -4,7 +4,7 @@ namespace Picea\ControlStructure; | ||||
| 
 | ||||
| class ForToken implements ControlStructure { | ||||
| 
 | ||||
|     public array $token = [ "for", "endfor" ]; | ||||
|     public array $token = [ "for", "endfor", "/for" ]; | ||||
| 
 | ||||
|     public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string { | ||||
|         switch($token) { | ||||
| @ -18,7 +18,8 @@ class ForToken implements ControlStructure { | ||||
|                 ]; | ||||
| 
 | ||||
|                 return "<?php for ($arguments): {$uid} = 1; ?>"; | ||||
|                  | ||||
| 
 | ||||
|             case "/for": | ||||
|             case "endfor": | ||||
|                 $last = end($context->iterationStack); | ||||
| 
 | ||||
|  | ||||
| @ -5,5 +5,17 @@ namespace Picea\ControlStructure; | ||||
| use DI\Definition\Source\AnnotationBasedAutowiring; | ||||
| 
 | ||||
| class ForeachToken extends AbstractLoop { | ||||
|     public array $token = [ "foreach", "endforeach" ]; | ||||
|     public array $token = [ "foreach", "endforeach", "/foreach" ]; | ||||
| 
 | ||||
|     public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string | ||||
|     { | ||||
|         switch($token) { | ||||
|             case "foreach": | ||||
|                 return $this->openLoop($context, $arguments, $token); | ||||
| 
 | ||||
|             case "endforeach": | ||||
|             case "/foreach": | ||||
|                 return $this->closeLoop($context, $arguments, $token); | ||||
|         } | ||||
|     } | ||||
| } | ||||
|  | ||||
| @ -33,7 +33,9 @@ class FunctionToken implements ControlStructure { | ||||
| 
 | ||||
|     protected function printFunction($context, ?string $arguments) : string | ||||
|     { | ||||
|         return "<?php function $arguments { ?>"; | ||||
|         $name = trim(explode('(', $arguments, 2)[0]); | ||||
| 
 | ||||
|         return "<?php if (! function_exists(__NAMESPACE__ . '\\$name')) { function $arguments { ?>"; | ||||
|     } | ||||
| 
 | ||||
|     protected function printReturn($context, ?string $arguments) : string | ||||
| @ -43,6 +45,6 @@ class FunctionToken implements ControlStructure { | ||||
| 
 | ||||
|     protected function printEndFunction($context) : string | ||||
|     { | ||||
|         return "<?php } ?>"; | ||||
|         return "<?php } } ?>"; | ||||
|     } | ||||
| } | ||||
| @ -4,7 +4,7 @@ namespace Picea\ControlStructure; | ||||
| 
 | ||||
| class IfToken implements ControlStructure { | ||||
| 
 | ||||
|     public array $token = [ "if", "else", "elseif", "endif" ]; | ||||
|     public array $token = [ "if", "else", "elseif", "endif", "/if" ]; | ||||
| 
 | ||||
|     public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string { | ||||
|         switch($token) { | ||||
| @ -16,7 +16,8 @@ class IfToken implements ControlStructure { | ||||
|              | ||||
|             case "elseif": | ||||
|                 return "<?php elseif ($arguments): ?>"; | ||||
|                  | ||||
| 
 | ||||
|             case "/if": | ||||
|             case "endif": | ||||
|                 return "<?php endif ?>"; | ||||
|         } | ||||
|  | ||||
| @ -11,7 +11,7 @@ enum PrintActionEnum : string { | ||||
| 
 | ||||
| class SectionToken implements ControlStructure { | ||||
| 
 | ||||
|     public array $token = [ "section", "endsection" ]; | ||||
|     public array $token = [ "section", "endsection", "/section" ]; | ||||
| 
 | ||||
|     protected PrintActionEnum $action = PrintActionEnum::default; | ||||
| 
 | ||||
| @ -29,6 +29,7 @@ class SectionToken implements ControlStructure { | ||||
|                 return $this->printSection($context, $arguments); | ||||
| 
 | ||||
|             case "endsection": | ||||
|             case "/endsection": | ||||
|                 if ( empty($context->sections) ) { | ||||
|                     throw new \RuntimeException("A section closing tag {% endsection %} was found without an opening {% section %} tag"); | ||||
|                 } | ||||
|  | ||||
| @ -23,11 +23,13 @@ class SwitchToken implements ControlStructure { | ||||
|                 } | ||||
| 
 | ||||
|                 return ( $output ?? "" ) . "case $arguments: ?>"; | ||||
|              | ||||
| 
 | ||||
|             case "endcase": | ||||
|             case "/case": | ||||
|                 return "<?php break; ?>"; | ||||
|                  | ||||
|             case "endswitch": | ||||
|             case "/switch": | ||||
|                 return "<?php endswitch; ?>";  | ||||
|         } | ||||
|     } | ||||
|  | ||||
							
								
								
									
										25
									
								
								src/ControlStructure/TryCatchToken.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								src/ControlStructure/TryCatchToken.php
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,25 @@ | ||||
| <?php | ||||
| 
 | ||||
| namespace Picea\ControlStructure; | ||||
| 
 | ||||
| class TryCatchToken implements ControlStructure { | ||||
| 
 | ||||
|     public array $token = [ "try", "catch", "finally", "endtry", "/try" ]; | ||||
| 
 | ||||
|     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": | ||||
|             case "/try": | ||||
|                 return "<?php } ?>"; | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @ -3,7 +3,7 @@ | ||||
| namespace Picea\ControlStructure; | ||||
| 
 | ||||
| class WhileToken extends AbstractLoop { | ||||
|     public array $token = [ "do", "while", "endwhile", ]; | ||||
|     public array $token = [ "do", "while", "endwhile", "/while" ]; | ||||
| 
 | ||||
|     public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string { | ||||
|         switch($token) { | ||||
| @ -23,10 +23,13 @@ class WhileToken extends AbstractLoop { | ||||
|                         return "<?php } while($arguments); ?>"; | ||||
|                     } | ||||
|                 } | ||||
| 
 | ||||
|                 return $this->openLoop($context, $arguments, $token); | ||||
| 
 | ||||
|             case "endwhile": | ||||
|             case "/while": | ||||
|                 return $this->closeLoop($context, $arguments, $token); | ||||
|         } | ||||
| 
 | ||||
|         return parent::parse($context, $arguments, $token); | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
|  | ||||
| @ -30,8 +30,8 @@ PATTERN; | ||||
| 
 | ||||
|     public array $tokens = [ "url" , "route", "asset", "slug" ]; | ||||
| 
 | ||||
|     #[\Deprecated]
 | ||||
|     protected bool $enforceExistingArguments = true; | ||||
|     ##[\Deprecated]
 | ||||
|     public bool $enforceExistingArguments = true; | ||||
| 
 | ||||
|     public function __construct(string $urlBase = "", string $assetToken = "", array $appUrl = [], bool $forceSSL = false) { | ||||
|         $this->urlBase = trim($urlBase, "/"); | ||||
| @ -120,7 +120,7 @@ PATTERN; | ||||
| 
 | ||||
|     public function buildRouteUrl(string $name, array $parameters = [], bool $appendVersion = false) : string | ||||
|     { | ||||
|         if ( false !== ( $route = $this->routes[$name] ?? false ) ) { | ||||
|         if ( false !== $route = $this->findRoute($name) ) { | ||||
|             return $this->buildUrl($this->prepareRoute($route, $parameters), $parameters, $appendVersion); | ||||
|         } | ||||
| 
 | ||||
| @ -149,14 +149,28 @@ PATTERN; | ||||
| 
 | ||||
|     public function registerRoute(string $name, string $route, string $class, string $method, array $routeMethods) : void | ||||
|     { | ||||
|         $this->routes[$name] = [ | ||||
|         $this->routes[] = $array = [ | ||||
|             'name' => $name, | ||||
|             'route' => $route, | ||||
|             'routeMethods' => $routeMethods, | ||||
|             'routeMethods' => array_map('strtoupper', $routeMethods), | ||||
|             'class' => $class, | ||||
|             'classMethod' => $method, | ||||
|         ]; | ||||
| 
 | ||||
|         $this->eventExecute(UrlRegisterRouteEvent::class, $name, $this->routes[$name]); | ||||
|         $this->eventExecute(UrlRegisterRouteEvent::class, $name, $array); | ||||
|     } | ||||
| 
 | ||||
|     protected function findRoute(string $name, string $method = "*") : false|array | ||||
|     { | ||||
|         foreach($this->routes as $route) { | ||||
|             if ( $route['name'] === $name ) { | ||||
|                 if ($method === '*' || in_array(strtoupper($method), $route['routeMethods']) ) { | ||||
|                     return $route; | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
| 
 | ||||
|         return false; | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
| @ -275,7 +289,7 @@ PATTERN; | ||||
|                     if ($default ?? false) { | ||||
|                         $value = $default; | ||||
|                     } | ||||
|                     elseif ( strpos($route, "[{$matches[0][0]}]") !== false && $this->enforceExistingArguments) { | ||||
|                     elseif ( $this->enforceExistingArguments && ! preg_match(sprintf("/\[\/?%s]/i", $item[0]), $route) ) { | ||||
|                         throw new \RuntimeException(sprintf("Error while preparing route %s : could not match variable '%s' into given arguments ( %s ) from %s::%s", $route, $variable, json_encode($arguments), $routeParam['class'], $routeParam['classMethod'])); | ||||
|                     } | ||||
|                 } | ||||
|  | ||||
| @ -32,6 +32,7 @@ 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()); | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user