From d0dc9902c7c1b98a81dbce79a07e308ea7c9fcb2 Mon Sep 17 00:00:00 2001 From: Jason McCreary Date: Sun, 12 Nov 2023 19:31:35 -0500 Subject: [PATCH 1/3] Add `@pushElseIf` and `@pushElse` --- .../Concerns/CompilesConditionals.php | 24 +++++++++++++++++++ tests/View/Blade/BladePushTest.php | 20 ++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/src/Illuminate/View/Compilers/Concerns/CompilesConditionals.php b/src/Illuminate/View/Compilers/Concerns/CompilesConditionals.php index 2b1c0e8e41d4..a5a487bf3040 100644 --- a/src/Illuminate/View/Compilers/Concerns/CompilesConditionals.php +++ b/src/Illuminate/View/Compilers/Concerns/CompilesConditionals.php @@ -373,6 +373,30 @@ protected function compilePushIf($expression) return "startPush({$parts[1]}); ?>"; } + /** + * Compile the else-if push statements into valid PHP. + * + * @param string $expression + * @return string + */ + protected function compilePushElseIf($expression) + { + $parts = explode(',', $this->stripParentheses($expression), 2); + + return "startPush({$parts[1]}); ?>"; + } + + /** + * Compile the else push statements into valid PHP. + * + * @param string $expression + * @return string + */ + protected function compilePushElse($expression) + { + return "startPush{$expression}; ?>"; + } + /** * Compile the end-push statements into valid PHP. * diff --git a/tests/View/Blade/BladePushTest.php b/tests/View/Blade/BladePushTest.php index 103c5c13b692..5a2b3284183a 100644 --- a/tests/View/Blade/BladePushTest.php +++ b/tests/View/Blade/BladePushTest.php @@ -65,6 +65,26 @@ public function testPushIfIsCompiled() @endPushIf'; $expected = 'startPush( \'foo\'); ?> test +stopPush(); endif; ?>'; + + $this->assertEquals($expected, $this->compiler->compileString($string)); + } + + public function testPushIfElseIsCompiled() + { + $string = '@pushIf(true, \'stack\') +if +@pushElseIf(false, \'stack\') +elseif +@pushElse(\'stack\') +else +@endPushIf'; + $expected = 'startPush( \'stack\'); ?> +if +startPush( \'stack\'); ?> +elseif +startPush(\'stack\'); ?> +else stopPush(); endif; ?>'; $this->assertEquals($expected, $this->compiler->compileString($string)); From b9e9c2f35200eafc246aeb90d35fa5f62077715b Mon Sep 17 00:00:00 2001 From: Jason McCreary Date: Mon, 13 Nov 2023 07:26:46 -0500 Subject: [PATCH 2/3] Stop push within each conditional branch --- .../View/Compilers/Concerns/CompilesConditionals.php | 4 ++-- tests/View/Blade/BladePushTest.php | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Illuminate/View/Compilers/Concerns/CompilesConditionals.php b/src/Illuminate/View/Compilers/Concerns/CompilesConditionals.php index a5a487bf3040..78e91a1ef4c4 100644 --- a/src/Illuminate/View/Compilers/Concerns/CompilesConditionals.php +++ b/src/Illuminate/View/Compilers/Concerns/CompilesConditionals.php @@ -383,7 +383,7 @@ protected function compilePushElseIf($expression) { $parts = explode(',', $this->stripParentheses($expression), 2); - return "startPush({$parts[1]}); ?>"; + return "stopPush(); elseif({$parts[0]}): \$__env->startPush({$parts[1]}); ?>"; } /** @@ -394,7 +394,7 @@ protected function compilePushElseIf($expression) */ protected function compilePushElse($expression) { - return "startPush{$expression}; ?>"; + return "stopPush(); else: \$__env->startPush{$expression}; ?>"; } /** diff --git a/tests/View/Blade/BladePushTest.php b/tests/View/Blade/BladePushTest.php index 5a2b3284183a..4f4f41fdc327 100644 --- a/tests/View/Blade/BladePushTest.php +++ b/tests/View/Blade/BladePushTest.php @@ -81,9 +81,9 @@ public function testPushIfElseIsCompiled() @endPushIf'; $expected = 'startPush( \'stack\'); ?> if -startPush( \'stack\'); ?> +stopPush(); elseif(false): $__env->startPush( \'stack\'); ?> elseif -startPush(\'stack\'); ?> +stopPush(); else: $__env->startPush(\'stack\'); ?> else stopPush(); endif; ?>'; From a611640637b2ae65b16c5b269614243efa5ae1a8 Mon Sep 17 00:00:00 2001 From: Jason McCreary Date: Tue, 14 Nov 2023 15:39:18 -0500 Subject: [PATCH 3/3] Symmetry --- .../View/Compilers/Concerns/CompilesConditionals.php | 4 ++-- tests/View/Blade/BladePushTest.php | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Illuminate/View/Compilers/Concerns/CompilesConditionals.php b/src/Illuminate/View/Compilers/Concerns/CompilesConditionals.php index 78e91a1ef4c4..b26494e822b5 100644 --- a/src/Illuminate/View/Compilers/Concerns/CompilesConditionals.php +++ b/src/Illuminate/View/Compilers/Concerns/CompilesConditionals.php @@ -379,7 +379,7 @@ protected function compilePushIf($expression) * @param string $expression * @return string */ - protected function compilePushElseIf($expression) + protected function compileElsePushIf($expression) { $parts = explode(',', $this->stripParentheses($expression), 2); @@ -392,7 +392,7 @@ protected function compilePushElseIf($expression) * @param string $expression * @return string */ - protected function compilePushElse($expression) + protected function compileElsePush($expression) { return "stopPush(); else: \$__env->startPush{$expression}; ?>"; } diff --git a/tests/View/Blade/BladePushTest.php b/tests/View/Blade/BladePushTest.php index 4f4f41fdc327..84f3d3f33bfd 100644 --- a/tests/View/Blade/BladePushTest.php +++ b/tests/View/Blade/BladePushTest.php @@ -74,9 +74,9 @@ public function testPushIfElseIsCompiled() { $string = '@pushIf(true, \'stack\') if -@pushElseIf(false, \'stack\') +@elsePushIf(false, \'stack\') elseif -@pushElse(\'stack\') +@elsePush(\'stack\') else @endPushIf'; $expected = 'startPush( \'stack\'); ?>