From 6d35c2d99721e6aa1f1483ebfd3ad4614334f495 Mon Sep 17 00:00:00 2001 From: Matheo Daninos Date: Sun, 10 Dec 2023 12:45:25 +0100 Subject: [PATCH] [LiveComponent] make serializer optional --- src/LiveComponent/composer.json | 4 ++-- src/LiveComponent/src/LiveComponentHydrator.php | 10 ++++++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/LiveComponent/composer.json b/src/LiveComponent/composer.json index 821379d248e..61bef60c500 100644 --- a/src/LiveComponent/composer.json +++ b/src/LiveComponent/composer.json @@ -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" }, @@ -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", diff --git a/src/LiveComponent/src/LiveComponentHydrator.php b/src/LiveComponent/src/LiveComponentHydrator.php index 9d439ca6e63..2ffb5ad2071 100644 --- a/src/LiveComponent/src/LiveComponentHydrator.php +++ b/src/LiveComponent/src/LiveComponentHydrator.php @@ -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()); } @@ -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))); } @@ -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()); }