@@ -86,6 +86,46 @@ that will do the required processing for your message::
86
86
}
87
87
}
88
88
89
+ Envelope
90
+ --------
91
+
92
+ The notion of an envelope is a concept that helps us to add some context around the
93
+ messages. An envelope is a message and a set of items. On a user perspective, this
94
+ allows to set some configuration around the message. For example the serialization
95
+ groups used when the message goes through the transport layer::
96
+
97
+ use Symfony\Component\Messenger\Envelope;
98
+ use Symfony\Component\Messenger\Transport\Serialization\SerializerConfiguration;
99
+
100
+ $bus->dispatch(
101
+ (new Envelope($message))->with(new SerializerConfiguration([
102
+ 'groups' => ['my_serialization_groups'],
103
+ ]))
104
+ );
105
+
106
+ Instead of dealing directly with the messages in the handlers and middleware you
107
+ can receive the envelope by implementing the ``EnvelopeAwareInterface `` marker
108
+ interface on your middleware or handler like this::
109
+
110
+ use Symfony\Component\Messenger\Middleware\MiddlewareInterface;
111
+ use Symfony\Component\Messenger\EnvelopeAwareInterface;
112
+
113
+ class MyOwnMiddleware implements MiddlewareInterface, EnvelopeAwareInterface
114
+ {
115
+ public function handle($message, callable $next)
116
+ {
117
+ // $message here is an `Envelope` object
118
+
119
+ return $next(
120
+ $message->with(new AnotherEnvelopeItem(/* ... */))
121
+ );
122
+ }
123
+ }
124
+
125
+ The above example will forward the message to the next middleware with an additional
126
+ envelope item. You can create your own items by implementing the ``EnvelopeItemInterface ``
127
+ interface.
128
+
89
129
Transports
90
130
----------
91
131
@@ -106,6 +146,7 @@ First, create your sender::
106
146
107
147
use App\Message\ImportantAction;
108
148
use Symfony\Component\Messenger\Transport\SenderInterface;
149
+ use Symfony\Component\Messenger\Envelope;
109
150
110
151
class ImportantActionToEmailSender implements SenderInterface
111
152
{
@@ -118,10 +159,12 @@ First, create your sender::
118
159
$this->toEmail = $toEmail;
119
160
}
120
161
121
- public function send($message )
162
+ public function send(Envelope $envelope )
122
163
{
164
+ $message = $envelope->getMessage();
165
+
123
166
if (!$message instanceof ImportantAction) {
124
- throw new \InvalidArgumentException(sprintf('Producer only supports "%s" messages.', ImportantAction::class));
167
+ throw new \InvalidArgumentException(sprintf('This transport only supports "%s" messages.', ImportantAction::class));
125
168
}
126
169
127
170
$this->mailer->send(
@@ -156,6 +199,7 @@ First, create your receiver::
156
199
use App\Message\NewOrder;
157
200
use Symfony\Component\Messenger\Transport\ReceiverInterface;
158
201
use Symfony\Component\Serializer\SerializerInterface;
202
+ use Symfony\Component\Messenger\Envelope;
159
203
160
204
class NewOrdersFromCsvFile implements ReceiverInterface
161
205
{
@@ -173,7 +217,9 @@ First, create your receiver::
173
217
$ordersFromCsv = $this->serializer->deserialize(file_get_contents($this->filePath), 'csv');
174
218
175
219
foreach ($ordersFromCsv as $orderFromCsv) {
176
- $handler(new NewOrder($orderFromCsv['id'], $orderFromCsv['account_id'], $orderFromCsv['amount']));
220
+ $order = new NewOrder($orderFromCsv['id'], $orderFromCsv['account_id'], $orderFromCsv['amount']);
221
+
222
+ $handler(new Envelope($order));
177
223
}
178
224
}
179
225
@@ -188,6 +234,6 @@ Receiver and Sender on the same bus
188
234
189
235
To allow us to receive and send messages on the same bus and prevent an infinite
190
236
loop, the message bus is equipped with the ``WrapIntoReceivedMessage `` middleware.
191
- It will wrap the received messages into ``ReceivedMessage `` objects and the
192
- ``SendMessageMiddleware `` middleware will know it should not route these
193
- messages again to a transport.
237
+ It will add a ``ReceivedMessage `` configuration to the message envelopes and the
238
+ ``SendMessageMiddleware `` middleware will know it should not route these messages
239
+ again to a transport.
0 commit comments