Skip to content

Commit

Permalink
Merge pull request #2 from Stillat/adds-support-for-anonymous-blade-c…
Browse files Browse the repository at this point in the history
…omponents

Adds support for anonymous Blade components
  • Loading branch information
JohnathonKoster authored Mar 6, 2023
2 parents 6ddf57a + 841f83c commit 1d056ea
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/Tags/BladeHost.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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()));
Expand Down
19 changes: 19 additions & 0 deletions tests/BladeComponentsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,25 @@ public function testBladeComponentsWithSlots()
Footer
</footer>
</div>
EXP;

$this->assertSame($expected, $this->renderString($template));
}

public function testRenderingAnonymousComponents()
{
$template = <<<'EOT'
<x-button />
<x-input />
<x-input type="password" />
EOT;

$expected = <<<'EXP'
<div>
A Blade Button!
</div>
<input type="text" />
<input type="password" />
EXP;

$this->assertSame($expected, $this->renderString($template));
Expand Down
3 changes: 3 additions & 0 deletions tests/__fixtures__/views/components/button.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div>
A Blade Button!
</div>
5 changes: 5 additions & 0 deletions tests/__fixtures__/views/components/input.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@props([
'type' => 'text',
])

<input type="{{ $type }}" />

0 comments on commit 1d056ea

Please sign in to comment.