diff --git a/src/Tags/BladeHost.php b/src/Tags/BladeHost.php index 476a6e1..d3f08e4 100644 --- a/src/Tags/BladeHost.php +++ b/src/Tags/BladeHost.php @@ -2,6 +2,7 @@ namespace Stillat\AntlersComponents\Tags; +use Illuminate\View\AnonymousComponent; use Illuminate\View\Compilers\BladeCompiler; use Illuminate\View\Compilers\ComponentTagCompiler; use Illuminate\View\ComponentAttributeBag; @@ -27,18 +28,31 @@ public function component(): string $componentTagCompiler = $this->makeComponentTagCompiler(); $componentName = $this->params->get('component'); $className = $componentTagCompiler->componentClass($componentName); + $attributes = new ComponentAttributeBag($this->params->except('component')->all()); $constructorParameters = []; $scopeData = $this->context->all(); $scopeData = array_merge($scopeData, $this->params->except('component')->all()); + $isAnonymous = false; + $anonymousViewName = $className; + + if (! class_exists($className)) { + $isAnonymous = true; + $className = AnonymousComponent::class; + } + if ($constructor = (new ReflectionClass($className))->getConstructor()) { $constructorParameters = collect($constructor->getParameters())->map->getName()->all(); $attributes = $attributes->except($constructorParameters); $constructorParameters = collect($scopeData)->only($constructorParameters)->all(); } + if ($isAnonymous) { + $constructorParameters = array_merge($constructorParameters, ['view' => $anonymousViewName, 'data' => []]); + } + $__env = $this->context['__env'] ?? view(); $component = $className::resolve($constructorParameters + ((array) $attributes->getIterator())); diff --git a/tests/BladeComponentsTest.php b/tests/BladeComponentsTest.php index dbae208..e8c4e80 100644 --- a/tests/BladeComponentsTest.php +++ b/tests/BladeComponentsTest.php @@ -62,6 +62,25 @@ public function testBladeComponentsWithSlots() Footer +EXP; + + $this->assertSame($expected, $this->renderString($template)); + } + + public function testRenderingAnonymousComponents() + { + $template = <<<'EOT' + + + +EOT; + + $expected = <<<'EXP' +
+ A Blade Button! +
+ + EXP; $this->assertSame($expected, $this->renderString($template)); diff --git a/tests/__fixtures__/views/components/button.blade.php b/tests/__fixtures__/views/components/button.blade.php new file mode 100644 index 0000000..f0729ff --- /dev/null +++ b/tests/__fixtures__/views/components/button.blade.php @@ -0,0 +1,3 @@ +
+ A Blade Button! +
\ No newline at end of file diff --git a/tests/__fixtures__/views/components/input.blade.php b/tests/__fixtures__/views/components/input.blade.php new file mode 100644 index 0000000..4935493 --- /dev/null +++ b/tests/__fixtures__/views/components/input.blade.php @@ -0,0 +1,5 @@ +@props([ + 'type' => 'text', +]) + + \ No newline at end of file