1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Lug package. |
5
|
|
|
* |
6
|
|
|
* (c) Eric GELOEN <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please read the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Lug\Bundle\ResourceBundle\DependencyInjection\Extension; |
13
|
|
|
|
14
|
|
|
use Lug\Bundle\ResourceBundle\ResourceBundleInterface; |
15
|
|
|
use Lug\Bundle\ResourceBundle\Util\ClassUtils; |
16
|
|
|
use Lug\Component\Resource\Model\Resource; |
17
|
|
|
use Lug\Component\Resource\Model\ResourceInterface; |
18
|
|
|
use Lug\Component\Translation\Factory\TranslatableFactory; |
19
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
20
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
21
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
22
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\ConfigurableExtension; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @author GeLo <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
class ResourceExtension extends ConfigurableExtension |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var ResourceBundleInterface |
31
|
|
|
*/ |
32
|
|
|
private $bundle; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param ResourceBundleInterface $bundle |
36
|
|
|
*/ |
37
|
8 |
|
public function __construct(ResourceBundleInterface $bundle) |
38
|
|
|
{ |
39
|
8 |
|
$this->bundle = $bundle; |
40
|
8 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* {@inheritdoc} |
44
|
|
|
*/ |
45
|
7 |
|
public function getConfiguration(array $config, ContainerBuilder $container) |
46
|
|
|
{ |
47
|
7 |
|
if (!class_exists($class = ClassUtils::getNamespace($this).'\\Configuration')) { |
48
|
|
|
$class = ResourceConfiguration::class; |
49
|
|
|
} |
50
|
|
|
|
51
|
7 |
|
return new $class($this->bundle); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritdoc} |
56
|
|
|
*/ |
57
|
8 |
|
public function getAlias() |
58
|
|
|
{ |
59
|
8 |
|
return $this->bundle->getAlias(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* {@inheritdoc} |
64
|
|
|
*/ |
65
|
7 |
|
protected function loadInternal(array $config, ContainerBuilder $container) |
66
|
|
|
{ |
67
|
7 |
|
foreach ($this->bundle->getResources() as $resource) { |
68
|
7 |
|
$resourceConfig = $config['resources'][$resource->getName()]; |
69
|
|
|
|
70
|
7 |
|
$this->configureResource($resource, $resourceConfig); |
71
|
7 |
|
$this->loadResource($resource, $container); |
72
|
|
|
|
73
|
7 |
|
if ($resource->getTranslation() !== null) { |
74
|
|
|
$this->configureResource($resource->getTranslation(), $resourceConfig['translation']); |
|
|
|
|
75
|
|
|
$this->loadResource($resource->getTranslation(), $container); |
|
|
|
|
76
|
|
|
} |
77
|
7 |
|
} |
78
|
|
|
|
79
|
7 |
|
$this->loadBundle($config, $container); |
80
|
7 |
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param mixed[] $config |
84
|
|
|
* @param ContainerBuilder $container |
85
|
|
|
*/ |
86
|
|
|
protected function loadBundle(array $config, ContainerBuilder $container) |
87
|
|
|
{ |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param ResourceInterface $resource |
92
|
|
|
* @param mixed[] $config |
93
|
|
|
*/ |
94
|
7 |
|
private function configureResource(ResourceInterface $resource, array $config) |
95
|
|
|
{ |
96
|
7 |
|
$driverConfig = $config['driver']; |
97
|
7 |
|
$mappingConfig = $driverConfig['mapping']; |
98
|
|
|
|
99
|
7 |
|
$resource->setModel($config['model']); |
100
|
7 |
|
$resource->setDriver(isset($driverConfig['name']) ? $driverConfig['name'] : null); |
101
|
7 |
|
$resource->setDriverManager(isset($driverConfig['manager']) ? $driverConfig['manager'] : null); |
102
|
7 |
|
$resource->setDriverMappingPath(isset($mappingConfig['path']) ? $mappingConfig['path'] : null); |
103
|
7 |
|
$resource->setDriverMappingFormat(isset($mappingConfig['format']) ? $mappingConfig['format'] : null); |
104
|
7 |
|
$resource->setRepository(isset($config['repository']) ? $config['repository'] : null); |
105
|
7 |
|
$resource->setFactory(isset($config['factory']) ? $config['factory'] : null); |
106
|
7 |
|
$resource->setForm(isset($config['form']) ? $config['form'] : null); |
107
|
7 |
|
$resource->setChoiceForm(isset($config['choice_form']) ? $config['choice_form'] : null); |
108
|
7 |
|
$resource->setDomainManager(isset($config['domain_manager']) ? $config['domain_manager'] : null); |
109
|
7 |
|
$resource->setController(isset($config['controller']) ? $config['controller'] : null); |
110
|
7 |
|
$resource->setIdPropertyPath(isset($config['id_property_path']) ? $config['id_property_path'] : null); |
111
|
7 |
|
$resource->setLabelPropertyPath(isset($config['label_property_path']) ? $config['label_property_path'] : null); |
112
|
7 |
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param ResourceInterface $resource |
116
|
|
|
* @param ContainerBuilder $container |
117
|
|
|
*/ |
118
|
7 |
|
private function loadResource(ResourceInterface $resource, ContainerBuilder $container) |
119
|
|
|
{ |
120
|
7 |
|
$container->setDefinition('lug.resource.'.$resource->getName(), $this->createResourceDefinition($resource)); |
121
|
|
|
|
122
|
7 |
|
if ($resource->getDriverManager() !== null) { |
123
|
7 |
|
$container->setAlias('lug.manager.'.$resource->getName(), $this->createManagerAlias($resource)); |
124
|
7 |
|
} |
125
|
|
|
|
126
|
7 |
View Code Duplication |
if ($resource->getRepository() !== null) { |
|
|
|
|
127
|
7 |
|
$repository = 'lug.repository.'.$resource->getName(); |
128
|
|
|
|
129
|
7 |
|
if (class_exists($resource->getRepository())) { |
130
|
7 |
|
$container->setDefinition($repository, $this->createRepositoryDefinition($resource)); |
131
|
7 |
|
} elseif ($repository !== $resource->getRepository()) { |
132
|
|
|
$container->setAlias($repository, $resource->getRepository()); |
133
|
|
|
} |
134
|
7 |
|
} |
135
|
|
|
|
136
|
7 |
View Code Duplication |
if ($resource->getFactory() !== null) { |
|
|
|
|
137
|
7 |
|
$factory = 'lug.factory.'.$resource->getName(); |
138
|
|
|
|
139
|
7 |
|
if (class_exists($resource->getFactory())) { |
140
|
7 |
|
$container->setDefinition($factory, $this->createFactoryDefinition($resource)); |
141
|
7 |
|
} elseif ($factory !== $resource->getFactory()) { |
142
|
|
|
$container->setAlias($factory, $resource->getFactory()); |
143
|
|
|
} |
144
|
7 |
|
} |
145
|
|
|
|
146
|
7 |
View Code Duplication |
if ($resource->getForm() !== null) { |
|
|
|
|
147
|
7 |
|
$form = 'lug.form.type.'.$resource->getName(); |
148
|
|
|
|
149
|
7 |
|
if (class_exists($resource->getForm())) { |
150
|
7 |
|
$container->setDefinition($form, $this->createFormDefinition($resource)); |
151
|
7 |
|
} elseif ($form !== $resource->getForm()) { |
152
|
|
|
$container->setAlias($form, $resource->getForm()); |
153
|
|
|
} |
154
|
7 |
|
} |
155
|
|
|
|
156
|
7 |
View Code Duplication |
if ($resource->getChoiceForm() !== null) { |
|
|
|
|
157
|
7 |
|
$choiceForm = 'lug.form.type.'.$resource->getName().'.choice'; |
158
|
|
|
|
159
|
7 |
|
if (class_exists($resource->getChoiceForm())) { |
160
|
7 |
|
$container->setDefinition($choiceForm, $this->createChoiceFormDefinition($resource)); |
161
|
7 |
|
} elseif ($choiceForm !== $resource->getChoiceForm()) { |
162
|
|
|
$container->setAlias($choiceForm, $resource->getChoiceForm()); |
163
|
|
|
} |
164
|
7 |
|
} |
165
|
|
|
|
166
|
7 |
View Code Duplication |
if ($resource->getDomainManager() !== null) { |
|
|
|
|
167
|
7 |
|
$domainManager = 'lug.domain_manager.'.$resource->getName(); |
168
|
|
|
|
169
|
7 |
|
if (class_exists($resource->getDomainManager())) { |
170
|
7 |
|
$container->setDefinition($domainManager, $this->createDomainManagerDefinition($resource)); |
171
|
7 |
|
} elseif ($domainManager !== $resource->getDomainManager()) { |
172
|
|
|
$container->setAlias($domainManager, $resource->getDomainManager()); |
173
|
|
|
} |
174
|
7 |
|
} |
175
|
|
|
|
176
|
7 |
View Code Duplication |
if ($resource->getController() !== null) { |
|
|
|
|
177
|
7 |
|
$controller = 'lug.controller.'.$resource->getName(); |
178
|
|
|
|
179
|
7 |
|
if (class_exists($resource->getController())) { |
180
|
7 |
|
$container->setDefinition($controller, $this->createControllerDefinition($resource)); |
181
|
7 |
|
} elseif ($controller !== $resource->getController()) { |
182
|
|
|
$container->setAlias($container, $resource->getController()); |
|
|
|
|
183
|
|
|
} |
184
|
7 |
|
} |
185
|
|
|
|
186
|
7 |
|
$container->addObjectResource($resource); |
187
|
7 |
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @param ResourceInterface $resource |
191
|
|
|
* |
192
|
|
|
* @return Definition |
193
|
|
|
*/ |
194
|
7 |
|
private function createResourceDefinition(ResourceInterface $resource) |
195
|
|
|
{ |
196
|
7 |
|
$definition = new Definition(Resource::class, [ |
197
|
7 |
|
$resource->getName(), |
198
|
7 |
|
$resource->getInterfaces(), |
199
|
7 |
|
$resource->getModel(), |
200
|
7 |
|
]); |
201
|
|
|
|
202
|
|
|
$definition |
203
|
7 |
|
->addMethodCall('setDriver', [$resource->getDriver()]) |
204
|
7 |
|
->addMethodCall('setDriverManager', [$resource->getDriverManager()]) |
205
|
7 |
|
->addMethodCall('setDriverMappingPath', [$resource->getDriverMappingPath()]) |
206
|
7 |
|
->addMethodCall('setDriverMappingFormat', [$resource->getDriverMappingFormat()]) |
207
|
7 |
|
->addMethodCall('setRepository', [$resource->getRepository()]) |
208
|
7 |
|
->addMethodCall('setFactory', [$resource->getFactory()]) |
209
|
7 |
|
->addMethodCall('setForm', [$resource->getForm()]) |
210
|
7 |
|
->addMethodCall('setChoiceForm', [$resource->getChoiceForm()]) |
211
|
7 |
|
->addMethodCall('setDomainManager', [$resource->getDomainManager()]) |
212
|
7 |
|
->addMethodCall('setController', [$resource->getController()]) |
213
|
7 |
|
->addMethodCall('setIdPropertyPath', [$resource->getIdPropertyPath()]) |
214
|
7 |
|
->addMethodCall('setLabelPropertyPath', [$resource->getLabelPropertyPath()]) |
215
|
7 |
|
->addTag('lug.resource'); |
216
|
|
|
|
217
|
7 |
|
if ($resource->getTranslation() !== null) { |
218
|
|
|
$definition->addMethodCall( |
219
|
|
|
'setTranslation', |
220
|
|
|
[new Reference('lug.resource.'.$resource->getTranslation()->getName())] |
221
|
|
|
); |
222
|
|
|
} |
223
|
|
|
|
224
|
7 |
|
return $definition; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* @param ResourceInterface $resource |
229
|
|
|
* |
230
|
|
|
* @return string |
231
|
|
|
*/ |
232
|
7 |
|
private function createManagerAlias(ResourceInterface $resource) |
233
|
|
|
{ |
234
|
7 |
|
if ($resource->getDriver() === ResourceInterface::DRIVER_DOCTRINE_MONGODB) { |
235
|
|
|
return 'doctrine_mongodb.odm.'.$resource->getDriverManager().'_document_manager'; |
236
|
|
|
} |
237
|
|
|
|
238
|
7 |
|
return 'doctrine.orm.'.$resource->getDriverManager().'_entity_manager'; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* @param ResourceInterface $resource |
243
|
|
|
* |
244
|
|
|
* @return Definition |
245
|
|
|
*/ |
246
|
7 |
|
private function createFactoryDefinition(ResourceInterface $resource) |
247
|
|
|
{ |
248
|
|
|
$arguments = [ |
249
|
7 |
|
new Reference('lug.resource.'.$resource->getName()), |
250
|
7 |
|
new Reference('property_accessor'), |
251
|
7 |
|
]; |
252
|
|
|
|
253
|
7 |
|
if (is_a($resource->getFactory(), TranslatableFactory::class, true)) { |
254
|
|
|
$arguments[] = new Reference('lug.translation.context.locale'); |
255
|
|
|
} |
256
|
|
|
|
257
|
7 |
|
$definition = new Definition($resource->getFactory(), $arguments); |
258
|
7 |
|
$definition->addTag('lug.factory', ['resource' => $resource->getName()]); |
259
|
|
|
|
260
|
7 |
|
return $definition; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* @param ResourceInterface $resource |
265
|
|
|
* |
266
|
|
|
* @return Definition |
267
|
|
|
*/ |
268
|
7 |
View Code Duplication |
private function createRepositoryDefinition(ResourceInterface $resource) |
|
|
|
|
269
|
|
|
{ |
270
|
7 |
|
$definition = new Definition($resource->getRepository(), [$resource->getModel()]); |
271
|
7 |
|
$definition->setFactory([new Reference('lug.manager.'.$resource->getName()), 'getRepository']); |
272
|
7 |
|
$definition->addTag('lug.repository', ['resource' => $resource->getName()]); |
273
|
|
|
|
274
|
7 |
|
return $definition; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* @param ResourceInterface $resource |
279
|
|
|
* |
280
|
|
|
* @return Definition |
281
|
|
|
*/ |
282
|
7 |
View Code Duplication |
private function createFormDefinition(ResourceInterface $resource) |
|
|
|
|
283
|
|
|
{ |
284
|
7 |
|
$definition = new Definition($resource->getForm(), [ |
285
|
7 |
|
new Reference('lug.resource.'.$resource->getName()), |
286
|
7 |
|
new Reference('lug.factory.'.$resource->getName()), |
287
|
7 |
|
]); |
288
|
|
|
|
289
|
7 |
|
$definition->addTag('form.type'); |
290
|
|
|
|
291
|
7 |
|
return $definition; |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* @param ResourceInterface $resource |
296
|
|
|
* |
297
|
|
|
* @return Definition |
298
|
|
|
*/ |
299
|
7 |
View Code Duplication |
private function createChoiceFormDefinition(ResourceInterface $resource) |
|
|
|
|
300
|
|
|
{ |
301
|
7 |
|
$definition = new Definition($resource->getChoiceForm(), [ |
302
|
7 |
|
new Reference('lug.resource.'.$resource->getName()), |
303
|
7 |
|
]); |
304
|
|
|
|
305
|
7 |
|
$definition->addTag('form.type'); |
306
|
|
|
|
307
|
7 |
|
return $definition; |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* @param ResourceInterface $resource |
312
|
|
|
* |
313
|
|
|
* @return Definition |
314
|
|
|
*/ |
315
|
7 |
|
private function createDomainManagerDefinition(ResourceInterface $resource) |
316
|
|
|
{ |
317
|
7 |
|
$definition = new Definition($resource->getDomainManager(), [ |
318
|
7 |
|
new Reference('lug.resource.'.$resource->getName()), |
319
|
7 |
|
new Reference('lug.resource.domain.event_dispatcher'), |
320
|
7 |
|
new Reference('lug.manager.'.$resource->getName()), |
321
|
7 |
|
new Reference('lug.repository.'.$resource->getName()), |
322
|
7 |
|
]); |
323
|
|
|
|
324
|
7 |
|
$definition->addTag('lug.domain_manager', ['resource' => $resource->getName()]); |
325
|
|
|
|
326
|
7 |
|
return $definition; |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* @param ResourceInterface $resource |
331
|
|
|
* |
332
|
|
|
* @return Definition |
333
|
|
|
*/ |
334
|
7 |
View Code Duplication |
private function createControllerDefinition(ResourceInterface $resource) |
|
|
|
|
335
|
|
|
{ |
336
|
7 |
|
$definition = new Definition($resource->getController(), [ |
337
|
7 |
|
new Reference('lug.resource.'.$resource->getName()), |
338
|
7 |
|
]); |
339
|
|
|
|
340
|
7 |
|
$definition->addMethodCall('setContainer', [new Reference('service_container')]); |
341
|
7 |
|
$definition->addTag('lug.controller', ['resource' => $resource->getName()]); |
342
|
|
|
|
343
|
7 |
|
return $definition; |
344
|
|
|
} |
345
|
|
|
} |
346
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: