diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php index f174c4c8a1b09..8c41ba64e7640 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php @@ -1958,6 +1958,7 @@ private function registerMessengerConfiguration(array $config, ContainerBuilder if (!interface_exists(DenormalizerInterface::class)) { $container->removeDefinition('serializer.normalizer.flatten_exception'); + $container->removeDefinition('serializer.normalizer.redelivery_stamp'); } if (ContainerBuilder::willBeAvailable('symfony/amqp-messenger', AmqpTransportFactory::class, ['symfony/framework-bundle', 'symfony/messenger'], true)) { diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/messenger.php b/src/Symfony/Bundle/FrameworkBundle/Resources/config/messenger.php index 813d503000de4..e5b0fec6d9a1d 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/messenger.php +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/messenger.php @@ -38,6 +38,7 @@ use Symfony\Component\Messenger\Transport\InMemoryTransportFactory; use Symfony\Component\Messenger\Transport\Sender\SendersLocator; use Symfony\Component\Messenger\Transport\Serialization\Normalizer\FlattenExceptionNormalizer; +use Symfony\Component\Messenger\Transport\Serialization\Normalizer\RedeliveryStampNormalizer; use Symfony\Component\Messenger\Transport\Serialization\PhpSerializer; use Symfony\Component\Messenger\Transport\Serialization\Serializer; use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface; @@ -73,6 +74,9 @@ ->set('serializer.normalizer.flatten_exception', FlattenExceptionNormalizer::class) ->tag('serializer.normalizer', ['priority' => -880]) + ->set('serializer.normalizer.redelivery_stamp', RedeliveryStampNormalizer::class) + ->tag('serializer.normalizer') + ->set('messenger.transport.native_php_serializer', PhpSerializer::class) // Middleware diff --git a/src/Symfony/Component/Messenger/Transport/Serialization/Normalizer/RedeliveryStampNormalizer.php b/src/Symfony/Component/Messenger/Transport/Serialization/Normalizer/RedeliveryStampNormalizer.php new file mode 100644 index 0000000000000..7dd1cc4041bd4 --- /dev/null +++ b/src/Symfony/Component/Messenger/Transport/Serialization/Normalizer/RedeliveryStampNormalizer.php @@ -0,0 +1,57 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Messenger\Transport\Serialization\Normalizer; + +use Symfony\Component\Messenger\Stamp\RedeliveryStamp; +use Symfony\Component\Serializer\Exception\InvalidArgumentException; +use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer; +use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; + +class RedeliveryStampNormalizer implements DenormalizerInterface +{ + public function denormalize($data, string $type, string $format = null, array $context = []) + { + $redeliveredAt = $data['redeliveredAt'] ?? null; + + return new RedeliveryStamp( + $data['retryCount'] ?? 0, + $redeliveredAt ? new \DateTimeImmutable($redeliveredAt) : null + ); + } + + public function supportsDenormalization($data, string $type, string $format = null): bool + { + return RedeliveryStamp::class === $type + && null === ($data['exceptionMessage'] ?? null) + && null === ($data['flattenException'] ?? null) + ; + } + + public function normalize($object, string $format = null, array $context = []) + { + if (! $object instanceof RedeliveryStamp) { + throw new InvalidArgumentException(); + } + + $dateTimeFormat = $context[DateTimeNormalizer::FORMAT_KEY] ?? \DateTimeInterface::RFC3339; + + return [ + 'retryCount' => $object->getRetryCount(), + 'redeliveredAt' => $object->getRedeliveredAt()->format($dateTimeFormat), + ]; + } + + public function supportsNormalization($data, string $format = null): bool + { + return $data instanceof RedeliveryStamp; + } +}