From 22d4530c2fbe815223eba7fe1fc1005d1f4d6cce Mon Sep 17 00:00:00 2001 From: CalebW Date: Wed, 20 Sep 2023 10:54:58 -0500 Subject: [PATCH] [10.x] Fix blade failing to compile when mixing inline/block @php directives (#48420) * Fix mixed inline/block php blade bug * added tests --- .../View/Compilers/BladeCompiler.php | 2 +- tests/View/Blade/BladePhpStatementsTest.php | 125 ++++++++++++++++++ 2 files changed, 126 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/View/Compilers/BladeCompiler.php b/src/Illuminate/View/Compilers/BladeCompiler.php index 62005da34610..aec1b00425c8 100644 --- a/src/Illuminate/View/Compilers/BladeCompiler.php +++ b/src/Illuminate/View/Compilers/BladeCompiler.php @@ -401,7 +401,7 @@ protected function storeVerbatimBlocks($value) */ protected function storePhpBlocks($value) { - return preg_replace_callback('/(?storeRawBlock(""); }, $value); } diff --git a/tests/View/Blade/BladePhpStatementsTest.php b/tests/View/Blade/BladePhpStatementsTest.php index 21a7d9deab8e..b90cc74a12a2 100644 --- a/tests/View/Blade/BladePhpStatementsTest.php +++ b/tests/View/Blade/BladePhpStatementsTest.php @@ -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 +
{{ $set }}
+ @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; + $this->assertEquals($expected, $this->compiler->compileString($string)); + } + + public function testCompilationOfMixedPhpStatements() + { + $string = '@php($set = true) @php ($hello = \'hi\') @php echo "Hello world" @endphp'; + $expected = ' '; + + $this->assertEquals($expected, $this->compiler->compileString($string)); + } + + public function testCompilationOfMixedUsageStatements() + { + $string = '@php ( + $classes = [ + \'admin-font-mono\', // Font weight + ]) + @endphp'; + $expected = ''; + + $this->assertEquals($expected, $this->compiler->compileString($string)); + } + + public function testMultilinePhpStatementsWithParenthesesCanBeCompiled() + { + $string = <<<'BLADE' + @php ( + $classes = [ + 'admin-font-mono' + ]) + @endphp + + Laravel + BLADE; + + $expected = <<<'PHP' + + + Laravel + PHP; + + $this->assertEquals($expected, $this->compiler->compileString($string)); + } + + public function testMixedOfPhpStatementsCanBeCompiled() + { + $string = <<<'BLADE' + @php($total = 0) + {{-- ... --}} +
{{ $total }}
+ @php + // ... + @endphp + BLADE; + + $expected = <<<'PHP' + + +
+ + PHP; + + $this->assertEquals($expected, $this->compiler->compileString($string)); + } + public function testStringWithParenthesisWithEndPHP() { $string = "@php(\$data = ['related_to' => 'issue#45388'];) {{ \$data }} @endphp";