Skip to content

Commit f57138f

Browse files
committed
Update the envelope update
1 parent 832a057 commit f57138f

File tree

1 file changed

+29
-12
lines changed

1 file changed

+29
-12
lines changed

components/messenger.rst

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -104,27 +104,45 @@ in an ``Envelope`` and add some ``SerializerConfiguration``::
104104
]))
105105
);
106106

107-
Instead of dealing directly with the message in the handlers or middleware, you
108-
can receive the envelope by implementing the ``EnvelopeAwareInterface`` marker,
109-
like this::
107+
At the moment, the Symfony Messenger has the following built-in envelopes:
110108

109+
1. :class:`Symfony\\Component\\Messenger\\Transport\\Serialization\\SerializerConfiguration`,
110+
to configure the serialization groups used by the transport.
111+
2. :class:`Symfony\\Component\\Messenger\\Middleware\\Configuration\\ValidationConfiguration`,
112+
to configure the validation groups used when the validation middleware is enabled.
113+
3. :class:`Symfony\\Component\\Messenger\\Asynchronous\\Transport\\ReceivedMessage`,
114+
an internal item that marks the message as received from a transport.
115+
116+
Instead of dealing directly with the messages in the middleware you can receive the
117+
envelope by implementing the :class:`Symfony\\Component\\Messenger\\EnvelopeAwareInterface`
118+
marker, like this::
119+
120+
use Symfony\Component\Messenger\Asynchronous\Transport\ReceivedMessage;
111121
use Symfony\Component\Messenger\Middleware\MiddlewareInterface;
112122
use Symfony\Component\Messenger\EnvelopeAwareInterface;
113123

114124
class MyOwnMiddleware implements MiddlewareInterface, EnvelopeAwareInterface
115125
{
116126
public function handle($message, callable $next)
117127
{
118-
// $message here is an `Envelope` object
128+
// $message here is an `Envelope` object, because this middleware
129+
// implements the EnvelopeAwareInterface interface. Otherwise,
130+
// it would be the "original" message.
131+
132+
if (null !== $message->get(ReceivedMessage::class)) {
133+
// Message just has been received...
134+
135+
// You could for example add another item.
136+
$message = $message->with(new AnotherEnvelopeItem(/* ... */));
137+
}
119138

120-
return $next(
121-
$message->with(new AnotherEnvelopeItem(/* ... */))
122-
);
139+
return $next($message);
123140
}
124141
}
125142

126143
The above example will forward the message to the next middleware with an additional
127-
envelope item. You can create your own items by implementing the ``EnvelopeItemInterface``
144+
envelope item if the message has just been received (i.e. has the `ReceivedMessage` item).
145+
You can create your own items by implementing the :class:`Symfony\\Component\\Messenger\\EnvelopeAwareInterface`
128146
interface.
129147

130148
Transports
@@ -234,7 +252,6 @@ Receiver and Sender on the same bus
234252
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
235253

236254
To allow us to receive and send messages on the same bus and prevent an infinite
237-
loop, the message bus is equipped with the ``WrapIntoReceivedMessage`` middleware.
238-
It will add a ``ReceivedMessage`` configuration to the message envelopes and the
239-
``SendMessageMiddleware`` middleware will know it should not route these messages
240-
again to a transport.
255+
loop, the message bus will add a :class:`Symfony\\Component\\Messenger\\Asynchronous\\Transport\\ReceivedMessage`
256+
envelope item to the message envelopes and the :class:`Symfony\\Component\\Messenger\\Asynchronous\\Middleware\\SendMessageMiddleware`
257+
middleware will know it should not route these messages again to a transport.

0 commit comments

Comments
 (0)