Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[Serializer] Use context to compute MetadataAwareNameConverter cache
  • Loading branch information
antograssiot committed Nov 6, 2019
1 parent 620e894 commit 33a3953
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 14 deletions.
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,19 @@ private function getCacheValueForAttributesMetadata(string $class, $context): ar

return $cache;
}

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

try {
$key = md5(serialize($context[AbstractNormalizer::GROUPS] ?? []));
} catch (\Exception $e) {
$key = '';
} finally {
return $propertyName.$key;
}
}
}
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));
}
}

0 comments on commit 33a3953

Please sign in to comment.