diff --git a/src/Illuminate/View/Compilers/BladeCompiler.php b/src/Illuminate/View/Compilers/BladeCompiler.php index 05fa9dc81d24..1bcf887a367e 100644 --- a/src/Illuminate/View/Compilers/BladeCompiler.php +++ b/src/Illuminate/View/Compilers/BladeCompiler.php @@ -4,6 +4,7 @@ use Illuminate\Support\Arr; use Illuminate\Support\Str; +use Illuminate\Support\Facades\View; class BladeCompiler extends Compiler implements CompilerInterface { @@ -435,6 +436,8 @@ public function component($path, $alias = null) { $alias = $alias ?: array_last(explode('.', $path)); + View::setComponentAlias($path, $alias); + $this->directive($alias, function ($expression) use ($path) { return $expression ? "startComponent('{$path}', {$expression}); ?>" diff --git a/src/Illuminate/View/Concerns/ManagesComponents.php b/src/Illuminate/View/Concerns/ManagesComponents.php index d523f7e8a26a..2c8cf756fa76 100644 --- a/src/Illuminate/View/Concerns/ManagesComponents.php +++ b/src/Illuminate/View/Concerns/ManagesComponents.php @@ -34,6 +34,23 @@ trait ManagesComponents */ protected $slotStack = []; + /** + * The component aliases. + */ + protected $componentAliases = []; + + /** + * Set a component alias. + * + * @param string $path + * @param string $alias + * @return void + */ + public function setComponentAlias($path, $alias) + { + $this->componentAliases[$alias] = $path; + } + /** * Start a component rendering process. * @@ -44,7 +61,7 @@ trait ManagesComponents public function startComponent($name, array $data = []) { if (ob_start()) { - $this->componentStack[] = $name; + $this->componentStack[] = $this->componentAliases[$name] ?? $name; $this->componentData[$this->currentComponent()] = $data; diff --git a/tests/View/Blade/AbstractBladeTestCase.php b/tests/View/Blade/AbstractBladeTestCase.php index 9bd29f53ffed..530fca39280e 100644 --- a/tests/View/Blade/AbstractBladeTestCase.php +++ b/tests/View/Blade/AbstractBladeTestCase.php @@ -4,15 +4,22 @@ use Mockery as m; use PHPUnit\Framework\TestCase; +use Illuminate\Support\Facades\View; use Illuminate\View\Compilers\BladeCompiler; abstract class AbstractBladeTestCase extends TestCase { protected $compiler; + protected $viewFactory; public function setUp() { $this->compiler = new BladeCompiler(m::mock('Illuminate\Filesystem\Filesystem'), __DIR__); + + $this->viewFactory = m::mock('Illuminate\View\Factory'); + + View::swap($this->viewFactory); + parent::setUp(); } diff --git a/tests/View/Blade/BladeCustomTest.php b/tests/View/Blade/BladeCustomTest.php index 2c9a8e54c2d1..e39bf6842731 100644 --- a/tests/View/Blade/BladeCustomTest.php +++ b/tests/View/Blade/BladeCustomTest.php @@ -93,6 +93,8 @@ public function testCustomIfElseConditions() public function testCustomComponents() { + $this->viewFactory->shouldReceive('setComponentAlias')->with('app.components.alert', 'alert'); + $this->compiler->component('app.components.alert', 'alert'); $string = '@alert @@ -104,6 +106,8 @@ public function testCustomComponents() public function testCustomComponentsWithSlots() { + $this->viewFactory->shouldReceive('setComponentAlias')->with('app.components.alert', 'alert'); + $this->compiler->component('app.components.alert', 'alert'); $string = '@alert([\'type\' => \'danger\']) @@ -115,6 +119,8 @@ public function testCustomComponentsWithSlots() public function testCustomComponentsDefaultAlias() { + $this->viewFactory->shouldReceive('setComponentAlias')->with('app.components.alert', 'alert'); + $this->compiler->component('app.components.alert'); $string = '@alert diff --git a/tests/View/ViewFactoryTest.php b/tests/View/ViewFactoryTest.php index efbccf85d3ea..bdf63a84c6ea 100755 --- a/tests/View/ViewFactoryTest.php +++ b/tests/View/ViewFactoryTest.php @@ -336,6 +336,23 @@ public function testComponentHandling() $this->assertEquals('title
component Taylor laravel.com', $contents); } + public function testComponentHandlingWithAlias() + { + $factory = $this->getFactory(); + $factory->getFinder()->shouldReceive('find')->with('component-path')->andReturn(__DIR__.'/fixtures/component.php'); + $factory->getEngineResolver()->shouldReceive('resolve')->andReturn(new \Illuminate\View\Engines\PhpEngine); + $factory->getDispatcher()->shouldReceive('dispatch'); + $factory->setComponentAlias('component-path', 'component-alias'); + $factory->startComponent('component-alias', ['name' => 'Taylor']); + $factory->slot('title'); + $factory->slot('website', 'laravel.com'); + echo 'title
'; + $factory->endSlot(); + echo 'component'; + $contents = $factory->renderComponent(); + $this->assertEquals('title
component Taylor laravel.com', $contents); + } + public function testTranslation() { $container = new \Illuminate\Container\Container;