1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nimble\ElasticBundle\DependencyInjection; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException; |
6
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
7
|
|
|
use Symfony\Component\Config\FileLocator; |
8
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
9
|
|
|
use Symfony\Component\DependencyInjection\DefinitionDecorator; |
10
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
11
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
12
|
|
|
use Symfony\Component\DependencyInjection\Loader; |
13
|
|
|
|
14
|
|
|
class NimbleElasticExtension extends Extension |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* {@inheritdoc} |
18
|
|
|
*/ |
19
|
|
|
public function load(array $configs, ContainerBuilder $container) |
20
|
|
|
{ |
21
|
|
|
$configuration = new Configuration(); |
22
|
|
|
$config = $this->processConfiguration($configuration, $configs); |
23
|
|
|
|
24
|
|
|
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config')); |
25
|
|
|
$loader->load('services.yml'); |
26
|
|
|
|
27
|
|
|
$this->processClients($config['default_client'], $config['clients'], $container); |
28
|
|
|
$this->processIndexes($config['indexes'], $container); |
29
|
|
|
$this->processListeners($config['synchronization_listeners'], $container); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param string $defaultClient |
34
|
|
|
* @param array $clientsConfig |
35
|
|
|
* @param ContainerBuilder $container |
36
|
|
|
*/ |
37
|
|
|
protected function processClients($defaultClient, array $clientsConfig, ContainerBuilder $container) |
38
|
|
|
{ |
39
|
|
|
if (!isset($clientsConfig[$defaultClient])) { |
40
|
|
|
throw new InvalidConfigurationException( |
41
|
|
|
sprintf('Default client "%s" must be configured in "nimble_elastic.clients".', $defaultClient) |
42
|
|
|
); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
foreach ($clientsConfig as $clientName => $clientConfig) { |
46
|
|
|
$clientServiceId = sprintf('nimble_elastic.client.%s', $clientName); |
47
|
|
|
|
48
|
|
|
$clientDefinition = new DefinitionDecorator('nimble_elastic.client_prototype'); |
49
|
|
|
$clientDefinition->setClass('Elasticsearch\Client'); |
50
|
|
|
$clientDefinition->setArguments([ |
51
|
|
|
$clientConfig['hosts'], |
52
|
|
|
$clientConfig['logging']['enabled'] ? new Reference($clientConfig['logging']['service']) : null, |
53
|
|
|
]); |
54
|
|
|
|
55
|
|
|
$clientDefinition->addTag('monolog.logger', ['channel' => 'elasticsearch']); |
56
|
|
|
|
57
|
|
|
$container->setDefinition($clientServiceId, $clientDefinition); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$container->setAlias('nimble_elastic.client', sprintf('nimble_elastic.client.%s', $defaultClient)); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param array $typesConfig |
65
|
|
|
* @return array |
66
|
|
|
*/ |
67
|
|
|
protected function buildTypesSettings(array $typesConfig) |
68
|
|
|
{ |
69
|
|
|
$types = []; |
70
|
|
|
|
71
|
|
|
foreach ($typesConfig as $typeName => $typeConfig) { |
72
|
|
|
$typeMappings = $typeConfig['mappings']; |
73
|
|
|
$types[$typeName] = []; |
74
|
|
|
|
75
|
|
|
if (!empty($typeMappings)) { |
76
|
|
|
$types[$typeName]['mappings'] = $typeMappings; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $types; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param array $indexesConfig |
85
|
|
|
* @param ContainerBuilder $container |
86
|
|
|
*/ |
87
|
|
|
protected function processIndexes(array $indexesConfig, ContainerBuilder $container) |
88
|
|
|
{ |
89
|
|
|
foreach ($indexesConfig as $indexId => $indexConfig) { |
90
|
|
|
$indexServiceId = sprintf('nimble_elastic.index.%s', $indexId); |
91
|
|
|
$clientServiceId = 'nimble_elastic.client'; |
92
|
|
|
|
93
|
|
|
$typesConfig = $indexConfig['types']; |
94
|
|
|
|
95
|
|
|
if (null !== $indexConfig['client']) { |
96
|
|
|
$clientServiceId = sprintf('nimble_elastic.client.%s', $indexConfig['client']); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$indexDefinition = new Definition('Nimble\ElasticBundle\Index\Index', [ |
100
|
|
|
$indexId, |
101
|
|
|
$indexConfig['name'], |
102
|
|
|
new Reference($clientServiceId), |
103
|
|
|
$indexConfig['settings'], |
104
|
|
|
$this->buildTypesSettings($typesConfig) |
105
|
|
|
]); |
106
|
|
|
|
107
|
|
|
$indexDefinition->addTag('nimble_elastic.index'); |
108
|
|
|
|
109
|
|
|
$container->setDefinition($indexServiceId, $indexDefinition); |
110
|
|
|
|
111
|
|
|
$this->processTypes($typesConfig, $indexId, $indexServiceId, $container); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param array $typesConfig |
117
|
|
|
* @param string $indexId |
118
|
|
|
* @param string $indexServiceId |
119
|
|
|
* @param ContainerBuilder $container |
120
|
|
|
*/ |
121
|
|
|
protected function processTypes(array $typesConfig, $indexId, $indexServiceId, ContainerBuilder $container) |
122
|
|
|
{ |
123
|
|
|
$populatorManagerServiceDefinition = $container->getDefinition('nimble_elastic.populator_manager'); |
124
|
|
|
|
125
|
|
|
foreach ($typesConfig as $typeName => $typeConfig) { |
126
|
|
|
$typeServiceId = sprintf('nimble_elastic.type.%s.%s', $indexId, $typeName); |
127
|
|
|
|
128
|
|
|
$typeServiceDefinition = new Definition('Nimble\ElasticBundle\Type\Type', [$typeName]); |
129
|
|
|
$typeServiceDefinition->setFactory([new Reference($indexServiceId), 'getType']); |
130
|
|
|
|
131
|
|
|
$container->setDefinition($typeServiceId, $typeServiceDefinition); |
132
|
|
|
|
133
|
|
|
$this->processEntities($typeConfig['entities'], $indexId, $typeServiceId, $typeName, $container); |
134
|
|
|
|
135
|
|
|
/* TODO: Break this into function. Allow to register fetchers via tags. */ |
136
|
|
|
if (isset($typeConfig['fetcher'])) { |
137
|
|
|
if (count($typeConfig['fetcher']) > 1) { |
138
|
|
|
throw new InvalidConfigurationException(sprintf('Type "%s.%s" must have only one fetcher defined.')); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
$fetcherServiceId = null; |
142
|
|
|
|
143
|
|
|
if (isset($typeConfig['fetcher']['service'])) { |
144
|
|
|
$fetcherServiceId = $typeConfig['fetcher']['service']; |
145
|
|
|
} elseif (isset($typeConfig['fetcher']['doctrine_orm_entity'])) { |
146
|
|
|
$fetcherServiceId = sprintf("nimble.elastic.fetcher.%s.%s", $indexId, $typeName); |
147
|
|
|
|
148
|
|
|
$fetcherServiceDefinition = new Definition( |
149
|
|
|
'Nimble\ElasticBundle\Doctrine\ORM\Populator\DoctrineORMPopulationFetcher', |
150
|
|
|
[ |
151
|
|
|
new Reference('doctrine.orm.entity_manager'), |
152
|
|
|
$typeConfig['fetcher']['doctrine_orm_entity'] |
153
|
|
|
] |
154
|
|
|
); |
155
|
|
|
|
156
|
|
|
$container->setDefinition($fetcherServiceId, $fetcherServiceDefinition); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
if ($fetcherServiceId) { |
160
|
|
|
$populatorManagerServiceDefinition->addMethodCall('registerFetcher', [ |
161
|
|
|
new Reference($fetcherServiceId), |
162
|
|
|
$indexId, |
163
|
|
|
$typeName |
164
|
|
|
]); |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @param array $entitiesConfig |
172
|
|
|
* @param string $indexId |
173
|
|
|
* @param string $typeServiceId |
174
|
|
|
* @param string $typeName |
175
|
|
|
* @param ContainerBuilder $container |
176
|
|
|
*/ |
177
|
|
|
protected function processEntities(array $entitiesConfig, $indexId, $typeServiceId, $typeName, ContainerBuilder $container) |
178
|
|
|
{ |
179
|
|
|
$transformerManagerDefinition = $container->getDefinition('nimble_elastic.transformer_manager'); |
180
|
|
|
|
181
|
|
|
foreach ($entitiesConfig as $entityClass => $entityConfig) { |
182
|
|
|
$synchronizerServiceId = sprintf('nimble_elastic.synchronizer.%s.%s.%s', |
183
|
|
|
$indexId, |
184
|
|
|
$typeName, |
185
|
|
|
$container->camelize($entityClass) |
186
|
|
|
); |
187
|
|
|
|
188
|
|
|
$synchronizerDefinition = new Definition('Nimble\ElasticBundle\Synchronizer\Synchronizer', [ |
189
|
|
|
$entityClass, |
190
|
|
|
new Reference($typeServiceId), |
191
|
|
|
$entityConfig['on_create'], |
192
|
|
|
$entityConfig['on_update'], |
193
|
|
|
$entityConfig['on_delete'], |
194
|
|
|
new Reference('nimble_elastic.transformer_manager'), |
195
|
|
|
$entityConfig['logger_service'] ? new Reference($entityConfig['logger_service']) : null, |
196
|
|
|
]); |
197
|
|
|
|
198
|
|
|
$synchronizerDefinition->addTag('nimble_elastic.synchronizer'); |
199
|
|
|
|
200
|
|
|
$container->setDefinition($synchronizerServiceId, $synchronizerDefinition); |
201
|
|
|
|
202
|
|
|
/* Transformer service is optional because it can be registered via tags. */ |
203
|
|
|
if (null !== $entityConfig['transformer_service']) { |
204
|
|
|
$transformerManagerDefinition->addMethodCall('registerTransformer', [ |
205
|
|
|
new Reference($entityConfig['transformer_service']), |
206
|
|
|
$indexId, |
207
|
|
|
$typeName |
208
|
|
|
]); |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* @param array $listeners |
215
|
|
|
* @param ContainerBuilder $container |
216
|
|
|
*/ |
217
|
|
|
protected function processListeners(array $listeners, ContainerBuilder $container) |
218
|
|
|
{ |
219
|
|
|
/* Enables doctrine orm event subscriber by tagging it. */ |
220
|
|
|
if ($listeners['doctrine_orm']['enabled']) { |
221
|
|
|
$listenerDefinition = $container->getDefinition('nimble_elastic.doctrine.orm.listener'); |
222
|
|
|
$listenerDefinition->addTag('doctrine.event_subscriber', [ |
223
|
|
|
'connection' => $listeners['doctrine_orm']['connection'], |
224
|
|
|
]); |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
|