Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3c9b00d

Browse files
tucksaunnicolas-grekas
authored andcommitted
[Serializer] Add methods getSupportedTypes to allow better performance
1 parent 898a723 commit 3c9b00d

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 <[email protected]>
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
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ class CustomNormalizer implements NormalizerInterface, DenormalizerInterface, Se
2222
use ObjectToPopulateTrait;
2323
use SerializerAwareTrait;
2424

25+
public function getSupportedTypes(?string $format): array
26+
{
27+
return [
28+
NormalizableInterface::class => __CLASS__ === static::class,
29+
];
30+
}
31+
2532
public function normalize(mixed $object, string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null
2633
{
2734
return $object->normalize($this->serializer, $format, $context);
@@ -60,8 +67,13 @@ public function supportsDenormalization(mixed $data, string $type, string $forma
6067
return is_subclass_of($type, DenormalizableInterface::class);
6168
}
6269

70+
/**
71+
* @deprecated since Symfony 6.3, use "getSupportedTypes()" instead
72+
*/
6373
public function hasCacheableSupportsMethod(): bool
6474
{
75+
trigger_deprecation('symfony/serializer', '6.3', 'The "%s()" method is deprecated, use "getSupportedTypes()" instead.', __METHOD__);
76+
6577
return __CLASS__ === static::class;
6678
}
6779
}

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,15 @@ public function __construct(MimeTypeGuesserInterface $mimeTypeGuesser = null)
4545
$this->mimeTypeGuesser = $mimeTypeGuesser;
4646
}
4747

48+
public function getSupportedTypes(?string $format): array
49+
{
50+
return [
51+
\SplFileInfo::class => __CLASS__ === static::class,
52+
\SplFileObject::class => __CLASS__ === static::class,
53+
File::class => __CLASS__ === static::class,
54+
];
55+
}
56+
4857
public function normalize(mixed $object, string $format = null, array $context = []): string
4958
{
5059
if (!$object instanceof \SplFileInfo) {
@@ -118,8 +127,13 @@ public function supportsDenormalization(mixed $data, string $type, string $forma
118127
return isset(self::SUPPORTED_TYPES[$type]);
119128
}
120129

130+
/**
131+
* @deprecated since Symfony 6.3, use "getSupportedTypes()" instead
132+
*/
121133
public function hasCacheableSupportsMethod(): bool
122134
{
135+
trigger_deprecation('symfony/serializer', '6.3', 'The "%s()" method is deprecated, use "getSupportedTypes()" instead.', __METHOD__);
136+
123137
return __CLASS__ === static::class;
124138
}
125139

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ public function __construct(array $defaultContext = [])
3333
$this->defaultContext = array_merge($this->defaultContext, $defaultContext);
3434
}
3535

36+
public function getSupportedTypes(?string $format): array
37+
{
38+
return [
39+
\DateInterval::class => __CLASS__ === static::class,
40+
];
41+
}
42+
3643
/**
3744
* @throws InvalidArgumentException
3845
*/
@@ -53,8 +60,13 @@ public function supportsNormalization(mixed $data, string $format = null /* , ar
5360
return $data instanceof \DateInterval;
5461
}
5562

63+
/**
64+
* @deprecated since Symfony 6.3, use "getSupportedTypes()" instead
65+
*/
5666
public function hasCacheableSupportsMethod(): bool
5767
{
68+
trigger_deprecation('symfony/serializer', '6.3', 'The "%s()" method is deprecated, use "getSupportedTypes()" instead.', __METHOD__);
69+
5870
return __CLASS__ === static::class;
5971
}
6072

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,15 @@ public function setDefaultContext(array $defaultContext): void
4747
$this->defaultContext = array_merge($this->defaultContext, $defaultContext);
4848
}
4949

50+
public function getSupportedTypes(?string $format): array
51+
{
52+
return [
53+
\DateTimeInterface::class => __CLASS__ === static::class,
54+
\DateTimeImmutable::class => __CLASS__ === static::class,
55+
\DateTime::class => __CLASS__ === static::class,
56+
];
57+
}
58+
5059
/**
5160
* @throws InvalidArgumentException
5261
*/
@@ -124,8 +133,13 @@ public function supportsDenormalization(mixed $data, string $type, string $forma
124133
return isset(self::SUPPORTED_TYPES[$type]);
125134
}
126135

136+
/**
137+
* @deprecated since Symfony 6.3, use "getSupportedTypes()" instead
138+
*/
127139
public function hasCacheableSupportsMethod(): bool
128140
{
141+
trigger_deprecation('symfony/serializer', '6.3', 'The "%s()" method is deprecated, use "getSupportedTypes()" instead.', __METHOD__);
142+
129143
return __CLASS__ === static::class;
130144
}
131145

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@
2222
*/
2323
class DateTimeZoneNormalizer implements NormalizerInterface, DenormalizerInterface, CacheableSupportsMethodInterface
2424
{
25+
public function getSupportedTypes(?string $format): array
26+
{
27+
return [
28+
\DateTimeZone::class => __CLASS__ === static::class,
29+
];
30+
}
31+
2532
/**
2633
* @throws InvalidArgumentException
2734
*/
@@ -66,8 +73,13 @@ public function supportsDenormalization(mixed $data, string $type, string $forma
6673
return \DateTimeZone::class === $type;
6774
}
6875

76+
/**
77+
* @deprecated since Symfony 6.3, use "getSupportedTypes()" instead
78+
*/
6979
public function hasCacheableSupportsMethod(): bool
7080
{
81+
trigger_deprecation('symfony/serializer', '6.3', 'The "%s()" method is deprecated, use "getSupportedTypes()" instead.', __METHOD__);
82+
7183
return __CLASS__ === static::class;
7284
}
7385
}

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

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
/**
2323
* @author Jordi Boggiano <[email protected]>
24+
*
25+
* @method getSupportedTypes(?string $format): ?array
2426
*/
2527
interface DenormalizerInterface
2628
{
@@ -49,12 +51,32 @@ public function denormalize(mixed $data, string $type, string $format = null, ar
4951
/**
5052
* Checks whether the given class is supported for denormalization by this normalizer.
5153
*
52-
* @param mixed $data Data to denormalize from
53-
* @param string $type The class to which the data should be denormalized
54-
* @param string|null $format The format being deserialized from
54+
* Since Symfony 6.3, this method will only be called if the type is
55+
* included in the supported types returned by getSupportedTypes().
56+
*
57+
* @see getSupportedTypes()
58+
*
59+
* @param mixed $data Data to denormalize from
60+
* @param string $type The class to which the data should be denormalized
61+
* @param string|null $format The format being deserialized from
5562
* @param array $context Options available to the denormalizer
5663
*
5764
* @return bool
5865
*/
5966
public function supportsDenormalization(mixed $data, string $type, string $format = null /* , array $context = [] */);
67+
68+
/*
69+
* Return the types supported for normalization by this denormalizer for
70+
* this format associated to a boolean value indicating if the result of
71+
* supports*() methods can be cached or if the result can not be cached
72+
* because it depends on the context.
73+
* Returning null means this denormalizer will be considered for
74+
* every format/class.
75+
* Return an empty array if no type is supported for this format.
76+
*
77+
* @param string $format The format being (de-)serialized from or into
78+
*
79+
* @return array<class-string|string, bool>|null
80+
*/
81+
/* public function getSupportedTypes(?string $format): ?array; */
6082
}

0 commit comments

Comments
 (0)