@@ -150,30 +150,30 @@ you can restrict each handler to a specific bus using the ``messenger.message_ha
150
150
This way, the ``App\MessageHandler\SomeCommandHandler `` handler will only be
151
151
known by the ``command.bus `` bus.
152
152
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 :
156
156
157
157
.. configuration-block ::
158
158
159
159
.. code-block :: yaml
160
160
161
161
# 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 }
162
171
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 }
177
177
178
178
.. code-block :: xml
179
179
@@ -185,32 +185,45 @@ the correct tag:
185
185
https://symfony.com/schema/dic/services/services-1.0.xsd" >
186
186
187
187
<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" >
190
193
<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" >
194
199
<tag name =" messenger.message_handler" bus =" query.bus" />
195
- </prototype >
200
+ </instanceof >
196
201
</services >
197
202
</container >
198
203
199
204
.. code-block :: php
200
205
201
206
// config/services.php
207
+ namespace Symfony\Component\DependencyInjection\Loader\Configurator;
202
208
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;
208
211
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
+ };
214
227
215
228
Debugging the Buses
216
229
-------------------
0 commit comments