Skip to content

Commit

Permalink
[10.x] Fix use statement compilation in Blade templates (#49479)
Browse files Browse the repository at this point in the history
* Fix use statement compilation in Blade templates

* added one more failing test

* fix

* fix typo
  • Loading branch information
MrPunyapal authored Dec 23, 2023
1 parent 8837c1f commit 90ed27d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ protected function compileUse($expression)
{
$segments = explode(',', preg_replace("/[\(\)]/", '', $expression));

$use = trim($segments[0], " '\"");
$use = ltrim(trim($segments[0], " '\""), '\\');
$as = isset($segments[1]) ? ' as '.trim($segments[1], " '\"") : '';

return "<?php use \\{$use}{$as}; ?>";
Expand Down
14 changes: 14 additions & 0 deletions tests/View/Blade/BladeUseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,18 @@ public function testUseStatementsWithoutAsAreCompiled()
$expected = "Foo <?php use \SomeNamespace\SomeClass; ?> bar";
$this->assertEquals($expected, $this->compiler->compileString($string));
}

public function testUseStatementsWithBackslashAtBeginningAreCompiled()
{
$string = "Foo @use('\SomeNamespace\SomeClass') bar";
$expected = "Foo <?php use \SomeNamespace\SomeClass; ?> bar";
$this->assertEquals($expected, $this->compiler->compileString($string));
}

public function testUseStatementsWithBackslashAtBeginningAndAliasedAreCompiled()
{
$string = "Foo @use('\SomeNamespace\SomeClass', 'Foo') bar";
$expected = "Foo <?php use \SomeNamespace\SomeClass as Foo; ?> bar";
$this->assertEquals($expected, $this->compiler->compileString($string));
}
}

0 comments on commit 90ed27d

Please sign in to comment.