Skip to content

Commit daf8429

Browse files
author
matheo
committed
fix exeception and use PropertyAccessor to read the value
1 parent 6e4854d commit daf8429

File tree

3 files changed

+47
-9
lines changed

3 files changed

+47
-9
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<!-- https://phpunit.de/manual/current/en/appendixes.configuration.html -->
4+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:noNamespaceSchemaLocation="vendor/bin/.phpunit/phpunit.xsd"
6+
colors="true"
7+
bootstrap="vendor/autoload.php"
8+
failOnRisky="true"
9+
failOnWarning="true"
10+
>
11+
<php>
12+
<ini name="error_reporting" value="-1" />
13+
<server name="KERNEL_CLASS" value="Symfony\UX\LiveComponent\Tests\Fixtures\Kernel" />
14+
<server name="DATABASE_URL" value="sqlite:///%kernel.project_dir%/var/data.db" />
15+
<env name="SHELL_VERBOSITY" value="-1"/>
16+
<server name="SYMFONY_DEPRECATIONS_HELPER" value="max[self]=0&amp;max[direct]=0"/>
17+
</php>
18+
19+
<testsuites>
20+
<testsuite name="symfony/ux-live-component Test Suite">
21+
<directory>./tests/</directory>
22+
</testsuite>
23+
</testsuites>
24+
25+
<filter>
26+
<whitelist processUncoveredFilesFromWhitelist="true">
27+
<directory suffix=".php">./src</directory>
28+
</whitelist>
29+
</filter>
30+
31+
<listeners>
32+
<listener class="Symfony\Bridge\PhpUnit\SymfonyTestsListener"/>
33+
</listeners>
34+
</phpunit>

src/LiveComponent/src/LiveComponentHydrator.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException;
1717
use Symfony\Component\PropertyAccess\Exception\UninitializedPropertyException;
1818
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
19+
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
20+
use Symfony\Component\PropertyInfo\PropertyInfoExtractor;
1921
use Symfony\Component\PropertyInfo\Type;
2022
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
2123
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
@@ -358,14 +360,14 @@ private function dehydrateValue(mixed $value, LivePropMetadata $propMetadata, ob
358360
if (!$this->isValueValidDehydratedValue($value)) {
359361
$badKeys = $this->getNonScalarKeys($value, $propMetadata->getName());
360362
$badKeysText = implode(', ', array_map(fn ($key) => sprintf('%s: %s', $key, $badKeys[$key]), array_keys($badKeys)));
361-
throw new \LogicException(sprintf('The LiveProp "%s" on component "%s" is an array, but it contains one or more keys that are not scalars: %s', $propMetadata->getName(), $parentObject::class, $badKeysText));
363+
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)));
362364
}
363365

364366
return $value;
365367
}
366368

367369
if (!\is_object($value)) {
368-
throw new \LogicException(sprintf('The "%s" object has a hydrateMethod of "%s" but the method does not exist.', $parentObject::class, $propMetadata->hydrateMethod()));
370+
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));
369371
}
370372

371373
if (!$propMetadata->getType() || $propMetadata->isBuiltIn()) {
@@ -395,10 +397,10 @@ private function dehydrateObjectValue(object $value, string $classType, ?string
395397
}
396398

397399
$dehydratedObjectValues = [];
398-
foreach ((new \ReflectionClass($classType))->getProperties() as $property) {
399-
$propertyValue = $this->propertyAccessor->getValue($value, $property->getName());
400-
$propMetadata = $this->liveComponentMetadataFactory->createLivePropMetadata($classType, $property->getName(), $property, new LiveProp());
401-
$dehydratedObjectValues[$property->getName()] = $this->dehydrateValue($propertyValue, $propMetadata, $parentObject);
400+
foreach ((new PropertyInfoExtractor([new ReflectionExtractor()]))->getProperties($classType) as $property) {
401+
$propertyValue = $this->propertyAccessor->getValue($value, $property);
402+
$propMetadata = $this->liveComponentMetadataFactory->createLivePropMetadata($classType, $property, new \ReflectionProperty($classType, $property), new LiveProp());
403+
$dehydratedObjectValues[$property] = $this->dehydrateValue($propertyValue, $propMetadata, $parentObject);
402404
}
403405

404406
return $dehydratedObjectValues;
@@ -479,8 +481,8 @@ private function hydrateObjectValue(mixed $value, string $className, bool $allow
479481
if (\is_array($value)) {
480482
$object = new $className();
481483
foreach ($value as $propertyName => $propertyValue) {
482-
$reflexionClass = new \ReflectionClass($className);
483-
$property = $reflexionClass->getProperty($propertyName);
484+
$reflectionClass = new \ReflectionClass($className);
485+
$property = $reflectionClass->getProperty($propertyName);
484486
$propMetadata = $this->liveComponentMetadataFactory->createLivePropMetadata($className, $propertyName, $property, new LiveProp());
485487
$this->propertyAccessor->setValue($object, $propertyName, $this->hydrateValue($propertyValue, $propMetadata, $component));
486488
}

src/LiveComponent/src/Twig/TemplateCacheWarmer.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,16 @@ public function __construct(private \IteratorAggregate $templateIterator, privat
2626
{
2727
}
2828

29-
public function warmUp(string $cacheDir): void
29+
public function warmUp(string $cacheDir): array
3030
{
3131
$map = [];
3232
foreach ($this->templateIterator as $item) {
3333
$map[bin2hex(random_bytes(16))] = $item;
3434
}
3535

3636
(new PhpArrayAdapter($cacheDir.'/'.$this->cacheFilename, new NullAdapter()))->warmUp(['map' => $map]);
37+
38+
return $map;
3739
}
3840

3941
public function isOptional(): bool

0 commit comments

Comments
 (0)