Skip to content

Commit

Permalink
[Serializer] Cache readability/writability computation
Browse files Browse the repository at this point in the history
  • Loading branch information
mtarld committed May 17, 2024
1 parent 450011a commit cd0cd9c
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions Normalizer/ObjectNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
class ObjectNormalizer extends AbstractObjectNormalizer
{
private static $reflectionCache = [];
private static $isReadableCache = [];
private static $isWritableCache = [];

protected $propertyAccessor;
protected $propertyInfoExtractor;
Expand Down Expand Up @@ -185,21 +187,23 @@ protected function isAllowedAttribute($classOrObject, string $attribute, ?string
if (!parent::isAllowedAttribute($classOrObject, $attribute, $format, $context)) {
return false;
}

$class = \is_object($classOrObject) ? \get_class($classOrObject) : $classOrObject;

if ($context['_read_attributes'] ?? true) {
return $this->propertyInfoExtractor->isReadable($class, $attribute) || $this->hasAttributeAccessorMethod($class, $attribute);
}
if (!isset(self::$isReadableCache[$class.$attribute])) {
self::$isReadableCache[$class.$attribute] = $this->propertyInfoExtractor->isReadable($class, $attribute) || $this->hasAttributeAccessorMethod($class, $attribute);
}

if ($this->propertyInfoExtractor->isWritable($class, $attribute)) {
return true;
return self::$isReadableCache[$class.$attribute];
}

if (($writeInfo = $this->writeInfoExtractor->getWriteInfo($class, $attribute)) && PropertyWriteInfo::TYPE_NONE !== $writeInfo->getType()) {
return true;
if (!isset(self::$isWritableCache[$class.$attribute])) {
self::$isWritableCache[$class.$attribute] = $this->propertyInfoExtractor->isWritable($class, $attribute)
|| (($writeInfo = $this->writeInfoExtractor->getWriteInfo($class, $attribute)) && PropertyWriteInfo::TYPE_NONE !== $writeInfo->getType());
}

return false;
return self::$isWritableCache[$class.$attribute];
}

private function hasAttributeAccessorMethod(string $class, string $attribute): bool
Expand Down

0 comments on commit cd0cd9c

Please sign in to comment.