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