|
7 | 7 | use Doctrine\Bundle\DoctrineBundle\DoctrineBundle;
|
8 | 8 | use Doctrine\DBAL\Types\Type as DoctrineType;
|
9 | 9 | use Doctrine\ORM\Version as DoctrineOrmVersion;
|
| 10 | +use MsgPhp\Domain\Command\EventMessageCommandHandler; |
| 11 | +use MsgPhp\Domain\Infra\{Console as ConsoleInfra, SimpleBus as SimpleBusInfra}; |
10 | 12 | use Ramsey\Uuid\Doctrine as DoctrineUuid;
|
11 | 13 | use SimpleBus\SymfonyBridge\SimpleBusCommandBusBundle;
|
12 |
| -use MsgPhp\Domain\Infra\Console as ConsoleInfra; |
| 14 | +use SimpleBus\SymfonyBridge\SimpleBusEventBusBundle; |
13 | 15 | use Symfony\Component\DependencyInjection\ChildDefinition;
|
14 | 16 | use Symfony\Component\DependencyInjection\Container;
|
15 | 17 | use Symfony\Component\DependencyInjection\ContainerBuilder;
|
16 | 18 | use Symfony\Component\DependencyInjection\Definition;
|
17 | 19 | use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
|
| 20 | +use Symfony\Component\DependencyInjection\Reference; |
| 21 | +use Symfony\Component\Messenger\MessageBusInterface; |
18 | 22 |
|
19 | 23 | /**
|
20 | 24 | * @author Roland Franssen <franssen.roland@gmail.com>
|
@@ -230,31 +234,64 @@ public static function configureDoctrineOrmRepositories(ContainerBuilder $contai
|
230 | 234 |
|
231 | 235 | public static function configureCommandMessages(ContainerBuilder $container, array $classMapping, array $commands): void
|
232 | 236 | {
|
233 |
| - $configure = function (string $tag) use ($container, $classMapping, $commands) { |
| 237 | + $configure = function (string $tag, string $attrName) use ($container, $classMapping, $commands) { |
234 | 238 | foreach ($container->findTaggedServiceIds($tag) as $id => $attr) {
|
235 | 239 | foreach ($attr as $attr) {
|
236 |
| - if (!isset($attr[$attrName = 'handles'])) { |
| 240 | + if (!isset($attr[$attrName])) { |
237 | 241 | continue;
|
238 | 242 | }
|
239 | 243 |
|
240 |
| - if ($commands[$command = $attr[$attrName]] ?? false) { |
241 |
| - if (isset($classMapping[$command])) { |
242 |
| - $container->getDefinition($id) |
243 |
| - ->addTag($tag, [$attrName => $classMapping[$command]]); |
244 |
| - } |
| 244 | + $enabled = $commands[$command = $attr[$attrName]] ?? false; |
245 | 245 |
|
| 246 | + if (!$enabled) { |
| 247 | + $container->removeDefinition($id); |
246 | 248 | continue;
|
247 | 249 | }
|
248 | 250 |
|
249 |
| - $container->removeDefinition($id); |
| 251 | + if (isset($classMapping[$command])) { |
| 252 | + $container->getDefinition($id) |
| 253 | + ->addTag($tag, [$attrName => $classMapping[$command], 'priority' => $attr['priority'] ?? 0]); |
| 254 | + } |
250 | 255 | }
|
251
EDBE
| 256 | }
|
252 | 257 | };
|
253 | 258 |
|
254 |
| - $configure('messenger.message_handler'); |
| 259 | + if (interface_exists(MessageBusInterface::class)) { |
| 260 | + $configure('messenger.message_handler', 'handles'); |
| 261 | + } |
255 | 262 |
|
256 | 263 | if (self::hasBundle($container, SimpleBusCommandBusBundle::class)) {
|
257 |
| - $configure('command_handler'); |
| 264 | + $configure('command_handler', 'handles'); |
| 265 | + } |
| 266 | + } |
| 267 | + |
| 268 | + public static function configureEventMessages(ContainerBuilder $container, array $classMapping, array $events): void |
| 269 | + { |
| 270 | + $configure = function (Definition $handler, string $tag, string $attrName) use ($classMapping, $events) { |
| 271 | + foreach ($events as $event) { |
| 272 | + $handler->addTag($tag, [$attrName => $event, 'priority' => -100]); |
| 273 | + |
| 274 | + if (isset($classMapping[$event])) { |
| 275 | + $handler->addTag($tag, [$attrName => $classMapping[$event], 'priority' => -100]); |
| 276 | + } |
| 277 | + } |
| 278 | + }; |
| 279 | + |
| 280 | + if (interface_exists(MessageBusInterface::class)) { |
| 281 | + $handler = self::registerAnonymous($container, EventMessageCommandHandler::class); |
| 282 | + |
| 283 | + $configure($handler, 'messenger.message_handler', 'handles'); |
| 284 | + } |
| 285 | + |
| 286 | + if (self::hasBundle($container, SimpleBusCommandBusBundle::class)) { |
| 287 | + $handler = self::registerAnonymous($container, EventMessageCommandHandler::class); |
| 288 | + $handler->setPublic(true); |
| 289 | + if (self::hasBundle($container, SimpleBusEventBusBundle::class)) { |
| 290 | + $handler->setArgument('$eventBus', self::registerAnonymous($container, SimpleBusInfra\DomainMessageBus::class) |
| 291 | + ->setArgument('$bus', new Reference('simple_bus.event_bus'))); |
| 292 | + } |
| 293 | + |
| 294 | + $configure($handler, 'command_handler', 'handles'); |
258 | 295 | }
|
259 | 296 | }
|
260 | 297 |
|
|
0 commit comments