Skip to content

Commit 7ad24d9

Browse files
committed
serializer optimisation
1 parent 5380e61 commit 7ad24d9

File tree

5 files changed

+74
-31
lines changed

5 files changed

+74
-31
lines changed

composer.lock

Lines changed: 28 additions & 28 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Symfony/Extensions/EmptyDataDenormalizer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,6 @@ public function denormalize($data, $class, $format = null, array $context = []):
4141
*/
4242
public function supportsDenormalization($data, $type, $format = null, array $context = []): bool
4343
{
44-
return \is_array($data) && 0 === \count($data);
44+
return empty($data);
4545
}
4646
}

src/Symfony/Extensions/EmptyDataNormalizer.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@
1919
*/
2020
final class EmptyDataNormalizer implements NormalizerInterface
2121
{
22+
/**
23+
* @var array<string, array<array-key, string>>
24+
*/
25+
private $localStorage = [];
26+
2227
/**
2328
* @inheritdoc
2429
*/
@@ -36,9 +41,23 @@ public function supportsNormalization($data, $format = null): bool
3641
{
3742
if(true === \is_object($data))
3843
{
39-
return 0 === \count((new \ReflectionClass($data))->getProperties());
44+
$class = \get_class($data);
45+
46+
if(false === isset($this->localStorage[$class]))
47+
{
48+
$this->localStorage[$class] = \array_map(
49+
static function(\ReflectionProperty $property): string
50+
{
51+
return (string) $property->name;
52+
},
53+
(new \ReflectionClass($data))->getProperties()
54+
);
55+
}
56+
57+
return empty($this->localStorage[$class]);
4058
}
4159

4260
return false;
4361
}
62+
4463
}

src/Symfony/Extensions/PropertyNameConverter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function normalize($propertyName): string
3939
*/
4040
public function denormalize($propertyName): string
4141
{
42-
if(false === isset($this->cache[$propertyName]))
42+
if(false === isset($this->localStorage[$propertyName]))
4343
{
4444
$joinedString = \preg_replace_callback(
4545
'/_(.?)/',

src/Symfony/Extensions/PropertyNormalizerWrapper.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@
2121
*/
2222
final class PropertyNormalizerWrapper extends PropertyNormalizer
2323
{
24+
/**
25+
* @var array<string, array<array-key, string>>
26+
*/
27+
private $localStorage = [];
28+
2429
/**
2530
* @inheritdoc
2631
*
@@ -37,4 +42,23 @@ protected function instantiateObject(
3742
{
3843
return $reflectionClass->newInstanceWithoutConstructor();
3944
}
45+
46+
/**
47+
* @inheritdoc
48+
*
49+
* @param object $object
50+
* @param string|null $format
51+
* @param array $context
52+
*/
53+
protected function extractAttributes($object, $format = null, array $context = []): array
54+
{
55+
$class = \get_class($object);
56+
57+
if(false === isset($this->localStorage[$class]))
58+
{
59+
$this->localStorage[$class] = parent::extractAttributes($object, $format, $context);
60+
}
61+
62+
return $this->localStorage[$class];
63+
}
4064
}

0 commit comments

Comments
 (0)