Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.3] Fix foreach blade compiler #15485

Merged
merged 5 commits into from
Sep 18, 2016
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 *([^\)]*)/s', $expression, $matches);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please add the i here as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure.


$iteratee = trim($matches[1]);

Expand Down
40 changes: 40 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,28 @@ public function testForelseStatementsAreCompiled()
$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