@@ -55,6 +55,9 @@ public function supportsNormalization($data, $format = null)
55
55
*/
56
56
public function normalize ($ object , $ format = null , array $ context = array ())
57
57
{
58
+ if (!isset ($ context ['cache_key ' ])) {
59
+ $ context ['cache_key ' ] = $ this ->getCacheKey ($ context );
60
+ }
58
61
if ($ this ->isCircularReference ($ object , $ context )) {
59
62
return $ this ->handleCircularReference ($ object );
60
63
}
@@ -104,6 +107,9 @@ public function supportsDenormalization($data, $type, $format = null)
104
107
*/
105
108
public function denormalize ($ data , $ class , $ format = null , array $ context = array ())
106
109
{
110
+ if (!isset ($ context ['cache_key ' ])) {
111
+ $ context ['cache_key ' ] = $ this ->getCacheKey ($ context );
112
+ }
107
113
$ allowedAttributes = $ this ->getAllowedAttributes ($ class , $ context , true );
108
114
$ normalizedData = $ this ->prepareForDenormalization ($ data );
109
115
@@ -130,6 +136,16 @@ public function denormalize($data, $class, $format = null, array $context = arra
130
136
return $ object ;
131
137
}
132
138
139
+ private function getCacheKey (array $ context )
140
+ {
141
+ try {
142
+ return md5 (serialize ($ context ));
143
+ } catch (\Exception $ exception ) {
144
+ // The context cannot be serialized, skip the cache
145
+ return false ;
146
+ }
147
+ }
148
+
133
149
/**
134
150
* Gets and caches attributes for this class and context.
135
151
*
@@ -140,37 +156,39 @@ public function denormalize($data, $class, $format = null, array $context = arra
140
156
*/
141
157
private function getAttributes ($ object , array $ context )
142
158
{
143
- try {
144
- $ serializedContext = serialize ($ context );
145
- } catch (\Exception $ exception ) {
146
- // The context cannot be serialized, skip the cache
147
- return $ this ->extractAttributes ($ object , $ context );
148
- }
149
-
150
- $ key = sprintf ('%s-%s ' , get_class ($ object ), $ serializedContext );
159
+ $ class = get_class ($ object );
160
+ $ key = $ class .'- ' .$ context ['cache_key ' ];
151
161
152
162
if (isset ($ this ->attributesCache [$ key ])) {
153
163
return $ this ->attributesCache [$ key ];
154
164
}
155
165
156
- return $ this ->attributesCache [$ key ] = $ this ->extractAttributes ($ object , $ context );
166
+ $ allowedAttributes = $ this ->getAllowedAttributes ($ object , $ context , true );
167
+
168
+ if (false !== $ allowedAttributes ) {
169
+ if ($ context ['cache_key ' ]) {
170
+ $ this ->attributesCache [$ key ] = $ allowedAttributes ;
171
+ }
172
+
173
+ return $ allowedAttributes ;
174
+ }
175
+
176
+ if (isset ($ this ->attributesCache [$ class ])) {
177
+ return $ this ->attributesCache [$ class ];
178
+ }
179
+
180
+ return $ this ->attributesCache [$ class ] = $ this ->extractAttributes ($ object );
157
181
}
158
182
159
183
/**
160
184
* Extracts attributes for this class and context.
161
185
*
162
186
* @param object $object
163
- * @param array $context
164
187
*
165
188
* @return string[]
166
189
*/
167
- private function extractAttributes ($ object, array $ context )
190
+ private function extractAttributes ($ object )
168
191
{
169
- $ allowedAttributes = $ this ->getAllowedAttributes ($ object , $ context , true );
170
- if (false !== $ allowedAttributes ) {
171
- return $ allowedAttributes ;
172
- }
173
-
174
192
// If not using groups, detect manually
175
193
$ attributes = array ();
176
194
0 commit comments