Skip to content
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

Add marker interface for event subscriber autoconfiguration #623

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions DependencyInjection/DoctrineMongoDBExtension.php
Expand Up @@ -6,6 +6,7 @@

use Doctrine\Bundle\MongoDBBundle\DependencyInjection\Compiler\FixturesCompilerPass;
use Doctrine\Bundle\MongoDBBundle\DependencyInjection\Compiler\ServiceRepositoryCompilerPass;
use Doctrine\Bundle\MongoDBBundle\EventSubscriber\EventSubscriberInterface;
use Doctrine\Bundle\MongoDBBundle\Fixture\ODMFixtureInterface;
use Doctrine\Bundle\MongoDBBundle\Repository\ServiceDocumentRepositoryInterface;
use Doctrine\Common\DataFixtures\Loader as DataFixturesLoader;
Expand Down Expand Up @@ -101,6 +102,9 @@ public function load(array $configs, ContainerBuilder $container)

$container->registerForAutoconfiguration(ServiceDocumentRepositoryInterface::class)
->addTag(ServiceRepositoryCompilerPass::REPOSITORY_SERVICE_TAG);

$container->registerForAutoconfiguration(EventSubscriberInterface::class)
->addTag('doctrine_mongodb.odm.event_subscriber');
}

/**
Expand Down
11 changes: 11 additions & 0 deletions EventSubscriber/EventSubscriberInterface.php
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Doctrine\Bundle\MongoDBBundle\EventSubscriber;

use Doctrine\Common\EventSubscriber;

interface EventSubscriberInterface extends EventSubscriber
{
}
96 changes: 95 additions & 1 deletion Resources/doc/events.rst
Expand Up @@ -63,13 +63,106 @@ when its event is dispatched.
Event Subscribers
-----------------

Use the ``doctrine_mongodb.odm.event_subscriber`` tag
Implement ``Doctrine\Bundle\MongoDBBundle\EventSubscriber\EventSubscriberInterface``
greg0ire marked this conversation as resolved.
Show resolved Hide resolved
and `autoconfiguration`_ to automatically register your class as a MongoODM
event subscriber.

.. code-block:: php

// src/App/EventSubscriber/MongoDB/ProductSubscriber.php
namespace App\EventSubscriber\MongoDB;

use App\Document\Product;
use Doctrine\Bundle\MongoDBBundle\EventSubscriber\EventSubscriberInterface;

class ProductSubscriber implements EventSubscriberInterface
{
public function getSubscribedEvents()
{
return [
// List events to subscribe
];
}
}

.. configuration-block::

.. code-block:: yaml

# config/services.yaml
services:

App\EventSubscriber\MongoDB\:
resource: '../src/EventSubscriber/MongoDB/*'
autoconfigure: true

.. code-block:: xml

<!-- config/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
https://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<prototype namespace="App\EventSubscriber\MongoDB\" resource="../src/EventSubscriber/MongoDB/*" autoconfigure="true" />
</services>
</container>

Alternatively, use the ``doctrine_mongodb.odm.event_subscriber`` tag
to register a subscriber. Subscribers must implement the
``Doctrine\Common\EventSubscriber`` interface, which means that they must
contain method returning the events they will observe. For this reason,
this tag has no ``event`` attribute, however the ``connection``,
``priority`` and ``lazy`` attributes are available.

.. code-block:: php

// src/App/EventSubscriber/MongoDB/ProductSubscriber.php
namespace App\EventSubscriber\MongoDB;

use App\Document\Product;
use Doctrine\Common\EventSubscriber;

class ProductSubscriber implements EventSubscriber
{
public function getSubscribedEvents()
{
return [
// List events to subscribe
];
}
}

.. configuration-block::

.. code-block:: yaml

# config/services.yaml
services:

App\EventSubscriber\MongoDB\:
resource: '../src/EventSubscriber/MongoDB/*'
tags:
- { name: doctrine_mongodb.odm.event_subscriber }

.. code-block:: xml

<!-- config/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
https://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<prototype namespace="App\EventSubscriber\MongoDB\" resource="../src/EventSubscriber/MongoDB/*">
<tag name="doctrine_mongodb.odm.event_subscriber" />
</prototype>
</services>
</container>

.. note::

Unlike Symfony event listeners, Doctrine's event manager expects each
Expand All @@ -80,3 +173,4 @@ this tag has no ``event`` attribute, however the ``connection``,
.. _`event dispatcher`: https://symfony.com/doc/current/components/event_dispatcher.html
.. _`Event Documentation`: https://www.doctrine-project.org/projects/doctrine-mongodb-odm/en/latest/reference/events.html
.. _`tagging`: https://symfony.com/doc/current/service_container/tags.html
.. _`autoconfiguration`: https://symfony.com/doc/current/service_container.html#the-autoconfigure-option