Skip to content

[Messenger] Exclude AsMessenger attribute from services WIP #60082

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

Closed
Closed
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
1 change: 1 addition & 0 deletions src/Symfony/Component/DependencyInjection/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
---
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Symfony\Component\DependencyInjection\Tests\Fixtures\Utils;

use Symfony\Component\Messenger\Attribute\AsMessage;

#[AsMessage]
class ObjectAsMessage
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Symfony\Component\DependencyInjection\Exception\OutOfBoundsException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\Messenger\Attribute\AsMessage;
use Symfony\Component\Messenger\Handler\HandlerDescriptor;
use Symfony\Component\Messenger\Handler\HandlersLocator;
use Symfony\Component\Messenger\TraceableMessageBus;
Expand Down Expand Up @@ -50,6 +51,7 @@ public function process(ContainerBuilder $container): void
$this->registerReceivers($container, $busIds);
}
$this->registerHandlers($container, $busIds);
$this->excludeMessagesFromServices($container);
}

private function registerHandlers(ContainerBuilder $container, array $busIds): void
Expand Down Expand Up @@ -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')
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Loading