@@ -48,6 +48,52 @@ to customize the normalized data. To do that, leverage the ``ObjectNormalizer``:
48
48
}
49
49
}
50
50
51
+ Caching the result of supportsNormalization
52
+ -------------------------------------------
53
+
54
+ .. versionadded :: 6.3
55
+
56
+ The ``getSupportedTypes() `` method was introduced in Symfony 6.3.
57
+
58
+ Both :class: `Symfony\\ Component\\ Serializer\\ Normalizer\\ NormalizerInterface ` and
59
+ :class: `Symfony\\ Component\\ Serializer\\ Normalizer\\ DenormalizerInterface ` contain a new method ``getSupportedTypes() ``.
60
+ This method allows normalizers or denormalizers to declare the type of objects they can handle, and whether they are
61
+ cacheable. With this info, even if the ``supports*() `` call is not cacheable, the Serializer can skip a ton of method calls
62
+ to ``supports*() `` improving performance substantially in some cases.
63
+
64
+ The ``getSupportedTypes() `` method should return an array where the keys represent the supported types, and the values
65
+ indicate whether the result of the ``supports*() `` method call can be cached or not. The format of the returned array is as
66
+ follows:
67
+
68
+ - The special key ``object `` can be used to indicate that the normalizer or denormalizer supports any classes or
69
+ interfaces.
70
+ - The special key ``* `` can be used to indicate that the normalizer or denormalizer might support any types.
71
+ - The other keys in the array should correspond to specific types that the normalizer or denormalizer supports.
72
+ - The values associated with each type should be a boolean indicating if the result of the ``supports*() `` method call for
73
+ that type can be cached or not. A value of ``true `` means that the result is cacheable, while ``false `` means that the
74
+ result is not cacheable.
75
+ - A ``null `` value for a type means that the normalizer or denormalizer does not support that type.
76
+
77
+ Here is an example of how to use the ``getSupportedTypes() `` method::
78
+
79
+ use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
80
+
81
+ class MyNormalizer implements NormalizerInterface
82
+ {
83
+ // ...
84
+
85
+ public function getSupportedTypes(?string $format): array
86
+ {
87
+ return [
88
+ 'object' => null, // Doesn't supports any classes or interfaces
89
+ '*' => false, // Supports any other types, but the result is not cacheable
90
+ MyCustomClass::class => true, // Supports MyCustomClass and result is cacheable
91
+ ];
92
+ }
93
+ }
94
+
95
+ Note that ``supports*() `` method implementations should not assume that ``getSupportedTypes() `` has been called before.
96
+
51
97
Registering it in your Application
52
98
----------------------------------
53
99
0 commit comments