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

[5.6] Allow the use of component('alias') as mentioned in the documentation #23096

Closed
wants to merge 3 commits 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
3 changes: 3 additions & 0 deletions src/Illuminate/View/Compilers/BladeCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\View;

class BladeCompiler extends Compiler implements CompilerInterface
{
Expand Down Expand Up @@ -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
? "<?php \$__env->startComponent('{$path}', {$expression}); ?>"
Expand Down
19 changes: 18 additions & 1 deletion src/Illuminate/View/Concerns/ManagesComponents.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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;

Expand Down
7 changes: 7 additions & 0 deletions tests/View/Blade/AbstractBladeTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
6 changes: 6 additions & 0 deletions tests/View/Blade/BladeCustomTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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\'])
Expand All @@ -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
Expand Down
17 changes: 17 additions & 0 deletions tests/View/ViewFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,23 @@ public function testComponentHandling()
$this->assertEquals('title<hr> 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<hr>';
$factory->endSlot();
echo 'component';
$contents = $factory->renderComponent();
$this->assertEquals('title<hr> component Taylor laravel.com', $contents);
}

public function testTranslation()
{
$container = new \Illuminate\Container\Container;
Expand Down