Skip to content

Commit ae73e5a

Browse files
committed
minor #268 [Live] refactor AddLiveAttributesSubscriber (kbond)
This PR was merged into the 2.x branch. Discussion ---------- [Live] refactor AddLiveAttributesSubscriber | Q | A | ------------- | --- | Bug fix? | no | New feature? | no | Tickets | n/a | License | MIT Cleanup from #220, this subscriber should not depend on the twig runtime. Commits ------- 2d429b0 [Live] refactor AddLiveAttributesSubscriber
2 parents b12ca0e + 2d429b0 commit ae73e5a

File tree

3 files changed

+37
-38
lines changed

3 files changed

+37
-38
lines changed

src/LiveComponent/src/DependencyInjection/LiveComponentExtension.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
use Symfony\Component\DependencyInjection\ContainerBuilder;
1717
use Symfony\Component\DependencyInjection\Extension\Extension;
1818
use Symfony\Component\DependencyInjection\Reference;
19-
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
20-
use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
2119
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
2220
use Symfony\UX\LiveComponent\ComponentValidator;
2321
use Symfony\UX\LiveComponent\ComponentValidatorInterface;
@@ -71,7 +69,7 @@ function (ChildDefinition $definition, AsLiveComponent $attribute) {
7169
->addTag('container.service_subscriber', ['key' => ComponentFactory::class, 'id' => 'ux.twig_component.component_factory'])
7270
->addTag('container.service_subscriber', ['key' => ComponentRenderer::class, 'id' => 'ux.twig_component.component_renderer'])
7371
->addTag('container.service_subscriber', ['key' => LiveComponentHydrator::class, 'id' => 'ux.live_component.component_hydrator'])
74-
->addTag('container.service_subscriber')
72+
->addTag('container.service_subscriber') // csrf
7573
;
7674

7775
$container->register('ux.live_component.twig.component_extension', LiveComponentTwigExtension::class)
@@ -80,11 +78,9 @@ function (ChildDefinition $definition, AsLiveComponent $attribute) {
8078

8179
$container->register('ux.live_component.twig.component_runtime', LiveComponentRuntime::class)
8280
->setArguments([
83-
new Reference('twig'),
8481
new Reference('ux.live_component.component_hydrator'),
8582
new Reference('ux.twig_component.component_factory'),
86-
new Reference(UrlGeneratorInterface::class),
87-
new Reference(CsrfTokenManagerInterface::class, ContainerBuilder::NULL_ON_INVALID_REFERENCE),
83+
new Reference('router'),
8884
])
8985
->addTag('twig.runtime')
9086
;
@@ -95,7 +91,8 @@ function (ChildDefinition $definition, AsLiveComponent $attribute) {
9591

9692
$container->register('ux.live_component.add_attributes_subscriber', AddLiveAttributesSubscriber::class)
9793
->addTag('kernel.event_subscriber')
98-
->addTag('container.service_subscriber', ['key' => LiveComponentRuntime::class, 'id' => 'ux.live_component.twig.component_runtime'])
94+
->addTag('container.service_subscriber', ['key' => LiveComponentHydrator::class, 'id' => 'ux.live_component.component_hydrator'])
95+
->addTag('container.service_subscriber') // csrf, twig & router
9996
;
10097

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

src/LiveComponent/src/EventListener/AddLiveAttributesSubscriber.php

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@
44

55
use Psr\Container\ContainerInterface;
66
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
7+
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
8+
use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
79
use Symfony\Contracts\Service\ServiceSubscriberInterface;
8-
use Symfony\UX\LiveComponent\Twig\LiveComponentRuntime;
10+
use Symfony\UX\LiveComponent\LiveComponentHydrator;
911
use Symfony\UX\TwigComponent\ComponentAttributes;
12+
use Symfony\UX\TwigComponent\ComponentMetadata;
1013
use Symfony\UX\TwigComponent\EventListener\PreRenderEvent;
14+
use Twig\Environment;
1115

1216
/**
1317
* @author Kevin Bond <kevinbond@gmail.com>
@@ -25,11 +29,7 @@ public function onPreRender(PreRenderEvent $event): void
2529
return;
2630
}
2731

28-
/** @var ComponentAttributes $attributes */
29-
$attributes = $this->container->get(LiveComponentRuntime::class)
30-
->getLiveAttributes($event->getComponent(), $event->getMetadata())
31-
;
32-
32+
$attributes = $this->getLiveAttributes($event->getComponent(), $event->getMetadata());
3333
$variables = $event->getVariables();
3434

3535
if (isset($variables['attributes']) && $variables['attributes'] instanceof ComponentAttributes) {
@@ -50,7 +50,33 @@ public static function getSubscribedEvents(): array
5050
public static function getSubscribedServices(): array
5151
{
5252
return [
53-
LiveComponentRuntime::class,
53+
LiveComponentHydrator::class,
54+
UrlGeneratorInterface::class,
55+
Environment::class,
56+
'?'.CsrfTokenManagerInterface::class,
5457
];
5558
}
59+
60+
private function getLiveAttributes(object $component, ComponentMetadata $metadata): ComponentAttributes
61+
{
62+
$url = $this->container->get(UrlGeneratorInterface::class)
63+
->generate('live_component', ['component' => $metadata->getName()])
64+
;
65+
$data = $this->container->get(LiveComponentHydrator::class)->dehydrate($component);
66+
$twig = $this->container->get(Environment::class);
67+
68+
$attributes = [
69+
'data-controller' => 'live',
70+
'data-live-url-value' => twig_escape_filter($twig, $url, 'html_attr'),
71+
'data-live-data-value' => twig_escape_filter($twig, json_encode($data, \JSON_THROW_ON_ERROR), 'html_attr'),
72+
];
73+
74+
if ($this->container->has(CsrfTokenManagerInterface::class)) {
75+
$attributes['data-live-csrf-value'] = $this->container->get(CsrfTokenManagerInterface::class)
76+
->getToken($metadata->getName())->getValue()
77+
;
78+
}
79+
80+
return new ComponentAttributes($attributes);
81+
}
5682
}

src/LiveComponent/src/Twig/LiveComponentRuntime.php

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,8 @@
1212
namespace Symfony\UX\LiveComponent\Twig;
1313

1414
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
15-
use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
1615
use Symfony\UX\LiveComponent\LiveComponentHydrator;
17-
use Symfony\UX\TwigComponent\ComponentAttributes;
1816
use Symfony\UX\TwigComponent\ComponentFactory;
19-
use Symfony\UX\TwigComponent\ComponentMetadata;
20-
use Twig\Environment;
2117

2218
/**
2319
* @author Kevin Bond <kevinbond@gmail.com>
@@ -27,11 +23,9 @@
2723
final class LiveComponentRuntime
2824
{
2925
public function __construct(
30-
private Environment $twig,
3126
private LiveComponentHydrator $hydrator,
3227
private ComponentFactory $factory,
3328
private UrlGeneratorInterface $urlGenerator,
34-
private ?CsrfTokenManagerInterface $csrfTokenManager = null
3529
) {
3630
}
3731

@@ -42,22 +36,4 @@ public function getComponentUrl(string $name, array $props = []): string
4236

4337
return $this->urlGenerator->generate('live_component', $params);
4438
}
45-
46-
public function getLiveAttributes(object $component, ComponentMetadata $metadata): ComponentAttributes
47-
{
48-
$url = $this->urlGenerator->generate('live_component', ['component' => $metadata->getName()]);
49-
$data = $this->hydrator->dehydrate($component);
50-
51-
$attributes = [
52-
'data-controller' => 'live',
53-
'data-live-url-value' => twig_escape_filter($this->twig, $url, 'html_attr'),
54-
'data-live-data-value' => twig_escape_filter($this->twig, json_encode($data, \JSON_THROW_ON_ERROR), 'html_attr'),
55-
];
56-
57-
if ($this->csrfTokenManager) {
58-
$attributes['data-live-csrf-value'] = $this->csrfTokenManager->getToken($metadata->getName())->getValue();
59-
}
60-
61-
return new ComponentAttributes($attributes);
62-
}
6339
}

0 commit comments

Comments
 (0)