Skip to content

Commit 11402b9

Browse files
committed
bug #1341 [LiveComponent] Make LiveComponentHydrator work without Serializer (smnandre)
This PR was squashed before being merged into the 2.x branch. Discussion ---------- [LiveComponent] Make LiveComponentHydrator work without Serializer | Q | A | ------------- | --- | Bug fix? | yes | New feature? | yes | License | MIT Allow nullable "serializer" argument in LiveComponentHydrator constructor. (fix #1340 and complete #1327) Commits ------- 10ca442 [LiveComponent] Make LiveComponentHydrator work without Serializer
2 parents e502400 + 10ca442 commit 11402b9

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

src/LiveComponent/src/DependencyInjection/LiveComponentExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ function (ChildDefinition $definition, AsLiveComponent $attribute) {
101101
tagged_iterator(LiveComponentBundle::HYDRATION_EXTENSION_TAG),
102102
new Reference('property_accessor'),
103103
new Reference('ux.live_component.metadata_factory'),
104-
new Reference('serializer'),
104+
new Reference('serializer', ContainerInterface::NULL_ON_INVALID_REFERENCE),
105105
'%kernel.secret%',
106106
])
107107
;

src/LiveComponent/src/LiveComponentHydrator.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ public function __construct(
5050
private iterable $hydrationExtensions,
5151
private PropertyAccessorInterface $propertyAccessor,
5252
private LiveComponentMetadataFactory $liveComponentMetadataFactory,
53-
private NormalizerInterface|DenormalizerInterface $normalizer,
54-
private string $secret
53+
private NormalizerInterface|DenormalizerInterface|null $serializer,
54+
private string $secret,
5555
) {
5656
}
5757

@@ -357,8 +357,14 @@ private function dehydrateValue(mixed $value, LivePropMetadata $propMetadata, ob
357357
if (!interface_exists(NormalizerInterface::class)) {
358358
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));
359359
}
360+
if (null === $this->serializer) {
361+
throw new \LogicException(sprintf('The LiveProp "%s" on component "%s" has "useSerializerForHydration: true", but no serializer has been set.', $propMetadata->getName(), $parentObject::class));
362+
}
363+
if (!$this->serializer instanceof NormalizerInterface) {
364+
throw new \LogicException(sprintf('The LiveProp "%s" on component "%s" has "useSerializerForHydration: true", but the given serializer does not implement NormalizerInterface.', $propMetadata->getName(), $parentObject::class));
365+
}
360366

361-
return $this->normalizer->normalize($value, 'json', $propMetadata->serializationContext());
367+
return $this->serializer->normalize($value, 'json', $propMetadata->serializationContext());
362368
}
363369

364370
if (\is_bool($value) || null === $value || is_numeric($value) || \is_string($value)) {
@@ -438,8 +444,14 @@ private function hydrateValue(mixed $value, LivePropMetadata $propMetadata, obje
438444
if (!interface_exists(DenormalizerInterface::class)) {
439445
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));
440446
}
447+
if (null === $this->serializer) {
448+
throw new \LogicException(sprintf('The LiveProp "%s" on component "%s" has "useSerializerForHydration: true", but no serializer has been set.', $propMetadata->getName(), $parentObject::class));
449+
}
450+
if (!$this->serializer instanceof DenormalizerInterface) {
451+
throw new \LogicException(sprintf('The LiveProp "%s" on component "%s" has "useSerializerForHydration: true", but the given serializer does not implement DenormalizerInterface.', $propMetadata->getName(), $parentObject::class));
452+
}
441453

442-
return $this->normalizer->denormalize($value, $propMetadata->getType(), 'json', $propMetadata->serializationContext());
454+
return $this->serializer->denormalize($value, $propMetadata->getType(), 'json', $propMetadata->serializationContext());
443455
}
444456

445457
if ($propMetadata->collectionValueType() && Type::BUILTIN_TYPE_OBJECT === $propMetadata->collectionValueType()->getBuiltinType()) {

0 commit comments

Comments
 (0)