diff --git a/src/Symfony/Component/DependencyInjection/CHANGELOG.md b/src/Symfony/Component/DependencyInjection/CHANGELOG.md index 07521bc863e42..abd7104c20bac 100644 --- a/src/Symfony/Component/DependencyInjection/CHANGELOG.md +++ b/src/Symfony/Component/DependencyInjection/CHANGELOG.md @@ -11,6 +11,7 @@ CHANGELOG for auto-configuration of classes excluded from the service container * Accept multiple auto-configuration callbacks for the same attribute class * Leverage native lazy objects when possible for lazy services + * Class attributes `#[AsMessage]` and `#[Entity]` are now automatically excluded as services injection, like `#[Exclude]` does 7.2 --- diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/Utils/ObjectAsMessage.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/Utils/ObjectAsMessage.php new file mode 100644 index 0000000000000..eaf0f9f8355e8 --- /dev/null +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/Utils/ObjectAsMessage.php @@ -0,0 +1,10 @@ +registerReceivers($container, $busIds); } $this->registerHandlers($container, $busIds); + $this->excludeMessagesFromServices($container); } private function registerHandlers(ContainerBuilder $container, array $busIds): void @@ -388,4 +390,12 @@ private function getServiceClass(ContainerBuilder $container, string $serviceId) return $definition->getClass(); } } + + private function excludeMessagesFromServices(ContainerBuilder $container): void + { + $container->registerAttributeForAutoconfiguration( + AsMessage::class, + static fn (ChildDefinition $definition) => $definition->addTag('container.excluded') + ); + } } diff --git a/src/Symfony/Component/Messenger/Tests/DependencyInjection/MessengerPassTest.php b/src/Symfony/Component/Messenger/Tests/DependencyInjection/MessengerPassTest.php index 4de01a129e546..df74dd11f6dd4 100644 --- a/src/Symfony/Component/Messenger/Tests/DependencyInjection/MessengerPassTest.php +++ b/src/Symfony/Component/Messenger/Tests/DependencyInjection/MessengerPassTest.php @@ -23,6 +23,7 @@ use Symfony\Component\DependencyInjection\Exception\RuntimeException; use Symfony\Component\DependencyInjection\Reference; use Symfony\Component\DependencyInjection\ServiceLocator; +use Symfony\Component\DependencyInjection\Tests\Fixtures\Utils\ObjectAsMessage; use Symfony\Component\Messenger\Attribute\AsMessageHandler; use Symfony\Component\Messenger\Bridge\Amqp\Transport\AmqpReceiver; use Symfony\Component\Messenger\Command\ConsumeMessagesCommand; @@ -385,6 +386,16 @@ public function testRegisterAbstractHandler() ); } + public function testRegisterClassesWithExcludedAttributes() + { + $container = $this->getContainerBuilder(); + $container->register(ObjectAsMessage::class); + + (new MessengerPass())->process($container); + + $this->assertSame(true, $container->getDefinition(ObjectAsMessage::class)->hasTag('container.excluded')); //false I miss something + } + public function testThrowsExceptionIfTheHandlerClassDoesNotExist() { $this->expectException(RuntimeException::class);