Skip to content

Commit 5a9498f

Browse files
committed
minor #13239 Bus restricting with Interfaces (CvekCoding)
This PR was submitted for the 5.0 branch but it was merged into the 4.4 branch instead. Discussion ---------- Bus restricting with Interfaces Proposed more flexible way to restrict handlers with buses - use interfaces for this. <!-- If your pull request fixes a BUG, use the oldest maintained branch that contains the bug (see https://symfony.com/roadmap for the list of maintained branches). If your pull request documents a NEW FEATURE, use the same Symfony branch where the feature was introduced (and `master` for features of unreleased versions). --> Commits ------- d291a99 Bus restricting with Interfaces
2 parents 145157b + d291a99 commit 5a9498f

File tree

2 files changed

+48
-33
lines changed

2 files changed

+48
-33
lines changed

messenger/multiple_buses.rst

Lines changed: 46 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -150,30 +150,30 @@ you can restrict each handler to a specific bus using the ``messenger.message_ha
150150
This way, the ``App\MessageHandler\SomeCommandHandler`` handler will only be
151151
known by the ``command.bus`` bus.
152152

153-
You can also automatically add this tag to a number of classes by following
154-
a naming convention and registering all of the handler services by name with
155-
the correct tag:
153+
You can also automatically add this tag to a number of classes by using
154+
the :ref:`_instanceof service configuration <di-instanceof>`. Using this,
155+
you can determine the message bus based on an implemented interface:
156156

157157
.. configuration-block::
158158

159159
.. code-block:: yaml
160160
161161
# config/services.yaml
162+
services:
163+
# ...
164+
165+
_instanceof:
166+
# all services implementing the CommandHandlerInterface
167+
# will be registered on the command.bus bus
168+
App\MessageHandler\CommandHandlerInterface:
169+
tags:
170+
- { name: messenger.message_handler, bus: command.bus }
162171
163-
# put this after the "App\" line that registers all your services
164-
command_handlers:
165-
namespace: App\MessageHandler\
166-
resource: '%kernel.project_dir%/src/MessageHandler/*CommandHandler.php'
167-
autoconfigure: false
168-
tags:
169-
- { name: messenger.message_handler, bus: command.bus }
170-
171-
query_handlers:
172-
namespace: App\MessageHandler\
173-
resource: '%kernel.project_dir%/src/MessageHandler/*QueryHandler.php'
174-
autoconfigure: false
175-
tags:
176-
- { name: messenger.message_handler, bus: query.bus }
172+
# while those implementing QueryHandlerInterface will be
173+
# registered on the query.bus bus
174+
App\MessageHandler\QueryHandlerInterface:
175+
tags:
176+
- { name: messenger.message_handler, bus: query.bus }
177177
178178
.. code-block:: xml
179179
@@ -185,32 +185,45 @@ the correct tag:
185185
https://symfony.com/schema/dic/services/services-1.0.xsd">
186186
187187
<services>
188-
<!-- command handlers -->
189-
<prototype namespace="App\MessageHandler\" resource="%kernel.project_dir%/src/MessageHandler/*CommandHandler.php" autoconfigure="false">
188+
<!-- ... -->
189+
190+
<!-- all services implementing the CommandHandlerInterface
191+
will be registered on the command.bus bus -->
192+
<instanceof id="App\MessageHandler\CommandHandlerInterface">
190193
<tag name="messenger.message_handler" bus="command.bus"/>
191-
</prototype>
192-
<!-- query handlers -->
193-
<prototype namespace="App\MessageHandler\" resource="%kernel.project_dir%/src/MessageHandler/*QueryHandler.php" autoconfigure="false">
194+
</instanceof>
195+
196+
<!-- while those implementing QueryHandlerInterface will be
197+
registered on the query.bus bus -->
198+
<instanceof id="App\MessageHandler\QueryHandlerInterface">
194199
<tag name="messenger.message_handler" bus="query.bus"/>
195-
</prototype>
200+
</instanceof>
196201
</services>
197202
</container>
198203
199204
.. code-block:: php
200205
201206
// config/services.php
207+
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
202208
203-
// Command handlers
204-
$container->services()
205-
->load('App\MessageHandler\\', '%kernel.project_dir%/src/MessageHandler/*CommandHandler.php')
206-
->autoconfigure(false)
207-
->tag('messenger.message_handler', ['bus' => 'command.bus']);
209+
use App\MessageHandler\CommandHandlerInterface;
210+
use App\MessageHandler\QueryHandlerInterface;
208211
209-
// Query handlers
210-
$container->services()
211-
->load('App\MessageHandler\\', '%kernel.project_dir%/src/MessageHandler/*QueryHandler.php')
212-
->autoconfigure(false)
213-
->tag('messenger.message_handler', ['bus' => 'query.bus']);
212+
return function(ContainerConfigurator $configurator) {
213+
$services = $configurator->services();
214+
215+
// ...
216+
217+
// all services implementing the CommandHandlerInterface
218+
// will be registered on the command.bus bus
219+
$services->instanceof(CommandHandlerInterface::class)
220+
->tag('messenger.message_handler', ['bus' => 'command.bus']);
221+
222+
// while those implementing QueryHandlerInterface will be
223+
// registered on the query.bus bus
224+
$services->instanceof(QueryHandlerInterface::class)
225+
->tag('messenger.message_handler', ['bus' => 'query.bus']);
226+
};
214227
215228
Debugging the Buses
216229
-------------------

service_container/tags.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ and many tags require additional arguments (beyond the ``name`` parameter).
6060
**For most users, this is all you need to know**. If you want to go further and
6161
learn how to create your own custom tags, keep reading.
6262

63+
.. _di-instanceof:
64+
6365
Autoconfiguring Tags
6466
--------------------
6567

0 commit comments

Comments
 (0)