diff --git a/messenger.rst b/messenger.rst index ed73d148ae2..5d6e4437a14 100644 --- a/messenger.rst +++ b/messenger.rst @@ -273,6 +273,116 @@ you can disable them like this: messenger.bus.default: default_middleware: false +Using Middleware Factories +~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Some third-party bundles and libraries provide configurable middleware via +factories. Using them requires a two-step configuration based on Symfony's +:doc:`dependency injection ` features: + +.. code-block:: yaml + + services: + + # Step 1: a factory class is registered as a service with the required + # dependencies to instantiate a middleware + doctrine.orm.messenger.middleware_factory.transaction: + class: Symfony\Bridge\Doctrine\Messenger\DoctrineTransactionMiddlewareFactory + arguments: ['@doctrine'] + + # Step 2: an abstract definition that will call the factory with default + # arguments or the one provided in the middleware config + messenger.middleware.doctrine_transaction_middleware: + class: Symfony\Bridge\Doctrine\Messenger\DoctrineTransactionMiddleware + factory: ['@doctrine.orm.messenger.middleware_factory.transaction', 'createMiddleware'] + abstract: true + # the default arguments to use when none provided from config. Example: + # middleware: + # - doctrine_transaction_middleware: ~ + arguments: ['default'] + +The "default" value in this example is the name of the entity manager to use, +which is the argument expected by the +``Symfony\Bridge\Doctrine\Messenger\DoctrineTransactionMiddlewareFactory::createMiddleware`` method. + +Then you can reference and configure the +``messenger.middleware.doctrine_transaction_middleware`` service as a middleware: + +.. configuration-block:: + + .. code-block:: yaml + + # config/packages/messenger.yaml + framework: + messenger: + buses: + command_bus: + middleware: + # Using defaults: + - doctrine_transaction_middleware + # Using another entity manager: + - doctrine_transaction_middleware: ['custom'] + + .. code-block:: xml + + + + + + + + + + + + custom + + + + + + + .. code-block:: php + + // config/packages/messenger.php + $container->loadFromExtension('framework', array( + 'messenger' => array( + 'buses' => array( + 'command_bus' => array( + 'middleware' => array( + // Using defaults: + 'doctrine_transaction_middleware', + // Using another entity manager + array('id' => 'doctrine_transaction_middleware', 'arguments' => array('custom')), + ), + ), + ), + ), + )); + +.. note:: + + The ``doctrine_transaction_middleware`` shortcut is a convention. The real + service id is prefixed with the ``messenger.middleware.`` namespace. + +.. note:: + + Middleware factories only allow scalar and array arguments in config (no + references to other services). For most advanced use-cases, register a + concrete definition of the middleware manually and use its id. + +.. tip:: + + The ``doctrine_transaction_middleware`` is a built-in middleware wired + automatically when the DoctrineBundle and the Messenger component are + installed and enabled. + Your own Transport ------------------