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 AsDocumentListener attribute #715

Merged
merged 1 commit into from
Nov 26, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions Attribute/AsDocumentListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Doctrine\Bundle\MongoDBBundle\Attribute;

use Attribute;

/**
* Service tag to autoconfigure document listeners.
*/
#[Attribute(Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)]
class AsDocumentListener
{
public function __construct(
public ?string $event = null,
public ?string $method = null,
public ?bool $lazy = null,
public ?string $connection = null,
IonBazan marked this conversation as resolved.
Show resolved Hide resolved
) {
}
}
12 changes: 12 additions & 0 deletions DependencyInjection/DoctrineMongoDBExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Doctrine\Bundle\MongoDBBundle\DependencyInjection;

use Doctrine\Bundle\MongoDBBundle\Attribute\AsDocumentListener;
use Doctrine\Bundle\MongoDBBundle\DependencyInjection\Compiler\FixturesCompilerPass;
use Doctrine\Bundle\MongoDBBundle\DependencyInjection\Compiler\ServiceRepositoryCompilerPass;
use Doctrine\Bundle\MongoDBBundle\EventSubscriber\EventSubscriberInterface;
Expand Down Expand Up @@ -129,6 +130,17 @@ public function load(array $configs, ContainerBuilder $container)
$container->registerForAutoconfiguration(EventSubscriberInterface::class)
->addTag('doctrine_mongodb.odm.event_subscriber');

if (method_exists($container, 'registerAttributeForAutoconfiguration')) {
$container->registerAttributeForAutoconfiguration(AsDocumentListener::class, static function (ChildDefinition $definition, AsDocumentListener $attribute) {
$definition->addTag('doctrine_mongodb.odm.event_listener', [
'event' => $attribute->event,
'method' => $attribute->method,
'lazy' => $attribute->lazy,
'connection' => $attribute->connection,
]);
});
}

$this->loadMessengerServices($container);
}

Expand Down
56 changes: 47 additions & 9 deletions Tests/DependencyInjection/DoctrineMongoDBExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
namespace Doctrine\Bundle\MongoDBBundle\Tests\DependencyInjection;

use Doctrine\Bundle\MongoDBBundle\DependencyInjection\DoctrineMongoDBExtension;
use Doctrine\Bundle\MongoDBBundle\Tests\DependencyInjection\Fixtures\Bundles\DocumentListenerBundle\EventListener\TestAttributeListener;
use PHPUnit\Framework\TestCase;
use Symfony\Bridge\Doctrine\Messenger\DoctrineClearEntityManagerWorkerSubscriber;
use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
Expand All @@ -18,6 +20,7 @@
use function class_exists;
use function interface_exists;
use function is_dir;
use function method_exists;
use function sys_get_temp_dir;

class DoctrineMongoDBExtensionTest extends TestCase
Expand Down Expand Up @@ -49,9 +52,7 @@ public function buildMinimalContainer(): ContainerBuilder
]));
}

/**
* @dataProvider parameterProvider
*/
/** @dataProvider parameterProvider */
public function testParameterOverride(string $option, string $parameter, string $value): void
{
$container = $this->buildMinimalContainer();
Expand All @@ -64,9 +65,48 @@ public function testParameterOverride(string $option, string $parameter, string
$this->assertEquals($value, $container->getParameter('doctrine_mongodb.odm.' . $parameter));
}

/**
* @param string|string[] $bundles
*/
/** @requires PHP 8 */
public function testAsDocumentListenerAttribute()
{
if (! method_exists(ContainerBuilder::class, 'getAutoconfiguredAttributes')) {
$this->markTestSkipped('symfony/dependency-injection 5.3.0 needed');
}

$container = $this->getContainer('DocumentListenerBundle');
$extension = new DoctrineMongoDBExtension();
$container->registerExtension($extension);

$extension->load([
[
'connections' => ['default' => []],
'document_managers' => [
'default' => [
'mappings' => ['DocumentListenerBundle' => 'attribute'],
],
],
],
], $container);

$container->register(TestAttributeListener::class, TestAttributeListener::class)
->setAutowired(true)
->setAutoconfigured(true)
->setPublic(false);
$container->setAlias('test_alias__' . TestAttributeListener::class, new Alias(TestAttributeListener::class, true));
$container->compile();

$listenerDefinition = $container->getDefinition('test_alias__' . TestAttributeListener::class);

self::assertSame([
[
'event' => 'prePersist',
'method' => 'onPrePersist',
'lazy' => true,
'connection' => 'test',
],
], $listenerDefinition->getTag('doctrine_mongodb.odm.event_listener'));
}

/** @param string|string[] $bundles */
private function getContainer($bundles = 'OtherXmlBundle'): ContainerBuilder
{
$bundles = (array) $bundles;
Expand Down Expand Up @@ -166,9 +206,7 @@ public function getAutomappingConfigurations(): array
];
}

/**
* @dataProvider getAutomappingConfigurations
*/
/** @dataProvider getAutomappingConfigurations */
public function testAutomapping(array $documentManagers): void
{
$container = $this->getContainer([
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Doctrine\Bundle\MongoDBBundle\Tests\DependencyInjection\Fixtures\Bundles\DocumentListenerBundle\Document;

use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;

#[MongoDB\Document]
class Test
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Doctrine\Bundle\MongoDBBundle\Tests\DependencyInjection\Fixtures\Bundles\DocumentListenerBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class DocumentListenerBundle extends Bundle
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Doctrine\Bundle\MongoDBBundle\Tests\DependencyInjection\Fixtures\Bundles\DocumentListenerBundle\EventListener;

use Doctrine\Bundle\MongoDBBundle\Attribute\AsDocumentListener;

#[AsDocumentListener(event: 'prePersist', method: 'onPrePersist', lazy: true, connection: 'test')]
class TestAttributeListener
{
public function onPrePersist(): void
{
}
}