1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the FOSElasticaBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) FriendsOfSymfony <http://friendsofsymfony.github.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
|
|
|
namespace FOS\ElasticaBundle\DependencyInjection; |
13
|
|
|
|
14
|
|
|
use Elastica\Client as ElasticaClient; |
15
|
|
|
use FOS\ElasticaBundle\Elastica\Client; |
16
|
|
|
use FOS\ElasticaBundle\Manager\RepositoryManagerInterface; |
17
|
|
|
use Symfony\Component\Config\FileLocator; |
18
|
|
|
use Symfony\Component\DependencyInjection\Alias; |
19
|
|
|
use Symfony\Component\DependencyInjection\ChildDefinition; |
20
|
|
|
use Symfony\Component\DependencyInjection\ContainerAwareInterface; |
21
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
22
|
|
|
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; |
23
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
24
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
25
|
|
|
|
26
|
|
|
class FOSElasticaExtension extends Extension |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* Definition of elastica clients as configured by this extension. |
30
|
|
|
* |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
private $clients = []; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* An array of indexes as configured by the extension. |
37
|
|
|
* |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
private $indexConfigs = []; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* An array of index templates as configured by the extension. |
44
|
|
|
* |
45
|
|
|
* @var array |
46
|
|
|
*/ |
47
|
|
|
private $indexTemplateConfigs = array(); |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* If we've encountered a type mapped to a specific persistence driver, it will be loaded |
51
|
|
|
* here. |
52
|
|
|
* |
53
|
|
|
* @var array |
54
|
|
|
*/ |
55
|
|
|
private $loadedDrivers = []; |
56
|
|
|
|
57
|
20 |
|
public function load(array $configs, ContainerBuilder $container) |
58
|
|
|
{ |
59
|
20 |
|
$configuration = $this->getConfiguration($configs, $container); |
60
|
20 |
|
$config = $this->processConfiguration($configuration, $configs); |
61
|
|
|
|
62
|
20 |
|
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
63
|
|
|
|
64
|
20 |
|
if (empty($config['clients']) || empty($config['indexes'])) { |
65
|
|
|
// No Clients or indexes are defined |
66
|
|
|
return; |
67
|
|
|
} |
68
|
|
|
|
69
|
20 |
|
foreach (['config', 'index', 'persister', 'provider', 'source', 'transformer', 'event_listener', 'commands'] as $basename) { |
70
|
20 |
|
$loader->load(sprintf('%s.xml', $basename)); |
71
|
|
|
} |
72
|
|
|
|
73
|
20 |
|
if (empty($config['default_client'])) { |
74
|
20 |
|
$keys = array_keys($config['clients']); |
75
|
20 |
|
$config['default_client'] = reset($keys); |
76
|
|
|
} |
77
|
|
|
|
78
|
20 |
|
if (empty($config['default_index'])) { |
79
|
20 |
|
$keys = array_keys($config['indexes']); |
80
|
20 |
|
$config['default_index'] = reset($keys); |
81
|
|
|
} |
82
|
|
|
|
83
|
20 |
|
if (isset($config['serializer'])) { |
84
|
1 |
|
$loader->load('serializer.xml'); |
85
|
|
|
|
86
|
1 |
|
$this->loadSerializer($config['serializer'], $container); |
87
|
|
|
} |
88
|
|
|
|
89
|
20 |
|
$this->loadClients($config['clients'], $container); |
90
|
20 |
|
$container->setAlias('fos_elastica.client', sprintf('fos_elastica.client.%s', $config['default_client'])); |
91
|
20 |
|
$container->getAlias('fos_elastica.client')->setPublic(true); |
92
|
20 |
|
$container->setAlias(ElasticaClient::class, new Alias('fos_elastica.client', false)); |
93
|
20 |
|
$container->setAlias(Client::class, 'fos_elastica.client'); |
94
|
20 |
|
$container->getAlias(Client::class)->setPublic(false); |
95
|
|
|
|
96
|
20 |
|
$this->loadIndexes($config['indexes'], $container); |
97
|
20 |
|
$container->setAlias('fos_elastica.index', sprintf('fos_elastica.index.%s', $config['default_index'])); |
98
|
20 |
|
$container->getAlias('fos_elastica.index')->setPublic(true); |
99
|
20 |
|
$container->setParameter('fos_elastica.default_index', $config['default_index']); |
100
|
|
|
|
101
|
20 |
|
if ($usedIndexNames = \array_intersect_key($config['indexes'], $config['index_templates'])) { |
102
|
|
|
throw new \DomainException( |
103
|
|
|
\sprintf( |
104
|
|
|
'Index names "%s" are already in use and can not be used for index templates names', |
105
|
|
|
\implode('","', \array_keys($usedIndexNames)) |
106
|
|
|
) |
107
|
|
|
); |
108
|
|
|
} |
109
|
20 |
|
$this->loadIndexTemplates($config['index_templates'], $container); |
110
|
|
|
|
111
|
20 |
|
$container->getDefinition('fos_elastica.config_source.container')->replaceArgument(0, $this->indexConfigs); |
112
|
|
|
$container |
113
|
20 |
|
->getDefinition('fos_elastica.config_source.template_container') |
114
|
20 |
|
->replaceArgument(0, $this->indexTemplateConfigs); |
115
|
|
|
|
116
|
20 |
|
$this->loadIndexManager($container); |
117
|
20 |
|
$this->loadIndexTemplateManager($container); |
118
|
|
|
|
119
|
20 |
|
$this->createDefaultManagerAlias($config['default_manager'], $container); |
120
|
20 |
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param array $config |
124
|
|
|
* @param ContainerBuilder $container |
125
|
|
|
* |
126
|
|
|
* @return Configuration |
127
|
|
|
*/ |
128
|
20 |
|
public function getConfiguration(array $config, ContainerBuilder $container) |
129
|
|
|
{ |
130
|
20 |
|
return new Configuration($container->getParameter('kernel.debug')); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Loads the configured clients. |
135
|
|
|
* |
136
|
|
|
* @param array $clients An array of clients configurations |
137
|
|
|
* @param ContainerBuilder $container A ContainerBuilder instance |
138
|
|
|
* |
139
|
|
|
* @return array |
140
|
|
|
*/ |
141
|
20 |
|
private function loadClients(array $clients, ContainerBuilder $container) |
142
|
|
|
{ |
143
|
20 |
|
foreach ($clients as $name => $clientConfig) { |
144
|
20 |
|
$clientId = sprintf('fos_elastica.client.%s', $name); |
145
|
|
|
|
146
|
20 |
|
$clientDef = new ChildDefinition('fos_elastica.client_prototype'); |
147
|
20 |
|
$clientDef->replaceArgument(0, $clientConfig); |
148
|
20 |
|
$clientDef->replaceArgument(1, null); |
149
|
|
|
|
150
|
20 |
|
$logger = $clientConfig['connections'][0]['logger']; |
151
|
20 |
|
if (false !== $logger) { |
152
|
20 |
|
$clientDef->addMethodCall('setLogger', [new Reference($logger)]); |
153
|
|
|
} |
154
|
|
|
|
155
|
20 |
|
$clientDef->addTag('fos_elastica.client'); |
156
|
|
|
|
157
|
20 |
|
$container->setDefinition($clientId, $clientDef); |
158
|
|
|
|
159
|
20 |
|
$this->clients[$name] = [ |
160
|
20 |
|
'id' => $clientId, |
161
|
20 |
|
'reference' => new Reference($clientId), |
162
|
|
|
]; |
163
|
|
|
} |
164
|
20 |
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Loads the configured indexes. |
168
|
|
|
* |
169
|
|
|
* @param array $indexes An array of indexes configurations |
170
|
|
|
* @param ContainerBuilder $container A ContainerBuilder instance |
171
|
|
|
* |
172
|
|
|
* @throws \InvalidArgumentException |
173
|
|
|
* |
174
|
|
|
* @return array |
175
|
|
|
*/ |
176
|
20 |
|
private function loadIndexes(array $indexes, ContainerBuilder $container) |
177
|
|
|
{ |
178
|
20 |
|
$indexableCallbacks = []; |
179
|
|
|
|
180
|
20 |
|
foreach ($indexes as $name => $index) { |
181
|
20 |
|
$indexId = sprintf('fos_elastica.index.%s', $name); |
182
|
20 |
|
$indexName = isset($index['index_name']) ? $index['index_name'] : $name; |
183
|
|
|
|
184
|
20 |
|
$indexDef = new ChildDefinition('fos_elastica.index_prototype'); |
185
|
20 |
|
$indexDef->setFactory([new Reference('fos_elastica.client'), 'getIndex']); |
186
|
20 |
|
$indexDef->replaceArgument(0, $indexName); |
187
|
20 |
|
$indexDef->addTag('fos_elastica.index', [ |
188
|
20 |
|
'name' => $name, |
189
|
|
|
]); |
190
|
|
|
|
191
|
20 |
View Code Duplication |
if (isset($index['client'])) { |
192
|
1 |
|
$client = $this->getClient($index['client']); |
193
|
|
|
|
194
|
1 |
|
$indexDef->setFactory([$client, 'getIndex']); |
195
|
|
|
} |
196
|
|
|
|
197
|
20 |
|
$container->setDefinition($indexId, $indexDef); |
198
|
20 |
|
$reference = new Reference($indexId); |
199
|
|
|
|
200
|
20 |
|
$this->indexConfigs[$name] = [ |
201
|
20 |
|
'elasticsearch_name' => $indexName, |
202
|
20 |
|
'reference' => $reference, |
203
|
20 |
|
'model' => $index['persistence']['model'] ?? null, |
204
|
20 |
|
'name' => $name, |
205
|
20 |
|
'settings' => $index['settings'], |
206
|
20 |
|
'type_prototype' => isset($index['type_prototype']) ? $index['type_prototype'] : [], |
207
|
20 |
|
'use_alias' => $index['use_alias'], |
208
|
|
|
]; |
209
|
|
|
|
210
|
20 |
|
if ($index['finder']) { |
211
|
|
|
$this->loadIndexFinder($container, $name, $reference); |
212
|
|
|
} |
213
|
|
|
|
214
|
20 |
|
$this->loadTypes((array) $index['types'], $container, $this->indexConfigs[$name]); |
215
|
|
|
|
216
|
20 |
|
if (isset($index['indexable_callback'])) { |
217
|
4 |
|
$indexableCallbacks[$name] = $this->buildCallback($index['indexable_callback'], $name); |
218
|
|
|
} |
219
|
|
|
|
220
|
20 |
|
if (isset($index['persistence'])) { |
221
|
14 |
|
$this->loadTypePersistenceIntegration($index['persistence'], $container, $reference, $name); |
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
|
225
|
20 |
|
$indexable = $container->getDefinition('fos_elastica.indexable'); |
226
|
20 |
|
$indexable->replaceArgument(0, $indexableCallbacks); |
227
|
20 |
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Loads the configured indexes. |
231
|
|
|
* |
232
|
|
|
* @param array $indexTemplates An array of indexes configurations |
233
|
|
|
* @param ContainerBuilder $container A ContainerBuilder instance |
234
|
|
|
* |
235
|
|
|
* @throws \InvalidArgumentException |
236
|
|
|
* |
237
|
|
|
* @return void |
238
|
|
|
*/ |
239
|
20 |
|
private function loadIndexTemplates(array $indexTemplates, ContainerBuilder $container) |
240
|
|
|
{ |
241
|
20 |
|
foreach ($indexTemplates as $name => $indexTemplate) { |
242
|
6 |
|
$indexId = sprintf('fos_elastica.index_template.%s', $name); |
243
|
6 |
|
$indexTemplateName = isset($indexTemplate['template_name']) ? $indexTemplate['template_name'] : $name; |
244
|
|
|
|
245
|
6 |
|
$indexDef = new ChildDefinition('fos_elastica.index_template_prototype'); |
246
|
6 |
|
$indexDef->setFactory([new Reference('fos_elastica.client'), 'getIndexTemplate']); |
247
|
6 |
|
$indexDef->replaceArgument(0, $indexTemplateName); |
248
|
6 |
|
$indexDef->addTag('fos_elastica.index_template', array( |
249
|
6 |
|
'name' => $name, |
250
|
|
|
)); |
251
|
|
|
|
252
|
6 |
View Code Duplication |
if (isset($indexTemplate['client'])) { |
253
|
6 |
|
$client = $this->getClient($indexTemplate['client']); |
254
|
6 |
|
$indexDef->setFactory([$client, 'getIndexTemplate']); |
255
|
|
|
} |
256
|
|
|
|
257
|
6 |
|
$container->setDefinition($indexId, $indexDef); |
258
|
6 |
|
$reference = new Reference($indexId); |
259
|
|
|
|
260
|
6 |
|
$this->indexTemplateConfigs[$name] = array( |
261
|
6 |
|
'elasticsearch_name' => $indexTemplateName, |
262
|
6 |
|
'reference' => $reference, |
263
|
6 |
|
'name' => $name, |
264
|
6 |
|
'settings' => $indexTemplate['settings'], |
265
|
6 |
|
'template' => $indexTemplate['template'], |
266
|
|
|
); |
267
|
|
|
|
268
|
6 |
|
$this->loadTypes( |
269
|
6 |
|
(array) $indexTemplate['types'], |
270
|
|
|
$container, |
271
|
6 |
|
$this->indexTemplateConfigs[$name] |
272
|
|
|
); |
273
|
|
|
} |
274
|
20 |
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* Loads the configured index finders. |
278
|
|
|
* |
279
|
|
|
* @param \Symfony\Component\DependencyInjection\ContainerBuilder $container |
280
|
|
|
* @param string $name The index name |
281
|
|
|
* @param Reference $index Reference to the related index |
282
|
|
|
* |
283
|
|
|
* @return string |
284
|
|
|
*/ |
285
|
|
|
private function loadIndexFinder(ContainerBuilder $container, string $name, Reference $index): void |
286
|
|
|
{ |
287
|
|
|
$finderId = sprintf('fos_elastica.finder.%s', $name); |
288
|
|
|
$finderDef = new ChildDefinition('fos_elastica.finder'); |
289
|
|
|
$finderDef->replaceArgument(0, $index); |
290
|
|
|
$finderDef->replaceArgument(1, new Reference(sprintf('fos_elastica.elastica_to_model_transformer.%s', $name))); |
291
|
|
|
|
292
|
|
|
$container->setDefinition($finderId, $finderDef); |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* Loads the configured types. |
297
|
|
|
*/ |
298
|
20 |
|
private function loadTypes(array $types, ContainerBuilder $container, array &$indexConfig): void |
299
|
|
|
{ |
300
|
20 |
|
foreach ($types as $name => $type) { |
301
|
20 |
|
$indexName = $indexConfig['name']; |
|
|
|
|
302
|
|
|
|
303
|
|
|
$typeConfig = [ |
304
|
20 |
|
'name' => $name, |
305
|
|
|
'mapping' => [], // An array containing anything that gets sent directly to ElasticSearch |
306
|
|
|
'config' => [], |
307
|
|
|
]; |
308
|
|
|
|
309
|
|
|
foreach ([ |
310
|
20 |
|
'dynamic_templates', |
311
|
|
|
'properties', |
312
|
|
|
'_all', |
313
|
|
|
'_id', |
314
|
|
|
'_routing', |
315
|
|
|
'_source', |
316
|
|
|
] as $field) { |
317
|
20 |
|
if (isset($type[$field])) { |
318
|
20 |
|
$typeConfig['mapping'][$field] = $type[$field]; |
319
|
|
|
} |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
foreach ([ |
323
|
20 |
|
'serializer', |
324
|
|
|
'analyzer', |
325
|
|
|
'search_analyzer', |
326
|
|
|
'dynamic', |
327
|
|
|
'date_detection', |
328
|
|
|
'dynamic_date_formats', |
329
|
|
|
'numeric_detection', |
330
|
|
|
] as $field) { |
331
|
20 |
|
$typeConfig['config'][$field] = array_key_exists($field, $type) ? |
332
|
20 |
|
$type[$field] : |
333
|
20 |
|
null; |
334
|
|
|
} |
335
|
|
|
|
336
|
20 |
|
$indexConfig['types'][$name] = $typeConfig; |
337
|
|
|
|
338
|
20 |
|
if ($container->hasDefinition('fos_elastica.serializer_callback_prototype')) { |
339
|
1 |
|
$indexSerializerId = sprintf('%s.serializer.callback', $indexConfig['reference']); |
340
|
1 |
|
$indexSerializerDef = new ChildDefinition('fos_elastica.serializer_callback_prototype'); |
341
|
|
|
|
342
|
1 |
|
if (isset($type['serializer']['groups'])) { |
343
|
1 |
|
$indexSerializerDef->addMethodCall('setGroups', [$type['serializer']['groups']]); |
344
|
|
|
} |
345
|
|
|
|
346
|
1 |
|
if (isset($type['serializer']['serialize_null'])) { |
347
|
1 |
|
$indexSerializerDef->addMethodCall('setSerializeNull', [$type['serializer']['serialize_null']]); |
348
|
|
|
} |
349
|
|
|
|
350
|
1 |
|
if (isset($type['serializer']['version'])) { |
351
|
1 |
|
$indexSerializerDef->addMethodCall('setVersion', [$type['serializer']['version']]); |
352
|
|
|
} |
353
|
|
|
|
354
|
1 |
|
$container->setDefinition($indexSerializerId, $indexSerializerDef); |
355
|
|
|
} |
356
|
|
|
} |
357
|
20 |
|
} |
358
|
|
|
|
359
|
4 |
|
private function buildCallback($indexCallback, $indexName) |
360
|
|
|
{ |
361
|
4 |
|
if (is_array($indexCallback)) { |
362
|
4 |
|
if (!isset($indexCallback[0])) { |
363
|
|
|
throw new \InvalidArgumentException(sprintf('Invalid indexable_callback for index %s', $indexName)); |
364
|
|
|
} |
365
|
|
|
|
366
|
4 |
|
$classOrServiceRef = $this->transformServiceReference($indexCallback[0]); |
367
|
4 |
|
if ($classOrServiceRef instanceof Reference && !isset($indexCallback[1])) { |
368
|
|
|
return $classOrServiceRef; // __invoke |
369
|
|
|
} |
370
|
|
|
|
371
|
4 |
|
if (!isset($indexCallback[1])) { |
372
|
|
|
throw new \InvalidArgumentException(sprintf('Invalid indexable_callback for index %s', $indexName)); |
373
|
|
|
} |
374
|
|
|
|
375
|
4 |
|
return [$classOrServiceRef, $indexCallback[1]]; |
376
|
|
|
} |
377
|
|
|
|
378
|
4 |
|
if (is_string($indexCallback)) { |
379
|
4 |
|
return $this->transformServiceReference($indexCallback); |
380
|
|
|
} |
381
|
|
|
|
382
|
|
|
throw new \InvalidArgumentException(sprintf('Invalid indexable_callback for index %s', $indexName)); |
383
|
|
|
} |
384
|
|
|
|
385
|
4 |
|
private function transformServiceReference($classOrService) |
386
|
|
|
{ |
387
|
4 |
|
return 0 === strpos($classOrService, '@') ? new Reference(substr($classOrService, 1)) : $classOrService; |
388
|
|
|
} |
389
|
|
|
|
390
|
|
|
/** |
391
|
|
|
* Loads the optional provider and finder for a type. |
392
|
|
|
*/ |
393
|
14 |
|
private function loadTypePersistenceIntegration(array $config, ContainerBuilder $container, Reference $indexRef, string $indexName): void |
394
|
|
|
{ |
395
|
14 |
|
if (isset($config['driver'])) { |
396
|
14 |
|
$this->loadDriver($container, $config['driver']); |
397
|
|
|
} |
398
|
|
|
|
399
|
14 |
|
$elasticaToModelTransformerId = $this->loadElasticaToModelTransformer($config, $container, $indexName); |
400
|
14 |
|
$modelToElasticaTransformerId = $this->loadModelToElasticaTransformer($config, $container, $indexName); |
401
|
14 |
|
$objectPersisterId = $this->loadObjectPersister($config, $indexRef, $container, $indexName, $modelToElasticaTransformerId); |
402
|
|
|
|
403
|
14 |
|
if (isset($config['provider'])) { |
404
|
14 |
|
$this->loadTypePagerProvider($config, $container, $indexName); |
405
|
|
|
} |
406
|
14 |
|
if (isset($config['finder'])) { |
407
|
14 |
|
$this->loadTypeFinder($config, $container, $elasticaToModelTransformerId, $indexRef, $indexName); |
408
|
|
|
} |
409
|
14 |
|
if (isset($config['listener']) && $config['listener']['enabled']) { |
410
|
13 |
|
$this->loadTypeListener($config, $container, $objectPersisterId, $indexName); |
411
|
|
|
} |
412
|
14 |
|
} |
413
|
|
|
|
414
|
|
|
/** |
415
|
|
|
* Creates and loads an ElasticaToModelTransformer. |
416
|
|
|
*/ |
417
|
14 |
|
private function loadElasticaToModelTransformer(array $persistenceConfig, ContainerBuilder $container, string $indexName): string |
418
|
|
|
{ |
419
|
14 |
|
if (isset($persistenceConfig['elastica_to_model_transformer']['service'])) { |
420
|
1 |
|
return $persistenceConfig['elastica_to_model_transformer']['service']; |
421
|
|
|
} |
422
|
|
|
|
423
|
|
|
/* Note: transformer services may conflict with "prototype.driver", if |
424
|
|
|
* the index and type names were "prototype" and a driver, respectively. |
425
|
|
|
*/ |
426
|
13 |
|
$abstractId = sprintf('fos_elastica.elastica_to_model_transformer.prototype.%s', $persistenceConfig['driver']); |
427
|
13 |
|
$serviceId = sprintf('fos_elastica.elastica_to_model_transformer.%s', $indexName); |
428
|
13 |
|
$serviceDef = new ChildDefinition($abstractId); |
429
|
13 |
|
$serviceDef->addTag('fos_elastica.elastica_to_model_transformer', ['index' => $indexName]); |
430
|
|
|
|
431
|
13 |
|
$serviceDef->replaceArgument(1, $persistenceConfig['model']); |
432
|
13 |
|
$serviceDef->replaceArgument(2, array_merge($persistenceConfig['elastica_to_model_transformer'], [ |
433
|
13 |
|
'identifier' => $persistenceConfig['identifier'], |
434
|
|
|
])); |
435
|
13 |
|
$container->setDefinition($serviceId, $serviceDef); |
436
|
|
|
|
437
|
13 |
|
return $serviceId; |
438
|
|
|
} |
439
|
|
|
|
440
|
|
|
/** |
441
|
|
|
* Creates and loads a ModelToElasticaTransformer for an index/type. |
442
|
|
|
*/ |
443
|
14 |
|
private function loadModelToElasticaTransformer(array $typeConfig, ContainerBuilder $container, string $indexName): string |
444
|
|
|
{ |
445
|
14 |
|
if (isset($typeConfig['model_to_elastica_transformer']['service'])) { |
446
|
|
|
return $typeConfig['model_to_elastica_transformer']['service']; |
447
|
|
|
} |
448
|
|
|
|
449
|
14 |
|
$abstractId = $container->hasDefinition('fos_elastica.serializer_callback_prototype') ? |
450
|
1 |
|
'fos_elastica.model_to_elastica_identifier_transformer' : |
451
|
14 |
|
'fos_elastica.model_to_elastica_transformer'; |
452
|
|
|
|
453
|
14 |
|
$serviceId = sprintf('fos_elastica.model_to_elastica_transformer.%s', $indexName); |
454
|
14 |
|
$serviceDef = new ChildDefinition($abstractId); |
455
|
14 |
|
$serviceDef->replaceArgument(0, [ |
456
|
14 |
|
'identifier' => $typeConfig['identifier'], |
457
|
14 |
|
'index' => $indexName, |
458
|
|
|
]); |
459
|
14 |
|
$container->setDefinition($serviceId, $serviceDef); |
460
|
|
|
|
461
|
14 |
|
return $serviceId; |
462
|
|
|
} |
463
|
|
|
|
464
|
|
|
/** |
465
|
|
|
* Creates and loads an object persister for a type. |
466
|
|
|
*/ |
467
|
14 |
|
private function loadObjectPersister(array $typeConfig, Reference $indexRef, ContainerBuilder $container, string $indexName, string $transformerId): string |
468
|
|
|
{ |
469
|
14 |
|
if (isset($typeConfig['persister']['service'])) { |
470
|
1 |
|
return $typeConfig['persister']['service']; |
471
|
|
|
} |
472
|
|
|
|
473
|
|
|
$arguments = [ |
474
|
13 |
|
$indexRef, |
475
|
13 |
|
new Reference($transformerId), |
476
|
13 |
|
$typeConfig['model'], |
477
|
|
|
]; |
478
|
|
|
|
479
|
13 |
|
if ($container->hasDefinition('fos_elastica.serializer_callback_prototype')) { |
480
|
1 |
|
$abstractId = 'fos_elastica.object_serializer_persister'; |
481
|
1 |
|
$callbackId = sprintf('%s.serializer.callback', $this->indexConfigs[$indexName]['reference']); |
482
|
1 |
|
$arguments[] = [new Reference($callbackId), 'serialize']; |
483
|
|
|
} else { |
484
|
12 |
|
$abstractId = 'fos_elastica.object_persister'; |
485
|
12 |
|
$mapping = $this->indexConfigs[$indexName]['types']['_doc']['mapping']; |
486
|
12 |
|
$argument = $mapping['properties']; |
487
|
12 |
|
$arguments[] = $argument; |
488
|
|
|
} |
489
|
|
|
|
490
|
13 |
|
$arguments[] = array_intersect_key($typeConfig['persister'], array_flip(['refresh'])); |
491
|
|
|
|
492
|
13 |
|
$serviceId = sprintf('fos_elastica.object_persister.%s', $indexName); |
493
|
13 |
|
$serviceDef = new ChildDefinition($abstractId); |
494
|
13 |
|
foreach ($arguments as $i => $argument) { |
495
|
13 |
|
$serviceDef->replaceArgument($i, $argument); |
496
|
|
|
} |
497
|
|
|
|
498
|
13 |
|
$serviceDef->addTag('fos_elastica.persister', ['index' => $indexName]); |
499
|
|
|
|
500
|
13 |
|
$container->setDefinition($serviceId, $serviceDef); |
501
|
|
|
|
502
|
13 |
|
return $serviceId; |
503
|
|
|
} |
504
|
|
|
|
505
|
|
|
/** |
506
|
|
|
* Loads a pager provider for a type. |
507
|
|
|
*/ |
508
|
14 |
|
private function loadTypePagerProvider(array $typeConfig, ContainerBuilder $container, string $indexName): string |
509
|
|
|
{ |
510
|
14 |
|
if (isset($typeConfig['provider']['service'])) { |
511
|
|
|
return $typeConfig['provider']['service']; |
512
|
|
|
} |
513
|
|
|
|
514
|
14 |
|
$baseConfig = $typeConfig['provider']; |
515
|
14 |
|
unset($baseConfig['service']); |
516
|
|
|
|
517
|
14 |
|
$driver = $typeConfig['driver']; |
518
|
|
|
|
519
|
14 |
|
switch ($driver) { |
520
|
14 |
|
case 'orm': |
521
|
13 |
|
$providerDef = new ChildDefinition('fos_elastica.pager_provider.prototype.'.$driver); |
522
|
13 |
|
$providerDef->replaceArgument(2, $typeConfig['model']); |
523
|
13 |
|
$providerDef->replaceArgument(3, $baseConfig); |
524
|
|
|
|
525
|
13 |
|
break; |
526
|
1 |
View Code Duplication |
case 'mongodb': |
527
|
|
|
$providerDef = new ChildDefinition('fos_elastica.pager_provider.prototype.'.$driver); |
528
|
|
|
$providerDef->replaceArgument(2, $typeConfig['model']); |
529
|
|
|
$providerDef->replaceArgument(3, $baseConfig); |
530
|
|
|
|
531
|
|
|
break; |
532
|
1 |
View Code Duplication |
case 'phpcr': |
533
|
1 |
|
$providerDef = new ChildDefinition('fos_elastica.pager_provider.prototype.'.$driver); |
534
|
1 |
|
$providerDef->replaceArgument(2, $typeConfig['model']); |
535
|
1 |
|
$providerDef->replaceArgument(3, $baseConfig); |
536
|
|
|
|
537
|
1 |
|
break; |
538
|
|
|
default: |
539
|
|
|
throw new \LogicException(sprintf('The pager provider for driver "%s" does not exist.', $driver)); |
540
|
|
|
} |
541
|
|
|
|
542
|
|
|
/* Note: provider services may conflict with "prototype.driver", if the |
543
|
|
|
* index and type names were "prototype" and a driver, respectively. |
544
|
|
|
*/ |
545
|
14 |
|
$providerId = sprintf('fos_elastica.pager_provider.%s', $indexName); |
546
|
14 |
|
$providerDef->addTag('fos_elastica.pager_provider', ['index' => $indexName]); |
547
|
|
|
|
548
|
14 |
|
$container->setDefinition($providerId, $providerDef); |
549
|
|
|
|
550
|
14 |
|
return $providerId; |
551
|
|
|
} |
552
|
|
|
|
553
|
|
|
/** |
554
|
|
|
* Loads doctrine listeners to handle indexing of new or updated objects. |
555
|
|
|
*/ |
556
|
13 |
|
private function loadTypeListener(array $typeConfig, ContainerBuilder $container, string $objectPersisterId, string $indexName): string |
557
|
|
|
{ |
558
|
13 |
|
if (isset($typeConfig['listener']['service'])) { |
559
|
|
|
return $typeConfig['listener']['service']; |
560
|
|
|
} |
561
|
|
|
|
562
|
|
|
/* Note: listener services may conflict with "prototype.driver", if the |
563
|
|
|
* index and type names were "prototype" and a driver, respectively. |
564
|
|
|
*/ |
565
|
13 |
|
$abstractListenerId = sprintf('fos_elastica.listener.prototype.%s', $typeConfig['driver']); |
566
|
13 |
|
$listenerId = sprintf('fos_elastica.listener.%s', $indexName); |
567
|
13 |
|
$listenerDef = new ChildDefinition($abstractListenerId); |
568
|
13 |
|
$listenerDef->replaceArgument(0, new Reference($objectPersisterId)); |
569
|
13 |
|
$listenerDef->replaceArgument(3, $typeConfig['listener']['logger'] ? |
570
|
|
|
new Reference($typeConfig['listener']['logger']) : |
571
|
13 |
|
null |
572
|
|
|
); |
573
|
|
|
$listenerConfig = [ |
574
|
13 |
|
'identifier' => $typeConfig['identifier'], |
575
|
13 |
|
'indexName' => $indexName, |
576
|
|
|
]; |
577
|
|
|
|
578
|
13 |
|
$tagName = null; |
579
|
13 |
|
switch ($typeConfig['driver']) { |
580
|
13 |
|
case 'orm': |
581
|
12 |
|
$tagName = 'doctrine.event_listener'; |
582
|
12 |
|
break; |
583
|
1 |
|
case 'phpcr': |
584
|
1 |
|
$tagName = 'doctrine_phpcr.event_listener'; |
585
|
1 |
|
break; |
586
|
|
|
case 'mongodb': |
587
|
|
|
$tagName = 'doctrine_mongodb.odm.event_listener'; |
588
|
|
|
break; |
589
|
|
|
} |
590
|
|
|
|
591
|
13 |
|
if ($typeConfig['listener']['defer']) { |
592
|
|
|
$listenerDef->setPublic(true); |
593
|
|
|
$listenerDef->addTag( |
594
|
|
|
'kernel.event_listener', |
595
|
|
|
['event' => 'kernel.terminate', 'method' => 'onTerminate'] |
596
|
|
|
); |
597
|
|
|
$listenerDef->addTag( |
598
|
|
|
'kernel.event_listener', |
599
|
|
|
['event' => 'console.terminate', 'method' => 'onTerminate'] |
600
|
|
|
); |
601
|
|
|
$listenerConfig['defer'] = true; |
602
|
|
|
} |
603
|
|
|
|
604
|
13 |
|
$listenerDef->replaceArgument(2, $listenerConfig); |
605
|
|
|
|
606
|
13 |
|
if (null !== $tagName) { |
607
|
13 |
|
foreach ($this->getDoctrineEvents($typeConfig) as $event) { |
608
|
13 |
|
$listenerDef->addTag($tagName, ['event' => $event]); |
609
|
|
|
} |
610
|
|
|
} |
611
|
|
|
|
612
|
13 |
|
$container->setDefinition($listenerId, $listenerDef); |
613
|
|
|
|
614
|
13 |
|
return $listenerId; |
615
|
|
|
} |
616
|
|
|
|
617
|
|
|
/** |
618
|
|
|
* Map Elastica to Doctrine events for the current driver. |
619
|
|
|
*/ |
620
|
13 |
|
private function getDoctrineEvents(array $typeConfig) |
621
|
|
|
{ |
622
|
13 |
|
switch ($typeConfig['driver']) { |
623
|
13 |
|
case 'orm': |
624
|
12 |
|
$eventsClass = '\Doctrine\ORM\Events'; |
625
|
12 |
|
break; |
626
|
1 |
|
case 'phpcr': |
627
|
1 |
|
$eventsClass = '\Doctrine\ODM\PHPCR\Event'; |
628
|
1 |
|
break; |
629
|
|
|
case 'mongodb': |
630
|
|
|
$eventsClass = '\Doctrine\ODM\MongoDB\Events'; |
631
|
|
|
break; |
632
|
|
|
default: |
633
|
|
|
throw new \InvalidArgumentException(sprintf('Cannot determine events for driver "%s"', $typeConfig['driver'])); |
634
|
|
|
} |
635
|
|
|
|
636
|
13 |
|
$events = []; |
637
|
|
|
$eventMapping = [ |
638
|
13 |
|
'insert' => [constant($eventsClass.'::postPersist')], |
639
|
13 |
|
'update' => [constant($eventsClass.'::postUpdate')], |
640
|
13 |
|
'delete' => [constant($eventsClass.'::preRemove')], |
641
|
13 |
|
'flush' => [constant($eventsClass.'::postFlush')], |
642
|
|
|
]; |
643
|
|
|
|
644
|
13 |
|
foreach ($eventMapping as $event => $doctrineEvents) { |
645
|
13 |
|
if (isset($typeConfig['listener'][$event]) && $typeConfig['listener'][$event]) { |
646
|
13 |
|
$events = array_merge($events, $doctrineEvents); |
647
|
|
|
} |
648
|
|
|
} |
649
|
|
|
|
650
|
13 |
|
return $events; |
651
|
|
|
} |
652
|
|
|
|
653
|
|
|
/** |
654
|
|
|
* Loads a Type specific Finder. |
655
|
|
|
*/ |
656
|
14 |
|
private function loadTypeFinder(array $typeConfig, ContainerBuilder $container, string $elasticaToModelId, Reference $indexRef, string $indexName): string |
657
|
|
|
{ |
658
|
14 |
|
if (isset($typeConfig['finder']['service'])) { |
659
|
|
|
$finderId = $typeConfig['finder']['service']; |
660
|
|
|
} else { |
661
|
14 |
|
$finderId = sprintf('fos_elastica.finder.%s', $indexName); |
662
|
14 |
|
$finderDef = new ChildDefinition('fos_elastica.finder'); |
663
|
14 |
|
$finderDef->replaceArgument(0, $indexRef); |
664
|
14 |
|
$finderDef->replaceArgument(1, new Reference($elasticaToModelId)); |
665
|
14 |
|
$container->setDefinition($finderId, $finderDef); |
666
|
|
|
} |
667
|
|
|
|
668
|
14 |
|
$arguments = [$indexName, new Reference($finderId)]; |
669
|
14 |
|
if (isset($typeConfig['repository'])) { |
670
|
4 |
|
$arguments[] = $typeConfig['repository']; |
671
|
|
|
} |
672
|
|
|
|
673
|
14 |
|
$container->getDefinition('fos_elastica.repository_manager') |
674
|
14 |
|
->addMethodCall('addType', $arguments); |
675
|
|
|
|
676
|
14 |
|
$managerId = sprintf('fos_elastica.manager.%s', $typeConfig['driver']); |
677
|
14 |
|
$container->getDefinition($managerId) |
678
|
14 |
|
->addMethodCall('addEntity', [$typeConfig['model'], $indexName]); |
679
|
|
|
|
680
|
14 |
|
return $finderId; |
681
|
|
|
} |
682
|
|
|
|
683
|
|
|
/** |
684
|
|
|
* Loads the index manager. |
685
|
|
|
**/ |
686
|
20 |
View Code Duplication |
private function loadIndexManager(ContainerBuilder $container): void |
687
|
|
|
{ |
688
|
|
|
$indexRefs = array_map(function ($index) { |
689
|
20 |
|
return $index['reference']; |
690
|
20 |
|
}, $this->indexConfigs); |
691
|
|
|
|
692
|
20 |
|
$managerDef = $container->getDefinition('fos_elastica.index_manager'); |
693
|
20 |
|
$managerDef->replaceArgument(0, $indexRefs); |
694
|
20 |
|
} |
695
|
|
|
|
696
|
|
|
/** |
697
|
|
|
* Load index template manager |
698
|
|
|
*/ |
699
|
20 |
View Code Duplication |
private function loadIndexTemplateManager(ContainerBuilder $container): void |
700
|
|
|
{ |
701
|
|
|
$indexTemplateRefs = array_map(function ($index) { |
702
|
6 |
|
return $index['reference']; |
703
|
20 |
|
}, $this->indexTemplateConfigs); |
704
|
|
|
|
705
|
20 |
|
$managerDef = $container->getDefinition('fos_elastica.index_template_manager'); |
706
|
20 |
|
$managerDef->replaceArgument(0, $indexTemplateRefs); |
707
|
20 |
|
} |
708
|
|
|
|
709
|
|
|
/** |
710
|
|
|
* Makes sure a specific driver has been loaded. |
711
|
|
|
*/ |
712
|
14 |
|
private function loadDriver(ContainerBuilder $container, string $driver): void |
713
|
|
|
{ |
714
|
14 |
|
if (in_array($driver, $this->loadedDrivers, true)) { |
715
|
5 |
|
return; |
716
|
|
|
} |
717
|
|
|
|
718
|
14 |
|
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
719
|
14 |
|
$loader->load($driver.'.xml'); |
720
|
14 |
|
$this->loadedDrivers[] = $driver; |
721
|
14 |
|
} |
722
|
|
|
|
723
|
|
|
/** |
724
|
|
|
* Loads and configures the serializer prototype. |
725
|
|
|
*/ |
726
|
1 |
|
private function loadSerializer(array $config, ContainerBuilder $container): void |
727
|
|
|
{ |
728
|
1 |
|
$container->setAlias('fos_elastica.serializer', $config['serializer']); |
729
|
|
|
|
730
|
1 |
|
$serializer = $container->getDefinition('fos_elastica.serializer_callback_prototype'); |
731
|
1 |
|
$serializer->setClass($config['callback_class']); |
732
|
|
|
|
733
|
1 |
|
if (is_subclass_of($config['callback_class'], ContainerAwareInterface::class)) { |
|
|
|
|
734
|
|
|
$serializer->addMethodCall('setContainer', [new Reference('service_container')]); |
735
|
|
|
} |
736
|
1 |
|
} |
737
|
|
|
|
738
|
|
|
/** |
739
|
|
|
* Creates a default manager alias for defined default manager or the first loaded driver. |
740
|
|
|
*/ |
741
|
20 |
|
private function createDefaultManagerAlias(string $defaultManager, ContainerBuilder $container): void |
742
|
|
|
{ |
743
|
20 |
|
if (0 == count($this->loadedDrivers)) { |
744
|
6 |
|
return; |
745
|
|
|
} |
746
|
|
|
|
747
|
14 |
|
if (count($this->loadedDrivers) > 1 |
748
|
14 |
|
&& in_array($defaultManager, $this->loadedDrivers, true) |
749
|
|
|
) { |
750
|
|
|
$defaultManagerService = $defaultManager; |
751
|
|
|
} else { |
752
|
14 |
|
$defaultManagerService = $this->loadedDrivers[0]; |
753
|
|
|
} |
754
|
|
|
|
755
|
14 |
|
$container->setAlias('fos_elastica.manager', sprintf('fos_elastica.manager.%s', $defaultManagerService)); |
756
|
14 |
|
$container->getAlias('fos_elastica.manager')->setPublic(true); |
757
|
14 |
|
$container->setAlias(RepositoryManagerInterface::class, 'fos_elastica.manager'); |
758
|
14 |
|
$container->getAlias(RepositoryManagerInterface::class)->setPublic(false); |
759
|
14 |
|
} |
760
|
|
|
|
761
|
|
|
/** |
762
|
|
|
* Returns a reference to a client given its configured name. |
763
|
|
|
* |
764
|
|
|
* @throws \InvalidArgumentException |
765
|
|
|
*/ |
766
|
7 |
|
private function getClient(string $clientName): Reference |
767
|
|
|
{ |
768
|
7 |
|
if (!array_key_exists($clientName, $this->clients)) { |
769
|
|
|
throw new \InvalidArgumentException(sprintf('The elastica client with name "%s" is not defined', $clientName)); |
770
|
|
|
} |
771
|
|
|
|
772
|
7 |
|
return $this->clients[$clientName]['reference']; |
773
|
|
|
} |
774
|
|
|
} |
775
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.