|
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
|
21 |
|
protected function loadInternal(array $mergedConfig, ContainerBuilder $container) |
|
20
|
|
|
{ |
|
21
|
21 |
|
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config/services')); |
|
22
|
21 |
|
$loader->load('services.yml'); |
|
23
|
|
|
|
|
24
|
21 |
|
$this->configureCommandBuses($mergedConfig, $container); |
|
25
|
21 |
|
$this->configureSecurity($mergedConfig, $container); |
|
26
|
21 |
|
} |
|
27
|
|
|
|
|
28
|
21 |
|
public function getAlias() |
|
29
|
|
|
{ |
|
30
|
21 |
|
return 'tactician'; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param array $mergedConfig |
|
35
|
|
|
* @param ContainerBuilder $container |
|
36
|
|
|
*/ |
|
37
|
21 |
|
private function configureCommandBuses(array $mergedConfig, ContainerBuilder $container) |
|
38
|
|
|
{ |
|
39
|
21 |
|
foreach ($mergedConfig['commandbus'] as $commandBusName => $commandBusConfig) { |
|
40
|
21 |
|
$middlewares = array_map( |
|
41
|
21 |
|
function ($middlewareServiceId) { |
|
42
|
21 |
|
return new Reference($middlewareServiceId); |
|
43
|
21 |
|
}, |
|
44
|
21 |
|
$commandBusConfig['middleware'] |
|
45
|
21 |
|
); |
|
46
|
|
|
|
|
47
|
21 |
|
$serviceName = 'tactician.commandbus.' . $commandBusName; |
|
48
|
21 |
|
$definition = new Definition($container->getParameter('tactician.commandbus.class'), [$middlewares]); |
|
49
|
21 |
|
$container->setDefinition($serviceName, $definition); |
|
50
|
|
|
|
|
51
|
21 |
|
if ($commandBusName === $mergedConfig['default_bus']) { |
|
52
|
21 |
|
$container->setAlias('tactician.commandbus', $serviceName); |
|
53
|
21 |
|
} |
|
54
|
21 |
|
$container->setParameter( |
|
55
|
21 |
|
sprintf('tactician.method_inflector.%s', $commandBusName), |
|
56
|
21 |
|
array_key_exists('method_inflector', $commandBusConfig) |
|
57
|
21 |
|
? $commandBusConfig['method_inflector'] |
|
58
|
21 |
|
: $mergedConfig['method_inflector'] |
|
59
|
21 |
|
); |
|
60
|
21 |
|
} |
|
61
|
21 |
|
$container->setParameter('tactician.commandbus.default', $mergedConfig['default_bus']); |
|
62
|
21 |
|
$container->setParameter('tactician.commandbus.ids', array_keys($mergedConfig['commandbus'])); |
|
63
|
21 |
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Configure the security voter if the security middleware is loaded. |
|
67
|
|
|
* |
|
68
|
|
|
* @param array $mergedConfig |
|
69
|
|
|
* @param ContainerBuilder $container |
|
70
|
|
|
*/ |
|
71
|
21 |
|
private function configureSecurity(array $mergedConfig, ContainerBuilder $container) |
|
72
|
|
|
{ |
|
73
|
21 |
|
foreach ($mergedConfig['commandbus'] as $commandBusConfig) { |
|
74
|
21 |
|
if (in_array('tactician.middleware.security', $commandBusConfig['middleware'])) { |
|
75
|
6 |
|
return $this->configureCommandSecurityVoter($mergedConfig, $container); |
|
76
|
|
|
} |
|
77
|
15 |
|
} |
|
78
|
15 |
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Configure the security voter. |
|
82
|
|
|
* |
|
83
|
|
|
* @param array $mergedConfig |
|
84
|
|
|
* @param ContainerBuilder $container |
|
85
|
|
|
*/ |
|
86
|
6 |
|
private function configureCommandSecurityVoter(array $mergedConfig, ContainerBuilder $container) |
|
87
|
|
|
{ |
|
88
|
6 |
|
if (!$container->has('tactician.middleware.security_voter')) { |
|
89
|
6 |
|
$definition = new Definition( |
|
90
|
6 |
|
HandleCommandVoter::class, |
|
91
|
|
|
[ |
|
92
|
6 |
|
new Reference('security.access.decision_manager'), |
|
93
|
6 |
|
$mergedConfig['security'] |
|
94
|
6 |
|
] |
|
95
|
6 |
|
); |
|
96
|
6 |
|
$definition->addTag('security.voter'); |
|
97
|
6 |
|
$container->setDefinition('tactician.middleware.security_voter', $definition); |
|
98
|
6 |
|
} |
|
99
|
6 |
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|