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' => '%kernel.root_dir%/../../src/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
|
|
|
|
54
|
|
|
public function load(array $configs, ContainerBuilder $container): array |
55
|
|
|
{ |
56
|
|
|
$processor = new Processor(); |
57
|
|
|
$configuration = new Configuration(); |
58
|
|
|
|
59
|
|
|
$config = $processor->processConfiguration($configuration, $configs); |
60
|
|
|
|
61
|
|
|
$loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
62
|
|
|
$loader->load('services.xml'); |
63
|
|
|
|
64
|
|
|
if ($config['db_driver']) { |
65
|
|
|
$loader->load(sprintf('%s.xml', $config['db_driver'])); |
66
|
|
|
$container->setParameter($this->getAlias().'.backend_type_'.$config['db_driver'], true); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
foreach (['messages'] as $basename) { |
70
|
|
|
$loader->load(sprintf('%s.xml', $basename)); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$this->remapParameters( |
74
|
|
|
$config, |
75
|
|
|
$container, |
76
|
|
|
[ |
77
|
|
|
'persist_commands' => 'hmlb_ddd.persist_commands', |
78
|
|
|
'persist_events' => 'hmlb_ddd.persist_events', |
79
|
|
|
] |
80
|
|
|
); |
81
|
|
|
|
82
|
|
|
$this->remapParametersNamespaces( |
83
|
|
|
$config, |
84
|
|
|
$container, |
85
|
|
|
[ |
86
|
|
|
'' => [ |
87
|
|
|
'db_driver' => 'hmlb_ddd.db_driver', |
88
|
|
|
'persistence_manager_name' => 'hmlb_ddd.persistence_manager_name', |
89
|
|
|
], |
90
|
|
|
] |
91
|
|
|
); |
92
|
|
|
|
93
|
|
|
return $config; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
private function remapParameters(array $config, ContainerBuilder $container, array $map) |
97
|
|
|
{ |
98
|
|
|
foreach ($map as $name => $paramName) { |
99
|
|
|
if (array_key_exists($name, $config)) { |
100
|
|
|
$container->setParameter($paramName, $config[$name]); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
private function remapParametersNamespaces(array $config, ContainerBuilder $container, array $namespaces) |
106
|
|
|
{ |
107
|
|
|
foreach ($namespaces as $ns => $map) { |
108
|
|
|
if ($ns) { |
109
|
|
|
if (!array_key_exists($ns, $config)) { |
110
|
|
|
continue; |
111
|
|
|
} |
112
|
|
|
$namespaceConfig = $config[$ns]; |
113
|
|
|
} else { |
114
|
|
|
$namespaceConfig = $config; |
115
|
|
|
} |
116
|
|
|
if (is_array($map)) { |
117
|
|
|
$this->remapParameters($namespaceConfig, $container, $map); |
118
|
|
|
} else { |
119
|
|
|
foreach ($namespaceConfig as $name => $value) { |
120
|
|
|
$container->setParameter(sprintf($map, $name), $value); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function getAlias(): string |
127
|
|
|
{ |
128
|
|
|
return 'hmlb_ddd'; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.