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
|
|
|
* @param array $mergedConfig |
22
|
|
|
* @param ContainerBuilder $container |
23
|
|
|
*/ |
24
|
72 |
|
protected function loadInternal(array $mergedConfig, ContainerBuilder $container) |
25
|
|
|
{ |
26
|
72 |
|
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config/services')); |
27
|
72 |
|
$loader->load('services.yml'); |
28
|
72 |
|
$container->setParameter('tactician.merged_config', $mergedConfig); |
29
|
72 |
|
$this->configureSecurity($mergedConfig, $container); |
30
|
72 |
|
$this->configureLogger($mergedConfig, $container); |
31
|
72 |
|
} |
32
|
|
|
|
33
|
75 |
|
public function getAlias() |
34
|
|
|
{ |
35
|
75 |
|
return 'tactician'; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Configure the security voter if the security middleware is loaded. |
40
|
|
|
* |
41
|
|
|
* @param array $mergedConfig |
42
|
|
|
* @param ContainerBuilder $container |
43
|
|
|
*/ |
44
|
72 |
|
private function configureSecurity(array $mergedConfig, ContainerBuilder $container) |
45
|
|
|
{ |
46
|
72 |
|
foreach ($mergedConfig['commandbus'] as $commandBusConfig) { |
47
|
72 |
|
if (in_array('tactician.middleware.security', $commandBusConfig['middleware'])) { |
48
|
56 |
|
return $this->configureCommandSecurityVoter($mergedConfig, $container); |
49
|
|
|
} |
50
|
|
|
} |
51
|
48 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Configure the security voter. |
55
|
|
|
* |
56
|
|
|
* @param array $mergedConfig |
57
|
|
|
* @param ContainerBuilder $container |
58
|
|
|
*/ |
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
|
24 |
|
HandleCommandVoter::class, |
64
|
|
|
[ |
65
|
24 |
|
new Reference('security.access.decision_manager'), |
66
|
24 |
|
$mergedConfig['security'] |
67
|
|
|
] |
68
|
|
|
); |
69
|
24 |
|
$definition->addTag('security.voter'); |
70
|
24 |
|
$container->setDefinition('tactician.middleware.security_voter', $definition); |
71
|
|
|
} |
72
|
24 |
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Configure the logger middleware. |
76
|
|
|
* |
77
|
|
|
* @param array $mergedConfig |
78
|
|
|
* @param ContainerBuilder $container |
79
|
|
|
*/ |
80
|
72 |
|
private function configureLogger(array $mergedConfig, ContainerBuilder $container) |
81
|
|
|
{ |
82
|
72 |
|
$this->configureLoggerFormatters($container); |
83
|
|
|
|
84
|
72 |
|
$loggerMiddleware = new Definition(LoggerMiddleware::class, [ |
85
|
72 |
|
new Reference($mergedConfig['logger_formatter']), |
86
|
72 |
|
new Reference('logger') |
87
|
|
|
]); |
88
|
72 |
|
$loggerMiddleware->setPublic(false); |
89
|
72 |
|
$loggerMiddleware->addTag('monolog.logger', ['channel' => 'command_bus']); |
90
|
|
|
|
91
|
72 |
|
$container->setDefinition('tactician.middleware.logger', $loggerMiddleware); |
92
|
72 |
|
} |
93
|
|
|
|
94
|
|
|
|
95
|
72 |
|
private function configureLoggerFormatters(ContainerBuilder $container) |
96
|
|
|
{ |
97
|
72 |
|
$container->setDefinition( |
98
|
72 |
|
'tactician.logger.class_properties_formatter', |
99
|
72 |
|
new Definition(ClassPropertiesFormatter::class) |
100
|
72 |
|
)->setPublic(false); |
101
|
|
|
|
102
|
72 |
|
$container->setDefinition( |
103
|
72 |
|
'tactician.logger.class_name_formatter', |
104
|
72 |
|
new Definition(ClassNameFormatter::class) |
105
|
72 |
|
)->setPublic(false); |
106
|
72 |
|
} |
107
|
|
|
} |
108
|
|
|
|