1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the ONGR package. |
5
|
|
|
* |
6
|
|
|
* (c) NFQ Technologies UAB <[email protected]> |
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 ONGR\ElasticsearchBundle\DependencyInjection\Compiler; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException; |
15
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
16
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
17
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
18
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Compiles elastic search data. |
22
|
|
|
*/ |
23
|
|
|
class MappingPass implements CompilerPassInterface |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* {@inheritdoc} |
27
|
|
|
*/ |
28
|
|
|
public function process(ContainerBuilder $container) |
29
|
|
|
{ |
30
|
|
|
$analysis = $container->getParameter('es.analysis'); |
31
|
|
|
$connections = $container->getParameter('es.connections'); |
32
|
|
|
$managers = $container->getParameter('es.managers'); |
33
|
|
|
|
34
|
|
|
$collector = $container->get('es.metadata_collector'); |
35
|
|
|
|
36
|
|
|
foreach ($managers as $managerName => $manager) { |
37
|
|
|
if (!empty($manager['index'])) { |
38
|
|
|
$connection = $manager['index']; |
39
|
|
|
} else { |
40
|
|
|
if (!isset($manager['connection']) || !isset($connections[$manager['connection']])) { |
41
|
|
|
throw new InvalidConfigurationException( |
42
|
|
|
'There is an error in the ES connection configuration of the manager: ' . $managerName |
43
|
|
|
); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$connection = $connections[$manager['connection']]; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$managerName = strtolower($managerName); |
50
|
|
|
|
51
|
|
|
$managerDefinition = new Definition( |
52
|
|
|
'ONGR\ElasticsearchBundle\Service\Manager', |
53
|
|
|
[ |
54
|
|
|
$managerName, |
55
|
|
|
$connection, |
56
|
|
|
$analysis, |
57
|
|
|
$manager, |
58
|
|
|
] |
59
|
|
|
); |
60
|
|
|
$managerDefinition->setFactory( |
61
|
|
|
[ |
62
|
|
|
new Reference('es.manager_factory'), |
63
|
|
|
'createManager', |
64
|
|
|
] |
65
|
|
|
); |
66
|
|
|
|
67
|
|
|
$container->setDefinition(sprintf('es.manager.%s', $managerName), $managerDefinition); |
68
|
|
|
|
69
|
|
|
// Make es.manager.default as es.manager service. |
70
|
|
|
if ($managerName === 'default') { |
71
|
|
|
$container->setAlias('es.manager', 'es.manager.default'); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$mappings = $collector->getMappings($manager['mappings']); |
75
|
|
|
|
76
|
|
|
// Building repository services. |
77
|
|
|
foreach ($mappings as $repositoryType => $repositoryDetails) { |
78
|
|
|
$repositoryDefinition = new Definition( |
79
|
|
|
'ONGR\ElasticsearchBundle\Service\Repository', |
80
|
|
|
[$repositoryDetails['namespace']] |
81
|
|
|
); |
82
|
|
|
$repositoryDefinition->setFactory( |
83
|
|
|
[ |
84
|
|
|
new Reference(sprintf('es.manager.%s', $managerName)), |
85
|
|
|
'getRepository', |
86
|
|
|
] |
87
|
|
|
); |
88
|
|
|
|
89
|
|
|
$repositoryId = sprintf('es.manager.%s.%s', $managerName, $repositoryType); |
90
|
|
|
$container->setDefinition($repositoryId, $repositoryDefinition); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|