@@ -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
151151known 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-------------------
0 commit comments