@@ -2980,6 +2980,99 @@ Then your handler will look like this::
2980
2980
The :class: `Symfony\\ Component\\ Messenger\\ Stamp\\ HandlerArgumentsStamp `
2981
2981
was introduced in Symfony 6.2.
2982
2982
2983
+ Message Serializer For Custom Data Formats
2984
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2985
+
2986
+ If you receive messages from other applications, it's possible that they are not
2987
+ exactly in the format you need. Not all applications will return a JSON message
2988
+ with ``body `` and ``headers `` fields. In those cases, you'll need to create a
2989
+ new message serializer implementing the
2990
+ :class: `Symfony\\ Component\\ Messenger\\ Transport\\ Serialization\\ SerializerInterface `.
2991
+ Let's say you want to create a message decoder::
2992
+
2993
+ namespace App\Messenger\Serializer;
2994
+
2995
+ use Symfony\Component\Messenger\Envelope;
2996
+ use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface;
2997
+
2998
+ class MessageWithTokenDecoder implements SerializerInterface
2999
+ {
3000
+ public function decode(array $encodedEnvelope): Envelope
3001
+ {
3002
+ $envelope = \json_decode($encodedEnvelope, true);
3003
+
3004
+ try {
3005
+ // parse the data you received with your custom fields
3006
+ $data = $envelope['data'];
3007
+ $data['token'] = $envelope['token'];
3008
+
3009
+ // other operations like getting information from stamps
3010
+ } catch (\Throwable $throwable) {
3011
+ // wrap any exception that may occur in the envelope to send it to the failure transport
3012
+ return new Envelope($throwable);
3013
+ }
3014
+
3015
+ return new Envelope($data);
3016
+ }
3017
+
3018
+ public function encode(Envelope $envelope): array
3019
+ {
3020
+ // this decoder does not encode messages, but you can implement it by returning
3021
+ // an array with serialized stamps if you need to send messages in a custom format
3022
+ throw new \LogicException('This serializer is only used for decoding messages.');
3023
+ }
3024
+ }
3025
+
3026
+ The next step is to tell Symfony to use this serializer in one or more of your
3027
+ transports:
3028
+
3029
+ .. configuration-block ::
3030
+
3031
+ .. code-block :: yaml
3032
+
3033
+ # config/packages/messenger.yaml
3034
+ framework :
3035
+ messenger :
3036
+ transports :
3037
+ my_transport :
3038
+ dsn : ' %env(MY_TRANSPORT_DSN)%'
3039
+ serializer : ' App\Messenger\Serializer\MessageWithTokenDecoder'
3040
+
3041
+ .. code-block :: xml
3042
+
3043
+ <!-- config/packages/messenger.xml -->
3044
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
3045
+ <container xmlns =" http://symfony.com/schema/dic/services"
3046
+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
3047
+ xmlns : framework =" http://symfony.com/schema/dic/symfony"
3048
+ xsi : schemaLocation =" http://symfony.com/schema/dic/services
3049
+ https://symfony.com/schema/dic/services/services-1.0.xsd
3050
+ http://symfony.com/schema/dic/symfony
3051
+ https://symfony.com/schema/dic/symfony/symfony-1.0.xsd" >
3052
+
3053
+ <framework : config >
3054
+ <framework : messenger >
3055
+ <framework : transport name =" my_transport" dsn =" %env(MY_TRANSPORT_DSN)%" serializer =" App\Messenger\Serializer\MessageWithTokenDecoder" >
3056
+ <!-- ... -->
3057
+ </framework : transport >
3058
+ </framework : messenger >
3059
+ </framework : config >
3060
+ </container >
3061
+
3062
+ .. code-block :: php
3063
+
3064
+ // config/packages/messenger.php
3065
+ use App\Messenger\Serializer\MessageWithTokenDecoder;
3066
+ use Symfony\Config\FrameworkConfig;
3067
+
3068
+ return static function (FrameworkConfig $framework): void {
3069
+ $messenger = $framework->messenger();
3070
+
3071
+ $messenger->transport('my_transport')
3072
+ ->dsn('%env(MY_TRANSPORT_DSN)%')
3073
+ ->serializer(MessageWithTokenDecoder::class);
3074
+ };
3075
+
2983
3076
.. _messenger-multiple-buses :
2984
3077
2985
3078
Multiple Buses, Command & Event Buses
0 commit comments