@@ -2876,6 +2876,99 @@ Then your handler will look like this::
28762876 }
28772877 }
28782878
2879+ Message Serializer For Custom Data Formats
2880+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2881+
2882+ If you receive messages from other applications, it's possible that they are not
2883+ exactly in the format you need. Not all applications will return a JSON message
2884+ with ``body `` and ``headers `` fields. In those cases, you'll need to create a
2885+ new message serializer implementing the
2886+ :class: `Symfony\\ Component\\ Messenger\\ Transport\\ Serialization\\ SerializerInterface `.
2887+ Let's say you want to create a message decoder::
2888+
2889+ namespace App\Messenger\Serializer;
2890+
2891+ use Symfony\Component\Messenger\Envelope;
2892+ use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface;
2893+
2894+ class MessageWithTokenDecoder implements SerializerInterface
2895+ {
2896+ public function decode(array $encodedEnvelope): Envelope
2897+ {
2898+ $envelope = \json_decode($encodedEnvelope, true);
2899+
2900+ try {
2901+ // parse the data you received with your custom fields
2902+ $data = $envelope['data'];
2903+ $data['token'] = $envelope['token'];
2904+
2905+ // other operations like getting information from stamps
2906+ } catch (\Throwable $throwable) {
2907+ // wrap any exception that may occur in the envelope to send it to the failure transport
2908+ return new Envelope($throwable);
2909+ }
2910+
2911+ return new Envelope($data);
2912+ }
2913+
2914+ public function encode(Envelope $envelope): array
2915+ {
2916+ // this decoder does not encode messages, but you can implement it by returning
2917+ // an array with serialized stamps if you need to send messages in a custom format
2918+ throw new \LogicException('This serializer is only used for decoding messages.');
2919+ }
2920+ }
2921+
2922+ The next step is to tell Symfony to use this serializer in one or more of your
2923+ transports:
2924+
2925+ .. configuration-block ::
2926+
2927+ .. code-block :: yaml
2928+
2929+ # config/packages/messenger.yaml
2930+ framework :
2931+ messenger :
2932+ transports :
2933+ my_transport :
2934+ dsn : ' %env(MY_TRANSPORT_DSN)%'
2935+ serializer : ' App\Messenger\Serializer\MessageWithTokenDecoder'
2936+
2937+ .. code-block :: xml
2938+
2939+ <!-- config/packages/messenger.xml -->
2940+ <?xml version =" 1.0" encoding =" UTF-8" ?>
2941+ <container xmlns =" http://symfony.com/schema/dic/services"
2942+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
2943+ xmlns : framework =" http://symfony.com/schema/dic/symfony"
2944+ xsi : schemaLocation =" http://symfony.com/schema/dic/services
2945+ https://symfony.com/schema/dic/services/services-1.0.xsd
2946+ http://symfony.com/schema/dic/symfony
2947+ https://symfony.com/schema/dic/symfony/symfony-1.0.xsd" >
2948+
2949+ <framework : config >
2950+ <framework : messenger >
2951+ <framework : transport name =" my_transport" dsn =" %env(MY_TRANSPORT_DSN)%" serializer =" App\Messenger\Serializer\MessageWithTokenDecoder" >
2952+ <!-- ... -->
2953+ </framework : transport >
2954+ </framework : messenger >
2955+ </framework : config >
2956+ </container >
2957+
2958+ .. code-block :: php
2959+
2960+ // config/packages/messenger.php
2961+ use App\Messenger\Serializer\MessageWithTokenDecoder;
2962+ use Symfony\Config\FrameworkConfig;
2963+
2964+ return static function (FrameworkConfig $framework): void {
2965+ $messenger = $framework->messenger();
2966+
2967+ $messenger->transport('my_transport')
2968+ ->dsn('%env(MY_TRANSPORT_DSN)%')
2969+ ->serializer(MessageWithTokenDecoder::class);
2970+ };
2971+
28792972 .. _messenger-multiple-buses :
28802973
28812974Multiple Buses, Command & Event Buses
0 commit comments