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