Skip to content

Commit a6f00ea

Browse files
WebMambaweaverryan
authored andcommitted
[LiveComponent] make serializer optional
1 parent bb32456 commit a6f00ea

File tree

2 files changed

+10
-21
lines changed

2 files changed

+10
-21
lines changed

src/LiveComponent/composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
"require": {
2929
"php": ">=8.1",
3030
"symfony/property-access": "^5.4.5|^6.0|^7.0",
31-
"symfony/serializer": "^5.4|^6.0|^7.0",
3231
"symfony/ux-twig-component": "^2.8",
3332
"twig/twig": "^2.14.7|^3.0.4"
3433
},
@@ -42,11 +41,12 @@
4241
"symfony/dependency-injection": "^5.4|^6.0|^7.0",
4342
"symfony/expression-language": "^5.4|^6.0|^7.0",
4443
"symfony/form": "^5.4|^6.0|^7.0",
45-
"symfony/options-resolver": "^5.4|^6.0|^7.0",
4644
"symfony/framework-bundle": "^5.4|^6.0|^7.0",
45+
"symfony/options-resolver": "^5.4|^6.0|^7.0",
4746
"symfony/phpunit-bridge": "^6.0|^7.0",
4847
"symfony/property-info": "^5.4|^6.0|^7.0",
4948
"symfony/security-csrf": "^5.4|^6.0|^7.0",
49+
"symfony/serializer": "^5.4|^6.0|^7.0",
5050
"symfony/twig-bundle": "^5.4|^6.0|^7.0",
5151
"symfony/validator": "^5.4|^6.0|^7.0",
5252
"zenstruck/browser": "^1.2.0",

src/LiveComponent/src/LiveComponentHydrator.php

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,10 @@ private function dehydrateValue(mixed $value, LivePropMetadata $propMetadata, ob
354354
}
355355

356356
if ($propMetadata->useSerializerForHydration()) {
357+
if (!interface_exists(NormalizerInterface::class)) {
358+
throw new \LogicException(sprintf('The LiveProp "%s" on component "%s" has "useSerializerForHydration: true", but the Serializer component is not installed. Try running "composer require symfony/serializer".', $propMetadata->getName(), $parentObject::class));
359+
}
360+
357361
return $this->normalizer->normalize($value, 'json', $propMetadata->serializationContext());
358362
}
359363

@@ -374,8 +378,6 @@ private function dehydrateValue(mixed $value, LivePropMetadata $propMetadata, ob
374378
}
375379

376380
if (!$this->isValueValidDehydratedValue($value)) {
377-
$badKeys = $this->getNonScalarKeys($value, $propMetadata->getName());
378-
$badKeysText = implode(', ', array_map(fn ($key) => sprintf('%s: %s', $key, $badKeys[$key]), array_keys($badKeys)));
379381
throw new \LogicException(throw new \LogicException(sprintf('Unable to dehydrate value of type "%s" for property "%s" on component "%s". Change this to a simpler type of an object that can be dehydrated. Or set the hydrateWith/dehydrateWith options in LiveProp or set "useSerializerForHydration: true" on the LiveProp to use the serializer.', get_debug_type($value), $propMetadata->getName(), $parentObject::class)));
380382
}
381383

@@ -433,6 +435,10 @@ private function hydrateValue(mixed $value, LivePropMetadata $propMetadata, obje
433435
}
434436

435437
if ($propMetadata->useSerializerForHydration()) {
438+
if (!interface_exists(DenormalizerInterface::class)) {
439+
throw new \LogicException(sprintf('The LiveProp "%s" on component "%s" has "useSerializerForHydration: true", but the Serializer component is not installed. Try running "composer require symfony/serializer".', $propMetadata->getName(), $parentObject::class));
440+
}
441+
436442
return $this->normalizer->denormalize($value, $propMetadata->getType(), 'json', $propMetadata->serializationContext());
437443
}
438444

@@ -532,23 +538,6 @@ private function isValueValidDehydratedValue(mixed $value): bool
532538
return true;
533539
}
534540

535-
private function getNonScalarKeys(array $value, string $path = ''): array
536-
{
537-
$nonScalarKeys = [];
538-
foreach ($value as $k => $v) {
539-
if (\is_array($v)) {
540-
$nonScalarKeys = array_merge($nonScalarKeys, $this->getNonScalarKeys($v, sprintf('%s.%s', $path, $k)));
541-
continue;
542-
}
543-
544-
if (!$this->isValueValidDehydratedValue($v)) {
545-
$nonScalarKeys[sprintf('%s.%s', $path, $k)] = get_debug_type($v);
546-
}
547-
}
548-
549-
return $nonScalarKeys;
550-
}
551-
552541
/**
553542
* Allows for specific keys to be written to a "fully-writable" array.
554543
*

0 commit comments

Comments
 (0)