diff --git a/src/Illuminate/View/Compilers/Concerns/CompilesUseStatements.php b/src/Illuminate/View/Compilers/Concerns/CompilesUseStatements.php index 49a524a5fd9f..cefd5fd6eb94 100644 --- a/src/Illuminate/View/Compilers/Concerns/CompilesUseStatements.php +++ b/src/Illuminate/View/Compilers/Concerns/CompilesUseStatements.php @@ -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 ""; + $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 ""; + return ""; } } diff --git a/tests/View/Blade/BladeUseTest.php b/tests/View/Blade/BladeUseTest.php index 980a266128a6..28072b64559c 100644 --- a/tests/View/Blade/BladeUseTest.php +++ b/tests/View/Blade/BladeUseTest.php @@ -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 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 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 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 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 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 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)); + } }