|
29 | 29 | use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
|
30 | 30 | use Symfony\Contracts\Service\ServiceSubscriberInterface;
|
31 | 31 | use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
|
| 32 | +use Symfony\UX\LiveComponent\Attribute\LiveArg; |
32 | 33 | use Symfony\UX\LiveComponent\LiveComponentHydrator;
|
33 | 34 | use Symfony\UX\TwigComponent\ComponentFactory;
|
34 | 35 | use Symfony\UX\TwigComponent\ComponentRenderer;
|
@@ -141,11 +142,19 @@ public function onKernelController(ControllerEvent $event): void
|
141 | 142 |
|
142 | 143 | $this->container->get(LiveComponentHydrator::class)->hydrate($component, $data);
|
143 | 144 |
|
144 |
| - // extra variables to be made available to the controller |
145 |
| - // (for "actions" only) |
146 |
| - parse_str($request->query->get('values'), $values); |
147 |
| - $request->attributes->add($values); |
148 | 145 | $request->attributes->set('_component', $component);
|
| 146 | + |
| 147 | + if (is_string($queryString = $request->query->get('args'))) { |
| 148 | + // extra variables to be made available to the controller |
| 149 | + // (for "actions" only) |
| 150 | + parse_str($queryString, $args); |
| 151 | + |
| 152 | + foreach ($this->readLiveActionArgs($component, $action) as $arg) { |
| 153 | + if (isset($args[$arg])) { |
| 154 | + $request->attributes->set($arg, $args[$arg]); |
| 155 | + } |
| 156 | + } |
| 157 | + } |
149 | 158 | }
|
150 | 159 |
|
151 | 160 | public function onKernelView(ViewEvent $event): void
|
@@ -250,4 +259,23 @@ private function isLiveComponentJsonRequest(Request $request): bool
|
250 | 259 | {
|
251 | 260 | return \in_array($request->getPreferredFormat(), [self::JSON_FORMAT, 'json'], true);
|
252 | 261 | }
|
| 262 | + |
| 263 | + /** |
| 264 | + * @return string[] |
| 265 | + */ |
| 266 | + private function readLiveActionArgs(object $component, string $action): array |
| 267 | + { |
| 268 | + $method = new \ReflectionMethod($component, $action); |
| 269 | + $liveArgs = []; |
| 270 | + |
| 271 | + foreach ($method->getParameters() as $parameter) { |
| 272 | + $attributes = $parameter->getAttributes(LiveArg::class); |
| 273 | + |
| 274 | + foreach ($attributes as $liveArg) { |
| 275 | + $liveArgs[] = $liveArg->newInstance(); |
| 276 | + } |
| 277 | + } |
| 278 | + |
| 279 | + return array_map(static fn (LiveArg $arg) => $arg->getName(), $liveArgs); |
| 280 | + } |
253 | 281 | }
|
0 commit comments