Skip to content

Commit a3cf1f9

Browse files
author
matheo
committed
add the ability to have static component
1 parent 8eb4e08 commit a3cf1f9

File tree

6 files changed

+41
-1
lines changed

6 files changed

+41
-1
lines changed

src/TwigComponent/src/ComponentFactory.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;
1818
use Symfony\UX\TwigComponent\Event\PostMountEvent;
1919
use Symfony\UX\TwigComponent\Event\PreMountEvent;
20+
use Twig\Environment;
2021

2122
/**
2223
* @author Kevin Bond <kevinbond@gmail.com>
@@ -32,13 +33,21 @@ public function __construct(
3233
private ServiceLocator $components,
3334
private PropertyAccessorInterface $propertyAccessor,
3435
private EventDispatcherInterface $eventDispatcher,
35-
private array $config
36+
private array $config,
37+
private Environment $environment,
3638
) {
3739
}
3840

3941
public function metadataFor(string $name): ComponentMetadata
4042
{
4143
if (!$config = $this->config[$name] ?? null) {
44+
if ($this->isStaticComponent($name)) {
45+
return new ComponentMetadata([
46+
'key' => $name,
47+
'template' => $this->getTemplateFromName($name),
48+
]);
49+
}
50+
4251
throw new \InvalidArgumentException(sprintf('Unknown component "%s". The registered components are: %s', $name, implode(', ', array_keys($this->config))));
4352
}
4453

@@ -143,6 +152,10 @@ private function mount(object $component, array &$data): void
143152
private function getComponent(string $name): object
144153
{
145154
if (!$this->components->has($name)) {
155+
if ($this->isStaticComponent($name)) {
156+
return new StaticComponent();
157+
}
158+
146159
throw new \InvalidArgumentException(sprintf('Unknown component "%s". The registered components are: %s', $name, implode(', ', array_keys($this->components->getProvidedServices()))));
147160
}
148161

@@ -182,4 +195,14 @@ private function postMount(object $component, array $data): array
182195

183196
return $data;
184197
}
198+
199+
private function isStaticComponent(string $name): bool
200+
{
201+
return $this->environment->getLoader()->exists($this->getTemplateFromName($name));
202+
}
203+
204+
private function getTemplateFromName(string $name): string
205+
{
206+
return str_replace('.', '/', $name).'.html.twig';
207+
}
185208
}

src/TwigComponent/src/DependencyInjection/TwigComponentExtension.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ static function (ChildDefinition $definition, AsTwigComponent $attribute) {
5353
new Reference('property_accessor'),
5454
new Reference('event_dispatcher'),
5555
class_exists(AbstractArgument::class) ? new AbstractArgument(sprintf('Added in %s.', TwigComponentPass::class)) : [],
56+
new Reference('twig'),
5657
])
5758
;
5859

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Symfony\UX\TwigComponent;
4+
5+
class StaticComponent
6+
{
7+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{{ component('static.button') }}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
I am static

src/TwigComponent/tests/Integration/ComponentExtensionTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,13 @@ public function testCanRenderEmbeddedComponent(): void
151151
$this->assertStringContainsString('custom td (1)', $output);
152152
}
153153

154+
public function testCanRenderStaticComponent(): void
155+
{
156+
$output = self::getContainer()->get(Environment::class)->render('render_static_component.html.twig');
157+
158+
$this->assertStringContainsString('I am static', $output);
159+
}
160+
154161
public function testComponentWithNamespace(): void
155162
{
156163
$output = $this->renderComponent('foo:bar:baz');

0 commit comments

Comments
 (0)