Skip to content

Commit

Permalink
bug #34246 [Serializer] Use context to compute MetadataAwareNameConve…
Browse files Browse the repository at this point in the history
…rter cache (antograssiot)

This PR was merged into the 4.3 branch.

Discussion
----------

[Serializer] Use context to compute MetadataAwareNameConverter cache

| Q             | A
| ------------- | ---
| Branch?       | 4.3
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       |
| License       | MIT
| Doc PR        |

This is a follow up PR to #34035 to address @nicolas-grekas 's [comment](#34035 (comment))

The static cache has been re-introduced and the context is used to compute the cache key used for denormalization

Commits
-------

0ac5346 [Serializer] Use context to compute MetadataAwareNameConverter cache
  • Loading branch information
nicolas-grekas committed Nov 6, 2019
2 parents 50633f2 + 0ac5346 commit e91488c
Show file tree
Hide file tree
Showing 2 changed files with 48 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,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] ?? []));
}
}
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 e91488c

Please sign in to comment.