Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e91488c

Browse files
bug #34246 [Serializer] Use context to compute MetadataAwareNameConverter 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
2 parents 50633f2 + 0ac5346 commit e91488c

File tree

2 files changed

+48
-14
lines changed

2 files changed

+48
-14
lines changed

src/Symfony/Component/Serializer/NameConverter/MetadataAwareNameConverter.php

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ final class MetadataAwareNameConverter implements AdvancedNameConverterInterface
2626
*/
2727
private $fallbackNameConverter;
2828

29-
private $normalizeCache = [];
29+
private static $normalizeCache = [];
3030

31-
private $denormalizeCache = [];
31+
private static $denormalizeCache = [];
3232

33-
private $attributesMetadataCache = [];
33+
private static $attributesMetadataCache = [];
3434

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

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

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

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

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

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

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

92-
private function getCacheValueForDenormalization($propertyName, string $class, $context)
93+
private function getCacheValueForDenormalization($propertyName, string $class, array $context)
9394
{
94-
if (!isset($this->attributesMetadataCache[$class])) {
95-
$this->attributesMetadataCache[$class] = $this->getCacheValueForAttributesMetadata($class, $context);
95+
$cacheKey = $this->getCacheKey($class, $context);
96+
if (!isset(self::$attributesMetadataCache[$cacheKey])) {
97+
self::$attributesMetadataCache[$cacheKey] = $this->getCacheValueForAttributesMetadata($class, $context);
9698
}
9799

98-
return $this->attributesMetadataCache[$class][$propertyName] ?? null;
100+
return self::$attributesMetadataCache[$cacheKey][$propertyName] ?? null;
99101
}
100102

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

106-
private function getCacheValueForAttributesMetadata(string $class, $context): array
108+
private function getCacheValueForAttributesMetadata(string $class, array $context): array
107109
{
108110
if (!$this->metadataFactory->hasMetadataFor($class)) {
109111
return [];
@@ -130,4 +132,13 @@ private function getCacheValueForAttributesMetadata(string $class, $context): ar
130132

131133
return $cache;
132134
}
135+
136+
private function getCacheKey(string $class, array $context): string
137+
{
138+
if (isset($context['cache_key'])) {
139+
return $class.'-'.$context['cache_key'];
140+
}
141+
142+
return $class.md5(serialize($context[AbstractNormalizer::GROUPS] ?? []));
143+
}
133144
}

src/Symfony/Component/Serializer/Tests/NameConverter/MetadataAwareNameConverterTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,18 @@ public function fallbackAttributeProvider()
117117
];
118118
}
119119

120+
/**
121+
* @dataProvider attributeAndContextProvider
122+
*/
123+
public function testNormalizeWithGroups($propertyName, $expected, $context = [])
124+
{
125+
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
126+
127+
$nameConverter = new MetadataAwareNameConverter($classMetadataFactory);
128+
129+
$this->assertEquals($expected, $nameConverter->normalize($propertyName, OtherSerializedNameDummy::class, null, $context));
130+
}
131+
120132
/**
121133
* @dataProvider attributeAndContextProvider
122134
*/
@@ -138,4 +150,15 @@ public function attributeAndContextProvider()
138150
['buz', 'buz', []],
139151
];
140152
}
153+
154+
public function testDenormalizeWithCacheContext()
155+
{
156+
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
157+
158+
$nameConverter = new MetadataAwareNameConverter($classMetadataFactory);
159+
160+
$this->assertEquals('buz', $nameConverter->denormalize('buz', OtherSerializedNameDummy::class, null, ['groups' => ['a']]));
161+
$this->assertEquals('buzForExport', $nameConverter->denormalize('buz', OtherSerializedNameDummy::class, null, ['groups' => ['b']]));
162+
$this->assertEquals('buz', $nameConverter->denormalize('buz', OtherSerializedNameDummy::class));
163+
}
141164
}

0 commit comments

Comments
 (0)