Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 1ca0337

Browse filesBrowse files
committed
minor #9774 [Messenger] Document middleware factories (ogizanagi, javiereguiluz)
This PR was merged into the 4.1 branch. Discussion ---------- [Messenger] Document middleware factories <!-- If your pull request fixes a BUG, use the oldest maintained branch that contains the bug (see https://symfony.com/roadmap for the list of maintained branches). If your pull request documents a NEW FEATURE, use the same Symfony branch where the feature was introduced (and `master` for features of unreleased versions). --> Refs: - Symfony PR: symfony/symfony#27128 - DoctrineBundle PR: doctrine/DoctrineBundle#817 Commits ------- c5ef7c8 Reowrd to restore the original meaning a87f21b Minor rewords 2880027 [Messenger] Document middleware factories
2 parents 5318f06 + c5ef7c8 commit 1ca0337
Copy full SHA for 1ca0337

File tree

1 file changed

+110
-0
lines changed
Filter options

1 file changed

+110
-0
lines changed

‎messenger.rst

Copy file name to clipboardExpand all lines: messenger.rst
+110Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,116 @@ you can disable them like this:
273273
messenger.bus.default:
274274
default_middleware: false
275275
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+
<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+
276386
Your own Transport
277387
------------------
278388

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.