Symfony Messenger with custom serializer
Using the JSON serializer makes the messages a lot more readable. But there are still instances where this is not enough or might even be a step back from the default serializer. For example when using UUIDs. When you want to have this, you need to define a custom serializer.
I've setup a custom serializer anyway to deserialize DTOs from JSON and to return a json serialized response. This is configured in my CustomSerializer
class.
Whenever I need a serializer I use it through the CustomSerializer
.
You can setup the serializer wherever you want. The nice advantage here is that I can make sure, that everything within the app is serialized the same way.
Now to the Messenger. The only thing that we need to do is to supply this serializer to the messenger. Therefore we will build a custom transport class like the following:
<?php
declare(strict_types=1);
namespace App\Messenger\Transport;
use App\Service\Serializer\CustomSerializer;
use Symfony\Component\Messenger\Transport\Serialization\Serializer;
final class CustomTransportSerializer extends Serializer
{
public function __construct(CustomSerializer $customSerializer)
{
parent::__construct($customSerializer->serializer);
}
}
We simply extend the JSON serializer from Symfony and inject our serializer instead of the default Symfony one. Then we only need to configure it in our messenger.yaml
.
framework:
messenger:
transports:
async:
serializer: App\Messenger\Transport\CustomTransportSerializer
And that's it. This way a UUIDInterface
in an PHP object will be serialized to JSON as a string
and will be deserialized back to a UUIDInterface
.