Skip to content

Commit 28a1d93

Browse files
committed
Merge branch '4.1'
* 4.1: Update service_container.rst hide Assetic pages from TOC Renaming variable [Validation] Added internal link Reword the header regarding the envelope Add message envelope in the documentation
2 parents 3927279 + e68cda5 commit 28a1d93

File tree

7 files changed

+82
-22
lines changed

7 files changed

+82
-22
lines changed

_build/redirection_map

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,12 @@
8080
/bundles/installation /bundles
8181
/cookbook/assetic/apply_to_option /frontend/assetic/apply_to_option
8282
/cookbook/assetic/asset_management /frontend/assetic/asset_management
83-
/cookbook/assetic/index /frontend/assetic
83+
/cookbook/assetic/index /frontend/assetic/index
8484
/cookbook/assetic/jpeg_optimize /frontend/assetic/jpeg_optimize
8585
/cookbook/assetic/php /frontend/assetic/php
8686
/cookbook/assetic/uglifyjs /frontend/assetic/uglifyjs
8787
/cookbook/assetic/yuicompressor /frontend/assetic/yuicompressor
88-
/assetic /frontend/assetic
88+
/assetic /frontend/assetic/index
8989
/assetic/apply_to_option /frontend/assetic/apply_to_option
9090
/assetic/asset_management /frontend/assetic/asset_management
9191
/assetic/jpeg_optimize /frontend/assetic/jpeg_optimize
@@ -370,13 +370,14 @@
370370
/event_dispatcher/class_extension /event_dispatcher
371371
/form /forms
372372
/form/use_virtual_forms /form/inherit_data_option
373-
/frontend/assetic/apply_to_option /frontend/assetic
374-
/frontend/assetic/asset_management /frontend/assetic
375-
/frontend/assetic/jpeg_optimize /frontend/assetic
376-
/frontend/assetic/php /frontend/assetic
377-
/frontend/assetic/uglifyjs /frontend/assetic
378-
/frontend/assetic/yuicompressor /frontend/assetic
379-
/reference/configuration/assetic /frontend/assetic
373+
/frontend/assetic /frontend/assetic/index
374+
/frontend/assetic/apply_to_option /frontend/assetic/index
375+
/frontend/assetic/asset_management /frontend/assetic/index
376+
/frontend/assetic/jpeg_optimize /frontend/assetic/index
377+
/frontend/assetic/php /frontend/assetic/index
378+
/frontend/assetic/uglifyjs /frontend/assetic/index
379+
/frontend/assetic/yuicompressor /frontend/assetic/index
380+
/reference/configuration/assetic /frontend/assetic/index
380381
/security/target_path /security
381382
/security/csrf_in_login_form /security/csrf
382383
/service_container/service_locators /service_container/service_subscribers_locators

components/messenger.rst

Lines changed: 68 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,62 @@ that will do the required processing for your message::
9595
}
9696
}
9797

98+
Adding Metadata to Messages (Envelopes)
99+
---------------------------------------
100+
101+
If you need to add metadata or some configuration to a message, wrap it with the
102+
:class:`Symfony\\Component\\Messenger\\Envelope` class. For example, to set the
103+
serialization groups used when the message goes through the transport layer, use
104+
the ``SerializerConfiguration`` envelope::
105+
106+
use Symfony\Component\Messenger\Envelope;
107+
use Symfony\Component\Messenger\Transport\Serialization\SerializerConfiguration;
108+
109+
$bus->dispatch(
110+
(new Envelope($message))->with(new SerializerConfiguration([
111+
'groups' => ['my_serialization_groups'],
112+
]))
113+
);
114+
115+
At the moment, the Symfony Messenger has the following built-in envelopes:
116+
117+
1. :class:`Symfony\\Component\\Messenger\\Transport\\Serialization\\SerializerConfiguration`,
118+
to configure the serialization groups used by the transport.
119+
2. :class:`Symfony\\Component\\Messenger\\Middleware\\Configuration\\ValidationConfiguration`,
120+
to configure the validation groups used when the validation middleware is enabled.
121+
3. :class:`Symfony\\Component\\Messenger\\Asynchronous\\Transport\\ReceivedMessage`,
122+
an internal item that marks the message as received from a transport.
123+
124+
Instead of dealing directly with the messages in the middleware you can receive the
125+
envelope by implementing the :class:`Symfony\\Component\\Messenger\\EnvelopeAwareInterface`
126+
marker, like this::
127+
128+
use Symfony\Component\Messenger\Asynchronous\Transport\ReceivedMessage;
129+
use Symfony\Component\Messenger\Middleware\MiddlewareInterface;
130+
use Symfony\Component\Messenger\EnvelopeAwareInterface;
131+
132+
class MyOwnMiddleware implements MiddlewareInterface, EnvelopeAwareInterface
133+
{
134+
public function handle($envelope, callable $next)
135+
{
136+
// $envelope here is an `Envelope` object, because this middleware
137+
// implements the EnvelopeAwareInterface interface.
138+
139+
if (null !== $envelope->get(ReceivedMessage::class)) {
140+
// Message just has been received...
141+
142+
// You could for example add another item.
143+
$envelope = $envelope->with(new AnotherEnvelopeItem(/* ... */));
144+
}
145+
146+
return $next($envelope);
147+
}
148+
}
149+
150+
The above example will forward the message to the next middleware with an additional
151+
envelope item *if* the message has just been received (i.e. has the `ReceivedMessage` item).
152+
You can create your own items by implementing :class:`Symfony\\Component\\Messenger\\EnvelopeAwareInterface`.
153+
98154
Transports
99155
----------
100156

@@ -115,6 +171,7 @@ First, create your sender::
115171

116172
use App\Message\ImportantAction;
117173
use Symfony\Component\Messenger\Transport\SenderInterface;
174+
use Symfony\Component\Messenger\Envelope;
118175

119176
class ImportantActionToEmailSender implements SenderInterface
120177
{
@@ -127,10 +184,12 @@ First, create your sender::
127184
$this->toEmail = $toEmail;
128185
}
129186

130-
public function send($message)
187+
public function send(Envelope $envelope)
131188
{
189+
$message = $envelope->getMessage();
190+
132191
if (!$message instanceof ImportantAction) {
133-
throw new \InvalidArgumentException(sprintf('Producer only supports "%s" messages.', ImportantAction::class));
192+
throw new \InvalidArgumentException(sprintf('This transport only supports "%s" messages.', ImportantAction::class));
134193
}
135194

136195
$this->mailer->send(
@@ -165,6 +224,7 @@ First, create your receiver::
165224
use App\Message\NewOrder;
166225
use Symfony\Component\Messenger\Transport\ReceiverInterface;
167226
use Symfony\Component\Serializer\SerializerInterface;
227+
use Symfony\Component\Messenger\Envelope;
168228

169229
class NewOrdersFromCsvFile implements ReceiverInterface
170230
{
@@ -182,7 +242,9 @@ First, create your receiver::
182242
$ordersFromCsv = $this->serializer->deserialize(file_get_contents($this->filePath), 'csv');
183243

184244
foreach ($ordersFromCsv as $orderFromCsv) {
185-
$handler(new NewOrder($orderFromCsv['id'], $orderFromCsv['account_id'], $orderFromCsv['amount']));
245+
$order = new NewOrder($orderFromCsv['id'], $orderFromCsv['account_id'], $orderFromCsv['amount']);
246+
247+
$handler(new Envelope($order));
186248
}
187249
}
188250

@@ -196,10 +258,9 @@ Receiver and Sender on the same Bus
196258
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
197259

198260
To allow sending and receiving messages on the same bus and prevent an infinite
199-
loop, the message bus is equipped with the ``WrapIntoReceivedMessage`` middleware.
200-
It will wrap the received messages into ``ReceivedMessage`` objects and the
201-
``SendMessageMiddleware`` middleware will know it should not route these
202-
messages again to a transport.
261+
loop, the message bus will add a :class:`Symfony\\Component\\Messenger\\Asynchronous\\Transport\\ReceivedMessage`
262+
envelope item to the message envelopes and the :class:`Symfony\\Component\\Messenger\\Asynchronous\\Middleware\\SendMessageMiddleware`
263+
middleware will know it should not route these messages again to a transport.
203264

204265
.. _blog posts about command buses: https://matthiasnoback.nl/tags/command%20bus/
205266
.. _SimpleBus project: http://simplebus.io

frontend.rst

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,14 @@ Full API
7777

7878
* `Full API`_: https://github.com/symfony/webpack-encore/blob/master/index.js
7979

80-
For more about Assetic, see :doc:`/frontend/assetic`.
81-
8280
Other Front-End Articles
8381
------------------------
8482

8583
.. toctree::
8684
:hidden:
8785
:glob:
8886

89-
frontend/assetic
87+
frontend/assetic/index
9088
frontend/encore/installation
9189
frontend/encore/simple-example
9290
frontend/encore/*
File renamed without changes.

frontend/encore/versus-assetic.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Encore Versus Assetic?
22
======================
33

4-
Symfony originally shipped with support for :doc:`Assetic </frontend/assetic>`: a
4+
Symfony originally shipped with support for :doc:`Assetic </frontend/assetic/index>`: a
55
pure PHP library capable of processing, combining and minifying CSS and JavaScript
66
files. And while Encore is now the recommended way of processing your assets, Assetic
77
still works well.

service_container.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1010,7 +1010,7 @@ admin email. In this case, each needs to have a unique service id:
10101010
<argument>[email protected]</argument>
10111011
</service>
10121012
1013-
<alias id="App\Updates\SiteUpdateManager" service="site_update_manager.superadmin" />
1013+
<service id="App\Updates\SiteUpdateManager" alias="site_update_manager.superadmin" />
10141014
</services>
10151015
</container>
10161016

validation.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ options can be specified in this way.
482482
This is purely meant to make the configuration of the most common option of
483483
a constraint shorter and quicker.
484484

485-
If you're ever unsure of how to specify an option, either check the API documentation
485+
If you're ever unsure of how to specify an option, either check :namespace:`Symfony\\Component\\Validator\\Constraints`
486486
for the constraint or play it safe by always passing in an array of options
487487
(the first method shown above).
488488

0 commit comments

Comments
 (0)