Skip to content

Commit 3ff8cfe

Browse files
srozeweaverryan
authored andcommitted
Add multiple buses configuration and type-hint example
1 parent a4bc592 commit 3ff8cfe

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

messenger.rst

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,57 @@ like this:
168168
The first argument is the receiver's service name. It might have been created by
169169
your ``transports`` configuration or it can be your own receiver.
170170

171+
Multiple buses
172+
--------------
173+
174+
If you are interested into architectures like CQRS, you might want to have multiple
175+
buses within your application.
176+
177+
You can create multiple buses (in this example, a command and an event bus) like
178+
this:
179+
180+
.. code-block:: yaml
181+
182+
framework:
183+
messenger:
184+
# The bus that is going to be injected when injecting MessageBusInterface:
185+
default_bus: commands
186+
187+
# Create buses
188+
buses:
189+
commands: ~
190+
events: ~
191+
192+
This will generate the ``messenger.bus.commands`` and ``messenger.bus.events`` services
193+
that you can inject in your services.
194+
195+
Type-hints and auto-wiring
196+
~~~~~~~~~~~~~~~~~~~~~~~~~~
197+
198+
Auto-wiring is a great feature that allows you to reduce the amount of configuration
199+
required for your service container to be created. When using multiple buses, by default,
200+
the auto-wiring will not work as it won't know why bus to inject in your own services.
201+
202+
In order to clarify this, you will have to create your own decorators for your message
203+
buses. Let's create one for your ``CommandBus``::
204+
205+
namespace App;
206+
207+
use Symfony\Component\Messenger\AbstractMessageBusDecorator;
208+
209+
final class CommandBus extends AbstractMessageBusDecorator
210+
{
211+
}
212+
213+
Last step is to register your service (and explicit its argument) to be able to typehint
214+
your ``CommandBus`` in your services:
215+
216+
.. code-block:: yaml
217+
218+
# config/services.yaml
219+
services:
220+
App\CommandBus: ['@messenger.bus.commands']
221+
171222
Your own Transport
172223
------------------
173224

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy