Skip to content

Commit 06b6c07

Browse files
committed
make AddLiveAttributesSubscriber lazy
1 parent 53ac356 commit 06b6c07

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

src/LiveComponent/src/DependencyInjection/LiveComponentExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ function (ChildDefinition $definition, AsLiveComponent $attribute) {
9696
;
9797

9898
$container->register('ux.live_component.add_attributes_subscriber', AddLiveAttributesSubscriber::class)
99-
->setArguments([new Reference('ux.live_component.twig.component_runtime')])
10099
->addTag('kernel.event_subscriber')
100+
->addTag('container.service_subscriber', ['key' => LiveComponentRuntime::class, 'id' => 'ux.live_component.twig.component_runtime'])
101101
;
102102

103103
$container->setAlias(ComponentValidatorInterface::class, ComponentValidator::class);

src/LiveComponent/src/EventListener/AddLiveAttributesSubscriber.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,20 @@
22

33
namespace Symfony\UX\LiveComponent\EventListener;
44

5+
use Psr\Container\ContainerInterface;
56
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
7+
use Symfony\Contracts\Service\ServiceSubscriberInterface;
68
use Symfony\UX\LiveComponent\Twig\LiveComponentRuntime;
79
use Symfony\UX\TwigComponent\ComponentAttributes;
810
use Symfony\UX\TwigComponent\EventListener\PreRenderEvent;
911

1012
/**
1113
* @author Kevin Bond <[email protected]>
1214
*/
13-
final class AddLiveAttributesSubscriber implements EventSubscriberInterface
15+
final class AddLiveAttributesSubscriber implements EventSubscriberInterface, ServiceSubscriberInterface
1416
{
15-
public function __construct(private LiveComponentRuntime $runtime)
17+
public function __construct(private ContainerInterface $container)
1618
{
17-
// todo make lazy?
1819
}
1920

2021
public function onPreRender(PreRenderEvent $event): void
@@ -24,7 +25,10 @@ public function onPreRender(PreRenderEvent $event): void
2425
return;
2526
}
2627

27-
$attributes = $this->runtime->getLiveAttributes($event->component, $event->config);
28+
/** @var ComponentAttributes $attributes */
29+
$attributes = $this->container->get(LiveComponentRuntime::class)
30+
->getLiveAttributes($event->component, $event->config)
31+
;
2832

2933
if (isset($event->context['attributes']) && $event->context['attributes'] instanceof ComponentAttributes) {
3034
// merge with existing attributes if available
@@ -38,4 +42,11 @@ public static function getSubscribedEvents(): array
3842
{
3943
return [PreRenderEvent::class => 'onPreRender'];
4044
}
45+
46+
public static function getSubscribedServices(): array
47+
{
48+
return [
49+
LiveComponentRuntime::class,
50+
];
51+
}
4152
}

0 commit comments

Comments
 (0)