8000 [Messenger] Add RedeliveryStamp (de)normalizer to avoid deprecations by Jean85 · Pull Request #45701 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Messenger] Add RedeliveryStamp (de)normalizer to avoid deprecations #45701

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

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
8000
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the correct approach? Or is there some solution to delegate this normalization to the default serialization (and to DateTimeNormalizer)?


return [
'retryCount' => $object->getRetryCount(),
'redeliveredAt' => $object->getRedeliveredAt()->format($dateTimeFormat),
];
}

public function supportsNormalization($data, string $format = null): bool
{
return $data instanceof RedeliveryStamp;
}
}
0