Skip to content

Commit c8a10a7

Browse files
matheoWebMamba
authored andcommitted
add the ability to have static component
1 parent 21d16ea commit c8a10a7

File tree

6 files changed

+42
-2
lines changed

6 files changed

+42
-2
lines changed

src/TwigComponent/src/ComponentFactory.php

Lines changed: 25 additions & 2 deletions
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 <[email protected]>
@@ -35,6 +36,7 @@ public function __construct(
3536
private EventDispatcherInterface $eventDispatcher,
3637
private array $config,
3738
private array $classMap,
39+
private Environment $environment,
3840
) {
3941
}
4042

@@ -43,7 +45,14 @@ public function metadataFor(string $name): ComponentMetadata
4345
$name = $this->classMap[$name] ?? $name;
4446

4547
if (!$config = $this->config[$name] ?? null) {
46-
$this->throwUnknownComponentException($name);
48+
if ($this->isStaticComponent($name)) {
49+
return new ComponentMetadata([
50+
'key' => $name,
51+
'template' => $this->getTemplateFromName($name),
52+
]);
53+
}
54+
55+
throw new \InvalidArgumentException(sprintf('Unknown component "%s". The registered components are: %s', $name, implode(', ', array_keys($this->config))));
4756
}
4857

4958
return new ComponentMetadata($config);
@@ -149,7 +158,11 @@ private function getComponent(string $name): object
149158
$name = $this->classMap[$name] ?? $name;
150159

151160
if (!$this->components->has($name)) {
152-
$this->throwUnknownComponentException($name);
161+
if ($this->isStaticComponent($name)) {
162+
return new StaticComponent();
163+
}
164+
165+
throw new \InvalidArgumentException(sprintf('Unknown component "%s". The registered components are: %s', $name, implode(', ', array_keys($this->components->getProvidedServices()))));
153166
}
154167

155168
return $this->components->get($name);
@@ -189,6 +202,16 @@ private function postMount(object $component, array $data): array
189202
return $data;
190203
}
191204

205+
private function isStaticComponent(string $name): bool
206+
{
207+
return $this->environment->getLoader()->exists($this->getTemplateFromName($name));
208+
}
209+
210+
private function getTemplateFromName(string $name): string
211+
{
212+
return str_replace('.', '/', $name).'.html.twig';
213+
}
214+
192215
/**
193216
* @return never
194217
*/

src/TwigComponent/src/DependencyInjection/TwigComponentExtension.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ class_exists(AbstractArgument::class) ? new AbstractArgument(sprintf('Added in %
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)