Skip to content

Commit d79f435

Browse files
committed
refactor MountedComponent to exclude metadata/include name
1 parent 2bcbc62 commit d79f435

File tree

9 files changed

+41
-38
lines changed

9 files changed

+41
-38
lines changed

src/LiveComponent/src/EventListener/AddLiveAttributesSubscriber.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public static function getSubscribedServices(): array
5959

6060
private function getLiveAttributes(MountedComponent $mounted): ComponentAttributes
6161
{
62-
$name = $mounted->getMetadata()->getName();
62+
$name = $mounted->getName();
6363
$url = $this->container->get(UrlGeneratorInterface::class)->generate('live_component', ['component' => $name]);
6464
$data = $this->container->get(LiveComponentHydrator::class)->dehydrate($mounted);
6565
$twig = $this->container->get(Environment::class);

src/LiveComponent/src/EventListener/LiveComponentSubscriber.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ public function onKernelRequest(RequestEvent $event): void
7474
$action = $request->get('action', 'get');
7575
$componentName = (string) $request->get('component');
7676

77+
$request->attributes->set('_component_name', $componentName);
78+
7779
try {
7880
/** @var ComponentMetadata $metadata */
7981
$metadata = $this->container->get(ComponentFactory::class)->metadataFor($componentName);
@@ -85,8 +87,6 @@ public function onKernelRequest(RequestEvent $event): void
8587
throw new NotFoundHttpException(sprintf('"%s" (%s) is not a Live Component.', $metadata->getClass(), $componentName));
8688
}
8789

88-
$request->attributes->set('_component_metadata', $metadata);
89-
9090
if ('get' === $action) {
9191
$defaultAction = trim($metadata->get('default_action', '__invoke'), '()');
9292

@@ -148,7 +148,7 @@ public function onKernelController(ControllerEvent $event): void
148148
$mounted = $this->container->get(LiveComponentHydrator::class)->hydrate(
149149
$component,
150150
$data,
151-
$request->attributes->get('_component_metadata')
151+
$request->attributes->get('_component_name')
152152
);
153153

154154
$request->attributes->set('_mounted_component', $mounted);

src/LiveComponent/src/LiveComponentHydrator.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
use Symfony\UX\LiveComponent\Attribute\LivePropContext;
1919
use Symfony\UX\LiveComponent\Exception\UnsupportedHydrationException;
2020
use Symfony\UX\TwigComponent\ComponentAttributes;
21-
use Symfony\UX\TwigComponent\ComponentMetadata;
2221
use Symfony\UX\TwigComponent\MountedComponent;
2322

2423
/**
@@ -116,7 +115,7 @@ public function dehydrate(MountedComponent $mounted): array
116115
return $data;
117116
}
118117

119-
public function hydrate(object $component, array $data, ComponentMetadata $metadata): MountedComponent
118+
public function hydrate(object $component, array $data, string $componentName): MountedComponent
120119
{
121120
$readonlyProperties = [];
122121

@@ -205,7 +204,7 @@ public function hydrate(object $component, array $data, ComponentMetadata $metad
205204
$component->{$method->name}();
206205
}
207206

208-
return new MountedComponent($component, $attributes, $metadata);
207+
return new MountedComponent($componentName, $component, $attributes);
209208
}
210209

211210
private function computeChecksum(array $data, array $readonlyProperties): string

src/LiveComponent/tests/Integration/LiveComponentHydratorTest.php

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
use Symfony\UX\LiveComponent\Tests\Fixtures\Entity\Entity1;
2121
use Symfony\UX\TwigComponent\ComponentAttributes;
2222
use Symfony\UX\TwigComponent\ComponentFactory;
23-
use Symfony\UX\TwigComponent\ComponentMetadata;
2423
use Symfony\UX\TwigComponent\MountedComponent;
2524
use function Zenstruck\Foundry\create;
2625
use Zenstruck\Foundry\Test\Factories;
@@ -67,7 +66,7 @@ public function testCanDehydrateAndHydrateLiveComponent(): void
6766

6867
$component = $factory->get('component1');
6968

70-
$hydrator->hydrate($component, $dehydrated, $mounted->getMetadata());
69+
$hydrator->hydrate($component, $dehydrated, $mounted->getName());
7170

7271
$this->assertSame($prop1->id, $component->prop1->id);
7372
$this->assertSame($prop2->format('c'), $component->prop2->format('c'));
@@ -94,7 +93,7 @@ public function testCanModifyWritableProps(): void
9493

9594
$component = $factory->get('component1');
9695

97-
$hydrator->hydrate($component, $dehydrated, $mounted->getMetadata());
96+
$hydrator->hydrate($component, $dehydrated, $mounted->getName());
9897

9998
$this->assertSame('new value', $component->prop3);
10099
}
@@ -119,7 +118,7 @@ public function testCannotModifyReadonlyProps(): void
119118
$component = $factory->get('component1');
120119

121120
$this->expectException(\RuntimeException::class);
122-
$hydrator->hydrate($component, $dehydrated, $mounted->getMetadata());
121+
$hydrator->hydrate($component, $dehydrated, $mounted->getName());
123122
}
124123

125124
public function testHydrationFailsIfChecksumMissing(): void
@@ -131,7 +130,7 @@ public function testHydrationFailsIfChecksumMissing(): void
131130
$factory = self::getContainer()->get('ux.twig_component.component_factory');
132131

133132
$this->expectException(\RuntimeException::class);
134-
$hydrator->hydrate($factory->get('component1'), [], $factory->metadataFor('component1'));
133+
$hydrator->hydrate($factory->get('component1'), [], 'component1');
135134
}
136135

137136
public function testHydrationFailsOnChecksumMismatch(): void
@@ -143,7 +142,7 @@ public function testHydrationFailsOnChecksumMismatch(): void
143142
$factory = self::getContainer()->get('ux.twig_component.component_factory');
144143

145144
$this->expectException(\RuntimeException::class);
146-
$hydrator->hydrate($factory->get('component1'), ['_checksum' => 'invalid'], $factory->metadataFor('component1'));
145+
$hydrator->hydrate($factory->get('component1'), ['_checksum' => 'invalid'], 'component1');
147146
}
148147

149148
public function testPreDehydrateAndPostHydrateHooksCalled(): void
@@ -173,7 +172,7 @@ public function testPreDehydrateAndPostHydrateHooksCalled(): void
173172
$this->assertFalse($component->preDehydrateCalled);
174173
$this->assertFalse($component->postHydrateCalled);
175174

176-
$hydrator->hydrate($component, $data, $mounted->getMetadata());
175+
$hydrator->hydrate($component, $data, $mounted->getName());
177176

178177
$this->assertFalse($component->preDehydrateCalled);
179178
$this->assertTrue($component->postHydrateCalled);
@@ -208,7 +207,7 @@ public function testDeletingEntityBetweenDehydrationAndHydrationSetsItToNull():
208207
/** @var Component1 $component */
209208
$component = $factory->get('component1');
210209

211-
$mounted = $hydrator->hydrate($component, $data, $mounted->getMetadata());
210+
$mounted = $hydrator->hydrate($component, $data, $mounted->getName());
212211

213212
$this->assertNull($component->prop1);
214213

@@ -242,7 +241,7 @@ public function testCorrectlyUsesCustomFrontendNameInDehydrateAndHydrate(): void
242241
/** @var Component3 $component */
243242
$component = $factory->get('component3');
244243

245-
$hydrator->hydrate($component, $dehydrated, $mounted->getMetadata());
244+
$hydrator->hydrate($component, $dehydrated, $mounted->getName());
246245

247246
$this->assertSame('value1', $component->prop1);
248247
$this->assertSame('value2', $component->prop2);
@@ -261,14 +260,14 @@ public function testCanDehydrateAndHydrateArrays(): void
261260
$instance = clone $component;
262261
$instance->prop = ['some', 'array'];
263262

264-
$dehydrated = $hydrator->dehydrate(new MountedComponent($instance, new ComponentAttributes([]), new ComponentMetadata([])));
263+
$dehydrated = $hydrator->dehydrate(new MountedComponent('my_component', $instance, new ComponentAttributes([])));
265264

266265
$this->assertArrayHasKey('prop', $dehydrated);
267266
$this->assertSame($instance->prop, $dehydrated['prop']);
268267

269268
$this->assertFalse(isset($component->prop));
270269

271-
$hydrator->hydrate($component, $dehydrated, new ComponentMetadata([]));
270+
$hydrator->hydrate($component, $dehydrated, 'my_component');
272271

273272
$this->assertSame($instance->prop, $component->prop);
274273
}
@@ -290,7 +289,7 @@ public function testCanDehydrateAndHydrateComponentsWithAttributes(): void
290289
$this->assertArrayHasKey('_attributes', $dehydrated);
291290
$this->assertSame($attributes, $dehydrated['_attributes']);
292291

293-
$mounted = $hydrator->hydrate($factory->get('with_attributes'), $dehydrated, $mounted->getMetadata());
292+
$mounted = $hydrator->hydrate($factory->get('with_attributes'), $dehydrated, $mounted->getName());
294293

295294
$this->assertSame($attributes, $mounted->getAttributes()->all());
296295
}
@@ -311,7 +310,7 @@ public function testCanDehydrateAndHydrateComponentsWithEmptyAttributes(): void
311310

312311
$this->assertArrayNotHasKey('_attributes', $dehydrated);
313312

314-
$mounted = $hydrator->hydrate($factory->get('with_attributes'), $dehydrated, $mounted->getMetadata());
313+
$mounted = $hydrator->hydrate($factory->get('with_attributes'), $dehydrated, $mounted->getName());
315314

316315
$this->assertSame([], $mounted->getAttributes()->all());
317316
}

src/TwigComponent/src/ComponentFactory.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,7 @@ public function create(string $name, array $data = []): MountedComponent
7676
}
7777
}
7878

79-
return new MountedComponent(
80-
$component,
81-
new ComponentAttributes(array_merge($attributes, $data)),
82-
$this->metadataFor($name)
83-
);
79+
return new MountedComponent($name, $component, new ComponentAttributes(array_merge($attributes, $data)));
8480
}
8581

8682
/**

src/TwigComponent/src/ComponentRenderer.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,11 @@ final class ComponentRenderer
2525
{
2626
private bool $safeClassesRegistered = false;
2727

28-
public function __construct(private Environment $twig, private EventDispatcherInterface $dispatcher)
29-
{
28+
public function __construct(
29+
private Environment $twig,
30+
private EventDispatcherInterface $dispatcher,
31+
private ComponentFactory $factory
32+
) {
3033
}
3134

3235
public function render(MountedComponent $mounted): string
@@ -37,7 +40,9 @@ public function render(MountedComponent $mounted): string
3740
$this->safeClassesRegistered = true;
3841
}
3942

40-
$this->dispatcher->dispatch($event = new PreRenderEvent($mounted));
43+
$event = new PreRenderEvent($mounted, $this->factory->metadataFor($mounted->getName()));
44+
45+
$this->dispatcher->dispatch($event);
4146

4247
return $this->twig->render($event->getTemplate(), $event->getVariables());
4348
}

src/TwigComponent/src/DependencyInjection/TwigComponentExtension.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ class_exists(AbstractArgument::class) ? new AbstractArgument(sprintf('Added in %
5656
->setArguments([
5757
new Reference('twig'),
5858
new Reference('event_dispatcher'),
59+
new Reference('ux.twig_component.component_factory'),
5960
])
6061
;
6162

src/TwigComponent/src/EventListener/PreRenderEvent.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ final class PreRenderEvent extends Event
2828
/**
2929
* @internal
3030
*/
31-
public function __construct(private MountedComponent $mounted)
31+
public function __construct(private MountedComponent $mounted, private ComponentMetadata $metadata)
3232
{
33-
$this->template = $this->mounted->getMetadata()->getTemplate();
33+
$this->template = $this->metadata->getTemplate();
3434
$this->variables = $this->mounted->getVariables();
3535
}
3636

@@ -77,9 +77,12 @@ public function setVariables(array $variables): self
7777

7878
public function getMetadata(): ComponentMetadata
7979
{
80-
return $this->mounted->getMetadata();
80+
return $this->metadata;
8181
}
8282

83+
/**
84+
* @internal
85+
*/
8386
public function getMountedComponent(): MountedComponent
8487
{
8588
return $this->mounted;

src/TwigComponent/src/MountedComponent.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,17 @@
2121
final class MountedComponent
2222
{
2323
public function __construct(
24+
private string $name,
2425
private object $component,
25-
private ComponentAttributes $attributes,
26-
private ComponentMetadata $metadata
26+
private ComponentAttributes $attributes
2727
) {
2828
}
2929

30+
public function getName(): string
31+
{
32+
return $this->name;
33+
}
34+
3035
public function getComponent(): object
3136
{
3237
return $this->component;
@@ -37,11 +42,6 @@ public function getAttributes(): ComponentAttributes
3742
return $this->attributes;
3843
}
3944

40-
public function getMetadata(): ComponentMetadata
41-
{
42-
return $this->metadata;
43-
}
44-
4545
public function getVariables(): array
4646
{
4747
return array_merge(

0 commit comments

Comments
 (0)