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

[Messenger] Document middleware factories #9774

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 24, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 110 additions & 0 deletions 110 messenger.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 </service_container>` 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

<!-- config/packages/messenger.xml -->
<container xmlns="http://symfony.com/schema/dic/symfony"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:framework="http://symfony.com/schema/dic/symfony"
xsi:schemaLocation="http://symfony.com/schema/dic/services
http://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/symfony
http://symfony.com/schema/dic/symfony/symfony-1.0.xsd">

<framework:config>
<framework:messenger>
<framework:bus name="command_bus">
<!-- Using defaults: -->
<framework:middleware id="doctrine_transaction_middleware" />
<!-- Using another entity manager -->
<framework:middleware id="doctrine_transaction_middleware">
<framework:argument>custom</framework:argument>
</framework:middleware>
</framework:bus>
</framework:messenger>
</framework:config>
</container>

.. 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
------------------

Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.