Skip to content

[Messenger] add XML and PHP code blocks #10044

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 1 commit into from
Jul 12, 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
97 changes: 77 additions & 20 deletions messenger.rst
Original file line number Diff line number Diff line change
Expand Up @@ -612,26 +612,83 @@ Some third-party bundles and libraries provide configurable middleware via
factories. Defining such 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 ones 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']
.. configuration-block::

.. code-block:: yaml

# config/services.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:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated to this but ... is there any reason for keep using snake-case IDs for services instead of FQCN IDs?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, potentially there could be other service for this class too. If they all used the class name as the service id would cause conflicts. So I think it's better to use custom IDs for these services.

class: Symfony\Bridge\Doctrine\Messenger\DoctrineTransactionMiddlewareFactory
arguments: ['@doctrine']

# Step 2: an abstract definition that will call the factory with default
# arguments or the ones 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']

.. code-block:: xml

<!-- cconfig/services.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services
http://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<!-- Step 1: a factory class is registered as a service with the required
dependencies to instantiate a middleware -->
<service id="doctrine.orm.messenger.middleware_factory.transaction"
class="Symfony\Bridge\Doctrine\Messenger\DoctrineTransactionMiddlewareFactory">

<argument type="service" id="doctrine" />
</service>

<!-- Step 2: an abstract definition that will call the factory with default
arguments or the ones provided in the middleware config -->
<service id="messenger.middleware.doctrine_transaction_middleware"
class="Symfony\Bridge\Doctrine\Messenger\DoctrineTransactionMiddleware"
abstract="true">

<factory service="doctrine.orm.messenger.middleware_factory.transaction"
method="createMiddleware" />
<argument>default</argument>
</service>
</services>
</container>

.. code-block:: php

// config/services.php
use Symfony\Bridge\Doctrine\Messenger\DoctrineTransactionMiddleware;
use Symfony\Bridge\Doctrine\Messenger\DoctrineTransactionMiddlewareFactory;
use Symfony\Component\DependencyInjection\Reference;

// Step 1: a factory class is registered as a service with the required
// dependencies to instantiate a middleware
$container
->register('doctrine.orm.messenger.middleware_factory.transaction', DoctrineTransactionMiddlewareFactory::class)
->setArguments(array(new Reference('doctrine')));

// Step 2: an abstract definition that will call the factory with default
// arguments or the ones provided in the middleware config
$container->register('messenger.middleware.doctrine_transaction_middleware', DoctrineTransactionMiddleware::class)
->setFactory(array(
new Reference('doctrine.orm.messenger.middleware_factory.transaction'),
'createMiddleware'
))
->setAbstract(true)
->setArguments(array('default'));

The "default" value in this example is the name of the entity manager to use,
which is the argument expected by the
Expand Down