Skip to content

Commit 056d5e8

Browse files
committed
[Twig] prepare for embedded components
1 parent 4262565 commit 056d5e8

File tree

4 files changed

+33
-48
lines changed

4 files changed

+33
-48
lines changed

src/TwigComponent/src/ComponentRenderer.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,19 @@ public function __construct(
3737
) {
3838
}
3939

40+
public function createAndRender(string $name, array $props = []): string
41+
{
42+
return $this->render($this->factory->create($name, $props));
43+
}
44+
4045
public function render(MountedComponent $mounted): string
46+
{
47+
$event = $this->preRender($mounted);
48+
49+
return $this->twig->render($event->getTemplate(), $event->getVariables());
50+
}
51+
52+
private function preRender(MountedComponent $mounted): PreRenderEvent
4153
{
4254
if (!$this->safeClassesRegistered) {
4355
$this->twig->getExtension(EscaperExtension::class)->addSafeClass(ComponentAttributes::class, ['html']);
@@ -64,7 +76,7 @@ public function render(MountedComponent $mounted): string
6476

6577
$this->dispatcher->dispatch($event);
6678

67-
return $this->twig->render($event->getTemplate(), $event->getVariables());
79+
return $event;
6880
}
6981

7082
private function exposedVariables(object $component, bool $exposePublicProps): \Iterator

src/TwigComponent/src/DependencyInjection/TwigComponentExtension.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
use Symfony\UX\TwigComponent\ComponentRenderer;
2525
use Symfony\UX\TwigComponent\DependencyInjection\Compiler\TwigComponentPass;
2626
use Symfony\UX\TwigComponent\Twig\ComponentExtension;
27-
use Symfony\UX\TwigComponent\Twig\ComponentRuntime;
2827

2928
/**
3029
* @author Kevin Bond <[email protected]>
@@ -67,14 +66,7 @@ class_exists(AbstractArgument::class) ? new AbstractArgument(sprintf('Added in %
6766

6867
$container->register('ux.twig_component.twig.component_extension', ComponentExtension::class)
6968
->addTag('twig.extension')
70-
;
71-
72-
$container->register('ux.twig_component.twig.component_runtime', ComponentRuntime::class)
73-
->setArguments([
74-
new Reference('ux.twig_component.component_factory'),
75-
new Reference('ux.twig_component.component_renderer'),
76-
])
77-
->addTag('twig.runtime')
69+
->addTag('container.service_subscriber', ['key' => ComponentRenderer::class, 'id' => 'ux.twig_component.component_renderer'])
7870
;
7971
}
8072
}

src/TwigComponent/src/Twig/ComponentExtension.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111

1212
namespace Symfony\UX\TwigComponent\Twig;
1313

14+
use Psr\Container\ContainerInterface;
15+
use Symfony\Contracts\Service\ServiceSubscriberInterface;
16+
use Symfony\UX\TwigComponent\ComponentRenderer;
1417
use Twig\Extension\AbstractExtension;
1518
use Twig\TwigFunction;
1619

@@ -21,12 +24,26 @@
2124
*
2225
* @internal
2326
*/
24-
final class ComponentExtension extends AbstractExtension
27+
final class ComponentExtension extends AbstractExtension implements ServiceSubscriberInterface
2528
{
29+
public function __construct(private ContainerInterface $container)
30+
{
31+
}
32+
33+
public static function getSubscribedServices(): array
34+
{
35+
return [ComponentRenderer::class];
36+
}
37+
2638
public function getFunctions(): array
2739
{
2840
return [
29-
new TwigFunction('component', [ComponentRuntime::class, 'render'], ['is_safe' => ['all']]),
41+
new TwigFunction('component', [$this, 'render'], ['is_safe' => ['all']]),
3042
];
3143
}
44+
45+
public function render(string $name, array $props = []): string
46+
{
47+
return $this->container->get(ComponentRenderer::class)->createAndRender($name, $props);
48+
}
3249
}

src/TwigComponent/src/Twig/ComponentRuntime.php

Lines changed: 0 additions & 36 deletions
This file was deleted.

0 commit comments

Comments
 (0)