@@ -26,11 +26,11 @@ final class MetadataAwareNameConverter implements AdvancedNameConverterInterface
26
26
*/
27
27
private $ fallbackNameConverter ;
28
28
29
- private $ normalizeCache = [];
29
+ private static $ normalizeCache = [];
30
30
31
- private $ denormalizeCache = [];
31
+ private static $ denormalizeCache = [];
32
32
33
- private $ attributesMetadataCache = [];
33
+ private static $ attributesMetadataCache = [];
34
34
35
35
public function __construct (ClassMetadataFactoryInterface $ metadataFactory , NameConverterInterface $ fallbackNameConverter = null )
36
36
{
@@ -47,11 +47,11 @@ public function normalize(string $propertyName, string $class = null, string $fo
47
47
return $ this ->normalizeFallback ($ propertyName , $ class , $ format , $ context );
48
48
}
49
49
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 );
52
52
}
53
53
54
- return $ this -> normalizeCache [$ class ][$ propertyName ] ?? $ this ->normalizeFallback ($ propertyName , $ class , $ format , $ context );
54
+ return self :: $ normalizeCache [$ class ][$ propertyName ] ?? $ this ->normalizeFallback ($ propertyName , $ class , $ format , $ context );
55
55
}
56
56
57
57
/**
@@ -63,11 +63,12 @@ public function denormalize(string $propertyName, string $class = null, string $
63
63
return $ this ->denormalizeFallback ($ propertyName , $ class , $ format , $ context );
64
64
}
65
65
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 );
68
69
}
69
70
70
- return $ this -> denormalizeCache [$ class ][$ propertyName ] ?? $ this ->denormalizeFallback ($ propertyName , $ class , $ format , $ context );
71
+ return self :: $ denormalizeCache [$ cacheKey ][$ propertyName ] ?? $ this ->denormalizeFallback ($ propertyName , $ class , $ format , $ context );
71
72
}
72
73
73
74
private function getCacheValueForNormalization (string $ propertyName , string $ class ): ?string
@@ -89,21 +90,22 @@ private function normalizeFallback(string $propertyName, string $class = null, s
89
90
return $ this ->fallbackNameConverter ? $ this ->fallbackNameConverter ->normalize ($ propertyName , $ class , $ format , $ context ) : $ propertyName ;
90
91
}
91
92
92
- private function getCacheValueForDenormalization (string $ propertyName , string $ class , $ context ): ?string
93
+ private function getCacheValueForDenormalization (string $ propertyName , string $ class , array $ context ): ?string
93
94
{
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 );
96
98
}
97
99
98
- return $ this -> attributesMetadataCache [$ class ][$ propertyName ] ?? null ;
100
+ return self :: $ attributesMetadataCache [$ cacheKey ][$ propertyName ] ?? null ;
99
101
}
100
102
101
103
private function denormalizeFallback (string $ propertyName , string $ class = null , string $ format = null , array $ context = []): string
102
104
{
103
105
return $ this ->fallbackNameConverter ? $ this ->fallbackNameConverter ->denormalize ($ propertyName , $ class , $ format , $ context ) : $ propertyName ;
104
106
}
105
107
106
- private function getCacheValueForAttributesMetadata (string $ class , $ context ): array
108
+ private function getCacheValueForAttributesMetadata (string $ class , array $ context ): array
107
109
{
108
110
if (!$ this ->metadataFactory ->hasMetadataFor ($ class )) {
109
111
return [];
@@ -130,4 +132,13 @@ private function getCacheValueForAttributesMetadata(string $class, $context): ar
130
132
131
133
return $ cache ;
132
134
}
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
+ }
133
144
}
0 commit comments