@@ -2813,6 +2813,99 @@ Then your handler will look like this::
2813
2813
The :class: `Symfony\\ Component\\ Messenger\\ Stamp\\ HandlerArgumentsStamp `
2814
2814
was introduced in Symfony 6.2.
2815
2815
2816
+ Message Serializer For Custom Data Formats
2817
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2818
+
2819
+ If you receive messages from other applications, it's possible that they are not
2820
+ exactly in the format you need. Not all applications will return a JSON message
2821
+ with ``body `` and ``headers `` fields. In those cases, you'll need to create a
2822
+ new message serializer implementing the
2823
+ :class: `Symfony\\ Component\\ Messenger\\ Transport\\ Serialization\\ SerializerInterface `.
2824
+ Let's say you want to create a message decoder::
2825
+
2826
+ namespace App\Messenger\Serializer;
2827
+
2828
+ use Symfony\Component\Messenger\Envelope;
2829
+ use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface;
2830
+
2831
+ class MessageWithTokenDecoder implements SerializerInterface
2832
+ {
2833
+ public function decode(array $encodedEnvelope): Envelope
2834
+ {
2835
+ $envelope = \json_decode($encodedEnvelope, true);
2836
+
2837
+ try {
2838
+ // parse the data you received with your custom fields
2839
+ $data = $envelope['data'];
2840
+ $data['token'] = $envelope['token'];
2841
+
2842
+ // other operations like getting information from stamps
2843
+ } catch (\Throwable $throwable) {
2844
+ // wrap any exception that may occur in the envelope to send it to the failure transport
2845
+ return new Envelope($throwable);
2846
+ }
2847
+
2848
+ return new Envelope($data);
2849
+ }
2850
+
2851
+ public function encode(Envelope $envelope): array
2852
+ {
2853
+ // this decoder does not encode messages, but you can implement it by returning
2854
+ // an array with serialized stamps if you need to send messages in a custom format
2855
+ throw new \LogicException('This serializer is only used for decoding messages.');
2856
+ }
2857
+ }
2858
+
2859
+ The next step is to tell Symfony to use this serializer in one or more of your
2860
+ transports:
2861
+
2862
+ .. configuration-block ::
2863
+
2864
+ .. code-block :: yaml
2865
+
2866
+ # config/packages/messenger.yaml
2867
+ framework :
2868
+ messenger :
2869
+ transports :
2870
+ my_transport :
2871
+ dsn : ' %env(MY_TRANSPORT_DSN)%'
2872
+ serializer : ' App\Messenger\Serializer\MessageWithTokenDecoder'
2873
+
2874
+ .. code-block :: xml
2875
+
2876
+ <!-- config/packages/messenger.xml -->
2877
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
2878
+ <container xmlns =" http://symfony.com/schema/dic/services"
2879
+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
2880
+ xmlns : framework =" http://symfony.com/schema/dic/symfony"
2881
+ xsi : schemaLocation =" http://symfony.com/schema/dic/services
2882
+ https://symfony.com/schema/dic/services/services-1.0.xsd
2883
+ http://symfony.com/schema/dic/symfony
2884
+ https://symfony.com/schema/dic/symfony/symfony-1.0.xsd" >
2885
+
2886
+ <framework : config >
2887
+ <framework : messenger >
2888
+ <framework : transport name =" my_transport" dsn =" %env(MY_TRANSPORT_DSN)%" serializer =" App\Messenger\Serializer\MessageWithTokenDecoder" >
2889
+ <!-- ... -->
2890
+ </framework : transport >
2891
+ </framework : messenger >
2892
+ </framework : config >
2893
+ </container >
2894
+
2895
+ .. code-block :: php
2896
+
2897
+ // config/packages/messenger.php
2898
+ use App\Messenger\Serializer\MessageWithTokenDecoder;
2899
+ use Symfony\Config\FrameworkConfig;
2900
+
2901
+ return static function (FrameworkConfig $framework): void {
2902
+ $messenger = $framework->messenger();
2903
+
2904
+ $messenger->transport('my_transport')
2905
+ ->dsn('%env(MY_TRANSPORT_DSN)%')
2906
+ ->serializer(MessageWithTokenDecoder::class);
2907
+ };
2908
+
2816
2909
.. _messenger-multiple-buses :
2817
2910
2818
2911
Multiple Buses, Command & Event Buses
0 commit comments