1
|
|
|
<?php |
2
|
|
|
namespace League\Tactician\Bundle\DependencyInjection\Compiler; |
3
|
|
|
|
4
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
5
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
6
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
7
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* This compiler pass maps Handler DI tags to specific commands |
11
|
|
|
*/ |
12
|
|
|
class CommandHandlerPass implements CompilerPassInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* You can modify the container here before it is dumped to PHP code. |
16
|
|
|
* |
17
|
|
|
* @param ContainerBuilder $container |
18
|
|
|
* |
19
|
|
|
* @throws \Exception |
20
|
|
|
* @api |
21
|
|
|
*/ |
22
|
15 |
|
public function process(ContainerBuilder $container) |
23
|
|
|
{ |
24
|
15 |
|
if (!$container->has('tactician.handler.locator.symfony')) { |
25
|
3 |
|
throw new \Exception('Missing tactician.handler.locator.symfony definition'); |
26
|
|
|
} |
27
|
|
|
|
28
|
12 |
|
$handlerLocator = $container->findDefinition('tactician.handler.locator.symfony'); |
29
|
|
|
|
30
|
12 |
|
$defaultMapping = []; |
31
|
12 |
|
$busIdToHandlerMapping = []; |
32
|
|
|
|
33
|
12 |
|
foreach ($container->findTaggedServiceIds('tactician.handler') as $id => $tags) { |
34
|
|
|
|
35
|
12 |
|
foreach ($tags as $attributes) { |
36
|
12 |
|
if (!isset($attributes['command'])) { |
37
|
3 |
|
throw new \Exception('The tactician.handler tag must always have a command attribute'); |
38
|
|
|
} |
39
|
|
|
|
40
|
9 |
|
if (isset($attributes['bus'])) { |
41
|
6 |
|
$this->abortIfInvalidBusId($attributes['bus'], $container); |
42
|
3 |
|
$busIdToHandlerMapping[$attributes['bus']][$attributes['command']] = $id; |
43
|
3 |
|
} else { |
44
|
3 |
|
$defaultMapping[$attributes['command']] = $id; |
45
|
|
|
} |
46
|
6 |
|
} |
47
|
6 |
|
} |
48
|
|
|
|
49
|
6 |
|
foreach ($busIdToHandlerMapping as $busId => $handlerMapping) { |
50
|
3 |
|
$container->setDefinition( |
51
|
3 |
|
'tactician.handler.locator.symfony.'.$busId, |
52
|
3 |
|
$this->buildLocatorDefinition($handlerMapping) |
53
|
3 |
|
); |
54
|
6 |
|
} |
55
|
|
|
|
56
|
6 |
|
$handlerLocator->addArgument($defaultMapping); |
57
|
6 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param string $id |
61
|
|
|
* @param ContainerBuilder $container |
62
|
|
|
* @throws Exception |
63
|
|
|
*/ |
64
|
6 |
|
protected function abortIfInvalidBusId($id, ContainerBuilder $container) |
65
|
|
|
{ |
66
|
6 |
|
$config = $container->getExtensionConfig('tactician'); |
67
|
|
|
|
68
|
6 |
|
if (!array_key_exists($id, $config['commandbus'])) { |
69
|
3 |
|
throw new \Exception('Invalid bus id "'.$id.'". Valid buses are: '.implode(', ', array_keys($config['commandbus']))); |
70
|
|
|
} |
71
|
3 |
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param array $handlerMapping |
75
|
|
|
* @return Definition |
76
|
|
|
*/ |
77
|
3 |
|
protected function buildLocatorDefinition(array $handlerMapping) |
78
|
|
|
{ |
79
|
3 |
|
return new Definition( |
80
|
3 |
|
'League\Tactician\Bundle\Handler\ContainerBasedHandlerLocator', |
81
|
|
|
[ |
82
|
3 |
|
new Reference('service_container'), |
83
|
3 |
|
$handlerMapping, |
84
|
|
|
] |
85
|
3 |
|
); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
} |
89
|
|
|
|