Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit d4ae374

Browse filesBrowse files
Merge branch '4.4'
* 4.4: [Serializer] Use context to compute MetadataAwareNameConverter cache [VarDumper] Mark StubCaster as @Final
2 parents 0f2fca1 + a1155ea commit d4ae374
Copy full SHA for d4ae374

File tree

3 files changed

+50
-14
lines changed
Filter options

3 files changed

+50
-14
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/NameConverter/MetadataAwareNameConverter.php
+25-14Lines 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(string $propertyName, string $class = null, string $fo
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(string $propertyName, string $class = null, string $
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(string $propertyName, string $class): ?string
@@ -89,21 +90,22 @@ private function normalizeFallback(string $propertyName, string $class = null, s
8990
return $this->fallbackNameConverter ? $this->fallbackNameConverter->normalize($propertyName, $class, $format, $context) : $propertyName;
9091
}
9192

92-
private function getCacheValueForDenormalization(string $propertyName, string $class, $context): ?string
93+
private function getCacheValueForDenormalization(string $propertyName, string $class, array $context): ?string
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(string $propertyName, string $class = null, string $format = null, array $context = []): string
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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Tests/NameConverter/MetadataAwareNameConverterTest.php
+23Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,18 @@ public function fallbackAttributeProvider(): array
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
}

‎src/Symfony/Component/VarDumper/Caster/StubCaster.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/VarDumper/Caster/StubCaster.php
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
* Casts a caster's Stub.
1818
*
1919
* @author Nicolas Grekas <p@tchwork.com>
20+
*
21+
* @final since Symfony 4.4
2022
*/
2123
class StubCaster
2224
{

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.