|
1
|
|
|
<?php |
|
2
|
|
|
namespace League\Tactician\Bundle\DependencyInjection\Compiler; |
|
3
|
|
|
|
|
4
|
|
|
use League\Tactician\Bundle\Handler\ContainerBasedHandlerLocator; |
|
5
|
|
|
use League\Tactician\Handler\CommandHandlerMiddleware; |
|
6
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
|
7
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
8
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
|
9
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* This compiler pass maps Handler DI tags to specific commands |
|
13
|
|
|
*/ |
|
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
|
18 |
|
public function process(ContainerBuilder $container) |
|
25
|
|
|
{ |
|
26
|
18 |
|
$defaultBusId = $container->getParameter('tactician.commandbus.default'); |
|
27
|
18 |
|
$busIds = $container->getParameter('tactician.commandbus.ids'); |
|
28
|
18 |
|
$busIdToHandlerMapping = []; |
|
29
|
18 |
|
|
|
30
|
|
|
foreach ($container->findTaggedServiceIds('tactician.handler') as $id => $tags) { |
|
31
|
18 |
|
|
|
32
|
|
|
foreach ($tags as $attributes) { |
|
33
|
18 |
|
if (!isset($attributes['command'])) { |
|
34
|
18 |
|
throw new \Exception('The tactician.handler tag must always have a command attribute'); |
|
35
|
3 |
|
} |
|
36
|
|
|
|
|
37
|
|
|
if (array_key_exists('bus', $attributes)) { |
|
38
|
15 |
|
$this->abortIfInvalidBusId($attributes['bus'], $busIds); |
|
39
|
12 |
|
} |
|
40
|
6 |
|
|
|
41
|
|
|
$busIdsDefined = array_key_exists('bus', $attributes) ? [$attributes['bus']] : $busIds; |
|
42
|
12 |
|
foreach ($busIdsDefined as $busId) { |
|
43
|
12 |
|
$busIdToHandlerMapping[$busId][$attributes['command']] = $id; |
|
44
|
12 |
|
} |
|
45
|
8 |
|
} |
|
46
|
8 |
|
} |
|
47
|
8 |
|
|
|
48
|
|
|
$container->setAlias( |
|
49
|
12 |
|
'tactician.handler.locator.symfony', |
|
50
|
12 |
|
'tactician.commandbus.'.$defaultBusId.'.handler.locator' |
|
51
|
12 |
|
); |
|
52
|
8 |
|
|
|
53
|
12 |
|
$container->setAlias( |
|
54
|
8 |
|
'tactician.middleware.command_handler', |
|
55
|
|
|
'tactician.commandbus.'.$defaultBusId.'.middleware.command_handler' |
|
56
|
12 |
|
); |
|
57
|
12 |
|
|
|
58
|
12 |
|
foreach ($busIds as $busId) { |
|
59
|
8 |
|
$locatorServiceId = 'tactician.commandbus.'.$busId.'.handler.locator'; |
|
60
|
8 |
|
$methodInflectorId = $container->getParameter(sprintf('tactician.method_inflector.%s', $busId)); |
|
61
|
|
|
$container->setDefinition( |
|
62
|
12 |
|
$locatorServiceId, |
|
63
|
12 |
|
$this->buildLocatorDefinition( |
|
64
|
12 |
|
// Build an empty locator if no command defined. To be sure extension still working |
|
65
|
8 |
|
array_key_exists($busId, $busIdToHandlerMapping) ? $busIdToHandlerMapping[$busId] : [] |
|
66
|
|
|
) |
|
67
|
12 |
|
); |
|
68
|
12 |
|
|
|
69
|
12 |
|
$container->setDefinition( |
|
70
|
8 |
|
'tactician.commandbus.'.$busId.'.middleware.command_handler', |
|
71
|
12 |
|
new Definition( |
|
72
|
|
|
CommandHandlerMiddleware::class, |
|
73
|
|
|
[ |
|
74
|
|
|
new Reference('tactician.handler.command_name_extractor.class_name'), |
|
75
|
|
|
new Reference($locatorServiceId), |
|
76
|
|
|
new Reference($methodInflectorId) |
|
77
|
|
|
] |
|
78
|
12 |
|
) |
|
79
|
|
|
); |
|
80
|
12 |
|
$this->guardInvalidMiddlewares($container, $busId); |
|
81
|
3 |
|
} |
|
82
|
|
|
} |
|
83
|
9 |
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param string $id |
|
86
|
|
|
* @param array $busIds |
|
87
|
|
|
* @throws Exception |
|
88
|
|
|
*/ |
|
89
|
12 |
|
protected function abortIfInvalidBusId($id, array $busIds) |
|
90
|
|
|
{ |
|
91
|
12 |
|
if (!in_array($id, $busIds)) { |
|
92
|
12 |
|
throw new \Exception('Invalid bus id "'.$id.'". Valid buses are: '.implode(', ', $busIds)); |
|
93
|
|
|
} |
|
94
|
12 |
|
} |
|
95
|
12 |
|
|
|
96
|
|
|
/** |
|
97
|
8 |
|
* @param array $handlerMapping |
|
98
|
|
|
* @return Definition |
|
99
|
|
|
*/ |
|
100
|
|
|
protected function buildLocatorDefinition(array $handlerMapping) |
|
101
|
|
|
{ |
|
102
|
|
|
return new Definition( |
|
103
|
|
|
ContainerBasedHandlerLocator::class, |
|
104
|
|
|
[ |
|
105
|
|
|
new Reference('service_container'), |
|
106
|
12 |
|
$handlerMapping, |
|
107
|
|
|
] |
|
108
|
12 |
|
); |
|
109
|
12 |
|
} |
|
110
|
|
|
|
|
111
|
12 |
|
private function guardInvalidMiddlewares(ContainerBuilder $container, $busId) |
|
112
|
12 |
|
{ |
|
113
|
12 |
|
$busDefinition = $container->getDefinition('tactician.commandbus.'.$busId); |
|
114
|
8 |
|
foreach ($busDefinition->getArgument(0) as $middlewareReference) { |
|
115
|
8 |
|
if (false === $container->has($middlewareReference)) { |
|
116
|
|
|
throw UnknownMiddleware::withId((string) $middlewareReference); |
|
117
|
|
|
} |
|
118
|
12 |
|
} |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|