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