Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Serializer] Use context to compute MetadataAwareNameConverter cache #34246

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ final class MetadataAwareNameConverter implements AdvancedNameConverterInterface
*/
private $fallbackNameConverter;

private $normalizeCache = [];
private static $normalizeCache = [];

private $denormalizeCache = [];
private static $denormalizeCache = [];

private $attributesMetadataCache = [];
private static $attributesMetadataCache = [];

public function __construct(ClassMetadataFactoryInterface $metadataFactory, NameConverterInterface $fallbackNameConverter = null)
{
Expand All @@ -47,11 +47,11 @@ public function normalize($propertyName, string $class = null, string $format =
return $this->normalizeFallback($propertyName, $class, $format, $context);
}

if (!isset($this->normalizeCache[$class][$propertyName])) {
$this->normalizeCache[$class][$propertyName] = $this->getCacheValueForNormalization($propertyName, $class);
if (!isset(self::$normalizeCache[$class][$propertyName])) {
self::$normalizeCache[$class][$propertyName] = $this->getCacheValueForNormalization($propertyName, $class);
}

return $this->normalizeCache[$class][$propertyName] ?? $this->normalizeFallback($propertyName, $class, $format, $context);
return self::$normalizeCache[$class][$propertyName] ?? $this->normalizeFallback($propertyName, $class, $format, $context);
}

/**
Expand All @@ -63,11 +63,12 @@ public function denormalize($propertyName, string $class = null, string $format
return $this->denormalizeFallback($propertyName, $class, $format, $context);
}

if (!isset($this->denormalizeCache[$class][$propertyName])) {
$this->denormalizeCache[$class][$propertyName] = $this->getCacheValueForDenormalization($propertyName, $class, $context);
$cacheKey = $this->getCacheKey($class, $context);
if (!isset(self::$denormalizeCache[$cacheKey][$propertyName])) {
self::$denormalizeCache[$cacheKey][$propertyName] = $this->getCacheValueForDenormalization($propertyName, $class, $context);
}

return $this->denormalizeCache[$class][$propertyName] ?? $this->denormalizeFallback($propertyName, $class, $format, $context);
return self::$denormalizeCache[$cacheKey][$propertyName] ?? $this->denormalizeFallback($propertyName, $class, $format, $context);
}

private function getCacheValueForNormalization($propertyName, string $class)
Expand All @@ -89,21 +90,22 @@ private function normalizeFallback($propertyName, string $class = null, string $
return $this->fallbackNameConverter ? $this->fallbackNameConverter->normalize($propertyName, $class, $format, $context) : $propertyName;
}

private function getCacheValueForDenormalization($propertyName, string $class, $context)
private function getCacheValueForDenormalization($propertyName, string $class, array $context)
{
if (!isset($this->attributesMetadataCache[$class])) {
$this->attributesMetadataCache[$class] = $this->getCacheValueForAttributesMetadata($class, $context);
$cacheKey = $this->getCacheKey($class, $context);
if (!isset(self::$attributesMetadataCache[$cacheKey])) {
self::$attributesMetadataCache[$cacheKey] = $this->getCacheValueForAttributesMetadata($class, $context);
}

return $this->attributesMetadataCache[$class][$propertyName] ?? null;
return self::$attributesMetadataCache[$cacheKey][$propertyName] ?? null;
}

private function denormalizeFallback($propertyName, string $class = null, string $format = null, array $context = [])
{
return $this->fallbackNameConverter ? $this->fallbackNameConverter->denormalize($propertyName, $class, $format, $context) : $propertyName;
}

private function getCacheValueForAttributesMetadata(string $class, $context): array
private function getCacheValueForAttributesMetadata(string $class, array $context): array
{
if (!$this->metadataFactory->hasMetadataFor($class)) {
return [];
Expand All @@ -130,4 +132,13 @@ private function getCacheValueForAttributesMetadata(string $class, $context): ar

return $cache;
}

private function getCacheKey(string $class, array $context): string
{
if (isset($context['cache_key'])) {
return $class.'-'.$context['cache_key'];
}

return $class.md5(serialize($context[AbstractNormalizer::GROUPS] ?? []));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,18 @@ public function fallbackAttributeProvider()
];
}

/**
* @dataProvider attributeAndContextProvider
*/
public function testNormalizeWithGroups($propertyName, $expected, $context = [])
{
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));

$nameConverter = new MetadataAwareNameConverter($classMetadataFactory);

$this->assertEquals($expected, $nameConverter->normalize($propertyName, OtherSerializedNameDummy::class, null, $context));
}

/**
* @dataProvider attributeAndContextProvider
*/
Expand All @@ -138,4 +150,15 @@ public function attributeAndContextProvider()
['buz', 'buz', []],
];
}

public function testDenormalizeWithCacheContext()
{
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));

$nameConverter = new MetadataAwareNameConverter($classMetadataFactory);

$this->assertEquals('buz', $nameConverter->denormalize('buz', OtherSerializedNameDummy::class, null, ['groups' => ['a']]));
$this->assertEquals('buzForExport', $nameConverter->denormalize('buz', OtherSerializedNameDummy::class, null, ['groups' => ['b']]));
$this->assertEquals('buz', $nameConverter->denormalize('buz', OtherSerializedNameDummy::class));
}
}