1
|
|
|
<?php namespace League\Tactician\Bundle\DependencyInjection; |
2
|
|
|
|
3
|
|
|
use Symfony\Component\Config\FileLocator; |
4
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
5
|
|
|
use Symfony\Component\DependencyInjection\Loader; |
6
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
7
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
8
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\ConfigurableExtension; |
9
|
|
|
|
10
|
|
|
class TacticianExtension extends ConfigurableExtension |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Configures the passed container according to the merged configuration. |
14
|
|
|
* |
15
|
|
|
* @param array $mergedConfig |
16
|
|
|
* @param ContainerBuilder $container |
17
|
|
|
*/ |
18
|
15 |
|
protected function loadInternal(array $mergedConfig, ContainerBuilder $container) |
19
|
|
|
{ |
20
|
15 |
|
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config/services')); |
21
|
15 |
|
$loader->load('services.yml'); |
22
|
|
|
|
23
|
15 |
|
$this->configureCommandBuses($mergedConfig, $container); |
24
|
15 |
|
$this->injectMethodNameInflector($mergedConfig, $container); |
25
|
15 |
|
} |
26
|
|
|
|
27
|
15 |
|
public function getAlias() |
28
|
|
|
{ |
29
|
15 |
|
return 'tactician'; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param array $mergedConfig |
34
|
|
|
* @param ContainerBuilder $container |
35
|
|
|
*/ |
36
|
15 |
|
private function configureCommandBuses(array $mergedConfig, ContainerBuilder $container) |
37
|
|
|
{ |
38
|
15 |
|
foreach ($mergedConfig['commandbus'] as $commandBusName => $commandBusConfig) { |
39
|
15 |
|
$middlewares = array_map( |
40
|
15 |
|
function ($middlewareServiceId) { |
41
|
15 |
|
return new Reference($middlewareServiceId); |
42
|
15 |
|
}, |
43
|
15 |
|
$commandBusConfig['middleware'] |
44
|
15 |
|
); |
45
|
|
|
|
46
|
15 |
|
$serviceName = 'tactician.commandbus.' . $commandBusName; |
47
|
15 |
|
$definition = new Definition($container->getParameter('tactician.commandbus.class'), [$middlewares]); |
48
|
15 |
|
$container->setDefinition($serviceName, $definition); |
49
|
|
|
|
50
|
15 |
|
if ($commandBusName === $mergedConfig['default_bus']) { |
51
|
15 |
|
$container->setAlias('tactician.commandbus', $serviceName); |
52
|
15 |
|
} |
53
|
15 |
|
} |
54
|
15 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Define the default Method Name Inflector. |
58
|
|
|
* This will fail silently if the command_handler service does not exist |
59
|
|
|
* |
60
|
|
|
* @param array $mergedConfig |
61
|
|
|
* @param ContainerBuilder $container |
62
|
|
|
*/ |
63
|
15 |
|
private function injectMethodNameInflector(array $mergedConfig, ContainerBuilder $container) |
64
|
|
|
{ |
65
|
15 |
|
if (! $container->has('tactician.middleware.command_handler')) { |
66
|
|
|
return; |
67
|
|
|
} |
68
|
|
|
|
69
|
15 |
|
$inflectorReference = new Reference($mergedConfig['method_inflector']); |
70
|
|
|
|
71
|
15 |
|
$handlerLocator = $container->findDefinition('tactician.middleware.command_handler'); |
72
|
15 |
|
$handlerLocator->replaceArgument(2, $inflectorReference); |
73
|
15 |
|
} |
74
|
|
|
} |
75
|
|
|
|