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