1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the ModularBundle project. |
5
|
|
|
* |
6
|
|
|
* (c) Anthonius Munthi <https://itstoni.com> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Doyo\Bundle\Modular\DependencyInjection; |
15
|
|
|
|
16
|
|
|
use Doyo\Bundle\Modular\Application\ModuleInterface; |
17
|
|
|
use Doyo\Bundle\Modular\Modules; |
18
|
|
|
use Symfony\Component\Config\FileLocator; |
19
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
20
|
|
|
use Symfony\Component\DependencyInjection\Extension\Extension; |
21
|
|
|
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface; |
22
|
|
|
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; |
23
|
|
|
|
24
|
|
|
class DoyoModularExtension extends Extension implements PrependExtensionInterface |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @psalm-suppress MixedArgument |
28
|
|
|
* @psalm-suppress MixedAssignment |
29
|
|
|
*/ |
30
|
|
|
public function prepend(ContainerBuilder $container): void |
31
|
|
|
{ |
32
|
|
|
$configs = $container->getExtensionConfig($this->getAlias()); |
33
|
|
|
$resolvingBag = $container->getParameterBag(); |
34
|
|
|
$configs = $resolvingBag->resolveValue($configs); |
35
|
|
|
/** @var array<array-key,array<array-key,string|bool|scalar>> $configs */ |
36
|
|
|
$configs = $this->processConfiguration(new Configuration(), $configs); |
37
|
|
|
$modules = new Modules(); |
38
|
|
|
$modules->buildModules($container); |
39
|
|
|
$container->set('doyo.modules', $modules); |
40
|
|
|
foreach ($modules->getModules() as $module) { |
41
|
|
|
$this->configureModule($configs, $container, $module); |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @psalm-suppress MixedArgumentTypeCoercion |
47
|
|
|
*/ |
48
|
|
|
public function load(array $configs, ContainerBuilder $container): void |
49
|
|
|
{ |
50
|
|
|
$configuration = new Configuration(); |
51
|
|
|
$config = $this->processConfiguration($configuration, $configs); |
52
|
|
|
|
53
|
|
|
$this->configureParams($container, $config); |
54
|
|
|
$locator = new FileLocator(__DIR__.'/../Resources/services'); |
55
|
|
|
$loader = new XmlFileLoader($container, $locator); |
56
|
|
|
$loader->load('services.xml'); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @psalm-suppress MixedArgument |
61
|
|
|
* @psalm-suppress MixedArrayAccess |
62
|
|
|
* @psalm-suppress PossiblyInvalidArrayOffset |
63
|
|
|
* @psalm-suppress PossiblyInvalidArgument |
64
|
|
|
* @psalm-param array<array-key,string|array<array-key,string>> $config |
65
|
|
|
*/ |
66
|
|
|
private function configureParams(ContainerBuilder $container, array $config): void |
67
|
|
|
{ |
68
|
|
|
$container->setParameter('doyo.modular.use_annotation', $config['use_annotation']); |
69
|
|
|
$container->setParameter('doyo.modular.module_dir', $config['module_root_dir']); |
70
|
|
|
$container->setParameter('doyo.modular.paths.api_platform', $config['config_paths']['api_platform']); |
71
|
|
|
$container->setParameter('doyo.modular.paths.validation', $config['config_paths']['validation']); |
72
|
|
|
$container->setParameter('doyo.modular.paths.serialization', $config['config_paths']['serialization']); |
73
|
|
|
|
74
|
|
|
$this->configureDoctrineParams($container, $config['doctrine']); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param array<array-key,string> $config |
|
|
|
|
79
|
|
|
*/ |
80
|
|
|
private function configureDoctrineParams(ContainerBuilder $container, array $config): void |
81
|
|
|
{ |
82
|
|
|
$ns = 'doyo.modular.doctrine'; |
83
|
|
|
|
84
|
|
|
foreach ($config as $key => $val) { |
85
|
|
|
$container->setParameter($ns.'.'.$key, $val); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param array<array-key,array<array-key,string|bool|scalar>> $configs |
|
|
|
|
91
|
|
|
*/ |
92
|
|
|
private function configureModule(array $configs, ContainerBuilder $container, ModuleInterface $module): void |
93
|
|
|
{ |
94
|
|
|
$this->prependSerialization($configs, $container, $module); |
95
|
|
|
if ($container->hasExtension('doctrine')) { |
96
|
|
|
$this->prependOrm($configs, $container, $module); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
if ($container->hasExtension('doctrine_mongodb')) { |
100
|
|
|
$this->prependMongoDB($configs, $container, $module); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
if ($container->hasExtension('api_platform')) { |
104
|
|
|
$this->prependApiPlatform($configs, $container, $module); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param array<array-key,array<array-key,string|bool|scalar>> $configs |
|
|
|
|
110
|
|
|
*/ |
111
|
|
|
private function prependSerialization(array $configs, ContainerBuilder $container, ModuleInterface $module): void |
112
|
|
|
{ |
113
|
|
|
//$configPath = (string) $container->getParameter('doyo.modular.paths.serialization'); |
114
|
|
|
$configPath = $configs['config_paths']['serialization']; |
115
|
|
|
$path = $module->getBasePath().'/'.$configPath; |
116
|
|
|
if (is_dir($path)) { |
117
|
|
|
$container->prependExtensionConfig('framework', [ |
118
|
|
|
'serializer' => [ |
119
|
|
|
'mapping' => [ |
120
|
|
|
'paths' => [ |
121
|
|
|
$path, |
122
|
|
|
], |
123
|
|
|
], |
124
|
|
|
], |
125
|
|
|
]); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param array<array-key,array<array-key,string|bool|scalar>> $configs |
|
|
|
|
131
|
|
|
* @psalm-suppress PossiblyInvalidCast |
132
|
|
|
* @psalm-suppress MixedAssignment |
133
|
|
|
* @psalm-suppress MixedArrayAccess |
134
|
|
|
*/ |
135
|
|
|
private function prependOrm(array $configs, ContainerBuilder $container, ModuleInterface $module): void |
136
|
|
|
{ |
137
|
|
|
$basePath = $module->getBasePath(); |
138
|
|
|
$config = $configs['doctrine']; |
139
|
|
|
$useAnnotation = $configs['use_annotation']; |
140
|
|
|
$nsSuffix = $config['entity_dir']; |
141
|
|
|
$entityDir = $basePath.\DIRECTORY_SEPARATOR.$nsSuffix; |
142
|
|
|
$mappingDir = $basePath.\DIRECTORY_SEPARATOR.$config['mapping_dir']; |
143
|
|
|
$mappingNS = $module->getNamespace().'\\'.$nsSuffix; |
144
|
|
|
|
145
|
|
|
if ( ! is_dir($entityDir)) { |
146
|
|
|
return; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
$extensionConfig = [ |
150
|
|
|
'orm' => [ |
151
|
|
|
'mappings' => [ |
152
|
|
|
$module->getName() => [ |
153
|
|
|
'is_bundle' => false, |
154
|
|
|
'type' => $useAnnotation ? 'annotation' : 'xml', |
155
|
|
|
'dir' => $useAnnotation ? $entityDir : $mappingDir, |
156
|
|
|
'prefix' => $mappingNS, |
157
|
|
|
'alias' => $module->getName(), |
158
|
|
|
], |
159
|
|
|
], |
160
|
|
|
], |
161
|
|
|
]; |
162
|
|
|
|
163
|
|
|
if (\count($resolved = $module->getResolveTargetEntities()) > 0) { |
164
|
|
|
$extensionConfig['orm']['resolve_target_entities'] = $resolved; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
$container->prependExtensionConfig('doctrine', $extensionConfig); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @param array<array-key,array<array-key,string|bool|scalar>> $configs |
|
|
|
|
172
|
|
|
* @psalm-suppress PossiblyInvalidCast |
173
|
|
|
* @psalm-suppress MixedAssignment |
174
|
|
|
* @psalm-suppress MixedArrayAccess |
175
|
|
|
*/ |
176
|
|
|
private function prependMongoDB(array $configs, ContainerBuilder $container, ModuleInterface $module): void |
177
|
|
|
{ |
178
|
|
|
$basePath = $module->getBasePath(); |
179
|
|
|
$config = $configs['doctrine']; |
180
|
|
|
$useAnnotation = $configs['use_annotation']; |
181
|
|
|
$nsSuffix = $config['document_dir']; |
182
|
|
|
$documentDir = $basePath.\DIRECTORY_SEPARATOR.$nsSuffix; |
183
|
|
|
$mappingDir = $basePath.\DIRECTORY_SEPARATOR.$config['mapping_dir']; |
184
|
|
|
$mappingNS = $module->getNamespace().'\\'.$nsSuffix; |
185
|
|
|
|
186
|
|
|
if ( ! is_dir($documentDir)) { |
187
|
|
|
return; |
188
|
|
|
} |
189
|
|
|
$extensionConfig = [ |
190
|
|
|
'document_managers' => [ |
191
|
|
|
'default' => [ |
192
|
|
|
'mappings' => [ |
193
|
|
|
$module->getName() => [ |
194
|
|
|
'is_bundle' => false, |
195
|
|
|
'type' => $useAnnotation ? 'annotation' : 'xml', |
196
|
|
|
'dir' => $useAnnotation ? $documentDir : $mappingDir, |
197
|
|
|
'prefix' => $mappingNS, |
198
|
|
|
'alias' => $module->getName(), |
199
|
|
|
], |
200
|
|
|
], |
201
|
|
|
], |
202
|
|
|
], |
203
|
|
|
]; |
204
|
|
|
|
205
|
|
|
if (\count($resolved = $module->getResolveTargetEntities()) > 0) { |
206
|
|
|
$extensionConfig['resolve_target_documents'] = $resolved; |
207
|
|
|
} |
208
|
|
|
$container->prependExtensionConfig('doctrine_mongodb', $extensionConfig); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* @param array<array-key,array<array-key,string|bool|scalar>> $configs |
|
|
|
|
213
|
|
|
*/ |
214
|
|
|
private function prependApiPlatform(array $configs, ContainerBuilder $container, ModuleInterface $module): void |
215
|
|
|
{ |
216
|
|
|
$useAnnotation = $configs['use_annotation']; |
217
|
|
|
$basePath = $module->getBasePath(); |
218
|
|
|
$apiConfig = $basePath.\DIRECTORY_SEPARATOR.$configs['config_paths']['api_platform']; |
219
|
|
|
$doctrine = $configs['doctrine']; |
220
|
|
|
$modelSuffix = $doctrine['use_orm'] ? $doctrine['entity_dir'] : $doctrine['document_dir']; |
221
|
|
|
$modelDir = $basePath.\DIRECTORY_SEPARATOR.$modelSuffix; |
222
|
|
|
$dir = $useAnnotation ? $modelDir : $apiConfig; |
223
|
|
|
|
224
|
|
|
if (is_dir($dir)) { |
225
|
|
|
$container->prependExtensionConfig('api_platform', [ |
226
|
|
|
'mapping' => [ |
227
|
|
|
'paths' => [$dir], |
228
|
|
|
], |
229
|
|
|
]); |
230
|
|
|
} |
231
|
|
|
} |
232
|
|
|
} |
233
|
|
|
|