Skip to content

Commit

Permalink
Fixes #45424 issue
Browse files Browse the repository at this point in the history
  • Loading branch information
imanghafoori1 committed Jan 3, 2023
1 parent 2b16766 commit e25a2f5
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 11 deletions.
25 changes: 20 additions & 5 deletions src/Illuminate/View/Compilers/BladeCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -504,11 +504,26 @@ protected function compileExtensions($value)
*/
protected function compileStatements($value)
{
return preg_replace_callback(
'/\B@(@?\w+(?:::\w+)?)([ \t]*)(\( ( (?>[^()]+) | (?3) )* \))?/x', function ($match) {
return $this->compileStatement($match);
}, $value
);
preg_match_all('/\B@(@?\w+(?:::\w+)?)([ \t]*)(\( ( (?>[^()]+) | (?3) )* \))?/x', $value, $matches);
for ($i = 0; isset($matches[0][$i]); $i++) {
$match = [
$matches[0][$i],
$matches[1][$i],
$matches[2][$i],
$matches[3][$i] ?: null,
$matches[4][$i] ?: null,
];
while (isset($match[4]) && Str::endsWith($match[0], ')') && Arr::last(token_get_all('<?php '.$match[0])) !== ')') {
$rest = Str::before(Str::after($value, $match[0]), ')');
$match[0] = $match[0].$rest.')';
$match[3] = $match[3].$rest.')';
$match[4] = $match[4].$rest;
}

$value = Str::replaceFirst($match[0], $this->compileStatement($match), $value);
}

return $value;
}

/**
Expand Down
3 changes: 2 additions & 1 deletion tests/View/Blade/BladeIncludesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public function testEachsAreCompiled()
public function testIncludesAreCompiled()
{
$this->assertSame('<?php echo $__env->make(\'foo\', \Illuminate\Support\Arr::except(get_defined_vars(), [\'__data\', \'__path\']))->render(); ?>', $this->compiler->compileString('@include(\'foo\')'));
$this->assertSame('<?php echo $__env->make(\'foo\', [\'a\' => \'a\'], \Illuminate\Support\Arr::except(get_defined_vars(), [\'__data\', \'__path\']))->render(); ?>', $this->compiler->compileString('@include(\'foo\', [\'a\' => \'a\'])'));
$this->assertSame('<?php echo $__env->make(name(foo), \Illuminate\Support\Arr::except(get_defined_vars(), [\'__data\', \'__path\']))->render(); ?>', $this->compiler->compileString('@include(name(foo))'));
}

Expand All @@ -37,6 +38,6 @@ public function testIncludeUnlessesAreCompiled()
public function testIncludeFirstsAreCompiled()
{
$this->assertSame('<?php echo $__env->first(["one", "two"], \Illuminate\Support\Arr::except(get_defined_vars(), [\'__data\', \'__path\']))->render(); ?>', $this->compiler->compileString('@includeFirst(["one", "two"])'));
$this->assertSame('<?php echo $__env->first(["one", "two"], ["foo" => "bar"], \Illuminate\Support\Arr::except(get_defined_vars(), [\'__data\', \'__path\']))->render(); ?>', $this->compiler->compileString('@includeFirst(["one", "two"], ["foo" => "bar"])'));
$this->assertSame('<?php echo $__env->first(["issue", "#45424)"], ["foo()" => "bar)"], \Illuminate\Support\Arr::except(get_defined_vars(), [\'__data\', \'__path\']))->render(); ?>', $this->compiler->compileString('@includeFirst(["issue", "#45424)"], ["foo()" => "bar)"])'));
}
}
16 changes: 11 additions & 5 deletions tests/View/Blade/BladePhpStatementsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,21 @@ public function testVerbatimAndPhpStatementsDontGetMixedUp()
$this->assertEquals($expected, $this->compiler->compileString($string));
}

public function testStringWithParenthesisCannotBeCompiled()
public function testStringWithParenthesisCanBeCompiled()
{
$string = "@php(\$data = ['test' => ')'])";
$string = "@php(\$data = ['single' => ')'])";
$expected = "<?php (\$data = ['single' => ')']); ?>";

$expected = "<?php (\$data = ['test' => ')']); ?>";
$this->assertEquals($expected, $this->compiler->compileString($string));

$string = "@php(\$data = ['(multiple)-))' => '((-))'])";
$expected = "<?php (\$data = ['(multiple)-))' => '((-))']); ?>";

$actual = "<?php (\$data = ['test' => '); ?>'])";
$this->assertEquals($expected, $this->compiler->compileString($string));

$this->assertEquals($actual, $this->compiler->compileString($string));
$this->assertSame('<?php echo $__env->renderEach(\'foo\', \'b)a)r\'); ?>', $this->compiler->compileString('@each(\'foo\', \'b)a)r\')'));
$this->assertSame('<?php echo $__env->make(\'test_for\', [\'issue))\' => \'(issue#45424))\'], \Illuminate\Support\Arr::except(get_defined_vars(), [\'__data\', \'__path\']))->render(); ?>', $this->compiler->compileString('@include(\'test_for\', [\'issue))\' => \'(issue#45424))\'])'));
$this->assertSame('( <?php echo $__env->make(\'test_for\', [\'not_too_much))\' => \'(issue#45424))\'], \Illuminate\Support\Arr::except(get_defined_vars(), [\'__data\', \'__path\']))->render(); ?>))', $this->compiler->compileString('( @include(\'test_for\', [\'not_too_much))\' => \'(issue#45424))\'])))'));
}

public function testStringWithEmptyStringDataValue()
Expand Down

0 comments on commit e25a2f5

Please sign in to comment.