Skip to content

[12.x] Improve @use directive to support function and const modifiers #55583

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 4 commits into from
Apr 29, 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
31 changes: 23 additions & 8 deletions src/Illuminate/View/Compilers/Concerns/CompilesUseStatements.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,35 @@ trait CompilesUseStatements
*/
protected function compileUse($expression)
{
$expression = preg_replace('/[()]/', '', $expression);
$expression = trim(preg_replace('/[()]/', '', $expression), " '\"");

// Preserve grouped imports as-is...
// Isolate alias...
if (str_contains($expression, '{')) {
$use = ltrim(trim($expression, " '\""), '\\');
$pathWithOptionalModifier = $expression;
$aliasWithLeadingSpace = '';
} else {
$segments = explode(',', $expression);
$pathWithOptionalModifier = trim($segments[0], " '\"");

return "<?php use \\{$use}; ?>";
$aliasWithLeadingSpace = isset($segments[1])
? ' as '.trim($segments[1], " '\"")
: '';
}

$segments = explode(',', $expression);
// Split modifier and path...
if (str_starts_with($pathWithOptionalModifier, 'function ')) {
$modifierWithTrailingSpace = 'function ';
$path = explode(' ', $pathWithOptionalModifier, 2)[1] ?? $pathWithOptionalModifier;
} elseif (str_starts_with($pathWithOptionalModifier, 'const ')) {
$modifierWithTrailingSpace = 'const ';
$path = explode(' ', $pathWithOptionalModifier, 2)[1] ?? $pathWithOptionalModifier;
} else {
$modifierWithTrailingSpace = '';
$path = $pathWithOptionalModifier;
}

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

return "<?php use \\{$use}{$as}; ?>";
return "<?php use {$modifierWithTrailingSpace}\\{$path}{$aliasWithLeadingSpace}; ?>";
}
}
66 changes: 66 additions & 0 deletions tests/View/Blade/BladeUseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,70 @@ public function testUseStatementWithBracesAndBackslashAreCompiledCorrectly()
$string = "Foo @use(\SomeNamespace\{Foo, Bar}) bar";
$this->assertEquals($expected, $this->compiler->compileString($string));
}

public function testUseStatementsWithModifiersAreCompiled()
{
$expected = 'Foo <?php use function \SomeNamespace\SomeFunction as Foo; ?> bar';

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

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

public function testUseStatementsWithModifiersWithoutAliasAreCompiled()
{
$expected = 'Foo <?php use const \SomeNamespace\SOME_CONST; ?> bar';

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

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

public function testUseStatementsWithModifiersAndBackslashAtBeginningAreCompiled()
{
$expected = 'Foo <?php use function \SomeNamespace\SomeFunction; ?> bar';

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

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

public function testUseStatementsWithModifiersBackslashAtBeginningAndAliasedAreCompiled()
{
$expected = 'Foo <?php use const \SomeNamespace\SOME_CONST as Foo; ?> bar';

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

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

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

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

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

public function testUseFunctionStatementWithBracesAndBackslashAreCompiledCorrectly()
{
$expected = 'Foo <?php use const \SomeNamespace\{FOO, BAR}; ?> bar';

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

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