Skip to content

Commit 13d7110

Browse files
committed
mildly reworking to follow the logic of what is happening more clearly (#5)
1 parent b47ece1 commit 13d7110

File tree

2 files changed

+42
-33
lines changed

2 files changed

+42
-33
lines changed

src/LiveComponent/src/Controller/BatchActionController.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,15 @@ public function __construct(private HttpKernelInterface $kernel)
2828
{
2929
}
3030

31-
public function __invoke(Request $request, MountedComponent $mounted, string $serviceId, array $actions): ?Response
31+
public function __invoke(Request $request, MountedComponent $_mounted_component, string $serviceId, array $actions): ?Response
3232
{
33-
$request->attributes->set('_mounted_component', $mounted);
34-
3533
foreach ($actions as $action) {
3634
$name = $action['name'] ?? throw new BadRequestHttpException('Invalid JSON');
3735

3836
$subRequest = $request->duplicate(attributes: [
3937
'_controller' => [$serviceId, $name],
4038
'_component_action_args' => $action['args'] ?? [],
41-
'_mounted_component' => $mounted,
39+
'_mounted_component' => $_mounted_component,
4240
'_route' => 'live_component',
4341
]);
4442

src/LiveComponent/src/EventListener/LiveComponentSubscriber.php

Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
use Symfony\Contracts\Service\ServiceSubscriberInterface;
3030
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
3131
use Symfony\UX\LiveComponent\Attribute\LiveArg;
32-
use Symfony\UX\LiveComponent\Controller\BatchActionController;
3332
use Symfony\UX\LiveComponent\LiveComponentHydrator;
3433
use Symfony\UX\TwigComponent\ComponentFactory;
3534
use Symfony\UX\TwigComponent\ComponentMetadata;
@@ -70,8 +69,7 @@ public function onKernelRequest(RequestEvent $event): void
7069
return;
7170
}
7271

73-
if (!$event->isMainRequest()) {
74-
// sub request
72+
if ($request->attributes->has('_controller')) {
7573
return;
7674
}
7775

@@ -120,11 +118,12 @@ public function onKernelRequest(RequestEvent $event): void
120118
$request->attributes->set('_controller', 'ux.live_component.batch_action_controller');
121119
$request->attributes->set('serviceId', $metadata->getServiceId());
122120
$request->attributes->set('actions', $data['actions']);
123-
$request->attributes->set('mounted', $this->container->get(LiveComponentHydrator::class)->hydrate(
121+
$request->attributes->set('_mounted_component', $this->container->get(LiveComponentHydrator::class)->hydrate(
124122
$this->container->get(ComponentFactory::class)->get($componentName),
125123
$data['data'],
126124
$componentName,
127125
));
126+
$request->attributes->set('_is_live_batch_action', true);
128127

129128
return;
130129
}
@@ -140,12 +139,12 @@ public function onKernelController(ControllerEvent $event): void
140139
return;
141140
}
142141

143-
$controller = $event->getController();
144-
145-
if ($controller instanceof BatchActionController) {
142+
if ($request->attributes->get('_is_live_batch_action')) {
146143
return;
147144
}
148145

146+
$controller = $event->getController();
147+
149148
if (!\is_array($controller) || 2 !== \count($controller)) {
150149
throw new \RuntimeException('Not a valid live component.');
151150
}
@@ -160,22 +159,29 @@ public function onKernelController(ControllerEvent $event): void
160159
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)));
161160
}
162161

163-
if ($event->isMainRequest()) {
164-
$data = $this->parseDataFor($request);
165-
166-
$request->attributes->set('_component_action_args', $data['args']);
162+
/*
163+
* Either we:
164+
* A) To not have a _mounted_component, so hydrate $component
165+
* B) We DO have a _mounted_component, so no need to hydrate,
166+
* but we DO need to make sure it's set as the controller.
167+
*/
168+
if (!$request->attributes->has('_mounted_component')) {
167169
$request->attributes->set('_mounted_component', $this->container->get(LiveComponentHydrator::class)->hydrate(
168170
$component,
169-
$data['data'],
171+
$this->parseDataFor($request)['data'],
170172
$request->attributes->get('_component_name')
171173
));
172174
} else {
173-
// sub-request
174-
$event->setController([$request->attributes->get('_mounted_component')->getComponent(), $action]);
175+
// override the component with our already-mounted version
176+
$component = $request->attributes->get('_mounted_component')->getComponent();
177+
$event->setController([
178+
$component,
179+
$action,
180+
]);
175181
}
176182

177-
$actionArguments = $request->attributes->get('_component_action_args', []);
178-
183+
// read the action arguments from the request, unless they're already set (batch sub-requests)
184+
$actionArguments = $request->attributes->get('_component_action_args', $this->parseDataFor($request)['args']);
179185
// extra variables to be made available to the controller
180186
// (for "actions" only)
181187
foreach (LiveArg::liveArgs($component, $action) as $parameter => $arg) {
@@ -194,21 +200,26 @@ public function onKernelController(ControllerEvent $event): void
194200
*/
195201
private function parseDataFor(Request $request): array
196202
{
197-
if ($request->query->has('data')) {
198-
return [
199-
'data' => json_decode($request->query->get('data'), true, 512, \JSON_THROW_ON_ERROR),
200-
'args' => [],
201-
'actions' => [],
202-
];
203-
}
203+
if (!$request->attributes->has('_live_request_data')) {
204+
205+
if ($request->query->has('data')) {
206+
return [
207+
'data' => json_decode($request->query->get('data'), true, 512, \JSON_THROW_ON_ERROR),
208+
'args' => [],
209+
'actions' => [],
210+
];
211+
}
204212

205-
$requestData = json_decode($request->getContent(), true, 512, \JSON_THROW_ON_ERROR);
213+
$requestData = json_decode($request->getContent(), true, 512, \JSON_THROW_ON_ERROR);
206214

207-
return [
208-
'data' => $requestData['data'] ?? [],
209-
'args' => $requestData['args'] ?? [],
210-
'actions' => $requestData['actions'] ?? [],
211-
];
215+
$request->attributes->set('_live_request_data', [
216+
'data' => $requestData['data'] ?? [],
217+
'args' => $requestData['args'] ?? [],
218+
'actions' => $requestData['actions'] ?? [],
219+
]);
220+
}
221+
222+
return $request->attributes->get('_live_request_data');
212223
}
213224

214225
public function onKernelView(ViewEvent $event): void

0 commit comments

Comments
 (0)