Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Messenger] Allow handler locator to be set #60753

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

Open
wants to merge 1 commit into
base: 7.4
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions src/Symfony/Component/Messenger/Bridge/AmazonSqs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

7.4
---

* Allow handler locator to be set by applications

7.3
---

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,13 @@ private function registerHandlers(ContainerBuilder $container, array $busIds): v
$container->addDefinitions($definitions);

foreach ($busIds as $bus) {
$container->register($locatorId = $bus.'.messenger.handlers_locator', HandlersLocator::class)
->setArgument(0, $handlersLocatorMappingByBus[$bus] ?? [])
;
$locatorId = $bus.'.messenger.handlers_locator';
if (!$container->has($locatorId)) {
$container->register($locatorId, HandlersLocator::class)
Comment on lines +169 to +170
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could it be interesting to give the handlers locator to the existing definition?

Suggested change
if (!$container->has($locatorId)) {
$container->register($locatorId, HandlersLocator::class)
($container->hasDefinition($locatorId) ? $container->getDefinition($locatorId) : $container->register($locatorId, HandlersLocator::class))

Copy link
Contributor Author

@maxbaldanza maxbaldanza Jun 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be but that means the first argument needs to be always be an array and if a new argument is added in Symfony in the future for the HandlersLocator then it could be classed as a breaking change because the arguments defined at application level would need to match.

For example, say a bool is now needed as a second param and someone has already defined the service with a different argument then you'd end up with an error: Argument #2 ($foo) must be of type string, bool given

I think it's safer with my suggestion but happy to change it if you prefer.

->setArgument(0, $handlersLocatorMappingByBus[$bus] ?? [])
;
}

if ($container->has($handleMessageId = $bus.'.middleware.handle_message')) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

while at it:

Suggested change
if ($container->has($handleMessageId = $bus.'.middleware.handle_message')) {
if ($container->hasDefinition($handleMessageId = $bus.'.middleware.handle_message')) {

$container->getDefinition($handleMessageId)
->replaceArgument(0, new Reference($locatorId))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -809,6 +809,17 @@ public function testItRegistersTheDebugCommand()
], $container->getDefinition('console.command.messenger_debug')->getArgument(0));
}

public function testCanOverrideHandlersLocator()
{
$container = $this->getContainerBuilder($busId = 'message_bus');

$container->register($busId. '.messenger.handlers_locator', \stdClass::class);
Copy link
Contributor Author

@maxbaldanza maxbaldanza Jun 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stdClass isn't the right class here but wasn't sure it was worth the effort to create a fake HandlersLocatorInterface but happy to add it if you would prefer


(new MessengerPass())->process($container);

$this->assertSame(\stdClass::class, $container->getDefinition($busId. '.messenger.handlers_locator')->getClass());
}

private function getContainerBuilder(string $busId = 'message_bus'): ContainerBuilder
{
$container = new ContainerBuilder();
Expand Down