Skip to content

[Messenger] Allow to override only a few of the abstract middleware definition #29034

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
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 @@ -1548,7 +1548,7 @@ private function registerMessengerConfiguration(array $config, ContainerBuilder
}

if ($container->getParameter('kernel.debug') && class_exists(Stopwatch::class)) {
array_unshift($middleware, array('id' => 'traceable', 'arguments' => array($busId)));
array_unshift($middleware, array('id' => 'traceable', 'arguments' => array(1 => $busId)));
}

$container->setParameter($busId.'.middleware', $middleware);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,8 @@ private function registerBusMiddleware(ContainerBuilder $container, string $busI
if (($definition = $container->findDefinition($messengerMiddlewareId))->isAbstract()) {
$childDefinition = new ChildDefinition($messengerMiddlewareId);
$count = \count($definition->getArguments());
foreach (array_values($arguments ?? array()) as $key => $argument) {

foreach (($arguments ?? array()) as $key => $argument) {
// Parent definition can provide default arguments.
// Replace each explicitly or add if not set:
$key < $count ? $childDefinition->replaceArgument($key, $argument) : $childDefinition->addArgument($argument);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -494,12 +494,14 @@ public function testRegistersMiddlewareFromServices()
$container = $this->getContainerBuilder($fooBusId = 'messenger.bus.foo');
$container->register('middleware_with_factory', UselessMiddleware::class)->addArgument('some_default')->setAbstract(true);
$container->register('middleware_with_factory_using_default', UselessMiddleware::class)->addArgument('some_default')->setAbstract(true);
$container->register('middleware_with_factory_using_a_lot_of_defaults', UselessMiddleware::class)->setArguments(array('one', 'two', 'three'))->setAbstract(true);
Copy link
Contributor

Choose a reason for hiding this comment

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

middleware_with_factory_replacing_defaults_by_index ? 😅 ("Using a lot of defaults" doesn't really convey the intent here)

$container->register(UselessMiddleware::class, UselessMiddleware::class);

$container->setParameter($middlewareParameter = $fooBusId.'.middleware', array(
array('id' => UselessMiddleware::class),
array('id' => 'middleware_with_factory', 'arguments' => array('foo', 'bar')),
array('id' => 'middleware_with_factory_using_default'),
array('id' => 'middleware_with_factory_using_a_lot_of_defaults', 'arguments' => array(1 => '2', 3 => 'four')),
));

(new MessengerPass())->process($container);
Expand All @@ -519,10 +521,18 @@ public function testRegistersMiddlewareFromServices()
'parent default argument is used'
);

$this->assertTrue($container->hasDefinition($factoryWithALotOfDefaultsChildMiddlewareId = $fooBusId.'.middleware.middleware_with_factory_using_a_lot_of_defaults'));
$this->assertEquals(
array('one', '2', 'three', 'four'),
$container->getDefinition($factoryWithALotOfDefaultsChildMiddlewareId)->getArguments(),
'parent arguments are not overwritten'
Copy link
Contributor

Choose a reason for hiding this comment

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

parent arguments are overwritten by index, and next ones appended

);

$this->assertEquals(array(
new Reference(UselessMiddleware::class),
new Reference($factoryChildMiddlewareId),
new Reference($factoryWithDefaultChildMiddlewareId),
new Reference($factoryWithALotOfDefaultsChildMiddlewareId),
), $container->getDefinition($fooBusId)->getArgument(0)->getValues());
$this->assertFalse($container->hasParameter($middlewareParameter));
}
Expand Down