From c3e446e23e860d41209f433e2ede55d6287aa20c Mon Sep 17 00:00:00 2001 From: Elf Sundae Date: Sun, 18 Sep 2016 23:39:12 +0800 Subject: [PATCH] [5.3] Fix foreach blade compiler (#15485) * Fix compileForeach regex * @foreach support uppercased AS * Add multi-line @foreach test * Add multi-line @forelse test * @forelse compiler can handle uppercased 'AS' --- .../View/Compilers/BladeCompiler.php | 4 +- tests/View/ViewBladeCompilerTest.php | 56 +++++++++++++++++++ 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/View/Compilers/BladeCompiler.php b/src/Illuminate/View/Compilers/BladeCompiler.php index de8f626869e3..575d3950df04 100644 --- a/src/Illuminate/View/Compilers/BladeCompiler.php +++ b/src/Illuminate/View/Compilers/BladeCompiler.php @@ -579,7 +579,7 @@ protected function compileFor($expression) */ protected function compileForeach($expression) { - preg_match('/\( *(.*) +as *([^\)]*)/i', $expression, $matches); + preg_match('/\( *(.*) +as *([^\)]*)/is', $expression, $matches); $iteratee = trim($matches[1]); @@ -624,7 +624,7 @@ protected function compileForelse($expression) { $empty = '$__empty_'.++$this->forelseCounter; - preg_match('/\( *(.*) +as *([^\)]*)/', $expression, $matches); + preg_match('/\( *(.*) +as *([^\)]*)/is', $expression, $matches); $iteratee = trim($matches[1]); diff --git a/tests/View/ViewBladeCompilerTest.php b/tests/View/ViewBladeCompilerTest.php index fc78b2d6fb78..f824df73c13b 100644 --- a/tests/View/ViewBladeCompilerTest.php +++ b/tests/View/ViewBladeCompilerTest.php @@ -451,6 +451,24 @@ public function testForeachStatementsAreCompileWithUppercaseSyntax() $this->assertEquals($expected, $compiler->compileString($string)); } + public function testForeachStatementsAreCompileWithMultipleLine() + { + $compiler = new BladeCompiler($this->getFiles(), __DIR__); + $string = '@foreach ([ +foo, +bar, +] as $label) +test +@endforeach'; + $expected = 'addLoop($__currentLoopData); foreach($__currentLoopData as $label): $__env->incrementLoopIndices(); $loop = $__env->getFirstLoop(); ?> +test +popLoop(); $loop = $__env->getFirstLoop(); ?>'; + $this->assertEquals($expected, $compiler->compileString($string)); + } + public function testNestedForeachStatementsAreCompiled() { $compiler = new BladeCompiler($this->getFiles(), __DIR__); @@ -510,6 +528,44 @@ public function testForelseStatementsAreCompiled() $this->assertEquals($expected, $compiler->compileString($string)); } + public function testForelseStatementsAreCompiledWithUppercaseSyntax() + { + $compiler = new BladeCompiler($this->getFiles(), __DIR__); + $string = '@forelse ($this->getUsers() AS $user) +breeze +@empty +empty +@endforelse'; + $expected = 'getUsers(); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $user): $__env->incrementLoopIndices(); $loop = $__env->getFirstLoop(); $__empty_1 = false; ?> +breeze +popLoop(); $loop = $__env->getFirstLoop(); if ($__empty_1): ?> +empty +'; + $this->assertEquals($expected, $compiler->compileString($string)); + } + + public function testForelseStatementsAreCompiledWithMultipleLine() + { + $compiler = new BladeCompiler($this->getFiles(), __DIR__); + $string = '@forelse ([ +foo, +bar, +] as $label) +breeze +@empty +empty +@endforelse'; + $expected = 'addLoop($__currentLoopData); foreach($__currentLoopData as $label): $__env->incrementLoopIndices(); $loop = $__env->getFirstLoop(); $__empty_1 = false; ?> +breeze +popLoop(); $loop = $__env->getFirstLoop(); if ($__empty_1): ?> +empty +'; + $this->assertEquals($expected, $compiler->compileString($string)); + } + public function testNestedForelseStatementsAreCompiled() { $compiler = new BladeCompiler($this->getFiles(), __DIR__);