29
29
use Symfony \Contracts \Service \ServiceSubscriberInterface ;
30
30
use Symfony \UX \LiveComponent \Attribute \AsLiveComponent ;
31
31
use Symfony \UX \LiveComponent \Attribute \LiveArg ;
32
- use Symfony \UX \LiveComponent \Controller \BatchActionController ;
33
32
use Symfony \UX \LiveComponent \LiveComponentHydrator ;
34
33
use Symfony \UX \TwigComponent \ComponentFactory ;
35
34
use Symfony \UX \TwigComponent \ComponentMetadata ;
@@ -70,8 +69,7 @@ public function onKernelRequest(RequestEvent $event): void
70
69
return ;
71
70
}
72
71
73
- if (!$ event ->isMainRequest ()) {
74
- // sub request
72
+ if ($ request ->attributes ->has ('_controller ' )) {
75
73
return ;
76
74
}
77
75
@@ -120,11 +118,12 @@ public function onKernelRequest(RequestEvent $event): void
120
118
$ request ->attributes ->set ('_controller ' , 'ux.live_component.batch_action_controller ' );
121
119
$ request ->attributes ->set ('serviceId ' , $ metadata ->getServiceId ());
122
120
$ 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 (
124
122
$ this ->container ->get (ComponentFactory::class)->get ($ componentName ),
125
123
$ data ['data ' ],
126
124
$ componentName ,
127
125
));
126
+ $ request ->attributes ->set ('_is_live_batch_action ' , true );
128
127
129
128
return ;
130
129
}
@@ -140,12 +139,12 @@ public function onKernelController(ControllerEvent $event): void
140
139
return ;
141
140
}
142
141
143
- $ controller = $ event ->getController ();
144
-
145
- if ($ controller instanceof BatchActionController) {
142
+ if ($ request ->attributes ->get ('_is_live_batch_action ' )) {
146
143
return ;
147
144
}
148
145
146
+ $ controller = $ event ->getController ();
147
+
149
148
if (!\is_array ($ controller ) || 2 !== \count ($ controller )) {
150
149
throw new \RuntimeException ('Not a valid live component. ' );
151
150
}
@@ -160,22 +159,29 @@ public function onKernelController(ControllerEvent $event): void
160
159
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 )));
161
160
}
162
161
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 ' )) {
167
169
$ request ->attributes ->set ('_mounted_component ' , $ this ->container ->get (LiveComponentHydrator::class)->hydrate (
168
170
$ component ,
169
- $ data ['data ' ],
171
+ $ this -> parseDataFor ( $ request ) ['data ' ],
170
172
$ request ->attributes ->get ('_component_name ' )
171
173
));
172
174
} 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
+ ]);
175
181
}
176
182
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 ' ]);
179
185
// extra variables to be made available to the controller
180
186
// (for "actions" only)
181
187
foreach (LiveArg::liveArgs ($ component , $ action ) as $ parameter => $ arg ) {
@@ -194,21 +200,26 @@ public function onKernelController(ControllerEvent $event): void
194
200
*/
195
201
private function parseDataFor (Request $ request ): array
196
202
{
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
+ }
204
212
205
- $ requestData = json_decode ($ request ->getContent (), true , 512 , \JSON_THROW_ON_ERROR );
213
+ $ requestData = json_decode ($ request ->getContent (), true , 512 , \JSON_THROW_ON_ERROR );
206
214
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 ' );
212
223
}
213
224
214
225
public function onKernelView (ViewEvent $ event ): void
0 commit comments