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 Blade @use directive #49179

Merged
merged 3 commits into from
Nov 30, 2023
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
1 change: 1 addition & 0 deletions src/Illuminate/View/Compilers/BladeCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class BladeCompiler extends Compiler implements CompilerInterface
Concerns\CompilesStacks,
Concerns\CompilesStyles,
Concerns\CompilesTranslations,
Concerns\CompilesUseStatements,
ReflectsClosures;

/**
Expand Down
22 changes: 22 additions & 0 deletions src/Illuminate/View/Compilers/Concerns/CompilesUseStatements.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Illuminate\View\Compilers\Concerns;

trait CompilesUseStatements
{
/**
* Compile the use statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileUse($expression)
{
$segments = explode(',', preg_replace("/[\(\)]/", '', $expression));

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

return "<?php use \\{$use}{$as}; ?>";
}
}
20 changes: 20 additions & 0 deletions tests/View/Blade/BladeUseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Illuminate\Tests\View\Blade;

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

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