1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Eps\Req2CmdBundle\DependencyInjection; |
5
|
|
|
|
6
|
|
|
use Symfony\Component\Config\FileLocator; |
7
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
8
|
|
|
use Symfony\Component\DependencyInjection\Extension\Extension; |
9
|
|
|
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; |
10
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
11
|
|
|
|
12
|
|
|
final class Req2CmdExtension extends Extension |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* {@inheritdoc} |
16
|
|
|
* @throws \Exception |
17
|
|
|
*/ |
18
|
|
|
public function load(array $configs, ContainerBuilder $container): void |
19
|
|
|
{ |
20
|
|
|
$configuration = new Req2CmdConfiguration(); |
21
|
|
|
$config = $this->processConfiguration($configuration, $configs); |
22
|
|
|
|
23
|
|
|
$loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config')); |
24
|
|
|
|
25
|
|
|
$loader->load('actions.xml'); |
26
|
|
|
$loader->load('extractors.xml'); |
27
|
|
|
$loader->load('listeners.xml'); |
28
|
|
|
$loader->load('param_mappers.xml'); |
29
|
|
|
$loader->load('command_bus.xml'); |
30
|
|
|
|
31
|
|
|
$this->configureExtractors($config, $container); |
32
|
|
|
$this->configureCommandBus($config, $container); |
33
|
|
|
$this->configureEventListeners($config, $container); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function getAlias(): string |
37
|
|
|
{ |
38
|
|
|
return 'req2cmd'; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
private function configureExtractors(array $config, ContainerBuilder $container): void |
42
|
|
|
{ |
43
|
|
|
$extractorId = (string)$config['extractor']['service_id']; |
44
|
|
|
$container->setAlias('eps.req2cmd.extractor', $extractorId); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
private function configureCommandBus(array $config, ContainerBuilder $container): void |
48
|
|
|
{ |
49
|
|
|
$commandBusId = (string)$config['command_bus']['service_id']; |
50
|
|
|
if ($commandBusId === 'eps.req2cmd.command_bus.tactician') { |
51
|
|
|
$busName = (string)$config['command_bus']['name']; |
52
|
|
|
$tacticianServiceName = 'tactician.commandbus.' . $busName; |
53
|
|
|
$busDefinition = $container->findDefinition('eps.req2cmd.command_bus.tactician'); |
54
|
|
|
$busDefinition->replaceArgument(0, new Reference($tacticianServiceName)); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$container->setAlias('eps.req2cmd.command_bus', $commandBusId); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
private function configureEventListeners(array $config, ContainerBuilder $container): void |
61
|
|
|
{ |
62
|
|
|
$listenersMap = [ |
63
|
|
|
'extractor' => 'eps.req2cmd.listener.extract_command' |
64
|
|
|
]; |
65
|
|
|
foreach ((array)$config['listeners'] as $listenerName => $listenerConfig) { |
66
|
|
|
$listenerId = $listenersMap[$listenerName]; |
67
|
|
|
if (!$listenerConfig['enabled']) { |
68
|
|
|
$container->removeDefinition($listenerId); |
69
|
|
|
continue; |
70
|
|
|
} |
71
|
|
|
$definition = $container->findDefinition($listenerId); |
72
|
|
|
$serviceTags = $definition->getTags(); |
73
|
|
|
foreach ($serviceTags as $tagName => $tags) { |
74
|
|
|
if ($tagName === 'kernel.event_listener') { |
75
|
|
|
foreach ($tags as $tagIdx => $tag) { |
76
|
|
|
$serviceTags[$tagName][$tagIdx]['priority'] = $listenerConfig['priority']; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
$definition->setTags($serviceTags); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|