23
23
use Symfony \Component \Serializer \Exception \NotNormalizableValueException ;
24
24
use Symfony \Component \Serializer \Exception \PartialDenormalizationException ;
25
25
use Symfony \Component \Serializer \Normalizer \AbstractObjectNormalizer ;
26
+ use Symfony \Component \Serializer \Normalizer \CacheableSupport ;
26
27
use Symfony \Component \Serializer \Normalizer \CacheableSupportsMethodInterface ;
27
28
use Symfony \Component \Serializer \Normalizer \ContextAwareDenormalizerInterface ;
28
29
use Symfony \Component \Serializer \Normalizer \ContextAwareNormalizerInterface ;
29
30
use Symfony \Component \Serializer \Normalizer \DenormalizerAwareInterface ;
30
31
use Symfony \Component \Serializer \Normalizer \DenormalizerInterface ;
31
32
use Symfony \Component \Serializer \Normalizer \NormalizerAwareInterface ;
32
33
use Symfony \Component \Serializer \Normalizer \NormalizerInterface ;
34
+ use Symfony \Component \Uid \AbstractUid ;
33
35
34
36
/**
35
37
* Serializer serializes and deserializes data.
@@ -247,34 +249,56 @@ public function supportsDenormalization(mixed $data, string $type, string $forma
247
249
private function getNormalizer (mixed $ data , ?string $ format , array $ context ): ?NormalizerInterface
248
250
{
249
251
$ type = \is_object ($ data ) ? $ data ::class : 'native- ' .\gettype ($ data );
250
-
252
+ $ minCached = CacheableSupport::SupportAlways;
253
+ $ minUncached = CacheableSupport::SupportNever;
251
254
if (!isset ($ this ->normalizerCache [$ format ][$ type ])) {
255
+ $ minCached = CacheableSupport::Support;
256
+ $ minUncached = CacheableSupport::SupportNot;
252
257
$ this ->normalizerCache [$ format ][$ type ] = [];
253
-
254
258
foreach ($ this ->normalizers as $ k => $ normalizer ) {
255
259
if (!$ normalizer instanceof NormalizerInterface) {
256
260
continue ;
257
261
}
258
262
259
- if (!$ normalizer instanceof CacheableSupportsMethodInterface || !$ normalizer ->hasCacheableSupportsMethod ()) {
260
- $ this ->normalizerCache [$ format ][$ type ][$ k ] = false ;
261
- } elseif ($ normalizer ->supportsNormalization ($ data , $ format , $ context )) {
262
- $ this ->normalizerCache [$ format ][$ type ][$ k ] = true ;
263
+ $ support = $ this ->supportsNormalizationWrapper ($ normalizer , $ data , $ format , $ context );
264
+ if (CacheableSupport::SupportNever === $ support ) {
265
+ continue ;
266
+ }
267
+
268
+ $ this ->normalizerCache [$ format ][$ type ][$ k ] = $ support ;
269
+ if (CacheableSupport::SupportAlways === $ support ) {
263
270
break ;
264
271
}
265
272
}
266
273
}
267
274
268
275
foreach ($ this ->normalizerCache [$ format ][$ type ] as $ k => $ cached ) {
269
276
$ normalizer = $ this ->normalizers [$ k ];
270
- if ($ cached || $ normalizer -> supportsNormalization ( $ data , $ format , $ context )) {
277
+ if ($ cached-> value >= $ minCached -> value || ( $ cached -> value > $ minUncached -> value && $ this -> supportsNormalizationWrapper ( $ normalizer , $ data , $ format , $ context)-> value > CacheableSupport::SupportNot-> value )) {
271
278
return $ normalizer ;
272
279
}
273
280
}
274
281
275
282
return null ;
276
283
}
277
284
285
+ /**
286
+ * Backwards-Compatibility layer for CacheableSupportsMethodInterface -> CacheableSupport.
287
+ */
288
+ private function supportsNormalizationWrapper (NormalizerInterface $ normalizer , mixed $ data , ?string $ format , array $ context ): CacheableSupport
289
+ {
290
+ $ value = $ normalizer ->supportsNormalization ($ data , $ format , $ context );
291
+ if (\is_bool ($ value )) {
292
+ trigger_deprecation ('symfony/serializer ' , '6.2 ' , 'Returning boolean from "%s::%s" is deprecated, return "%s" instead. ' , NormalizerInterface::class, 'supports() ' , CacheableSupport::class);
293
+ }
294
+
295
+ return match ($ value ) {
296
+ true => $ normalizer instanceof CacheableSupportsMethodInterface && $ normalizer ->hasCacheableSupportsMethod () ? CacheableSupport::SupportAlways : CacheableSupport::Support,
297
+ false => $ normalizer instanceof CacheableSupportsMethodInterface && $ normalizer ->hasCacheableSupportsMethod () ? CacheableSupport::SupportNever : CacheableSupport::SupportNot,
298
+ default => $ value
299
+ };
300
+ }
301
+
278
302
/**
279
303
* Returns a matching denormalizer.
280
304
*
@@ -285,33 +309,57 @@ private function getNormalizer(mixed $data, ?string $format, array $context): ?N
285
309
*/
286
310
private function getDenormalizer (mixed $ data , string $ class , ?string $ format , array $ context ): ?DenormalizerInterface
287
311
{
312
+ $ minCached = CacheableSupport::SupportAlways;
313
+ $ minUncached = CacheableSupport::SupportNever;
288
314
if (!isset ($ this ->denormalizerCache [$ format ][$ class ])) {
315
+ $ minCached = CacheableSupport::Support;
316
+ $ minUncached = CacheableSupport::SupportNot;
289
317
$ this ->denormalizerCache [$ format ][$ class ] = [];
290
318
291
319
foreach ($ this ->normalizers as $ k => $ normalizer ) {
292
320
if (!$ normalizer instanceof DenormalizerInterface) {
293
321
continue ;
294
322
}
295
323
296
- if (!$ normalizer instanceof CacheableSupportsMethodInterface || !$ normalizer ->hasCacheableSupportsMethod ()) {
297
- $ this ->denormalizerCache [$ format ][$ class ][$ k ] = false ;
298
- } elseif ($ normalizer ->supportsDenormalization (null , $ class , $ format , $ context )) {
299
- $ this ->denormalizerCache [$ format ][$ class ][$ k ] = true ;
324
+ $ support = $ this ->supportsDenormalizationWrapper ($ normalizer , $ data , $ class , $ format , $ context );
325
+ if (CacheableSupport::SupportNever === $ support ) {
326
+ continue ;
327
+ }
328
+
329
+ $ this ->denormalizerCache [$ format ][$ class ][$ k ] = $ support ;
330
+ if (CacheableSupport::SupportAlways === $ support ) {
300
331
break ;
301
332
}
302
333
}
303
334
}
304
335
305
336
foreach ($ this ->denormalizerCache [$ format ][$ class ] as $ k => $ cached ) {
306
337
$ normalizer = $ this ->normalizers [$ k ];
307
- if ($ cached || $ normalizer -> supportsDenormalization ( $ data , $ class , $ format , $ context )) {
338
+ if ($ cached-> value >= $ minCached -> value || ( $ cached -> value > $ minUncached -> value && $ this -> supportsDenormalizationWrapper ( $ normalizer , $ data , $ class , $ format , $ context)-> value > CacheableSupport::SupportNot-> value )) {
308
339
return $ normalizer ;
309
340
}
310
341
}
311
342
312
343
return null ;
313
344
}
314
345
346
+ /**
347
+ * Backwards-Compatibility layer for CacheableSupportsMethodInterface -> CacheableSupport.
348
+ */
349
+ private function supportsDenormalizationWrapper (DenormalizerInterface $ normalizer , mixed $ data , string $ class , ?string $ format , array $ context ): CacheableSupport
350
+ {
351
+ $ value = $ normalizer ->supportsDenormalization ($ data , $ class , $ format , $ context );
352
+ if (\is_bool ($ value )) {
353
+ trigger_deprecation ('symfony/serializer ' , '6.2 ' , 'Returning boolean from "%s::%s" is deprecated, return "%s" instead. ' , DenormalizerInterface::class, 'supports() ' , CacheableSupport::class);
354
+ }
355
+
356
+ return match ($ value ) {
357
+ true => $ normalizer instanceof CacheableSupportsMethodInterface && $ normalizer ->hasCacheableSupportsMethod () ? CacheableSupport::SupportAlways : CacheableSupport::Support,
358
+ false => $ normalizer instanceof CacheableSupportsMethodInterface && $ normalizer ->hasCacheableSupportsMethod () ? CacheableSupport::SupportNever : CacheableSupport::SupportNot,
359
+ default => $ value
360
+ };
361
+ }
362
+
315
363
final public function encode (mixed $ data , string $ format , array $ context = []): string
316
364
{
317
365
return $ this ->encoder ->encode ($ data , $ format , $ context );
0 commit comments