1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare (strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace HMLB\DDDBundle\DependencyInjection; |
6
|
|
|
|
7
|
|
|
use Symfony\Component\Config\Definition\Processor; |
8
|
|
|
use Symfony\Component\Config\FileLocator; |
9
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
10
|
|
|
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface; |
11
|
|
|
use Symfony\Component\DependencyInjection\Loader; |
12
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* HMLBDDDExtension. |
16
|
|
|
* |
17
|
|
|
* @author Hugues Maignol <[email protected]> |
18
|
|
|
*/ |
19
|
|
|
class HMLBDDDExtension extends Extension implements PrependExtensionInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* We add mapping information for our Messages Classes. |
23
|
|
|
* |
24
|
|
|
* todo: It should be dynamic for non default entity_manager name |
25
|
|
|
*/ |
26
|
|
|
public function prepend(ContainerBuilder $container) |
27
|
|
|
{ |
28
|
|
|
$bundles = $container->getParameter('kernel.bundles'); |
29
|
|
|
|
30
|
|
|
if (isset($bundles['DoctrineBundle'])) { |
31
|
|
|
$mappingConfig = [ |
32
|
|
|
'orm' => [ |
33
|
|
|
'entity_managers' => [ |
34
|
|
|
'default' => [ |
35
|
|
|
'mappings' => [ |
36
|
|
|
'HMLBDDDBundle' => [ |
37
|
|
|
'mapping' => true, |
38
|
|
|
'type' => 'xml', |
39
|
|
|
'dir' => __DIR__.'/../Resources/config/doctrine', |
40
|
|
|
'prefix' => 'HMLB\DDD', |
41
|
|
|
'is_bundle' => false, |
42
|
|
|
], |
43
|
|
|
], |
44
|
|
|
], |
45
|
|
|
], |
46
|
|
|
], |
47
|
|
|
]; |
48
|
|
|
|
49
|
|
|
$container->getExtension('doctrine'); |
50
|
|
|
$container->prependExtensionConfig('doctrine', $mappingConfig); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$container->prependExtensionConfig( |
54
|
|
|
'command_bus', |
55
|
|
|
[ |
56
|
|
|
'command_name_resolver_strategy' => 'named_message', |
57
|
|
|
] |
58
|
|
|
); |
59
|
|
|
$container->prependExtensionConfig( |
60
|
|
|
'event_bus', |
61
|
|
|
[ |
62
|
|
|
'event_name_resolver_strategy' => 'named_message', |
63
|
|
|
] |
64
|
|
|
); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function load(array $configs, ContainerBuilder $container): array |
68
|
|
|
{ |
69
|
|
|
$processor = new Processor(); |
70
|
|
|
$configuration = new Configuration(); |
71
|
|
|
$config = $processor->processConfiguration($configuration, $configs); |
72
|
|
|
|
73
|
|
|
$loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
74
|
|
|
$loader->load('services.xml'); |
75
|
|
|
|
76
|
|
|
if ($config['db_driver']) { |
77
|
|
|
$loader->load(sprintf('%s.xml', $config['db_driver'])); |
78
|
|
|
$container->setParameter($this->getAlias().'.backend_type_'.$config['db_driver'], true); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
foreach (['messages'] as $basename) { |
82
|
|
|
$loader->load(sprintf('%s.xml', $basename)); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$container->setAlias('hmlb_ddd.repository.command', $config['persistence']['command_repository_service']); |
86
|
|
|
$container->setAlias('hmlb_ddd.repository.event', $config['persistence']['event_repository_service']); |
87
|
|
|
|
88
|
|
|
$this->remapParametersNamespaces( |
89
|
|
|
$config, |
90
|
|
|
$container, |
91
|
|
|
[ |
92
|
|
|
'' => [ |
93
|
|
|
'db_driver' => 'hmlb_ddd.db_driver', |
94
|
|
|
], |
95
|
|
|
'persistence' => [ |
96
|
|
|
'persist_commands' => 'hmlb_ddd.persistence.persist_commands', |
97
|
|
|
'persist_events' => 'hmlb_ddd.persistence.persist_events', |
98
|
|
|
'command_repository_service' => 'hmlb_ddd.persistence.command_repository_service', |
99
|
|
|
'event_repository_service' => 'hmlb_ddd.persistence.event_repository_service', |
100
|
|
|
], |
101
|
|
|
] |
102
|
|
|
); |
103
|
|
|
|
104
|
|
|
return $config; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
private function remapParameters(array $config, ContainerBuilder $container, array $map) |
108
|
|
|
{ |
109
|
|
|
foreach ($map as $name => $paramName) { |
110
|
|
|
if (array_key_exists($name, $config)) { |
111
|
|
|
$container->setParameter($paramName, $config[$name]); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
private function remapParametersNamespaces(array $config, ContainerBuilder $container, array $namespaces) |
117
|
|
|
{ |
118
|
|
|
foreach ($namespaces as $ns => $map) { |
119
|
|
|
if ($ns) { |
120
|
|
|
if (!array_key_exists($ns, $config)) { |
121
|
|
|
continue; |
122
|
|
|
} |
123
|
|
|
$namespaceConfig = $config[$ns]; |
124
|
|
|
} else { |
125
|
|
|
$namespaceConfig = $config; |
126
|
|
|
} |
127
|
|
|
if (is_array($map)) { |
128
|
|
|
$this->remapParameters($namespaceConfig, $container, $map); |
129
|
|
|
} else { |
130
|
|
|
foreach ($namespaceConfig as $name => $value) { |
131
|
|
|
$container->setParameter(sprintf($map, $name), $value); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function getAlias(): string |
138
|
|
|
{ |
139
|
|
|
return 'hmlb_ddd'; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|