1 | <?php |
||
14 | class CommandHandlerPass implements CompilerPassInterface |
||
15 | { |
||
16 | /** |
||
17 | * You can modify the container here before it is dumped to PHP code. |
||
18 | * |
||
19 | * @param ContainerBuilder $container |
||
20 | * |
||
21 | * @throws \Exception |
||
22 | * @api |
||
23 | */ |
||
24 | 33 | public function process(ContainerBuilder $container) |
|
25 | { |
||
26 | 33 | $defaultBusId = $container->getParameter('tactician.commandbus.default'); |
|
27 | 33 | $busIds = $container->getParameter('tactician.commandbus.ids'); |
|
28 | 33 | $busIdToHandlerMapping = []; |
|
29 | |||
30 | 33 | foreach ($container->findTaggedServiceIds('tactician.handler') as $id => $tags) { |
|
31 | |||
32 | 30 | foreach ($tags as $attributes) { |
|
33 | 30 | if (!isset($attributes['command'])) { |
|
34 | 3 | throw new \Exception('The tactician.handler tag must always have a command attribute'); |
|
35 | } |
||
36 | |||
37 | 27 | if (array_key_exists('bus', $attributes)) { |
|
38 | 18 | $this->abortIfInvalidBusId($attributes['bus'], $busIds); |
|
39 | 8 | } |
|
40 | |||
41 | 21 | $busIdsDefined = array_key_exists('bus', $attributes) ? [$attributes['bus']] : $busIds; |
|
42 | 21 | foreach ($busIdsDefined as $busId) { |
|
43 | 21 | $busIdToHandlerMapping[$busId][$attributes['command']] = $id; |
|
44 | 14 | } |
|
45 | 14 | } |
|
46 | 16 | } |
|
47 | |||
48 | 24 | $container->setAlias( |
|
49 | 24 | 'tactician.handler.locator.symfony', |
|
50 | 24 | 'tactician.commandbus.'.$defaultBusId.'.handler.locator' |
|
51 | 16 | ); |
|
52 | |||
53 | 24 | $container->setAlias( |
|
54 | 24 | 'tactician.middleware.command_handler', |
|
55 | 24 | 'tactician.commandbus.'.$defaultBusId.'.middleware.command_handler' |
|
56 | 16 | ); |
|
57 | |||
58 | 24 | foreach ($busIds as $busId) { |
|
59 | 24 | $locatorServiceId = 'tactician.commandbus.'.$busId.'.handler.locator'; |
|
60 | 24 | $methodInflectorId = $container->getParameter(sprintf('tactician.method_inflector.%s', $busId)); |
|
61 | 24 | $container->setDefinition( |
|
62 | 16 | $locatorServiceId, |
|
63 | 24 | $this->buildLocatorDefinition( |
|
64 | // Build an empty locator if no command defined. To be sure extension still working |
||
65 | 24 | array_key_exists($busId, $busIdToHandlerMapping) ? $busIdToHandlerMapping[$busId] : [] |
|
66 | 16 | ) |
|
67 | 16 | ); |
|
68 | |||
69 | 24 | $container->setDefinition( |
|
70 | 24 | 'tactician.commandbus.'.$busId.'.middleware.command_handler', |
|
71 | 24 | new Definition( |
|
72 | 24 | CommandHandlerMiddleware::class, |
|
73 | [ |
||
74 | 24 | new Reference('tactician.handler.command_name_extractor.class_name'), |
|
75 | 24 | new Reference($locatorServiceId), |
|
76 | 24 | new Reference($methodInflectorId) |
|
77 | 16 | ] |
|
78 | 16 | ) |
|
79 | 16 | ); |
|
80 | 24 | $this->guardInvalidMiddlewares($container, $busId); |
|
81 | 14 | } |
|
82 | 21 | } |
|
83 | |||
84 | /** |
||
85 | * @param string $id |
||
86 | * @param array $busIds |
||
87 | * @throws Exception |
||
88 | */ |
||
89 | 18 | protected function abortIfInvalidBusId($id, array $busIds) |
|
95 | |||
96 | /** |
||
97 | * @param array $handlerMapping |
||
98 | * @return Definition |
||
99 | */ |
||
100 | 24 | protected function buildLocatorDefinition(array $handlerMapping) |
|
110 | |||
111 | 24 | private function guardInvalidMiddlewares(ContainerBuilder $container, $busId) |
|
120 | } |
||
121 |