-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Messenger] Middleware factories support in config #27128
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
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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bridge\Doctrine\Messenger; | ||
|
||
use Symfony\Bridge\Doctrine\ManagerRegistry; | ||
|
||
/** | ||
* Create a Doctrine ORM transaction middleware to be used in a message bus from an entity manager name. | ||
* | ||
* @author Maxime Steinhausser <maxime.steinhausser@gmail.com> | ||
* | ||
* @experimental in 4.1 | ||
* @final | ||
*/ | ||
class DoctrineTransactionMiddlewareFactory | ||
{ | ||
private $managerRegistry; | ||
|
||
public function __construct(ManagerRegistry $managerRegistry) | ||
{ | ||
$this->managerRegistry = $managerRegistry; | ||
} | ||
|
||
public function createMiddleware(string $managerName): DoctrineTransactionMiddleware | ||
{ | ||
return new DoctrineTransactionMiddleware($this->managerRegistry, $managerName); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
$container->loadFromExtension('framework', array( | ||
'messenger' => array( | ||
'buses' => array( | ||
'command_bus' => array( | ||
'middleware' => array( | ||
array( | ||
'foo' => array('qux'), | ||
'bar' => array('baz'), | ||
), | ||
), | ||
), | ||
), | ||
), | ||
)); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
framework: | ||
messenger: | ||
buses: | ||
command_bus: | ||
middleware: | ||
- foo: ['qux'] | ||
bar: ['baz'] |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -221,24 +221,37 @@ private function registerBusToCollector(ContainerBuilder $container, string $bus | |
$container->getDefinition('messenger.data_collector')->addMethodCall('registerBus', array($busId, new Reference($tracedBusId))); | ||
} | ||
|
||
private function registerBusMiddleware(ContainerBuilder $container, string $busId, array $middleware) | ||
private function registerBusMiddleware(ContainerBuilder $container, string $busId, array $middlewareCollection) | ||
{ | ||
$container->getDefinition($busId)->replaceArgument(0, array_map(function (string $name) use ($container, $busId) { | ||
if (!$container->has($messengerMiddlewareId = 'messenger.middleware.'.$name)) { | ||
$messengerMiddlewareId = $name; | ||
$middlewareReferences = array(); | ||
foreach ($middlewareCollection as $middlewareItem) { | ||
$id = $middlewareItem['id']; | ||
$arguments = $middlewareItem['arguments'] ?? array(); | ||
if (!$container->has($messengerMiddlewareId = 'messenger.middleware.'.$id)) { | ||
$messengerMiddlewareId = $id; | ||
} | ||
|
||
if (!$container->has($messengerMiddlewareId)) { | ||
throw new RuntimeException(sprintf('Invalid middleware "%s": define such service to be able to use it.', $name)); | ||
throw new RuntimeException(sprintf('Invalid middleware "%s": define such service to be able to use it.', $id)); | ||
} | ||
|
||
if ($container->getDefinition($messengerMiddlewareId)->isAbstract()) { | ||
if (($definition = $container->findDefinition($messengerMiddlewareId))->isAbstract()) { | ||
$childDefinition = new ChildDefinition($messengerMiddlewareId); | ||
$count = \count($definition->getArguments()); | ||
foreach (array_values($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); | ||
} | ||
|
||
$container->setDefinition($messengerMiddlewareId = $busId.'.middleware.'.$name, $childDefinition); | ||
$container->setDefinition($messengerMiddlewareId = $busId.'.middleware.'.$id, $childDefinition); | ||
} elseif ($arguments) { | ||
throw new RuntimeException(sprintf('Invalid middleware factory "%s": a middleware factory must be an abstract definition.', $id)); | ||
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. Could we put that first as a simple 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. I don't think so, it'll still need the abstract check and won't save any indent, right? |
||
} | ||
|
||
return new Reference($messengerMiddlewareId); | ||
}, $middleware)); | ||
$middlewareReferences[] = new Reference($messengerMiddlewareId); | ||
} | ||
|
||
$container->getDefinition($busId)->replaceArgument(0, $middlewareReferences); | ||
} | ||
} |
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.
Why not just using
setArgument
here?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.
Due to the way child definitions arguments work, using
replaceArgument
saves an extra hard codedindex_
prefix for the key here.symfony/src/Symfony/Component/DependencyInjection/ChildDefinition.php
Lines 98 to 99 in d264fce