Skip to content

Commit b8cd262

Browse files
committed
add MountedComponent object
1 parent d638782 commit b8cd262

File tree

12 files changed

+145
-100
lines changed

12 files changed

+145
-100
lines changed

src/LiveComponent/src/EventListener/AddLiveAttributesSubscriber.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function onPreRender(PreRenderEvent $event): void
2727

2828
/** @var ComponentAttributes $attributes */
2929
$attributes = $this->container->get(LiveComponentRuntime::class)
30-
->getLiveAttributes($event->getComponent(), $event->getMetadata())
30+
->getLiveAttributes($event->getMountedComponent())
3131
;
3232

3333
$variables = $event->getVariables();

src/LiveComponent/src/EventListener/LiveComponentSubscriber.php

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
use Symfony\UX\TwigComponent\ComponentFactory;
3434
use Symfony\UX\TwigComponent\ComponentMetadata;
3535
use Symfony\UX\TwigComponent\ComponentRenderer;
36+
use Symfony\UX\TwigComponent\MountedComponent;
3637

3738
/**
3839
* @author Kevin Bond <[email protected]>
@@ -141,9 +142,13 @@ public function onKernelController(ControllerEvent $event): void
141142
throw new NotFoundHttpException(sprintf('The action "%s" either doesn\'t exist or is not allowed in "%s". Make sure it exist and has the LiveAction attribute above it.', $action, \get_class($component)));
142143
}
143144

144-
$this->container->get(LiveComponentHydrator::class)->hydrate($component, $data);
145+
$mounted = $this->container->get(LiveComponentHydrator::class)->hydrate(
146+
$component,
147+
$data,
148+
$request->attributes->get('_component_metadata')
149+
);
145150

146-
$request->attributes->set('_component', $component);
151+
$request->attributes->set('_mounted_component', $mounted);
147152

148153
if (!\is_string($queryString = $request->query->get('args'))) {
149154
return;
@@ -167,7 +172,7 @@ public function onKernelView(ViewEvent $event): void
167172
return;
168173
}
169174

170-
$response = $this->createResponse($request->attributes->get('_component'), $request);
175+
$response = $this->createResponse($request->attributes->get('_mounted_component'), $request);
171176

172177
$event->setResponse($response);
173178
}
@@ -184,14 +189,14 @@ public function onKernelException(ExceptionEvent $event): void
184189
return;
185190
}
186191

187-
$component = $request->attributes->get('_component');
192+
$mounted = $request->attributes->get('_mounted_component');
188193

189194
// in case the exception was too early somehow
190-
if (!$component) {
195+
if (!$mounted) {
191196
return;
192197
}
193198

194-
$response = $this->createResponse($component, $request);
199+
$response = $this->createResponse($mounted, $request);
195200
$event->setResponse($response);
196201
}
197202

@@ -229,15 +234,16 @@ public static function getSubscribedEvents(): array
229234
];
230235
}
231236

232-
private function createResponse(object $component, Request $request): Response
237+
private function createResponse(MountedComponent $mounted, Request $request): Response
233238
{
239+
$component = $mounted->getComponent();
240+
234241
foreach (AsLiveComponent::beforeReRenderMethods($component) as $method) {
235242
$component->{$method->name}();
236243
}
237244

238245
$html = $this->container->get(ComponentRenderer::class)->render(
239-
$component,
240-
$request->attributes->get('_component_metadata')
246+
$mounted,
241247
);
242248

243249
return new Response($html);

src/LiveComponent/src/LiveComponentHydrator.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
1818
use Symfony\UX\LiveComponent\Attribute\LivePropContext;
1919
use Symfony\UX\LiveComponent\Exception\UnsupportedHydrationException;
20+
use Symfony\UX\TwigComponent\ComponentMetadata;
21+
use Symfony\UX\TwigComponent\MountedComponent;
2022

2123
/**
2224
* @author Kevin Bond <[email protected]>
@@ -45,8 +47,10 @@ public function __construct(iterable $propertyHydrators, PropertyAccessorInterfa
4547
$this->secret = $secret;
4648
}
4749

48-
public function dehydrate(object $component): array
50+
public function dehydrate(MountedComponent $mounted): array
4951
{
52+
$component = $mounted->getComponent();
53+
5054
foreach (AsLiveComponent::preDehydrateMethods($component) as $method) {
5155
$component->{$method->name}();
5256
}
@@ -105,7 +109,7 @@ public function dehydrate(object $component): array
105109
return $data;
106110
}
107111

108-
public function hydrate(object $component, array $data): void
112+
public function hydrate(object $component, array $data, ComponentMetadata $metadata): MountedComponent
109113
{
110114
$readonlyProperties = [];
111115

@@ -187,6 +191,8 @@ public function hydrate(object $component, array $data): void
187191
foreach (AsLiveComponent::postHydrateMethods($component) as $method) {
188192
$component->{$method->name}();
189193
}
194+
195+
return new MountedComponent($component, $metadata);
190196
}
191197

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

src/LiveComponent/src/Twig/LiveComponentRuntime.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
use Symfony\UX\LiveComponent\LiveComponentHydrator;
1717
use Symfony\UX\TwigComponent\ComponentAttributes;
1818
use Symfony\UX\TwigComponent\ComponentFactory;
19-
use Symfony\UX\TwigComponent\ComponentMetadata;
19+
use Symfony\UX\TwigComponent\MountedComponent;
2020
use Twig\Environment;
2121

2222
/**
@@ -37,16 +37,16 @@ public function __construct(
3737

3838
public function getComponentUrl(string $name, array $props = []): string
3939
{
40-
$component = $this->factory->create($name, $props);
41-
$params = ['component' => $name] + $this->hydrator->dehydrate($component);
40+
$mounted = $this->factory->create($name, $props);
41+
$params = ['component' => $name] + $this->hydrator->dehydrate($mounted);
4242

4343
return $this->urlGenerator->generate('live_component', $params);
4444
}
4545

46-
public function getLiveAttributes(object $component, ComponentMetadata $metadata): ComponentAttributes
46+
public function getLiveAttributes(MountedComponent $mounted): ComponentAttributes
4747
{
48-
$url = $this->urlGenerator->generate('live_component', ['component' => $metadata->getName()]);
49-
$data = $this->hydrator->dehydrate($component);
48+
$url = $this->urlGenerator->generate('live_component', ['component' => $mounted->getMetadata()->getName()]);
49+
$data = $this->hydrator->dehydrate($mounted);
5050

5151
$attributes = [
5252
'data-controller' => 'live',
@@ -55,7 +55,7 @@ public function getLiveAttributes(object $component, ComponentMetadata $metadata
5555
];
5656

5757
if ($this->csrfTokenManager) {
58-
$attributes['data-live-csrf-value'] = $this->csrfTokenManager->getToken($metadata->getName())->getValue();
58+
$attributes['data-live-csrf-value'] = $this->csrfTokenManager->getToken($mounted->getMetadata()->getName())->getValue();
5959
}
6060

6161
return new ComponentAttributes($attributes);

src/LiveComponent/tests/Functional/EventListener/LiveComponentSubscriberTest.php

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@
1414
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
1515
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
1616
use Symfony\UX\LiveComponent\LiveComponentHydrator;
17-
use Symfony\UX\LiveComponent\Tests\Fixture\Component\Component1;
18-
use Symfony\UX\LiveComponent\Tests\Fixture\Component\Component2;
19-
use Symfony\UX\LiveComponent\Tests\Fixture\Component\Component6;
2017
use Symfony\UX\LiveComponent\Tests\Fixture\Entity\Entity1;
2118
use Symfony\UX\TwigComponent\ComponentFactory;
2219
use Zenstruck\Browser\Response\HtmlResponse;
@@ -42,7 +39,6 @@ public function testCanRenderComponentAsHtml(): void
4239
/** @var ComponentFactory $factory */
4340
$factory = self::getContainer()->get('ux.twig_component.component_factory');
4441

45-
/** @var Component1 $component */
4642
$component = $factory->create('component1', [
4743
'prop1' => $entity = create(Entity1::class)->object(),
4844
'prop2' => $date = new \DateTime('2021-03-05 9:23'),
@@ -72,10 +68,7 @@ public function testCanExecuteComponentAction(): void
7268
/** @var ComponentFactory $factory */
7369
$factory = self::getContainer()->get('ux.twig_component.component_factory');
7470

75-
/** @var Component2 $component */
76-
$component = $factory->create('component2');
77-
78-
$dehydrated = $hydrator->dehydrate($component);
71+
$dehydrated = $hydrator->dehydrate($factory->create('component2'));
7972
$token = null;
8073

8174
$this->browser()
@@ -159,10 +152,7 @@ public function testBeforeReRenderHookOnlyExecutedDuringAjax(): void
159152
/** @var ComponentFactory $factory */
160153
$factory = self::getContainer()->get('ux.twig_component.component_factory');
161154

162-
/** @var Component2 $component */
163-
$component = $factory->create('component2');
164-
165-
$dehydrated = $hydrator->dehydrate($component);
155+
$dehydrated = $hydrator->dehydrate($factory->create('component2'));
166156

167157
$this->browser()
168158
->visit('/render-template/template1')
@@ -182,10 +172,7 @@ public function testCanRedirectFromComponentAction(): void
182172
/** @var ComponentFactory $factory */
183173
$factory = self::getContainer()->get('ux.twig_component.component_factory');
184174

185-
/** @var Component2 $component */
186-
$component = $factory->create('component2');
187-
188-
$dehydrated = $hydrator->dehydrate($component);
175+
$dehydrated = $hydrator->dehydrate($factory->create('component2'));
189176
$token = null;
190177

191178
$this->browser()
@@ -223,10 +210,7 @@ public function testInjectsLiveArgs(): void
223210
/** @var ComponentFactory $factory */
224211
$factory = self::getContainer()->get('ux.twig_component.component_factory');
225212

226-
/** @var Component6 $component */
227-
$component = $factory->create('component6');
228-
229-
$dehydrated = $hydrator->dehydrate($component);
213+
$dehydrated = $hydrator->dehydrate($factory->create('component6'));
230214
$token = null;
231215

232216
$dehydratedWithArgs = array_merge($dehydrated, [

0 commit comments

Comments
 (0)