Skip to content

Commit

Permalink
[5.3] Fix foreach blade compiler (#15485)
Browse files Browse the repository at this point in the history
* Fix compileForeach regex

* @foreach support uppercased AS

* Add multi-line @foreach test

* Add multi-line @forelse test

* @forelse compiler can handle uppercased 'AS'
  • Loading branch information
ElfSundae authored and taylorotwell committed Sep 18, 2016
1 parent 25c76ea commit c3e446e
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Illuminate/View/Compilers/BladeCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);

Expand Down Expand Up @@ -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]);

Expand Down
56 changes: 56 additions & 0 deletions tests/View/ViewBladeCompilerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '<?php $__currentLoopData = [
foo,
bar,
]; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $label): $__env->incrementLoopIndices(); $loop = $__env->getFirstLoop(); ?>
test
<?php endforeach; $__env->popLoop(); $loop = $__env->getFirstLoop(); ?>';
$this->assertEquals($expected, $compiler->compileString($string));
}

public function testNestedForeachStatementsAreCompiled()
{
$compiler = new BladeCompiler($this->getFiles(), __DIR__);
Expand Down Expand Up @@ -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 = '<?php $__empty_1 = true; $__currentLoopData = $this->getUsers(); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $user): $__env->incrementLoopIndices(); $loop = $__env->getFirstLoop(); $__empty_1 = false; ?>
breeze
<?php endforeach; $__env->popLoop(); $loop = $__env->getFirstLoop(); if ($__empty_1): ?>
empty
<?php endif; ?>';
$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 = '<?php $__empty_1 = true; $__currentLoopData = [
foo,
bar,
]; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $label): $__env->incrementLoopIndices(); $loop = $__env->getFirstLoop(); $__empty_1 = false; ?>
breeze
<?php endforeach; $__env->popLoop(); $loop = $__env->getFirstLoop(); if ($__empty_1): ?>
empty
<?php endif; ?>';
$this->assertEquals($expected, $compiler->compileString($string));
}

public function testNestedForelseStatementsAreCompiled()
{
$compiler = new BladeCompiler($this->getFiles(), __DIR__);
Expand Down

0 comments on commit c3e446e

Please sign in to comment.