1
|
|
|
<?php namespace League\Tactician\Bundle\DependencyInjection; |
2
|
|
|
|
3
|
|
|
use League\Tactician\Bundle\Security\Voter\HandleCommandVoter; |
4
|
|
|
use Symfony\Component\Config\FileLocator; |
5
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
6
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
7
|
|
|
use Symfony\Component\DependencyInjection\Loader; |
8
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
9
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\ConfigurableExtension; |
10
|
|
|
|
11
|
|
|
class TacticianExtension extends ConfigurableExtension |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Configures the passed container according to the merged configuration. |
15
|
|
|
* |
16
|
|
|
* @param array $mergedConfig |
17
|
|
|
* @param ContainerBuilder $container |
18
|
|
|
*/ |
19
|
24 |
|
protected function loadInternal(array $mergedConfig, ContainerBuilder $container) |
20
|
|
|
{ |
21
|
24 |
|
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config/services')); |
22
|
24 |
|
$loader->load('services.yml'); |
23
|
|
|
|
24
|
24 |
|
$this->configureCommandBuses($mergedConfig, $container); |
25
|
24 |
|
$this->injectMethodNameInflector($mergedConfig, $container); |
26
|
24 |
|
$this->configureSecurity($mergedConfig, $container); |
27
|
24 |
|
} |
28
|
|
|
|
29
|
24 |
|
public function getAlias() |
30
|
|
|
{ |
31
|
24 |
|
return 'tactician'; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param array $mergedConfig |
36
|
|
|
* @param ContainerBuilder $container |
37
|
|
|
*/ |
38
|
24 |
|
private function configureCommandBuses(array $mergedConfig, ContainerBuilder $container) |
39
|
|
|
{ |
40
|
24 |
|
foreach ($mergedConfig['commandbus'] as $commandBusName => $commandBusConfig) { |
41
|
24 |
|
$middlewares = array_map( |
42
|
24 |
|
function ($middlewareServiceId) { |
43
|
24 |
|
return new Reference($middlewareServiceId); |
44
|
24 |
|
}, |
45
|
24 |
|
$commandBusConfig['middleware'] |
46
|
24 |
|
); |
47
|
|
|
|
48
|
24 |
|
$serviceName = 'tactician.commandbus.' . $commandBusName; |
49
|
24 |
|
$definition = new Definition($container->getParameter('tactician.commandbus.class'), [$middlewares]); |
50
|
24 |
|
$container->setDefinition($serviceName, $definition); |
51
|
|
|
|
52
|
24 |
|
if ($commandBusName === $mergedConfig['default_bus']) { |
53
|
24 |
|
$container->setAlias('tactician.commandbus', $serviceName); |
54
|
24 |
|
} |
55
|
24 |
|
} |
56
|
24 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Define the default Method Name Inflector. |
60
|
|
|
* This will fail silently if the command_handler service does not exist |
61
|
|
|
* |
62
|
|
|
* @param array $mergedConfig |
63
|
|
|
* @param ContainerBuilder $container |
64
|
|
|
*/ |
65
|
24 |
|
private function injectMethodNameInflector(array $mergedConfig, ContainerBuilder $container) |
66
|
|
|
{ |
67
|
24 |
|
if (! $container->has('tactician.middleware.command_handler')) { |
68
|
|
|
return; |
69
|
|
|
} |
70
|
|
|
|
71
|
24 |
|
$inflectorReference = new Reference($mergedConfig['method_inflector']); |
72
|
|
|
|
73
|
24 |
|
$handlerLocator = $container->findDefinition('tactician.middleware.command_handler'); |
74
|
24 |
|
$handlerLocator->replaceArgument(2, $inflectorReference); |
75
|
24 |
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Configure the security voter if the security middleware is loaded. |
79
|
|
|
* |
80
|
|
|
* @param array $mergedConfig |
81
|
|
|
* @param ContainerBuilder $container |
82
|
|
|
*/ |
83
|
24 |
|
private function configureSecurity(array $mergedConfig, ContainerBuilder $container) |
84
|
|
|
{ |
85
|
24 |
|
foreach ($mergedConfig['commandbus'] as $commandBusConfig) { |
86
|
24 |
|
if (in_array('tactician.middleware.security', $commandBusConfig['middleware'])) { |
87
|
6 |
|
return $this->configureCommandSecurityVoter($mergedConfig, $container); |
88
|
|
|
} |
89
|
18 |
|
} |
90
|
18 |
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Configure the security voter. |
94
|
|
|
* |
95
|
|
|
* @param array $mergedConfig |
96
|
|
|
* @param ContainerBuilder $container |
97
|
|
|
*/ |
98
|
6 |
|
private function configureCommandSecurityVoter(array $mergedConfig, ContainerBuilder $container) |
99
|
|
|
{ |
100
|
6 |
|
if (!$container->has('tactician.middleware.security_voter')) { |
101
|
6 |
|
$definition = new Definition( |
102
|
6 |
|
HandleCommandVoter::class, |
103
|
|
|
[ |
104
|
6 |
|
new Reference('security.access.decision_manager'), |
105
|
6 |
|
$mergedConfig['security'] |
106
|
6 |
|
] |
107
|
6 |
|
); |
108
|
6 |
|
$definition->addTag('security.voter'); |
109
|
6 |
|
$container->setDefinition('tactician.middleware.security_voter', $definition); |
110
|
6 |
|
} |
111
|
6 |
|
} |
112
|
|
|
} |
113
|
|
|
|