Skip to content

Commit 0ec35ce

Browse files
committed
Add message envelope in the documentation
1 parent a2140f0 commit 0ec35ce

File tree

1 file changed

+52
-6
lines changed

1 file changed

+52
-6
lines changed

components/messenger.rst

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,46 @@ that will do the required processing for your message::
8686
}
8787
}
8888

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+
89129
Transports
90130
----------
91131

@@ -106,6 +146,7 @@ First, create your sender::
106146

107147
use App\Message\ImportantAction;
108148
use Symfony\Component\Messenger\Transport\SenderInterface;
149+
use Symfony\Component\Messenger\Envelope;
109150

110151
class ImportantActionToEmailSender implements SenderInterface
111152
{
@@ -118,10 +159,12 @@ First, create your sender::
118159
$this->toEmail = $toEmail;
119160
}
120161

121-
public function send($message)
162+
public function send(Envelope $envelope)
122163
{
164+
$message = $envelope->getMessage();
165+
123166
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));
125168
}
126169

127170
$this->mailer->send(
@@ -156,6 +199,7 @@ First, create your receiver::
156199
use App\Message\NewOrder;
157200
use Symfony\Component\Messenger\Transport\ReceiverInterface;
158201
use Symfony\Component\Serializer\SerializerInterface;
202+
use Symfony\Component\Messenger\Envelope;
159203

160204
class NewOrdersFromCsvFile implements ReceiverInterface
161205
{
@@ -173,7 +217,9 @@ First, create your receiver::
173217
$ordersFromCsv = $this->serializer->deserialize(file_get_contents($this->filePath), 'csv');
174218

175219
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));
177223
}
178224
}
179225

@@ -188,6 +234,6 @@ Receiver and Sender on the same bus
188234

189235
To allow us to receive and send messages on the same bus and prevent an infinite
190236
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

Comments
 (0)