From d291a99ee339d3b4b0fb33c825f2a76b6ce867be Mon Sep 17 00:00:00 2001 From: CvekCoding <36374606+CvekCoding@users.noreply.github.com> Date: Mon, 24 Feb 2020 21:16:59 +0300 Subject: [PATCH] Bus restricting with Interfaces Proposed more flexible way to restrict handlers with buses - use interfaces for this. --- messenger/multiple_buses.rst | 79 +++++++++++++++++++++--------------- service_container/tags.rst | 2 + 2 files changed, 48 insertions(+), 33 deletions(-) diff --git a/messenger/multiple_buses.rst b/messenger/multiple_buses.rst index 5136553dac2..c79aeb7b2b7 100644 --- a/messenger/multiple_buses.rst +++ b/messenger/multiple_buses.rst @@ -150,30 +150,30 @@ you can restrict each handler to a specific bus using the ``messenger.message_ha This way, the ``App\MessageHandler\SomeCommandHandler`` handler will only be known by the ``command.bus`` bus. -You can also automatically add this tag to a number of classes by following -a naming convention and registering all of the handler services by name with -the correct tag: +You can also automatically add this tag to a number of classes by using +the :ref:`_instanceof service configuration `. Using this, +you can determine the message bus based on an implemented interface: .. configuration-block:: .. code-block:: yaml # config/services.yaml + services: + # ... + + _instanceof: + # all services implementing the CommandHandlerInterface + # will be registered on the command.bus bus + App\MessageHandler\CommandHandlerInterface: + tags: + - { name: messenger.message_handler, bus: command.bus } - # put this after the "App\" line that registers all your services - command_handlers: - namespace: App\MessageHandler\ - resource: '%kernel.project_dir%/src/MessageHandler/*CommandHandler.php' - autoconfigure: false - tags: - - { name: messenger.message_handler, bus: command.bus } - - query_handlers: - namespace: App\MessageHandler\ - resource: '%kernel.project_dir%/src/MessageHandler/*QueryHandler.php' - autoconfigure: false - tags: - - { name: messenger.message_handler, bus: query.bus } + # while those implementing QueryHandlerInterface will be + # registered on the query.bus bus + App\MessageHandler\QueryHandlerInterface: + tags: + - { name: messenger.message_handler, bus: query.bus } .. code-block:: xml @@ -185,32 +185,45 @@ the correct tag: https://symfony.com/schema/dic/services/services-1.0.xsd"> - - + + + + - - - + + + + - + .. code-block:: php // config/services.php + namespace Symfony\Component\DependencyInjection\Loader\Configurator; - // Command handlers - $container->services() - ->load('App\MessageHandler\\', '%kernel.project_dir%/src/MessageHandler/*CommandHandler.php') - ->autoconfigure(false) - ->tag('messenger.message_handler', ['bus' => 'command.bus']); + use App\MessageHandler\CommandHandlerInterface; + use App\MessageHandler\QueryHandlerInterface; - // Query handlers - $container->services() - ->load('App\MessageHandler\\', '%kernel.project_dir%/src/MessageHandler/*QueryHandler.php') - ->autoconfigure(false) - ->tag('messenger.message_handler', ['bus' => 'query.bus']); + return function(ContainerConfigurator $configurator) { + $services = $configurator->services(); + + // ... + + // all services implementing the CommandHandlerInterface + // will be registered on the command.bus bus + $services->instanceof(CommandHandlerInterface::class) + ->tag('messenger.message_handler', ['bus' => 'command.bus']); + + // while those implementing QueryHandlerInterface will be + // registered on the query.bus bus + $services->instanceof(QueryHandlerInterface::class) + ->tag('messenger.message_handler', ['bus' => 'query.bus']); + }; Debugging the Buses ------------------- diff --git a/service_container/tags.rst b/service_container/tags.rst index 8bddd65c795..2f60f369b97 100644 --- a/service_container/tags.rst +++ b/service_container/tags.rst @@ -60,6 +60,8 @@ and many tags require additional arguments (beyond the ``name`` parameter). **For most users, this is all you need to know**. If you want to go further and learn how to create your own custom tags, keep reading. +.. _di-instanceof: + Autoconfiguring Tags --------------------