Skip to content

Commit 17dae2f

Browse files
committed
[Messenger] Document middleware factories
1 parent a2140f0 commit 17dae2f

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

messenger.rst

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,68 @@ you can disable them like this:
273273
messenger.bus.default:
274274
default_middleware: false
275275
276+
Using middleware factories
277+
~~~~~~~~~~~~~~~~~~~~~~~~~~
278+
279+
Some third-parties may expose you configurable middleware by using factories.
280+
Such factories are actually relying on the Symfony DI capabilities and consist
281+
of this kind of two services:
282+
283+
.. code-block:: yaml
284+
285+
services:
286+
287+
# A factory class is registered as a service with required dependencies
288+
# to instantiate a middleware:
289+
doctrine.orm.messenger.middleware_factory.transaction:
290+
class: Symfony\Bridge\Doctrine\Messenger\DoctrineTransactionMiddlewareFactory
291+
arguments: ['@doctrine']
292+
293+
# An abstract definition that will call the factory with default arguments
294+
# or the one provided in the middleware config:
295+
messenger.middleware.doctrine_transaction_middleware:
296+
class: Symfony\Bridge\Doctrine\Messenger\DoctrineTransactionMiddleware
297+
factory: ['@doctrine.orm.messenger.middleware_factory.transaction', 'createMiddleware']
298+
abstract: true
299+
# the default arguments to use when none provided from config.
300+
# i.e:
301+
# middleware:
302+
# - doctrine_transaction_middleware: ~
303+
arguments: ['default']
304+
305+
The "default" value in this example corresponds to the entity manager name to use, as expected by the
306+
``Symfony\Bridge\Doctrine\Messenger\DoctrineTransactionMiddlewareFactory::createMiddleware`` method as argument.
307+
308+
Then you can reference and configure the ``messenger.middleware.doctrine_transaction_middleware``
309+
service as a middleware:
310+
311+
.. code-block:: yaml
312+
313+
framework:
314+
messenger:
315+
buses:
316+
command_bus:
317+
middleware:
318+
# Using defaults:
319+
- doctrine_transaction_middleware
320+
# Using another entity manager:
321+
- doctrine_transaction_middleware: ['custom']
322+
323+
.. note::
324+
325+
  The shorthand ``doctrine_transaction_middleware`` name can be used by convention,
326+
as the service id is prefixed with the ``messenger.middleware.`` namespace.
327+
328+
.. note::
329+
330+
  Middleware factories only allow scalar and array arguments in config (no service reference).
331+
For most advanced use-cases, register a concrete definition of the middleware yourself and use its id.
332+
333+
.. tip::
334+
335+
  The ``doctrine_transaction_middleware`` is an existing middleware wired
336+
by the DoctrineBundle if installed and the Messenger component enabled.
337+
276338
Your own Transport
277339
------------------
278340

0 commit comments

Comments
 (0)