Skip to content
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
17 changes: 14 additions & 3 deletions src/Illuminate/View/Compilers/ComponentTagCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,16 +206,27 @@ protected function componentString(string $component, array $attributes)
*/
protected function componentClass(string $component)
{
$viewFactory = Container::getInstance()->make(Factory::class);

if (isset($this->aliases[$component])) {
return $this->aliases[$component];
if (class_exists($alias = $this->aliases[$component])) {
return $alias;
}

if ($viewFactory->exists($alias)) {
return $alias;
}

throw new InvalidArgumentException(
"Unable to locate class or view [{$alias}] for component [{$component}]."
);
}

if (class_exists($class = $this->guessClassName($component))) {
return $class;
}

if (Container::getInstance()->make(Factory::class)
->exists($view = "components.{$component}")) {
if ($viewFactory->exists($view = "components.{$component}")) {
return $view;
}

Expand Down
41 changes: 41 additions & 0 deletions tests/View/Blade/BladeComponentTagCompilerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\View\Compilers\BladeCompiler;
use Illuminate\View\Compilers\ComponentTagCompiler;
use Illuminate\View\Component;
use InvalidArgumentException;
use Mockery;

class BladeComponentTagCompilerTest extends AbstractBladeTestCase
Expand All @@ -27,6 +28,8 @@ public function testSlotsCanBeCompiled()

public function testBasicComponentParsing()
{
$this->mockViewFactory();

$result = (new ComponentTagCompiler(['alert' => TestAlertComponent::class]))->compileTags('<div><x-alert type="foo" limit="5" @click="foo" required /><x-alert /></div>');

$this->assertSame("<div> @component('Illuminate\Tests\View\Blade\TestAlertComponent', [])
Expand Down Expand Up @@ -124,6 +127,8 @@ public function testClassNamesCanBeGuessedWithNamespaces()

public function testComponentsCanBeCompiledWithHyphenAttributes()
{
$this->mockViewFactory();

$result = (new ComponentTagCompiler(['alert' => TestAlertComponent::class]))->compileTags('<x-alert class="bar" wire:model="foo" x-on:click="bar" @click="baz" />');

$this->assertSame("@component('Illuminate\Tests\View\Blade\TestAlertComponent', [])
Expand Down Expand Up @@ -206,6 +211,42 @@ public function __toString()
$this->assertEquals(1, BladeCompiler::sanitizeComponentAttribute(1));
$this->assertEquals(e('<hi>'), BladeCompiler::sanitizeComponentAttribute($class));
}

public function testItThrowsAnExceptionForNonExistingAliases()
{
$container = new Container;
$container->instance(Application::class, $app = Mockery::mock(Application::class));
$container->instance(Factory::class, $factory = Mockery::mock(Factory::class));
$app->shouldReceive('getNamespace')->andReturn('App\\');
$factory->shouldReceive('exists')->andReturn(false);
Container::setInstance($container);

$this->expectException(InvalidArgumentException::class);

(new ComponentTagCompiler(['alert' => 'foo.bar']))->compileTags('<x-alert />');
}

public function testItThrowsAnExceptionForNonExistingClass()
{
$container = new Container;
$container->instance(Application::class, $app = Mockery::mock(Application::class));
$container->instance(Factory::class, $factory = Mockery::mock(Factory::class));
$app->shouldReceive('getNamespace')->andReturn('App\\');
$factory->shouldReceive('exists')->andReturn(false);
Container::setInstance($container);

$this->expectException(InvalidArgumentException::class);

(new ComponentTagCompiler)->compileTags('<x-alert />');
}

protected function mockViewFactory($existsSucceeds = true)
{
$container = new Container;
$container->instance(Factory::class, $factory = Mockery::mock(Factory::class));
$factory->shouldReceive('exists')->andReturn($existsSucceeds);
Container::setInstance($container);
}
}

class TestAlertComponent extends Component
Expand Down