Skip to content

[12.x] Fix group imports in Blade @use directive #55461

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

Merged
merged 2 commits into from
Apr 18, 2025
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
11 changes: 10 additions & 1 deletion src/Illuminate/View/Compilers/Concerns/CompilesUseStatements.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,16 @@ trait CompilesUseStatements
*/
protected function compileUse($expression)
{
$segments = explode(',', preg_replace("/[\(\)]/", '', $expression));
$expression = preg_replace('/[()]/', '', $expression);

// Preserve grouped imports as-is...
if (str_contains($expression, '{')) {
$use = ltrim(trim($expression, " '\""), '\\');

return "<?php use \\{$use}; ?>";
}

$segments = explode(',', $expression);

$use = ltrim(trim($segments[0], " '\""), '\\');
$as = isset($segments[1]) ? ' as '.trim($segments[1], " '\"") : '';
Expand Down
22 changes: 22 additions & 0 deletions tests/View/Blade/BladeUseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,26 @@ public function testUseStatementsWithBackslashAtBeginningAndAliasedAreCompiled()
$string = "Foo @use(\SomeNamespace\SomeClass, Foo) bar";
$this->assertEquals($expected, $this->compiler->compileString($string));
}

public function testUseStatementsWithBracesAreCompiledCorrectly()
{
$expected = "Foo <?php use \SomeNamespace\{Foo, Bar}; ?> bar";

$string = "Foo @use('SomeNamespace\{Foo, Bar}') bar";
$this->assertEquals($expected, $this->compiler->compileString($string));

$string = "Foo @use(SomeNamespace\{Foo, Bar}) bar";
$this->assertEquals($expected, $this->compiler->compileString($string));
}

public function testUseStatementWithBracesAndBackslashAreCompiledCorrectly()
{
$expected = "Foo <?php use \SomeNamespace\{Foo, Bar}; ?> bar";

$string = "Foo @use('\SomeNamespace\{Foo, Bar}') bar";
$this->assertEquals($expected, $this->compiler->compileString($string));

$string = "Foo @use(\SomeNamespace\{Foo, Bar}) bar";
$this->assertEquals($expected, $this->compiler->compileString($string));
}
}