Skip to content

Commit

Permalink
fix exeception and use PropertyAccessor to read the value
Browse files Browse the repository at this point in the history
  • Loading branch information
matheo committed Sep 21, 2023
1 parent 6e4854d commit daf8429
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 9 deletions.
34 changes: 34 additions & 0 deletions src/LiveComponent/phpunit.xml.dist.bak
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>

<!-- https://phpunit.de/manual/current/en/appendixes.configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/bin/.phpunit/phpunit.xsd"
colors="true"
bootstrap="vendor/autoload.php"
failOnRisky="true"
failOnWarning="true"
>
<php>
<ini name="error_reporting" value="-1" />
<server name="KERNEL_CLASS" value="Symfony\UX\LiveComponent\Tests\Fixtures\Kernel" />
<server name="DATABASE_URL" value="sqlite:///%kernel.project_dir%/var/data.db" />
<env name="SHELL_VERBOSITY" value="-1"/>
<server name="SYMFONY_DEPRECATIONS_HELPER" value="max[self]=0&amp;max[direct]=0"/>
</php>

<testsuites>
<testsuite name="symfony/ux-live-component Test Suite">
<directory>./tests/</directory>
</testsuite>
</testsuites>

<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src</directory>
</whitelist>
</filter>

<listeners>
<listener class="Symfony\Bridge\PhpUnit\SymfonyTestsListener"/>
</listeners>
</phpunit>
18 changes: 10 additions & 8 deletions src/LiveComponent/src/LiveComponentHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException;
use Symfony\Component\PropertyAccess\Exception\UninitializedPropertyException;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
use Symfony\Component\PropertyInfo\PropertyInfoExtractor;
use Symfony\Component\PropertyInfo\Type;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
Expand Down Expand Up @@ -358,14 +360,14 @@ 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(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));
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)));
}

return $value;
}

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

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

$dehydratedObjectValues = [];
foreach ((new \ReflectionClass($classType))->getProperties() as $property) {
$propertyValue = $this->propertyAccessor->getValue($value, $property->getName());
$propMetadata = $this->liveComponentMetadataFactory->createLivePropMetadata($classType, $property->getName(), $property, new LiveProp());
$dehydratedObjectValues[$property->getName()] = $this->dehydrateValue($propertyValue, $propMetadata, $parentObject);
foreach ((new PropertyInfoExtractor([new ReflectionExtractor()]))->getProperties($classType) as $property) {
$propertyValue = $this->propertyAccessor->getValue($value, $property);
$propMetadata = $this->liveComponentMetadataFactory->createLivePropMetadata($classType, $property, new \ReflectionProperty($classType, $property), new LiveProp());
$dehydratedObjectValues[$property] = $this->dehydrateValue($propertyValue, $propMetadata, $parentObject);
}

return $dehydratedObjectValues;
Expand Down Expand Up @@ -479,8 +481,8 @@ private function hydrateObjectValue(mixed $value, string $className, bool $allow
if (\is_array($value)) {
$object = new $className();
foreach ($value as $propertyName => $propertyValue) {
$reflexionClass = new \ReflectionClass($className);
$property = $reflexionClass->getProperty($propertyName);
$reflectionClass = new \ReflectionClass($className);
$property = $reflectionClass->getProperty($propertyName);
$propMetadata = $this->liveComponentMetadataFactory->createLivePropMetadata($className, $propertyName, $property, new LiveProp());
$this->propertyAccessor->setValue($object, $propertyName, $this->hydrateValue($propertyValue, $propMetadata, $component));
}
Expand Down
4 changes: 3 additions & 1 deletion src/LiveComponent/src/Twig/TemplateCacheWarmer.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,16 @@ public function __construct(private \IteratorAggregate $templateIterator, privat
{
}

public function warmUp(string $cacheDir): void
public function warmUp(string $cacheDir): array
{
$map = [];
foreach ($this->templateIterator as $item) {
$map[bin2hex(random_bytes(16))] = $item;
}

(new PhpArrayAdapter($cacheDir.'/'.$this->cacheFilename, new NullAdapter()))->warmUp(['map' => $map]);

return $map;
}

public function isOptional(): bool
Expand Down

0 comments on commit daf8429

Please sign in to comment.