@@ -273,6 +273,116 @@ you can disable them like this:
273
273
messenger.bus.default :
274
274
default_middleware : false
275
275
276
+ Using Middleware Factories
277
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
278
+
279
+ Some third-party bundles and libraries provide configurable middleware via
280
+ factories. Using them requires a two-step configuration based on Symfony's
281
+ :doc: `dependency injection </service_container >` features:
282
+
283
+ .. code-block :: yaml
284
+
285
+ services :
286
+
287
+ # Step 1: a factory class is registered as a service with the required
288
+ # dependencies to instantiate a middleware
289
+ doctrine.orm.messenger.middleware_factory.transaction :
290
+ class : Symfony\Bridge\Doctrine\Messenger\DoctrineTransactionMiddlewareFactory
291
+ arguments : ['@doctrine']
292
+
293
+ # Step 2: an abstract definition that will call the factory with default
294
+ # arguments 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. Example:
300
+ # middleware:
301
+ # - doctrine_transaction_middleware: ~
302
+ arguments : ['default']
303
+
304
+ The "default" value in this example is the name of the entity manager to use,
305
+ which is the argument expected by the
306
+ ``Symfony\Bridge\Doctrine\Messenger\DoctrineTransactionMiddlewareFactory::createMiddleware `` method.
307
+
308
+ Then you can reference and configure the
309
+ ``messenger.middleware.doctrine_transaction_middleware `` 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
+<
E30A
/span> <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 ``doctrine_transaction_middleware `` shortcut is a convention. The real
372
+ 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
377
+ references to other services). For most advanced use-cases, register a
378
+ concrete definition of the middleware manually and use its id.
379
+
380
+ .. tip ::
381
+
382
+ The ``doctrine_transaction_middleware `` is a built-in middleware wired
383
+ automatically when the DoctrineBundle and the Messenger component are
384
+ installed and enabled.
385
+
276
386
Your own Transport
277
387
------------------
278
388
0 commit comments