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

[10.x] Fix blade failing to compile when mixing inline/block @php directives #48420

Merged
merged 2 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion src/Illuminate/View/Compilers/BladeCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ protected function storeVerbatimBlocks($value)
*/
protected function storePhpBlocks($value)
{
return preg_replace_callback('/(?<!@)@php(.*?)@endphp/s', function ($matches) {
return preg_replace_callback('/(?<!@)@php((?:.(?!(?<!@)@php))*?)@endphp/s', function ($matches) {
return $this->storeRawBlock("<?php{$matches[1]}?>");
}, $value);
}
Expand Down
125 changes: 125 additions & 0 deletions tests/View/Blade/BladePhpStatementsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,131 @@ public function testPhpStatementsWithExpressionAreCompiled()
$this->assertEquals($expected, $this->compiler->compileString($string));
}

public function testMixedPhpStatementsAreCompiled()
{
$string = <<<'BLADE'
@php($set = true)
@php($set = true) @php ($set = false;) @endphp
@php ($set = true) @php($set = false;)@endphp
<div>{{ $set }}</div>
@php
$set = false;
@endphp
@php (
$set = false;
)@endphp
@php(
$set = false;
)@endphp
@php (
$set = false
)
@php(
$set = false
)
@php
$set = '@@php';
@endphp
BLADE;
$expected = <<<'PHP'
<?php ($set = true); ?>
<?php ($set = true); ?> <?php ($set = false;) ?>
<?php ($set = true); ?> <?php($set = false;)?>
<div><?php echo e($set); ?></div>
<?php
$set = false;
?>
<?php (
$set = false;
)?>
<?php(
$set = false;
)?>
<?php (
$set = false
); ?>
<?php (
$set = false
); ?>
<?php
$set = '@@php';
?>
PHP;
$this->assertEquals($expected, $this->compiler->compileString($string));
}

public function testCompilationOfMixedPhpStatements()
{
$string = '@php($set = true) @php ($hello = \'hi\') @php echo "Hello world" @endphp';
$expected = '<?php ($set = true); ?> <?php ($hello = \'hi\'); ?> <?php echo "Hello world" ?>';

$this->assertEquals($expected, $this->compiler->compileString($string));
}

public function testCompilationOfMixedUsageStatements()
{
$string = '@php (
$classes = [
\'admin-font-mono\', // Font weight
])
@endphp';
$expected = '<?php (
$classes = [
\'admin-font-mono\', // Font weight
])
?>';

$this->assertEquals($expected, $this->compiler->compileString($string));
}

public function testMultilinePhpStatementsWithParenthesesCanBeCompiled()
{
$string = <<<'BLADE'
@php (
$classes = [
'admin-font-mono'
])
@endphp

<span class="{{ implode(' ', $classes) }}">Laravel</span>
BLADE;

$expected = <<<'PHP'
<?php (
$classes = [
'admin-font-mono'
])
?>

<span class="<?php echo e(implode(' ', $classes)); ?>">Laravel</span>
PHP;

$this->assertEquals($expected, $this->compiler->compileString($string));
}

public function testMixedOfPhpStatementsCanBeCompiled()
{
$string = <<<'BLADE'
@php($total = 0)
{{-- ... --}}
<div>{{ $total }}</div>
@php
// ...
@endphp
BLADE;

$expected = <<<'PHP'
<?php ($total = 0); ?>

<div><?php echo e($total); ?></div>
<?php
// ...
?>
PHP;

$this->assertEquals($expected, $this->compiler->compileString($string));
}

public function testStringWithParenthesisWithEndPHP()
{
$string = "@php(\$data = ['related_to' => 'issue#45388'];) {{ \$data }} @endphp";
Expand Down