From ef95576d74300d36914c97616c17a30228e211ce Mon Sep 17 00:00:00 2001 From: Dave Mc Nicoll Date: Fri, 3 Oct 2025 18:34:32 +0000 Subject: [PATCH] - Added a loop index in AbstractLoop control struct. --- src/ControlStructure/AbstractLoop.php | 56 +++++++++++++++++--------- src/ControlStructure/BlockToken.php | 5 ++- src/ControlStructure/ForToken.php | 5 ++- src/ControlStructure/ForeachToken.php | 14 ++++++- src/ControlStructure/IfToken.php | 5 ++- src/ControlStructure/SectionToken.php | 3 +- src/ControlStructure/SwitchToken.php | 4 +- src/ControlStructure/TryCatchToken.php | 4 +- src/ControlStructure/WhileToken.php | 11 +++-- src/Extension/UrlExtension.php | 2 +- 10 files changed, 73 insertions(+), 36 deletions(-) diff --git a/src/ControlStructure/AbstractLoop.php b/src/ControlStructure/AbstractLoop.php index 39305ff..6bbf2e6 100644 --- a/src/ControlStructure/AbstractLoop.php +++ b/src/ControlStructure/AbstractLoop.php @@ -4,30 +4,18 @@ 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 ""; +<<<<<<< HEAD + return ""; case "endwhile": case "endforeach": @@ -43,6 +31,34 @@ abstract class AbstractLoop implements ControlStructure { array_pop($context->iterationStack); return $output; +======= + if ( $count > 0 ) { + $name .= "[" . end($stack)['uid'] . "]"; +>>>>>>> 579a917 (- Added a loop index in AbstractLoop control struct.) } + + $context->iterationStack[] = [ + 'or' => false, + 'uid' => $name, + 'token' => "end{$token}", + ]; + + return ""; + } + + protected function closeLoop(\Picea\Compiler\Context &$context, ?string $arguments, string $token,) : string + { + $last = end($context->iterationStack); + + if ( $last['or'] === false ) { + $output = ""; + } + else { + $output = ""; + } + + array_pop($context->iterationStack); + + return $output; } } diff --git a/src/ControlStructure/BlockToken.php b/src/ControlStructure/BlockToken.php index ca05514..cfa04e6 100644 --- a/src/ControlStructure/BlockToken.php +++ b/src/ControlStructure/BlockToken.php @@ -4,7 +4,7 @@ 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 { @@ -20,6 +20,7 @@ class BlockToken implements ControlStructure { return ""; + case "/block": case "endblock": $depth--; return "render(\$___class__template); unset(\$___block); ?>"; @@ -83,6 +84,7 @@ class BlockToken implements ControlStructure { PHP; } + case "/slot": case "endslot": $def = ( $slotDefinitions[$depth] ?? end($slotDefinitions) ); @@ -106,7 +108,6 @@ class BlockToken implements ControlStructure { } } - public function inlineHtml(? object $proxy, string $viewPath, ... $variables) { return $this->renderHtml($viewPath, [ 'inlineVariables' => $variables ], $proxy); } diff --git a/src/ControlStructure/ForToken.php b/src/ControlStructure/ForToken.php index 67d438f..699fa40 100644 --- a/src/ControlStructure/ForToken.php +++ b/src/ControlStructure/ForToken.php @@ -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 ""; - + + case "/for": case "endfor": $last = end($context->iterationStack); diff --git a/src/ControlStructure/ForeachToken.php b/src/ControlStructure/ForeachToken.php index 21f172a..e8d109e 100644 --- a/src/ControlStructure/ForeachToken.php +++ b/src/ControlStructure/ForeachToken.php @@ -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); + } + } } diff --git a/src/ControlStructure/IfToken.php b/src/ControlStructure/IfToken.php index 98f55f2..fc87cdd 100644 --- a/src/ControlStructure/IfToken.php +++ b/src/ControlStructure/IfToken.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 ""; - + + case "/if": case "endif": return ""; } diff --git a/src/ControlStructure/SectionToken.php b/src/ControlStructure/SectionToken.php index fc2272d..29e562b 100644 --- a/src/ControlStructure/SectionToken.php +++ b/src/ControlStructure/SectionToken.php @@ -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"); } diff --git a/src/ControlStructure/SwitchToken.php b/src/ControlStructure/SwitchToken.php index 0b0611c..9768c8d 100644 --- a/src/ControlStructure/SwitchToken.php +++ b/src/ControlStructure/SwitchToken.php @@ -23,11 +23,13 @@ class SwitchToken implements ControlStructure { } return ( $output ?? "" ) . "case $arguments: ?>"; - + case "endcase": + case "/case": return ""; case "endswitch": + case "/switch": return ""; } } diff --git a/src/ControlStructure/TryCatchToken.php b/src/ControlStructure/TryCatchToken.php index 501eec1..0f09fad 100644 --- a/src/ControlStructure/TryCatchToken.php +++ b/src/ControlStructure/TryCatchToken.php @@ -4,12 +4,11 @@ namespace Picea\ControlStructure; class TryCatchToken implements ControlStructure { - public array $token = [ "try", "catch", "finally", "endtry" ]; + 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 ""; case "catch": @@ -19,6 +18,7 @@ class TryCatchToken implements ControlStructure { return ""; case "endtry": + case "/try": return ""; } } diff --git a/src/ControlStructure/WhileToken.php b/src/ControlStructure/WhileToken.php index 901aa6b..042412d 100644 --- a/src/ControlStructure/WhileToken.php +++ b/src/ControlStructure/WhileToken.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 ""; } } + + return $this->openLoop($context, $arguments, $token); + + case "endwhile": + case "/while": + return $this->closeLoop($context, $arguments, $token); } - return parent::parse($context, $arguments, $token); } } - - diff --git a/src/Extension/UrlExtension.php b/src/Extension/UrlExtension.php index dc06f45..afe3c5f 100644 --- a/src/Extension/UrlExtension.php +++ b/src/Extension/UrlExtension.php @@ -31,7 +31,7 @@ PATTERN; public array $tokens = [ "url" , "route", "asset", "slug" ]; ##[\Deprecated] - protected bool $enforceExistingArguments = true; + public bool $enforceExistingArguments = true; public function __construct(string $urlBase = "", string $assetToken = "", array $appUrl = [], bool $forceSSL = false) { $this->urlBase = trim($urlBase, "/");