12
12
namespace Symfony \Component \Serializer \NameConverter ;
13
13
14
14
use Symfony \Component \Serializer \Mapping \Factory \ClassMetadataFactoryInterface ;
15
+ use Symfony \Component \Serializer \Normalizer \AbstractNormalizer ;
15
16
16
17
/**
17
18
* @author Fabien Bourigault <[email protected] >
@@ -62,11 +63,12 @@ public function denormalize($propertyName, string $class = null, string $format
62
63
return $ this ->denormalizeFallback ($ propertyName , $ class , $ format , $ context );
63
64
}
64
65
65
- if (!isset (self ::$ denormalizeCache [$ class ][$ propertyName ])) {
66
- self ::$ denormalizeCache [$ class ][$ propertyName ] = $ this ->getCacheValueForDenormalization ($ propertyName , $ class );
66
+ $ cacheKey = $ this ->getCacheKey ($ propertyName , $ context );
67
+ if (!isset (self ::$ denormalizeCache [$ class ][$ cacheKey ])) {
68
+ self ::$ denormalizeCache [$ class ][$ cacheKey ] = $ this ->getCacheValueForDenormalization ($ propertyName , $ class , $ context );
67
69
}
68
70
69
- return self ::$ denormalizeCache [$ class ][$ propertyName ] ?? $ this ->denormalizeFallback ($ propertyName , $ class , $ format , $ context );
71
+ return self ::$ denormalizeCache [$ class ][$ cacheKey ] ?? $ this ->denormalizeFallback ($ propertyName , $ class , $ format , $ context );
70
72
}
71
73
72
74
private function getCacheValueForNormalization ($ propertyName , string $ class )
@@ -88,21 +90,21 @@ private function normalizeFallback($propertyName, string $class = null, string $
88
90
return $ this ->fallbackNameConverter ? $ this ->fallbackNameConverter ->normalize ($ propertyName , $ class , $ format , $ context ) : $ propertyName ;
89
91
}
90
92
91
- private function getCacheValueForDenormalization ($ propertyName , string $ class )
93
+ private function getCacheValueForDenormalization ($ propertyName , string $ class, $ context )
92
94
{
93
95
if (!isset (self ::$ attributesMetadataCache [$ class ])) {
94
- self ::$ attributesMetadataCache [$ class ] = $ this ->getCacheValueForAttributesMetadata ($ class );
96
+ self ::$ attributesMetadataCache [$ class ] = $ this ->getCacheValueForAttributesMetadata ($ class, $ context );
95
97
}
96
98
97
- return self ::$ attributesMetadataCache [$ class ][$ propertyName ] ?? null ;
99
+ return self ::$ attributesMetadataCache [$ class ][$ this -> getCacheKey ( $ propertyName, $ context ) ] ?? null ;
98
100
}
99
101
100
102
private function denormalizeFallback ($ propertyName , string $ class = null , string $ format = null , array $ context = [])
101
103
{
102
104
return $ this ->fallbackNameConverter ? $ this ->fallbackNameConverter ->denormalize ($ propertyName , $ class , $ format , $ context ) : $ propertyName ;
103
105
}
104
106
105
- private function getCacheValueForAttributesMetadata (string $ class ): array
107
+ private function getCacheValueForAttributesMetadata (string $ class, $ context ): array
106
108
{
107
109
if (!$ this ->metadataFactory ->hasMetadataFor ($ class )) {
108
110
return [];
@@ -116,9 +118,33 @@ private function getCacheValueForAttributesMetadata(string $class): array
116
118
continue ;
117
119
}
118
120
119
- $ cache [$ metadata ->getSerializedName ()] = $ name ;
121
+ if (!($ groups = $ metadata ->getGroups ()) && ($ context [AbstractNormalizer::GROUPS ] ?? [])) {
122
+ continue ;
123
+ }
124
+ if ($ groups && !array_intersect ($ groups , $ context [AbstractNormalizer::GROUPS ] ?? [])) {
125
+ continue ;
126
+ }
127
+
128
+
129
+ $ cache [$ this ->getCacheKey ($ metadata ->getSerializedName (), $ context )] = $ name ;
120
130
}
121
131
122
132
return $ cache ;
123
133
}
134
+
135
+ private function getCacheKey ($ propertyName , array $ context ): string
136
+ {
137
+ if (isset ($ context ['cache_key ' ])) {
138
+ return $ propertyName .$ context ['cache_key ' ];
139
+ }
140
+
141
+ try {
142
+ $ key = md5 (serialize ($ context [AbstractNormalizer::GROUPS ] ?? []));
143
+ } catch (\Exception $ e ) {
144
+ $ key = '' ;
145
+ } finally {
146
+ return $ propertyName .$ key ;
147
+ }
148
+
149
+ }
124
150
}
0 commit comments