Skip to content

[Messenger] Add messenger configuration to make a message bus not get all handlers by default #42693

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
Original file line number Diff line number Diff line change
Expand Up @@ -1555,6 +1555,9 @@ function ($a) {
->booleanNode('allow_no_senders')->defaultTrue()->end()
->end()
->end()
->booleanNode('default_handlers')
->defaultTrue()
->end()
->arrayNode('middleware')
->performNoDeepMerging()
->beforeNormalization()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2036,7 +2036,7 @@ private function registerMessengerConfiguration(array $config, ContainerBuilder
}

$container->setParameter($busId.'.middleware', $middleware);
$container->register($busId, MessageBus::class)->addArgument([])->addTag('messenger.bus');
$container->register($busId, MessageBus::class)->addArgument([])->addTag('messenger.bus', ['default_handlers' => $bus['default_handlers'] ?? true]);

if ($busId === $config['default_bus']) {
$container->setAlias('messenger.default_bus', $busId)->setPublic(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,14 @@ class MessengerPass implements CompilerPassInterface
public function process(ContainerBuilder $container)
{
$busIds = [];
$defaultBusIds = [];
foreach ($container->findTaggedServiceIds('messenger.bus') as $busId => $tags) {
$busIds[] = $busId;
$tag = reset($tags);
if ($tag['default_handlers'] ?? true) {
$defaultBusIds[] = $busId;
}

if ($container->hasParameter($busMiddlewareParameter = $busId.'.middleware')) {
$this->registerBusMiddleware($container, $busId, $container->getParameter($busMiddlewareParameter));

Expand All @@ -52,10 +58,10 @@ public function process(ContainerBuilder $container)
if ($container->hasDefinition('messenger.receiver_locator')) {
$this->registerReceivers($container, $busIds);
}
$this->registerHandlers($container, $busIds);
$this->registerHandlers($container, $busIds, $defaultBusIds);
}

private function registerHandlers(ContainerBuilder $container, array $busIds)
private function registerHandlers(ContainerBuilder $container, array $busIds, array $defaultBusIds)
{
$definitions = [];
$handlersByBusAndMessage = [];
Expand All @@ -81,7 +87,7 @@ private function registerHandlers(ContainerBuilder $container, array $busIds)
}

$message = null;
$handlerBuses = (array) ($tag['bus'] ?? $busIds);
$handlerBuses = (array) ($tag['bus'] ?? $defaultBusIds);

foreach ($handles as $message => $options) {
$buses = $handlerBuses;
Expand Down