Skip to content
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

[10.x] Add support for array-based use statements in Blade templates #49481

Closed
wants to merge 1 commit into from
Closed
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
22 changes: 18 additions & 4 deletions src/Illuminate/View/Compilers/Concerns/CompilesUseStatements.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,25 @@ trait CompilesUseStatements
*/
protected function compileUse($expression)
{
$segments = explode(',', preg_replace("/[\(\)]/", '', $expression));
$expression = preg_replace("/[\(\)]/", '', $expression);

$use = ltrim(trim($segments[0], " '\""), '\\');
$as = isset($segments[1]) ? ' as '.trim($segments[1], " '\"") : '';
// if it is not start with '[' therefore it is single namespace
// so we need to convert it to $namespace => $alias expression
if (! str_starts_with($expression, '[')) {
$expression = str_replace(',', '=>', $expression);
}

return "<?php use \\{$use}{$as}; ?>";
// it is start with '[' therefore it is array and it may have multiple namespaces
// as it won't be valid json we need to parse it manually and get namespaces and aliases
// below code is to get namespaces and aliases from [$namespace => $alias, ...] expression
$namespaces = explode(',', trim(preg_replace('/\\\\/', '\\', $expression), '[]'));

$useStatements = '<?php';
foreach ($namespaces as $namespace) {
[$use, $as] = array_pad(explode('=>', $namespace, 2), 2, '');
$useStatements .= ' use \\'.ltrim(trim($use, " '\""), '\\').($as ? ' as '.ltrim(trim($as, " '\""), '\\') : '').';';
}

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

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

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

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

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