Skip to content

Commit 22d4530

Browse files
authored
[10.x] Fix blade failing to compile when mixing inline/block @php directives (#48420)
* Fix mixed inline/block php blade bug * added tests
1 parent bbf0b4c commit 22d4530

File tree

2 files changed

+126
-1
lines changed

2 files changed

+126
-1
lines changed

src/Illuminate/View/Compilers/BladeCompiler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ protected function storeVerbatimBlocks($value)
401401
*/
402402
protected function storePhpBlocks($value)
403403
{
404-
return preg_replace_callback('/(?<!@)@php(.*?)@endphp/s', function ($matches) {
404+
return preg_replace_callback('/(?<!@)@php((?:.(?!(?<!@)@php))*?)@endphp/s', function ($matches) {
405405
return $this->storeRawBlock("<?php{$matches[1]}?>");
406406
}, $value);
407407
}

tests/View/Blade/BladePhpStatementsTest.php

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,131 @@ public function testPhpStatementsWithExpressionAreCompiled()
1111
$this->assertEquals($expected, $this->compiler->compileString($string));
1212
}
1313

14+
public function testMixedPhpStatementsAreCompiled()
15+
{
16+
$string = <<<'BLADE'
17+
@php($set = true)
18+
@php($set = true) @php ($set = false;) @endphp
19+
@php ($set = true) @php($set = false;)@endphp
20+
<div>{{ $set }}</div>
21+
@php
22+
$set = false;
23+
@endphp
24+
@php (
25+
$set = false;
26+
)@endphp
27+
@php(
28+
$set = false;
29+
)@endphp
30+
@php (
31+
$set = false
32+
)
33+
@php(
34+
$set = false
35+
)
36+
@php
37+
$set = '@@php';
38+
@endphp
39+
BLADE;
40+
$expected = <<<'PHP'
41+
<?php ($set = true); ?>
42+
<?php ($set = true); ?> <?php ($set = false;) ?>
43+
<?php ($set = true); ?> <?php($set = false;)?>
44+
<div><?php echo e($set); ?></div>
45+
<?php
46+
$set = false;
47+
?>
48+
<?php (
49+
$set = false;
50+
)?>
51+
<?php(
52+
$set = false;
53+
)?>
54+
<?php (
55+
$set = false
56+
); ?>
57+
<?php (
58+
$set = false
59+
); ?>
60+
<?php
61+
$set = '@@php';
62+
?>
63+
PHP;
64+
$this->assertEquals($expected, $this->compiler->compileString($string));
65+
}
66+
67+
public function testCompilationOfMixedPhpStatements()
68+
{
69+
$string = '@php($set = true) @php ($hello = \'hi\') @php echo "Hello world" @endphp';
70+
$expected = '<?php ($set = true); ?> <?php ($hello = \'hi\'); ?> <?php echo "Hello world" ?>';
71+
72+
$this->assertEquals($expected, $this->compiler->compileString($string));
73+
}
74+
75+
public function testCompilationOfMixedUsageStatements()
76+
{
77+
$string = '@php (
78+
$classes = [
79+
\'admin-font-mono\', // Font weight
80+
])
81+
@endphp';
82+
$expected = '<?php (
83+
$classes = [
84+
\'admin-font-mono\', // Font weight
85+
])
86+
?>';
87+
88+
$this->assertEquals($expected, $this->compiler->compileString($string));
89+
}
90+
91+
public function testMultilinePhpStatementsWithParenthesesCanBeCompiled()
92+
{
93+
$string = <<<'BLADE'
94+
@php (
95+
$classes = [
96+
'admin-font-mono'
97+
])
98+
@endphp
99+
100+
<span class="{{ implode(' ', $classes) }}">Laravel</span>
101+
BLADE;
102+
103+
$expected = <<<'PHP'
104+
<?php (
105+
$classes = [
106+
'admin-font-mono'
107+
])
108+
?>
109+
110+
<span class="<?php echo e(implode(' ', $classes)); ?>">Laravel</span>
111+
PHP;
112+
113+
$this->assertEquals($expected, $this->compiler->compileString($string));
114+
}
115+
116+
public function testMixedOfPhpStatementsCanBeCompiled()
117+
{
118+
$string = <<<'BLADE'
119+
@php($total = 0)
120+
{{-- ... --}}
121+
<div>{{ $total }}</div>
122+
@php
123+
// ...
124+
@endphp
125+
BLADE;
126+
127+
$expected = <<<'PHP'
128+
<?php ($total = 0); ?>
129+
130+
<div><?php echo e($total); ?></div>
131+
<?php
132+
// ...
133+
?>
134+
PHP;
135+
136+
$this->assertEquals($expected, $this->compiler->compileString($string));
137+
}
138+
14139
public function testStringWithParenthesisWithEndPHP()
15140
{
16141
$string = "@php(\$data = ['related_to' => 'issue#45388'];) {{ \$data }} @endphp";

0 commit comments

Comments
 (0)