8000 [Messenger] Document middleware factories · symfony/symfony-docs@2880027 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2880027

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

File tree

1 file changed

+108
-0
lines changed

1 file changed

+108
-0
lines changed

messenger.rst

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,114 @@ 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+
.. configuration-block::
312+
313+
.. code-block:: yaml
314+
315+
# config/packages/messenger.yaml
316+
framework:
317+
messenger:
318+
buses:
319+
command_bus:
320+
middleware:
321+
# Using defaults:
322+
- doctrine_transaction_middleware
323+
# Using another entity manager:
324+
- doctrine_transaction_middleware: ['custom']
325+
326+
.. code-block:: xml
327+
328+
<!-- config/packages/messenger.xml -->
329+
<container xmlns="http://symfony.com/schema/dic/symfony"
330+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
331+
xmlns:framework="http://symfony.com/schema/dic/symfony"
332+
xsi:schemaLocation="http://symfony.com/schema/dic/services
333+
http://symfony.com/schema/dic/services/services-1.0.xsd
334+
http://symfony.com/schema/dic/symfony
335+
http://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
336+
337+
<framework:config>
338+
<framework:messenger>
339+
<framework:bus name="command_bus">
340+
<!-- Using defaults: -->
341+
<framework:middleware id="doctrine_transaction_middleware" />
342+
<!-- Using another entity manager -->
343+
<framework:middleware id="doctrine_transaction_middleware">
344+
<framework:argument>custom</framework:argument>
345+
</framework:middleware>
346+
</framework:bus>
347+
</framework:messenger>
348+
</framework:config>
349+
</container>
350+
351+
.. code-block:: php
352+
353+
// config/packages/messenger.php
354+
$container->loadFromExtension('framework', array(
355+
'messenger' => array(
356+
'buses' => array(
357+
'command_bus' => array(
358+
'middleware' => array(
359+
// Using defaults:
360+
'doctrine_transaction_middleware',
361+
// Using another entity manager
362+
array('id' => 'doctrine_transaction_middleware', 'arguments' => array('custom')),
363+
),
364+
),
365+
),
366+
),
367+
));
368+
369+
.. note::
370+
371+
  The shorthand ``doctrine_transaction_middleware`` name can be used by convention,
372+
as the service id is prefixed with the ``messenger.middleware.`` namespace.
373+
374+
.. note::
375+
376+
  Middleware factories only allow scalar and array arguments in config (no service reference).
377+
For most advanced use-cases, register a concrete definition of the middleware yourself and use its id.
378+
379+
.. tip::
380+
381+
  The ``doctrine_transaction_middleware`` is an existing middleware wired
382+
by the DoctrineBundle if installed and the Messenger component enabled.
383+
276384
Your own Transport
277385
------------------
278386

0 commit comments

Comments
 (0)
0