-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[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
base: 7.4
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
--- | ||
|
||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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) | ||||||
->setArgument(0, $handlersLocatorMappingByBus[$bus] ?? []) | ||||||
; | ||||||
} | ||||||
|
||||||
if ($container->has($handleMessageId = $bus.'.middleware.handle_message')) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. while at it:
Suggested change
|
||||||
$container->getDefinition($handleMessageId) | ||||||
->replaceArgument(0, new Reference($locatorId)) | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
(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(); | ||
|
There was a problem hiding this comment.
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?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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.