diff --git a/Attribute/AsDocumentListener.php b/Attribute/AsDocumentListener.php new file mode 100644 index 00000000..0c73e8ce --- /dev/null +++ b/Attribute/AsDocumentListener.php @@ -0,0 +1,22 @@ +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); } diff --git a/Tests/DependencyInjection/DoctrineMongoDBExtensionTest.php b/Tests/DependencyInjection/DoctrineMongoDBExtensionTest.php index 6ca333b7..c876040c 100644 --- a/Tests/DependencyInjection/DoctrineMongoDBExtensionTest.php +++ b/Tests/DependencyInjection/DoctrineMongoDBExtensionTest.php @@ -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; @@ -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 @@ -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(); @@ -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; @@ -166,9 +206,7 @@ public function getAutomappingConfigurations(): array ]; } - /** - * @dataProvider getAutomappingConfigurations - */ + /** @dataProvider getAutomappingConfigurations */ public function testAutomapping(array $documentManagers): void { $container = $this->getContainer([ diff --git a/Tests/DependencyInjection/Fixtures/Bundles/DocumentListenerBundle/Document/Test.php b/Tests/DependencyInjection/Fixtures/Bundles/DocumentListenerBundle/Document/Test.php new file mode 100644 index 00000000..e0f67ecc --- /dev/null +++ b/Tests/DependencyInjection/Fixtures/Bundles/DocumentListenerBundle/Document/Test.php @@ -0,0 +1,12 @@ +