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

Skip to content

[Serializer] Add methods getSupportedTypes to allow better performance #49291

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 10, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
[Serializer] Add methods getSupportedTypes to allow better performance
  • Loading branch information
tucksaun authored and fabpot committed Mar 10, 2023
commit 400685a68b00b0932f8ef41096578872b643099c
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ public function normalize(mixed $object, string $format = null, array $context =
return $normalized;
}

public function getSupportedTypes(?string $format): array
{
return [
FlattenException::class => false,
];
}

public function supportsNormalization(mixed $data, string $format = null, array $context = []): bool
{
return $data instanceof FlattenException && ($context[Serializer::MESSENGER_SERIALIZATION_CONTEXT] ?? false);
Expand Down
2 changes: 2 additions & 0 deletions src/Symfony/Component/Serializer/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ CHANGELOG

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

6.2
---
Expand Down
15 changes: 15 additions & 0 deletions src/Symfony/Component/Serializer/Debug/TraceableNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ public function __construct(
) {
}

public function getSupportedTypes(?string $format): ?array
{
// @deprecated remove condition in 7.0
if (!method_exists($this->normalizer, 'getSupportedTypes')) {
return null;
}

return $this->normalizer->getSupportedTypes($format);
}

public function normalize(mixed $object, string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null
{
if (!$this->normalizer instanceof NormalizerInterface) {
Expand Down Expand Up @@ -114,8 +124,13 @@ public function setDenormalizer(DenormalizerInterface $denormalizer): void
$this->normalizer->setDenormalizer($denormalizer);
}

/**
* @deprecated since Symfony 6.3, use "getSupportedTypes()" instead
*/
public function hasCacheableSupportsMethod(): bool
{
trigger_deprecation('symfony/serializer', '6.3', 'The "%s()" method is deprecated, use "getSupportedTypes()" instead.', __METHOD__);

return $this->normalizer instanceof CacheableSupportsMethodInterface && $this->normalizer->hasCacheableSupportsMethod();
}

Expand Down
10 changes: 10 additions & 0 deletions src/Symfony/Component/Serializer/Debug/TraceableSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,16 @@ public function decode(string $data, string $format, array $context = []): mixed
return $result;
}

public function getSupportedTypes(?string $format): ?array
{
// @deprecated remove condition in 7.0
if (!method_exists($this->serializer, 'getSupportedTypes')) {
return null;
}

return $this->serializer->getSupportedTypes($format);
}

public function supportsNormalization(mixed $data, string $format = null, array $context = []): bool
{
return $this->serializer->supportsNormalization($data, $format, $context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,13 @@ public function __construct(ClassMetadataFactoryInterface $classMetadataFactory
}
}

/**
* @deprecated since Symfony 6.3, use "getSupportedTypes()" instead
*/
public function hasCacheableSupportsMethod(): bool
{
trigger_deprecation('symfony/serializer', '6.3', 'The "%s()" method is deprecated, use "getSupportedTypes()" instead.', __METHOD__);

return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ abstract protected function getAttributeValue(object $object, string $attribute,
*/
public function supportsDenormalization(mixed $data, string $type, string $format = null /* , array $context = [] */)
{
return class_exists($type) || (interface_exists($type, false) && $this->classDiscriminatorResolver && null !== $this->classDiscriminatorResolver->getMappingForClass($type));
return class_exists($type) || (interface_exists($type, false) && null !== $this->classDiscriminatorResolver?->getMappingForClass($type));
}

public function denormalize(mixed $data, string $type, string $format = null, array $context = [])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ class ArrayDenormalizer implements ContextAwareDenormalizerInterface, Denormaliz
{
use DenormalizerAwareTrait;

public function getSupportedTypes(?string $format): ?array
{
// @deprecated remove condition in 7.0
if (!method_exists($this->denormalizer, 'getSupportedTypes')) {
return null;
}

return $this->denormalizer->getSupportedTypes($format);
}

/**
* @throws NotNormalizableValueException
*/
Expand Down Expand Up @@ -69,8 +79,13 @@ public function supportsDenormalization(mixed $data, string $type, string $forma
&& $this->denormalizer->supportsDenormalization($data, substr($type, 0, -2), $format, $context);
}

/**
* @deprecated since Symfony 6.3, use "getSupportedTypes()" instead
*/
public function hasCacheableSupportsMethod(): bool
{
trigger_deprecation('symfony/serializer', '6.3', 'The "%s()" method is deprecated, use "getSupportedTypes()" instead.', __METHOD__);

return $this->denormalizer instanceof CacheableSupportsMethodInterface && $this->denormalizer->hasCacheableSupportsMethod();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ final class BackedEnumNormalizer implements NormalizerInterface, DenormalizerInt
*/
public const ALLOW_INVALID_VALUES = 'allow_invalid_values';

public function getSupportedTypes(?string $format): array
{
return [
\BackedEnum::class => true,
];
}

public function normalize(mixed $object, string $format = null, array $context = []): int|string
{
if (!$object instanceof \BackedEnum) {
Expand Down Expand Up @@ -78,8 +85,13 @@ public function supportsDenormalization(mixed $data, string $type, string $forma
return is_subclass_of($type, \BackedEnum::class);
}

/**
* @deprecated since Symfony 6.3, use "getSupportedTypes()" instead
*/
public function hasCacheableSupportsMethod(): bool
{
trigger_deprecation('symfony/serializer', '6.3', 'The "%s()" method is deprecated, use "getSupportedTypes()" instead.', __METHOD__);

return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
* supports*() methods will be cached by type and format.
*
* @author Kévin Dunglas <[email protected]>
*
* @deprecated since Symfony 6.3, implement "getSupportedTypes(?string $format)" instead
*/
interface CacheableSupportsMethodInterface
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ public function __construct(array $defaultContext = [], NameConverterInterface $
$this->nameConverter = $nameConverter;
}

public function getSupportedTypes(?string $format): ?array
{
return [
ConstraintViolationListInterface::class => __CLASS__ === static::class,
];
}

public function normalize(mixed $object, string $format = null, array $context = []): array
{
if (\array_key_exists(self::PAYLOAD_FIELDS, $context)) {
Expand Down Expand Up @@ -109,8 +116,13 @@ public function supportsNormalization(mixed $data, string $format = null /* , ar
return $data instanceof ConstraintViolationListInterface;
}

/**
* @deprecated since Symfony 6.3, use "getSupportedTypes()" instead
*/
public function hasCacheableSupportsMethod(): bool
{
trigger_deprecation('symfony/serializer', '6.3', 'The "%s()" method is deprecated, use "getSupportedTypes()" instead.', __METHOD__);

return __CLASS__ === static::class;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ class CustomNormalizer implements NormalizerInterface, DenormalizerInterface, Se
use ObjectToPopulateTrait;
use SerializerAwareTrait;

public function getSupportedTypes(?string $format): array
{
return [
NormalizableInterface::class => __CLASS__ === static::class,
];
}

public function normalize(mixed $object, string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null
{
return $object->normalize($this->serializer, $format, $context);
Expand Down Expand Up @@ -60,8 +67,13 @@ public function supportsDenormalization(mixed $data, string $type, string $forma
return is_subclass_of($type, DenormalizableInterface::class);
}

/**
* @deprecated since Symfony 6.3, use "getSupportedTypes()" instead
*/
public function hasCacheableSupportsMethod(): bool
{
trigger_deprecation('symfony/serializer', '6.3', 'The "%s()" method is deprecated, use "getSupportedTypes()" instead.', __METHOD__);

return __CLASS__ === static::class;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ public function __construct(MimeTypeGuesserInterface $mimeTypeGuesser = null)
$this->mimeTypeGuesser = $mimeTypeGuesser;
}

public function getSupportedTypes(?string $format): array
{
return [
\SplFileInfo::class => __CLASS__ === static::class,
\SplFileObject::class => __CLASS__ === static::class,
File::class => __CLASS__ === static::class,
];
}

public function normalize(mixed $object, string $format = null, array $context = []): string
{
if (!$object instanceof \SplFileInfo) {
Expand Down Expand Up @@ -118,8 +127,13 @@ public function supportsDenormalization(mixed $data, string $type, string $forma
return isset(self::SUPPORTED_TYPES[$type]);
}

/**
* @deprecated since Symfony 6.3, use "getSupportedTypes()" instead
*/
public function hasCacheableSupportsMethod(): bool
{
trigger_deprecation('symfony/serializer', '6.3', 'The "%s()" method is deprecated, use "getSupportedTypes()" instead.', __METHOD__);

return __CLASS__ === static::class;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ public function __construct(array $defaultContext = [])
$this->defaultContext = array_merge($this->defaultContext, $defaultContext);
}

public function getSupportedTypes(?string $format): array
{
return [
\DateInterval::class => __CLASS__ === static::class,
];
}

/**
* @throws InvalidArgumentException
*/
Expand All @@ -53,8 +60,13 @@ public function supportsNormalization(mixed $data, string $format = null /* , ar
return $data instanceof \DateInterval;
}

/**
* @deprecated since Symfony 6.3, use "getSupportedTypes()" instead
*/
public function hasCacheableSupportsMethod(): bool
{
trigger_deprecation('symfony/serializer', '6.3', 'The "%s()" method is deprecated, use "getSupportedTypes()" instead.', __METHOD__);

return __CLASS__ === static::class;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ public function setDefaultContext(array $defaultContext): void
$this->defaultContext = array_merge($this->defaultContext, $defaultContext);
}

public function getSupportedTypes(?string $format): array
{
return [
\DateTimeInterface::class => __CLASS__ === static::class,
\DateTimeImmutable::class => __CLASS__ === static::class,
\DateTime::class => __CLASS__ === static::class,
];
}

/**
* @throws InvalidArgumentException
*/
Expand Down Expand Up @@ -124,8 +133,13 @@ public function supportsDenormalization(mixed $data, string $type, string $forma
return isset(self::SUPPORTED_TYPES[$type]);
}

/**
* @deprecated since Symfony 6.3, use "getSupportedTypes()" instead
*/
public function hasCacheableSupportsMethod(): bool
{
trigger_deprecation('symfony/serializer', '6.3', 'The "%s()" method is deprecated, use "getSupportedTypes()" instead.', __METHOD__);

return __CLASS__ === static::class;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@
*/
class DateTimeZoneNormalizer implements NormalizerInterface, DenormalizerInterface, CacheableSupportsMethodInterface
{
public function getSupportedTypes(?string $format): array
{
return [
\DateTimeZone::class => __CLASS__ === static::class,
];
}

/**
* @throws InvalidArgumentException
*/
Expand Down Expand Up @@ -66,8 +73,13 @@ public function supportsDenormalization(mixed $data, string $type, string $forma
return \DateTimeZone::class === $type;
}

/**
* @deprecated since Symfony 6.3, use "getSupportedTypes()" instead
*/
public function hasCacheableSupportsMethod(): bool
{
trigger_deprecation('symfony/serializer', '6.3', 'The "%s()" method is deprecated, use "getSupportedTypes()" instead.', __METHOD__);

return __CLASS__ === static::class;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

/**
* @author Jordi Boggiano <[email protected]>
*
* @method getSupportedTypes(?string $format): ?array
*/
interface DenormalizerInterface
{
Expand Down Expand Up @@ -49,12 +51,32 @@ public function denormalize(mixed $data, string $type, string $format = null, ar
/**
* Checks whether the given class is supported for denormalization by this normalizer.
*
* @param mixed $data Data to denormalize from
* @param string $type The class to which the data should be denormalized
* @param string|null $format The format being deserialized from
* Since Symfony 6.3, this method will only be called if the type is
* included in the supported types returned by getSupportedTypes().
*
* @see getSupportedTypes()
*
* @param mixed $data Data to denormalize from
* @param string $type The class to which the data should be denormalized
* @param string|null $format The format being deserialized from
* @param array $context Options available to the denormalizer
*
* @return bool
*/
public function supportsDenormalization(mixed $data, string $type, string $format = null /* , array $context = [] */);

/*
* Return the types supported for normalization by this denormalizer for
* this format associated to a boolean value indicating if the result of
* supports*() methods can be cached or if the result can not be cached
* because it depends on the context.
* Returning null means this denormalizer will be considered for
* every format/class.
* Return an empty array if no type is supported for this format.
*
* @param string $format The format being (de-)serialized from or into
*
* @return array<class-string|string, bool>|null
*/
/* public function getSupportedTypes(?string $format): ?array; */
}
Loading