Skip to content

Commit 400685a

Browse files
tucksaunfabpot
authored andcommitted
[Serializer] Add methods getSupportedTypes to allow better performance
1 parent a7e0b05 commit 400685a

40 files changed

+559
-43
lines changed

src/Symfony/Component/Messenger/Transport/Serialization/Normalizer/FlattenExceptionNormalizer.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ public function normalize(mixed $object, string $format = null, array $context =
4545
return $normalized;
4646
}
4747

48+
public function getSupportedTypes(?string $format): array
49+
{
50+
return [
51+
FlattenException::class => false,
52+
];
53+
}
54+
4855
public function supportsNormalization(mixed $data, string $format = null, array $context = []): bool
4956
{
5057
return $data instanceof FlattenException && ($context[Serializer::MESSENGER_SERIALIZATION_CONTEXT] ?? false);

src/Symfony/Component/Serializer/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ CHANGELOG
66

77
* Add `XmlEncoder::SAVE_OPTIONS` context option
88
* Add `BackedEnumNormalizer::ALLOW_INVALID_VALUES` context option
9+
* Add method `getSupportedTypes(?string $format)` to `NormalizerInterface` and `DenormalizerInterface`
910
* Deprecate `MissingConstructorArgumentsException` in favor of `MissingConstructorArgumentException`
11+
* Deprecate `CacheableSupportsMethodInterface` in favor of the new `getSupportedTypes(?string $format)` methods
1012

1113
6.2
1214
---

src/Symfony/Component/Serializer/Debug/TraceableNormalizer.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,16 @@ public function __construct(
3535
) {
3636
}
3737

38+
public function getSupportedTypes(?string $format): ?array
39+
{
40+
// @deprecated remove condition in 7.0
41+
if (!method_exists($this->normalizer, 'getSupportedTypes')) {
42+
return null;
43+
}
44+
45+
return $this->normalizer->getSupportedTypes($format);
46+
}
47+
3848
public function normalize(mixed $object, string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null
3949
{
4050
if (!$this->normalizer instanceof NormalizerInterface) {
@@ -114,8 +124,13 @@ public function setDenormalizer(DenormalizerInterface $denormalizer): void
114124
$this->normalizer->setDenormalizer($denormalizer);
115125
}
116126

127+
/**
128+
* @deprecated since Symfony 6.3, use "getSupportedTypes()" instead
129+
*/
117130
public function hasCacheableSupportsMethod(): bool
118131
{
132+
trigger_deprecation('symfony/serializer', '6.3', 'The "%s()" method is deprecated, use "getSupportedTypes()" instead.', __METHOD__);
133+
119134
return $this->normalizer instanceof CacheableSupportsMethodInterface && $this->normalizer->hasCacheableSupportsMethod();
120135
}
121136

src/Symfony/Component/Serializer/Debug/TraceableSerializer.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,16 @@ public function decode(string $data, string $format, array $context = []): mixed
128128
return $result;
129129
}
130130

131+
public function getSupportedTypes(?string $format): ?array
132+
{
133+
// @deprecated remove condition in 7.0
134+
if (!method_exists($this->serializer, 'getSupportedTypes')) {
135+
return null;
136+
}
137+
138+
return $this->serializer->getSupportedTypes($format);
139+
}
140+
131141
public function supportsNormalization(mixed $data, string $format = null, array $context = []): bool
132142
{
133143
return $this->serializer->supportsNormalization($data, $format, $context);

src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,13 @@ public function __construct(ClassMetadataFactoryInterface $classMetadataFactory
150150
}
151151
}
152152

153+
/**
154+
* @deprecated since Symfony 6.3, use "getSupportedTypes()" instead
155+
*/
153156
public function hasCacheableSupportsMethod(): bool
154157
{
158+
trigger_deprecation('symfony/serializer', '6.3', 'The "%s()" method is deprecated, use "getSupportedTypes()" instead.', __METHOD__);
159+
155160
return false;
156161
}
157162

src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ abstract protected function getAttributeValue(object $object, string $attribute,
300300
*/
301301
public function supportsDenormalization(mixed $data, string $type, string $format = null /* , array $context = [] */)
302302
{
303-
return class_exists($type) || (interface_exists($type, false) && $this->classDiscriminatorResolver && null !== $this->classDiscriminatorResolver->getMappingForClass($type));
303+
return class_exists($type) || (interface_exists($type, false) && null !== $this->classDiscriminatorResolver?->getMappingForClass($type));
304304
}
305305

306306
public function denormalize(mixed $data, string $type, string $format = null, array $context = [])

src/Symfony/Component/Serializer/Normalizer/ArrayDenormalizer.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,16 @@ class ArrayDenormalizer implements ContextAwareDenormalizerInterface, Denormaliz
2727
{
2828
use DenormalizerAwareTrait;
2929

30+
public function getSupportedTypes(?string $format): ?array
31+
{
32+
// @deprecated remove condition in 7.0
33+
if (!method_exists($this->denormalizer, 'getSupportedTypes')) {
34+
return null;
35+
}
36+
37+
return $this->denormalizer->getSupportedTypes($format);
38+
}
39+
3040
/**
3141
* @throws NotNormalizableValueException
3242
*/
@@ -69,8 +79,13 @@ public function supportsDenormalization(mixed $data, string $type, string $forma
6979
&& $this->denormalizer->supportsDenormalization($data, substr($type, 0, -2), $format, $context);
7080
}
7181

82+
/**
83+
* @deprecated since Symfony 6.3, use "getSupportedTypes()" instead
84+
*/
7285
public function hasCacheableSupportsMethod(): bool
7386
{
87+
trigger_deprecation('symfony/serializer', '6.3', 'The "%s()" method is deprecated, use "getSupportedTypes()" instead.', __METHOD__);
88+
7489
return $this->denormalizer instanceof CacheableSupportsMethodInterface && $this->denormalizer->hasCacheableSupportsMethod();
7590
}
7691
}

src/Symfony/Component/Serializer/Normalizer/BackedEnumNormalizer.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ final class BackedEnumNormalizer implements NormalizerInterface, DenormalizerInt
2727
*/
2828
public const ALLOW_INVALID_VALUES = 'allow_invalid_values';
2929

30+
public function getSupportedTypes(?string $format): array
31+
{
32+
return [
33+
\BackedEnum::class => true,
34+
];
35+
}
36+
3037
public function normalize(mixed $object, string $format = null, array $context = []): int|string
3138
{
3239
if (!$object instanceof \BackedEnum) {
@@ -78,8 +85,13 @@ public function supportsDenormalization(mixed $data, string $type, string $forma
7885
return is_subclass_of($type, \BackedEnum::class);
7986
}
8087

88+
/**
89+
* @deprecated since Symfony 6.3, use "getSupportedTypes()" instead
90+
*/
8191
public function hasCacheableSupportsMethod(): bool
8292
{
93+
trigger_deprecation('symfony/serializer', '6.3', 'The "%s()" method is deprecated, use "getSupportedTypes()" instead.', __METHOD__);
94+
8395
return true;
8496
}
8597
}

src/Symfony/Component/Serializer/Normalizer/CacheableSupportsMethodInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
* supports*() methods will be cached by type and format.
2020
*
2121
* @author Kévin Dunglas <dunglas@gmail.com>
22+
*
23+
* @deprecated since Symfony 6.3, implement "getSupportedTypes(?string $format)" instead
2224
*/
2325
interface CacheableSupportsMethodInterface
2426
{

src/Symfony/Component/Serializer/Normalizer/ConstraintViolationListNormalizer.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ public function __construct(array $defaultContext = [], NameConverterInterface $
3939
$this->nameConverter = $nameConverter;
4040
}
4141

42+
public function getSupportedTypes(?string $format): ?array
43+
{
44+
return [
45+
ConstraintViolationListInterface::class => __CLASS__ === static::class,
46+
];
47+
}
48+
4249
public function normalize(mixed $object, string $format = null, array $context = []): array
4350
{
4451
if (\array_key_exists(self::PAYLOAD_FIELDS, $context)) {
@@ -109,8 +116,13 @@ public function supportsNormalization(mixed $data, string $format = null /* , ar
109116
return $data instanceof ConstraintViolationListInterface;
110117
}
111118

119+
/**
120+
* @deprecated since Symfony 6.3, use "getSupportedTypes()" instead
121+
*/
112122
public function hasCacheableSupportsMethod(): bool
113123
{
124+
trigger_deprecation('symfony/serializer', '6.3', 'The "%s()" method is deprecated, use "getSupportedTypes()" instead.', __METHOD__);
125+
114126
return __CLASS__ === static::class;
115127
}
116128
}

0 commit comments

Comments
 (0)