Skip to content

Commit

Permalink
feature #1327 [LiveComponent] make serializer optional (WebMamba)
Browse files Browse the repository at this point in the history
This PR was squashed before being merged into the 2.x branch.

Discussion
----------

[LiveComponent] make serializer optional

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | yes
| Issues        |
| License       | MIT

Commits
-------

a6f00ea [LiveComponent] make serializer optional
  • Loading branch information
weaverryan committed Dec 17, 2023
2 parents 98488f7 + a6f00ea commit e502400
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 21 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
27 changes: 8 additions & 19 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" has "useSerializerForHydration: true", 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" has "useSerializerForHydration: true", 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 Expand Up @@ -532,23 +538,6 @@ private function isValueValidDehydratedValue(mixed $value): bool
return true;
}

private function getNonScalarKeys(array $value, string $path = ''): array
{
$nonScalarKeys = [];
foreach ($value as $k => $v) {
if (\is_array($v)) {
$nonScalarKeys = array_merge($nonScalarKeys, $this->getNonScalarKeys($v, sprintf('%s.%s', $path, $k)));
continue;
}

if (!$this->isValueValidDehydratedValue($v)) {
$nonScalarKeys[sprintf('%s.%s', $path, $k)] = get_debug_type($v);
}
}

return $nonScalarKeys;
}

/**
* Allows for specific keys to be written to a "fully-writable" array.
*
Expand Down

0 comments on commit e502400

Please sign in to comment.