Skip to content

Commit

Permalink
[LiveComponent] make serializer optional
Browse files Browse the repository at this point in the history
  • Loading branch information
WebMamba committed Dec 10, 2023
1 parent bb32456 commit 6d35c2d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/LiveComponent/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
"require": {
"php": ">=8.1",
"symfony/property-access": "^5.4.5|^6.0|^7.0",
"symfony/serializer": "^5.4|^6.0|^7.0",
"symfony/ux-twig-component": "^2.8",
"twig/twig": "^2.14.7|^3.0.4"
},
Expand All @@ -42,11 +41,12 @@
"symfony/dependency-injection": "^5.4|^6.0|^7.0",
"symfony/expression-language": "^5.4|^6.0|^7.0",
"symfony/form": "^5.4|^6.0|^7.0",
"symfony/options-resolver": "^5.4|^6.0|^7.0",
"symfony/framework-bundle": "^5.4|^6.0|^7.0",
"symfony/options-resolver": "^5.4|^6.0|^7.0",
"symfony/phpunit-bridge": "^6.0|^7.0",
"symfony/property-info": "^5.4|^6.0|^7.0",
"symfony/security-csrf": "^5.4|^6.0|^7.0",
"symfony/serializer": "^5.4|^6.0|^7.0",
"symfony/twig-bundle": "^5.4|^6.0|^7.0",
"symfony/validator": "^5.4|^6.0|^7.0",
"zenstruck/browser": "^1.2.0",
Expand Down
10 changes: 8 additions & 2 deletions src/LiveComponent/src/LiveComponentHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,10 @@ private function dehydrateValue(mixed $value, LivePropMetadata $propMetadata, ob
}

if ($propMetadata->useSerializerForHydration()) {
if (!interface_exists(NormalizerInterface::class)) {
throw new \LogicException(sprintf('The LiveProp "%s" on component "%s" is using the serializer to hydrate, but the Serializer component is not installed. Try running "composer require symfony/serializer".', $propMetadata->getName(), $parentObject::class));
}

return $this->normalizer->normalize($value, 'json', $propMetadata->serializationContext());
}

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

if (!$this->isValueValidDehydratedValue($value)) {
$badKeys = $this->getNonScalarKeys($value, $propMetadata->getName());
$badKeysText = implode(', ', array_map(fn ($key) => sprintf('%s: %s', $key, $badKeys[$key]), array_keys($badKeys)));
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)));
}

Expand Down Expand Up @@ -433,6 +435,10 @@ private function hydrateValue(mixed $value, LivePropMetadata $propMetadata, obje
}

if ($propMetadata->useSerializerForHydration()) {
if (!interface_exists(DenormalizerInterface::class)) {
throw new \LogicException(sprintf('The LiveProp "%s" on component "%s" is using the serializer to hydrate, but the Serializer component is not installed. Try running "composer require symfony/serializer".', $propMetadata->getName(), $parentObject::class));
}

return $this->normalizer->denormalize($value, $propMetadata->getType(), 'json', $propMetadata->serializationContext());
}

Expand Down

0 comments on commit 6d35c2d

Please sign in to comment.