-
-
Notifications
You must be signed in to change notification settings - Fork 0
Batch action tweaks #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -29,7 +29,6 @@ | |
| use Symfony\Contracts\Service\ServiceSubscriberInterface; | ||
| use Symfony\UX\LiveComponent\Attribute\AsLiveComponent; | ||
| use Symfony\UX\LiveComponent\Attribute\LiveArg; | ||
| use Symfony\UX\LiveComponent\Controller\BatchActionController; | ||
| use Symfony\UX\LiveComponent\LiveComponentHydrator; | ||
| use Symfony\UX\TwigComponent\ComponentFactory; | ||
| use Symfony\UX\TwigComponent\ComponentMetadata; | ||
|
|
@@ -70,8 +69,7 @@ public function onKernelRequest(RequestEvent $event): void | |
| return; | ||
| } | ||
|
|
||
| if (!$event->isMainRequest()) { | ||
| // sub request | ||
| if ($request->attributes->has('_controller')) { | ||
| return; | ||
| } | ||
|
|
||
|
|
@@ -120,11 +118,12 @@ public function onKernelRequest(RequestEvent $event): void | |
| $request->attributes->set('_controller', 'ux.live_component.batch_action_controller'); | ||
| $request->attributes->set('serviceId', $metadata->getServiceId()); | ||
| $request->attributes->set('actions', $data['actions']); | ||
| $request->attributes->set('mounted', $this->container->get(LiveComponentHydrator::class)->hydrate( | ||
| $request->attributes->set('_mounted_component', $this->container->get(LiveComponentHydrator::class)->hydrate( | ||
| $this->container->get(ComponentFactory::class)->get($componentName), | ||
| $data['data'], | ||
| $componentName, | ||
| )); | ||
| $request->attributes->set('_is_live_batch_action', true); | ||
|
|
||
| return; | ||
| } | ||
|
|
@@ -140,12 +139,12 @@ public function onKernelController(ControllerEvent $event): void | |
| return; | ||
| } | ||
|
|
||
| $controller = $event->getController(); | ||
|
|
||
| if ($controller instanceof BatchActionController) { | ||
| if ($request->attributes->get('_is_live_batch_action')) { | ||
| return; | ||
| } | ||
|
|
||
| $controller = $event->getController(); | ||
|
|
||
| if (!\is_array($controller) || 2 !== \count($controller)) { | ||
| throw new \RuntimeException('Not a valid live component.'); | ||
| } | ||
|
|
@@ -160,22 +159,29 @@ public function onKernelController(ControllerEvent $event): void | |
| 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))); | ||
| } | ||
|
|
||
| if ($event->isMainRequest()) { | ||
| $data = $this->parseDataFor($request); | ||
|
|
||
| $request->attributes->set('_component_action_args', $data['args']); | ||
| /* | ||
| * Either we: | ||
| * A) To not have a _mounted_component, so hydrate $component | ||
| * B) We DO have a _mounted_component, so no need to hydrate, | ||
| * but we DO need to make sure it's set as the controller. | ||
| */ | ||
| if (!$request->attributes->has('_mounted_component')) { | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Once I dug in, I better understood the limitations and complexities that you were dealing with. This is really the only significant change. I wanted the "role" of The job of A) Either mount the component (which is already set as the controller object. Or, if already mounted, make sure the mounted component IS the controller object. B) Figure out the arguments to pass to the controller. That feels more straightforward to me. |
||
| $request->attributes->set('_mounted_component', $this->container->get(LiveComponentHydrator::class)->hydrate( | ||
| $component, | ||
| $data['data'], | ||
| $this->parseDataFor($request)['data'], | ||
| $request->attributes->get('_component_name') | ||
| )); | ||
| } else { | ||
| // sub-request | ||
| $event->setController([$request->attributes->get('_mounted_component')->getComponent(), $action]); | ||
| // override the component with our already-mounted version | ||
| $component = $request->attributes->get('_mounted_component')->getComponent(); | ||
| $event->setController([ | ||
| $component, | ||
| $action, | ||
| ]); | ||
| } | ||
|
|
||
| $actionArguments = $request->attributes->get('_component_action_args', []); | ||
|
|
||
| // read the action arguments from the request, unless they're already set (batch sub-requests) | ||
| $actionArguments = $request->attributes->get('_component_action_args', $this->parseDataFor($request)['args']); | ||
| // extra variables to be made available to the controller | ||
| // (for "actions" only) | ||
| foreach (LiveArg::liveArgs($component, $action) as $parameter => $arg) { | ||
|
|
@@ -194,21 +200,26 @@ public function onKernelController(ControllerEvent $event): void | |
| */ | ||
| private function parseDataFor(Request $request): array | ||
| { | ||
| if ($request->query->has('data')) { | ||
| return [ | ||
| 'data' => json_decode($request->query->get('data'), true, 512, \JSON_THROW_ON_ERROR), | ||
| 'args' => [], | ||
| 'actions' => [], | ||
| ]; | ||
| } | ||
| if (!$request->attributes->has('_live_request_data')) { | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added this so that we could call this method ad-hoc as many times as we want (which helps make the above methods more clear) and not worry about performance. |
||
|
|
||
| if ($request->query->has('data')) { | ||
| return [ | ||
| 'data' => json_decode($request->query->get('data'), true, 512, \JSON_THROW_ON_ERROR), | ||
| 'args' => [], | ||
| 'actions' => [], | ||
| ]; | ||
| } | ||
|
|
||
| $requestData = json_decode($request->getContent(), true, 512, \JSON_THROW_ON_ERROR); | ||
| $requestData = json_decode($request->getContent(), true, 512, \JSON_THROW_ON_ERROR); | ||
|
|
||
| return [ | ||
| 'data' => $requestData['data'] ?? [], | ||
| 'args' => $requestData['args'] ?? [], | ||
| 'actions' => $requestData['actions'] ?? [], | ||
| ]; | ||
| $request->attributes->set('_live_request_data', [ | ||
| 'data' => $requestData['data'] ?? [], | ||
| 'args' => $requestData['args'] ?? [], | ||
| 'actions' => $requestData['actions'] ?? [], | ||
| ]); | ||
| } | ||
|
|
||
| return $request->attributes->get('_live_request_data'); | ||
| } | ||
|
|
||
| public function onKernelView(ViewEvent $event): void | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
idk, just a preference - but super minor