Skip to content

Commit 4909915

Browse files
committed
Setting childrenFingerprints from request onto MountedComponent
1 parent 2ffbd2c commit 4909915

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

src/LiveComponent/src/EventListener/LiveComponentSubscriber.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Psr\Container\ContainerInterface;
1515
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
16+
use Symfony\Component\HttpFoundation\Exception\JsonException;
1617
use Symfony\Component\HttpFoundation\Request;
1718
use Symfony\Component\HttpFoundation\Response;
1819
use Symfony\Component\HttpKernel\Event\ControllerEvent;
@@ -198,16 +199,18 @@ public function onKernelController(ControllerEvent $event): void
198199
* data: array,
199200
* args: array,
200201
* actions: array
202+
* childrenFingerprints: array
201203
* }
202204
*/
203-
private function parseDataFor(Request $request): array
205+
private static function parseDataFor(Request $request): array
204206
{
205207
if (!$request->attributes->has('_live_request_data')) {
206208
if ($request->query->has('data')) {
207209
$liveRequestData = [
208-
'data' => json_decode($request->query->get('data'), true, 512, \JSON_THROW_ON_ERROR),
210+
'data' => self::parseJsonFromQuery($request, 'data'),
209211
'args' => [],
210212
'actions' => [],
213+
'childrenFingerprints' => self::parseJsonFromQuery($request, 'childrenFingerprints'),
211214
];
212215
} else {
213216
$requestData = $request->toArray();
@@ -216,6 +219,7 @@ private function parseDataFor(Request $request): array
216219
'data' => $requestData['data'] ?? [],
217220
'args' => $requestData['args'] ?? [],
218221
'actions' => $requestData['actions'] ?? [],
222+
'childrenFingerprints' => $requestData['childrenFingerprints'] ?? []
219223
];
220224
}
221225

@@ -322,6 +326,21 @@ private function hydrateComponent(object $component, string $componentName, Requ
322326
$componentName
323327
);
324328

329+
$mountedComponent->addExtraMetadata('childrenFingerprints', $this->parseDataFor($request)['childrenFingerprints']);
330+
325331
return $mountedComponent;
326332
}
333+
334+
private static function parseJsonFromQuery(Request $request, string $key): array
335+
{
336+
if (!$request->query->has($key)) {
337+
return [];
338+
}
339+
340+
try {
341+
return json_decode($request->query->get($key), true, 512, \JSON_THROW_ON_ERROR);
342+
} catch (\JsonException $exception) {
343+
throw new JsonException(sprintf('Invalid JSON on query string %s.', $key), 0, $exception);
344+
}
345+
}
327346
}

src/TwigComponent/src/MountedComponent.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@
2020
*/
2121
final class MountedComponent
2222
{
23+
/**
24+
* Any extra metadata that might be useful to set.
25+
*
26+
* @var array<string, string>
27+
*/
28+
private array $extraMetadata = [];
29+
2330
/**
2431
* @param array|null $inputProps if the component was just originally created,
2532
* (not hydrated from a request), this is the
@@ -56,4 +63,23 @@ public function getInputProps(): array
5663

5764
return $this->inputProps;
5865
}
66+
67+
public function addExtraMetadata(string $key, mixed $metadata): void
68+
{
69+
$this->extraMetadata[$key] = $metadata;
70+
}
71+
72+
public function hasExtraMetadata(string $key): bool
73+
{
74+
return array_key_exists($key, $this->extraMetadata);
75+
}
76+
77+
public function getExtraMetadata(string $key): mixed
78+
{
79+
if (!$this->hasExtraMetadata($key)) {
80+
throw new \InvalidArgumentException(sprintf('No extra metadata for key "%s" found.', $key));
81+
}
82+
83+
return $this->extraMetadata[$key];
84+
}
5985
}

0 commit comments

Comments
 (0)