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 |
|
$resource->setDriver($config['driver']['name']); |
97
|
7 |
|
$resource->setDriverManager($config['driver']['manager']); |
98
|
7 |
|
$resource->setDriverMappingPath($config['driver']['mapping']['path']); |
99
|
7 |
|
$resource->setDriverMappingFormat($config['driver']['mapping']['format']); |
100
|
7 |
|
$resource->setModel($config['model']); |
101
|
7 |
|
$resource->setRepository($config['repository']); |
102
|
7 |
|
$resource->setFactory(isset($config['factory']) ? $config['factory'] : null); |
103
|
7 |
|
$resource->setForm(isset($config['form']) ? $config['form'] : null); |
104
|
7 |
|
$resource->setChoiceForm(isset($config['choice_form']) ? $config['choice_form'] : null); |
105
|
7 |
|
$resource->setDomainManager(isset($config['domain_manager']) ? $config['domain_manager'] : null); |
106
|
7 |
|
$resource->setController(isset($config['controller']) ? $config['controller'] : null); |
107
|
7 |
|
$resource->setIdPropertyPath(isset($config['id_property_path']) ? $config['id_property_path'] : null); |
108
|
7 |
|
$resource->setLabelPropertyPath(isset($config['label_property_path']) ? $config['label_property_path'] : null); |
109
|
7 |
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param ResourceInterface $resource |
113
|
|
|
* @param ContainerBuilder $container |
114
|
|
|
*/ |
115
|
7 |
|
private function loadResource(ResourceInterface $resource, ContainerBuilder $container) |
116
|
|
|
{ |
117
|
7 |
|
$name = $resource->getName(); |
118
|
7 |
|
$repository = 'lug.repository.'.$name; |
119
|
7 |
|
$factory = 'lug.factory.'.$name; |
120
|
7 |
|
$form = 'lug.form.type.'.$name; |
121
|
7 |
|
$choiceForm = $form.'.choice'; |
122
|
7 |
|
$domainManager = 'lug.domain_manager.'.$name; |
123
|
7 |
|
$controller = 'lug.controller.'.$name; |
124
|
|
|
|
125
|
7 |
|
$container->setDefinition('lug.resource.'.$resource->getName(), $this->createResourceDefinition($resource)); |
126
|
7 |
|
$container->setAlias('lug.manager.'.$resource->getName(), $this->createManagerAlias($resource)); |
127
|
|
|
|
128
|
7 |
|
if (class_exists($resource->getRepository())) { |
129
|
7 |
|
$container->setDefinition($repository, $this->createRepositoryDefinition($resource)); |
130
|
7 |
|
} elseif ($repository !== $resource->getRepository()) { |
131
|
|
|
$container->setAlias($repository, $resource->getRepository()); |
132
|
|
|
} |
133
|
|
|
|
134
|
7 |
View Code Duplication |
if ($resource->getFactory() !== null) { |
|
|
|
|
135
|
7 |
|
if (class_exists($resource->getFactory())) { |
136
|
7 |
|
$container->setDefinition($factory, $this->createFactoryDefinition($resource)); |
137
|
7 |
|
} elseif ($factory !== $resource->getFactory()) { |
138
|
|
|
$container->setAlias($factory, $resource->getFactory()); |
139
|
|
|
} |
140
|
7 |
|
} |
141
|
|
|
|
142
|
7 |
View Code Duplication |
if ($resource->getForm() !== null) { |
|
|
|
|
143
|
7 |
|
if (class_exists($resource->getForm())) { |
144
|
7 |
|
$container->setDefinition($form, $this->createFormDefinition($resource)); |
145
|
7 |
|
} elseif ($form !== $resource->getForm()) { |
146
|
|
|
$container->setAlias($form, $resource->getForm()); |
147
|
|
|
} |
148
|
7 |
|
} |
149
|
|
|
|
150
|
7 |
View Code Duplication |
if ($resource->getChoiceForm() !== null) { |
|
|
|
|
151
|
7 |
|
if (class_exists($resource->getChoiceForm())) { |
152
|
7 |
|
$container->setDefinition($choiceForm, $this->createChoiceFormDefinition($resource)); |
153
|
7 |
|
} elseif ($choiceForm !== $resource->getChoiceForm()) { |
154
|
|
|
$container->setAlias($choiceForm, $resource->getChoiceForm()); |
155
|
|
|
} |
156
|
7 |
|
} |
157
|
|
|
|
158
|
7 |
View Code Duplication |
if ($resource->getDomainManager() !== null) { |
|
|
|
|
159
|
7 |
|
if (class_exists($resource->getDomainManager())) { |
160
|
7 |
|
$container->setDefinition($domainManager, $this->createDomainManagerDefinition($resource)); |
161
|
7 |
|
} elseif ($domainManager !== $resource->getDomainManager()) { |
162
|
|
|
$container->setAlias($domainManager, $resource->getDomainManager()); |
163
|
|
|
} |
164
|
7 |
|
} |
165
|
|
|
|
166
|
7 |
View Code Duplication |
if ($resource->getController() !== null) { |
|
|
|
|
167
|
7 |
|
if (class_exists($resource->getController())) { |
168
|
7 |
|
$container->setDefinition($controller, $this->createControllerDefinition($resource)); |
169
|
7 |
|
} elseif ($controller !== $resource->getController()) { |
170
|
|
|
$container->setAlias($container, $resource->getController()); |
|
|
|
|
171
|
|
|
} |
172
|
7 |
|
} |
173
|
|
|
|
174
|
7 |
|
$container->addClassResource(new \ReflectionClass($resource)); |
175
|
7 |
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @param ResourceInterface $resource |
179
|
|
|
* |
180
|
|
|
* @return Definition |
181
|
|
|
*/ |
182
|
7 |
|
private function createResourceDefinition(ResourceInterface $resource) |
183
|
|
|
{ |
184
|
|
|
$arguments = [ |
185
|
7 |
|
$resource->getName(), |
186
|
7 |
|
$resource->getDriver(), |
187
|
7 |
|
$resource->getDriverManager(), |
188
|
7 |
|
$resource->getDriverMappingPath(), |
189
|
7 |
|
$resource->getDriverMappingFormat(), |
190
|
7 |
|
$resource->getInterfaces(), |
191
|
7 |
|
$resource->getModel(), |
192
|
7 |
|
$resource->getRepository(), |
193
|
7 |
|
$resource->getFactory(), |
194
|
7 |
|
$resource->getForm(), |
195
|
7 |
|
$resource->getChoiceForm(), |
196
|
7 |
|
$resource->getDomainManager(), |
197
|
7 |
|
$resource->getController(), |
198
|
7 |
|
$resource->getIdPropertyPath(), |
199
|
7 |
|
$resource->getLabelPropertyPath(), |
200
|
7 |
|
]; |
201
|
|
|
|
202
|
7 |
|
if ($resource->getTranslation() !== null) { |
203
|
|
|
$arguments[] = new Reference('lug.resource.'.$resource->getTranslation()->getName()); |
204
|
|
|
} |
205
|
|
|
|
206
|
7 |
|
$definition = new Definition(Resource::class, $arguments); |
207
|
7 |
|
$definition->addTag('lug.resource'); |
208
|
|
|
|
209
|
7 |
|
return $definition; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* @param ResourceInterface $resource |
214
|
|
|
* |
215
|
|
|
* @return string |
216
|
|
|
*/ |
217
|
7 |
|
private function createManagerAlias(ResourceInterface $resource) |
218
|
|
|
{ |
219
|
7 |
|
if ($resource->getDriver() === ResourceInterface::DRIVER_DOCTRINE_MONGODB) { |
220
|
|
|
return 'doctrine_mongodb.odm.'.$resource->getDriverManager().'_document_manager'; |
221
|
|
|
} |
222
|
|
|
|
223
|
7 |
|
return 'doctrine.orm.'.$resource->getDriverManager().'_entity_manager'; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* @param ResourceInterface $resource |
228
|
|
|
* |
229
|
|
|
* @return Definition |
230
|
|
|
*/ |
231
|
7 |
|
private function createFactoryDefinition(ResourceInterface $resource) |
232
|
|
|
{ |
233
|
|
|
$arguments = [ |
234
|
7 |
|
new Reference('lug.resource.'.$resource->getName()), |
235
|
7 |
|
new Reference('property_accessor'), |
236
|
7 |
|
]; |
237
|
|
|
|
238
|
7 |
|
if (is_a($resource->getFactory(), TranslatableFactory::class, true)) { |
239
|
|
|
$arguments[] = new Reference('lug.translation.context.locale'); |
240
|
|
|
} |
241
|
|
|
|
242
|
7 |
|
$definition = new Definition($resource->getFactory(), $arguments); |
243
|
7 |
|
$definition->addTag('lug.factory', ['resource' => $resource->getName()]); |
244
|
|
|
|
245
|
7 |
|
return $definition; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* @param ResourceInterface $resource |
250
|
|
|
* |
251
|
|
|
* @return Definition |
252
|
|
|
*/ |
253
|
7 |
View Code Duplication |
private function createRepositoryDefinition(ResourceInterface $resource) |
|
|
|
|
254
|
|
|
{ |
255
|
7 |
|
$definition = new Definition($resource->getRepository(), [$resource->getModel()]); |
256
|
7 |
|
$definition->setFactory([new Reference('lug.manager.'.$resource->getName()), 'getRepository']); |
257
|
7 |
|
$definition->addTag('lug.repository', ['resource' => $resource->getName()]); |
258
|
|
|
|
259
|
7 |
|
return $definition; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* @param ResourceInterface $resource |
264
|
|
|
* |
265
|
|
|
* @return Definition |
266
|
|
|
*/ |
267
|
7 |
View Code Duplication |
private function createFormDefinition(ResourceInterface $resource) |
|
|
|
|
268
|
|
|
{ |
269
|
7 |
|
$definition = new Definition($resource->getForm(), [ |
270
|
7 |
|
new Reference('lug.resource.'.$resource->getName()), |
271
|
7 |
|
new Reference('lug.factory.'.$resource->getName()), |
272
|
7 |
|
]); |
273
|
|
|
|
274
|
7 |
|
$definition->addTag('form.type'); |
275
|
|
|
|
276
|
7 |
|
return $definition; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* @param ResourceInterface $resource |
281
|
|
|
* |
282
|
|
|
* @return Definition |
283
|
|
|
*/ |
284
|
7 |
View Code Duplication |
private function createChoiceFormDefinition(ResourceInterface $resource) |
|
|
|
|
285
|
|
|
{ |
286
|
7 |
|
$definition = new Definition($resource->getChoiceForm(), [ |
287
|
7 |
|
new Reference('lug.resource.'.$resource->getName()), |
288
|
7 |
|
]); |
289
|
|
|
|
290
|
7 |
|
$definition->addTag('form.type'); |
291
|
|
|
|
292
|
7 |
|
return $definition; |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* @param ResourceInterface $resource |
297
|
|
|
* |
298
|
|
|
* @return Definition |
299
|
|
|
*/ |
300
|
7 |
|
private function createDomainManagerDefinition(ResourceInterface $resource) |
301
|
|
|
{ |
302
|
7 |
|
$definition = new Definition($resource->getDomainManager(), [ |
303
|
7 |
|
new Reference('lug.resource.'.$resource->getName()), |
304
|
7 |
|
new Reference('lug.resource.domain.event_dispatcher'), |
305
|
7 |
|
new Reference('lug.manager.'.$resource->getName()), |
306
|
7 |
|
new Reference('lug.repository.'.$resource->getName()), |
307
|
7 |
|
]); |
308
|
|
|
|
309
|
7 |
|
$definition->addTag('lug.domain_manager', ['resource' => $resource->getName()]); |
310
|
|
|
|
311
|
7 |
|
return $definition; |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* @param ResourceInterface $resource |
316
|
|
|
* |
317
|
|
|
* @return Definition |
318
|
|
|
*/ |
319
|
7 |
View Code Duplication |
private function createControllerDefinition(ResourceInterface $resource) |
|
|
|
|
320
|
|
|
{ |
321
|
7 |
|
$definition = new Definition($resource->getController(), [ |
322
|
7 |
|
new Reference('lug.resource.'.$resource->getName()), |
323
|
7 |
|
]); |
324
|
|
|
|
325
|
7 |
|
$definition->addMethodCall('setContainer', [new Reference('service_container')]); |
326
|
7 |
|
$definition->addTag('lug.controller', ['resource' => $resource->getName()]); |
327
|
|
|
|
328
|
7 |
|
return $definition; |
329
|
|
|
} |
330
|
|
|
} |
331
|
|
|
|
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: