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 |
|
$defaultMapping = []; |
|
|
|
|
25
|
15 |
|
$defaultBusId = $this->getDefaultBusId($container); |
26
|
15 |
|
$busIdToHandlerMapping = []; |
27
|
|
|
|
28
|
15 |
|
foreach ($container->findTaggedServiceIds('tactician.handler') as $id => $tags) { |
29
|
|
|
|
30
|
15 |
|
foreach ($tags as $attributes) { |
31
|
15 |
|
if (!isset($attributes['command'])) { |
32
|
3 |
|
throw new \Exception('The tactician.handler tag must always have a command attribute'); |
33
|
|
|
} |
34
|
|
|
|
35
|
12 |
|
if (array_key_exists('bus', $attributes)) { |
36
|
9 |
|
$this->abortIfInvalidBusId($attributes['bus'], $container); |
37
|
6 |
|
} |
38
|
|
|
|
39
|
9 |
|
$busId = array_key_exists('bus', $attributes) ? $attributes['bus'] : $defaultBusId; |
40
|
|
|
|
41
|
9 |
|
$busIdToHandlerMapping[$busId][$attributes['command']] = $id; |
42
|
9 |
|
} |
43
|
9 |
|
} |
44
|
|
|
|
45
|
9 |
|
foreach ($busIdToHandlerMapping as $busId => $handlerMapping) { |
46
|
9 |
|
$locatorServiceId = 'tactician.commandbus.'.$busId.'.handler.locator'; |
47
|
9 |
|
$container->setDefinition( |
48
|
9 |
|
$locatorServiceId, |
49
|
9 |
|
$this->buildLocatorDefinition($handlerMapping) |
50
|
9 |
|
); |
51
|
|
|
|
52
|
9 |
|
$container->setDefinition( |
53
|
9 |
|
'tactician.commandbus.'.$busId.'.middleware.command_handler', |
54
|
9 |
|
$this->buildCommandHandlerDefinition($locatorServiceId, $container) |
55
|
9 |
|
); |
56
|
9 |
|
} |
57
|
|
|
|
58
|
9 |
|
$container->setAlias( |
59
|
9 |
|
'tactician.handler.locator.symfony', |
60
|
9 |
|
'tactician.commandbus.'.$defaultBusId.'.handler.locator' |
61
|
9 |
|
); |
62
|
|
|
|
63
|
9 |
|
$container->setAlias( |
64
|
9 |
|
'tactician.middleware.command_handler', |
65
|
9 |
|
'tactician.commandbus.'.$defaultBusId.'.middleware.command_handler' |
66
|
9 |
|
); |
67
|
9 |
|
} |
68
|
|
|
|
69
|
15 |
|
protected function getDefaultBusId(ContainerBuilder $container) |
70
|
|
|
{ |
71
|
15 |
|
$config = $container->getExtensionConfig('tactician'); |
72
|
|
|
|
73
|
15 |
|
return $config['default_bus']; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param string $id |
78
|
|
|
* @param ContainerBuilder $container |
79
|
|
|
* @throws Exception |
80
|
|
|
*/ |
81
|
9 |
|
protected function abortIfInvalidBusId($id, ContainerBuilder $container) |
82
|
|
|
{ |
83
|
9 |
|
$config = $container->getExtensionConfig('tactician'); |
84
|
|
|
|
85
|
9 |
|
if (!array_key_exists($id, $config['commandbus'])) { |
86
|
3 |
|
throw new \Exception('Invalid bus id "'.$id.'". Valid buses are: '.implode(', ', array_keys($config['commandbus']))); |
87
|
|
|
} |
88
|
6 |
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param array $handlerMapping |
92
|
|
|
* @return Definition |
93
|
|
|
*/ |
94
|
9 |
|
protected function buildLocatorDefinition(array $handlerMapping) |
95
|
|
|
{ |
96
|
9 |
|
return new Definition( |
97
|
9 |
|
'League\Tactician\Bundle\Handler\ContainerBasedHandlerLocator', |
98
|
|
|
[ |
99
|
9 |
|
new Reference('service_container'), |
100
|
9 |
|
$handlerMapping, |
101
|
|
|
] |
102
|
9 |
|
); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param string $locatorServiceId |
107
|
|
|
* @return Definition |
108
|
|
|
*/ |
109
|
9 |
|
protected function buildCommandHandlerDefinition($locatorServiceId, ContainerBuilder $container) |
110
|
|
|
{ |
111
|
9 |
|
$config = $container->getExtensionConfig('tactician'); |
112
|
|
|
|
113
|
9 |
|
return new Definition( |
114
|
9 |
|
'League\Tactician\Handler\CommandHandlerMiddleware', |
115
|
|
|
[ |
116
|
9 |
|
new Reference('tactician.handler.command_name_extractor.class_name'), |
117
|
9 |
|
new Reference($locatorServiceId), |
118
|
9 |
|
new Reference($config['method_inflector']) |
119
|
9 |
|
] |
120
|
9 |
|
); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
} |
124
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.