1 | <?php |
||
18 | class Configuration implements ConfigurationInterface |
||
19 | { |
||
20 | private const SUPPORTED_DRIVERS = ['orm', 'mongodb', 'phpcr']; |
||
21 | |||
22 | /** |
||
23 | * If the kernel is running in debug mode. |
||
24 | * |
||
25 | * @var bool |
||
26 | */ |
||
27 | private $debug; |
||
28 | |||
29 | 34 | public function __construct($debug) |
|
33 | |||
34 | /** |
||
35 | * Generates the configuration tree. |
||
36 | * |
||
37 | * @return TreeBuilder |
||
38 | */ |
||
39 | 34 | public function getConfigTreeBuilder() |
|
69 | |||
70 | /** |
||
71 | * Returns the array node used for "dynamic_templates". |
||
72 | */ |
||
73 | 34 | private function getDynamicTemplateNode() |
|
99 | |||
100 | /** |
||
101 | * Returns the array node used for "types". |
||
102 | */ |
||
103 | 34 | private function getTypesNode() |
|
104 | { |
||
105 | 34 | $node = $this->createTreeBuilderNode('types'); |
|
106 | |||
107 | $node |
||
108 | 34 | ->useAttributeAsKey('name') |
|
109 | 34 | ->prototype('array') |
|
110 | 34 | ->treatNullLike([]) |
|
111 | 34 | ->beforeNormalization() |
|
112 | 34 | ->ifNull() |
|
113 | 34 | ->thenEmptyArray() |
|
114 | 34 | ->end() |
|
115 | // Support multiple dynamic_template formats to match the old bundle style |
||
116 | // and the way ElasticSearch expects them |
||
117 | 34 | ->beforeNormalization() |
|
118 | ->ifTrue(function ($v) { |
||
119 | 24 | return isset($v['dynamic_templates']); |
|
120 | 34 | }) |
|
121 | ->then(function ($v) { |
||
122 | 5 | $dt = []; |
|
123 | 5 | foreach ($v['dynamic_templates'] as $key => $type) { |
|
124 | 5 | if (is_int($key)) { |
|
125 | 5 | $dt[] = $type; |
|
126 | } else { |
||
127 | $dt[][$key] = $type; |
||
128 | } |
||
129 | } |
||
130 | |||
131 | 5 | $v['dynamic_templates'] = $dt; |
|
132 | |||
133 | 5 | return $v; |
|
134 | 34 | }) |
|
135 | 34 | ->end() |
|
136 | 34 | ->children() |
|
137 | 34 | ->booleanNode('date_detection')->end() |
|
138 | 34 | ->arrayNode('dynamic_date_formats')->prototype('scalar')->end()->end() |
|
139 | 34 | ->scalarNode('analyzer')->end() |
|
140 | 34 | ->booleanNode('numeric_detection')->end() |
|
141 | 34 | ->scalarNode('dynamic')->end() |
|
142 | 34 | ->end() |
|
143 | 34 | ->append($this->getIdNode()) |
|
144 | 34 | ->append($this->getPropertiesNode()) |
|
145 | 34 | ->append($this->getDynamicTemplateNode()) |
|
146 | 34 | ->append($this->getSourceNode()) |
|
147 | 34 | ->append($this->getRoutingNode()) |
|
148 | 34 | ->append($this->getAllNode()) |
|
149 | 34 | ->end() |
|
150 | ; |
||
151 | |||
152 | 34 | return $node; |
|
153 | } |
||
154 | |||
155 | /** |
||
156 | * Returns the array node used for "properties". |
||
157 | */ |
||
158 | 34 | private function getPropertiesNode() |
|
159 | { |
||
160 | 34 | $node = $this->createTreeBuilderNode('properties'); |
|
161 | |||
162 | $node |
||
163 | 34 | ->useAttributeAsKey('name') |
|
164 | 34 | ->prototype('variable') |
|
165 | 34 | ->treatNullLike([]); |
|
166 | |||
167 | 34 | return $node; |
|
168 | } |
||
169 | |||
170 | /** |
||
171 | * Returns the array node used for "_id". |
||
172 | */ |
||
173 | 34 | private function getIdNode() |
|
174 | { |
||
175 | 34 | $node = $this->createTreeBuilderNode('_id'); |
|
176 | |||
177 | $node |
||
178 | 34 | ->children() |
|
179 | 34 | ->scalarNode('path')->end() |
|
180 | 34 | ->end() |
|
181 | ; |
||
182 | |||
183 | 34 | return $node; |
|
184 | } |
||
185 | |||
186 | /** |
||
187 | * Returns the array node used for "_source". |
||
188 | */ |
||
189 | 34 | private function getSourceNode() |
|
190 | { |
||
191 | 34 | $node = $this->createTreeBuilderNode('_source'); |
|
192 | |||
193 | $node |
||
194 | 34 | ->children() |
|
195 | 34 | ->arrayNode('excludes') |
|
196 | 34 | ->useAttributeAsKey('name') |
|
197 | 34 | ->prototype('scalar')->end() |
|
198 | 34 | ->end() |
|
199 | 34 | ->arrayNode('includes') |
|
200 | 34 | ->useAttributeAsKey('name') |
|
201 | 34 | ->prototype('scalar')->end() |
|
202 | 34 | ->end() |
|
203 | 34 | ->scalarNode('compress')->end() |
|
204 | 34 | ->scalarNode('compress_threshold')->end() |
|
205 | 34 | ->scalarNode('enabled')->defaultTrue()->end() |
|
206 | 34 | ->end() |
|
207 | ; |
||
208 | |||
209 | 34 | return $node; |
|
210 | } |
||
211 | |||
212 | /** |
||
213 | * Returns the array node used for "_routing". |
||
214 | */ |
||
215 | 34 | private function getRoutingNode() |
|
216 | { |
||
217 | 34 | $node = $this->createTreeBuilderNode('_routing'); |
|
218 | |||
219 | $node |
||
220 | 34 | ->children() |
|
221 | 34 | ->scalarNode('required')->end() |
|
222 | 34 | ->scalarNode('path')->end() |
|
223 | 34 | ->end() |
|
224 | ; |
||
225 | |||
226 | 34 | return $node; |
|
227 | } |
||
228 | |||
229 | /** |
||
230 | * Returns the array node used for "_all". |
||
231 | */ |
||
232 | 34 | private function getAllNode() |
|
233 | { |
||
234 | 34 | $node = $this->createTreeBuilderNode('_all'); |
|
235 | |||
236 | $node |
||
237 | 34 | ->children() |
|
238 | 34 | ->scalarNode('enabled')->defaultValue(true)->end() |
|
239 | 34 | ->scalarNode('analyzer')->end() |
|
240 | 34 | ->end() |
|
241 | ; |
||
242 | |||
243 | 34 | return $node; |
|
244 | } |
||
245 | |||
246 | /** |
||
247 | * @return ArrayNodeDefinition|\Symfony\Component\Config\Definition\Builder\NodeDefinition |
||
248 | */ |
||
249 | 34 | private function getPersistenceNode() |
|
250 | { |
||
251 | 34 | $node = $this->createTreeBuilderNode('persistence'); |
|
252 | |||
253 | $node |
||
254 | 34 | ->validate() |
|
255 | ->ifTrue(function ($v) { |
||
256 | 16 | return isset($v['driver']) && 'orm' !== $v['driver'] && !empty($v['elastica_to_model_transformer']['hints']); |
|
257 | 34 | }) |
|
258 | 34 | ->thenInvalid('Hints are only supported by the "orm" driver') |
|
259 | 34 | ->end() |
|
260 | 34 | ->children() |
|
261 | 34 | ->scalarNode('driver') |
|
262 | 34 | ->defaultValue('orm') |
|
263 | 34 | ->validate() |
|
264 | 34 | ->ifNotInArray(self::SUPPORTED_DRIVERS) |
|
265 | 34 | ->thenInvalid('The driver %s is not supported. Please choose one of '.json_encode(self::SUPPORTED_DRIVERS)) |
|
266 | 34 | ->end() |
|
267 | 34 | ->end() |
|
268 | 34 | ->scalarNode('model')->defaultValue(null)->end() |
|
269 | 34 | ->scalarNode('repository')->end() |
|
270 | 34 | ->scalarNode('identifier')->defaultValue('id')->end() |
|
271 | 34 | ->arrayNode('provider') |
|
272 | 34 | ->addDefaultsIfNotSet() |
|
273 | 34 | ->children() |
|
274 | 34 | ->scalarNode('batch_size')->defaultValue(100)->end() |
|
275 | 34 | ->scalarNode('clear_object_manager')->defaultTrue()->end() |
|
276 | 34 | ->scalarNode('debug_logging') |
|
277 | 34 | ->defaultValue($this->debug) |
|
278 | 34 | ->treatNullLike(true) |
|
279 | 34 | ->end() |
|
280 | 34 | ->scalarNode('query_builder_method')->defaultValue('createQueryBuilder')->end() |
|
281 | 34 | ->scalarNode('service')->end() |
|
282 | 34 | ->end() |
|
283 | 34 | ->end() |
|
284 | 34 | ->arrayNode('listener') |
|
285 | 34 | ->addDefaultsIfNotSet() |
|
286 | 34 | ->children() |
|
287 | 34 | ->booleanNode('enabled')->defaultTrue()->end() |
|
288 | 34 | ->scalarNode('insert')->defaultTrue()->end() |
|
289 | 34 | ->scalarNode('update')->defaultTrue()->end() |
|
290 | 34 | ->scalarNode('delete')->defaultTrue()->end() |
|
291 | 34 | ->scalarNode('flush')->defaultTrue()->end() |
|
292 | 34 | ->booleanNode('defer')->defaultFalse()->end() |
|
293 | 34 | ->scalarNode('logger') |
|
294 | 34 | ->defaultFalse() |
|
295 | 34 | ->treatNullLike('fos_elastica.logger') |
|
296 | 34 | ->treatTrueLike('fos_elastica.logger') |
|
297 | 34 | ->end() |
|
298 | 34 | ->scalarNode('service')->end() |
|
299 | 34 | ->end() |
|
300 | 34 | ->end() |
|
301 | 34 | ->arrayNode('finder') |
|
302 | 34 | ->addDefaultsIfNotSet() |
|
303 | 34 | ->children() |
|
304 | 34 | ->scalarNode('service')->end() |
|
305 | 34 | ->end() |
|
306 | 34 | ->end() |
|
307 | 34 | ->arrayNode('elastica_to_model_transformer') |
|
308 | 34 | ->addDefaultsIfNotSet() |
|
309 | 34 | ->children() |
|
310 | 34 | ->arrayNode('hints') |
|
311 | 34 | ->prototype('array') |
|
312 | 34 | ->children() |
|
313 | 34 | ->scalarNode('name')->end() |
|
314 | 34 | ->scalarNode('value')->end() |
|
315 | 34 | ->end() |
|
316 | 34 | ->end() |
|
317 | 34 | ->end() |
|
318 | 34 | ->booleanNode('hydrate')->defaultTrue()->end() |
|
319 | 34 | ->booleanNode('ignore_missing') |
|
320 | 34 | ->defaultFalse() |
|
321 | 34 | ->info('Silently ignore results returned from Elasticsearch without corresponding persistent object.') |
|
322 | 34 | ->end() |
|
323 | 34 | ->scalarNode('query_builder_method')->defaultValue('createQueryBuilder')->end() |
|
324 | 34 | ->scalarNode('service')->end() |
|
325 | 34 | ->end() |
|
326 | 34 | ->end() |
|
327 | 34 | ->arrayNode('model_to_elastica_transformer') |
|
328 | 34 | ->addDefaultsIfNotSet() |
|
329 | 34 | ->children() |
|
330 | 34 | ->scalarNode('service')->end() |
|
331 | 34 | ->end() |
|
332 | 34 | ->end() |
|
333 | 34 | ->arrayNode('persister') |
|
334 | 34 | ->addDefaultsIfNotSet() |
|
335 | 34 | ->children() |
|
336 | 34 | ->enumNode('refresh') |
|
337 | 34 | ->treatTrueLike('true') |
|
338 | 34 | ->treatFalseLike('false') |
|
339 | 34 | ->values(['true', 'wait_for', 'false']) |
|
340 | 34 | ->end() |
|
341 | 34 | ->scalarNode('service')->end() |
|
342 | 34 | ->end() |
|
343 | 34 | ->end() |
|
344 | 34 | ->end(); |
|
345 | |||
346 | 34 | return $node; |
|
347 | } |
||
348 | |||
349 | /** |
||
350 | * @return ArrayNodeDefinition|\Symfony\Component\Config\Definition\Builder\NodeDefinition |
||
351 | */ |
||
352 | 34 | private function getSerializerNode() |
|
353 | { |
||
354 | 34 | $node = $this->createTreeBuilderNode('serializer'); |
|
355 | |||
356 | $node |
||
357 | 34 | ->addDefaultsIfNotSet() |
|
358 | 34 | ->children() |
|
359 | 34 | ->arrayNode('groups') |
|
360 | 34 | ->treatNullLike([]) |
|
361 | 34 | ->prototype('scalar')->end() |
|
362 | 34 | ->end() |
|
363 | 34 | ->scalarNode('version')->end() |
|
364 | 34 | ->booleanNode('serialize_null') |
|
365 | 34 | ->defaultFalse() |
|
366 | 34 | ->end() |
|
367 | 34 | ->end(); |
|
368 | |||
369 | 34 | return $node; |
|
370 | } |
||
371 | |||
372 | /** |
||
373 | * Adds the configuration for the "clients" key. |
||
374 | */ |
||
375 | 34 | private function addClientsSection(ArrayNodeDefinition $rootNode) |
|
376 | { |
||
377 | $rootNode |
||
378 | 34 | ->fixXmlConfig('client') |
|
379 | 34 | ->children() |
|
380 | 34 | ->arrayNode('clients') |
|
381 | 34 | ->useAttributeAsKey('id') |
|
382 | 34 | ->prototype('array') |
|
383 | 34 | ->performNoDeepMerging() |
|
384 | // Elastica names its properties with camel case, support both |
||
385 | 34 | ->beforeNormalization() |
|
386 | ->ifTrue(function ($v) { |
||
387 | 32 | return isset($v['connection_strategy']); |
|
388 | 34 | }) |
|
389 | ->then(function ($v) { |
||
390 | 5 | $v['connectionStrategy'] = $v['connection_strategy']; |
|
391 | 5 | unset($v['connection_strategy']); |
|
392 | |||
393 | 5 | return $v; |
|
394 | 34 | }) |
|
395 | 34 | ->end() |
|
396 | // If there is no connections array key defined, assume a single connection. |
||
397 | 34 | ->beforeNormalization() |
|
398 | ->ifTrue(function ($v) { |
||
399 | 32 | return is_array($v) && !array_key_exists('connections', $v); |
|
400 | 34 | }) |
|
401 | ->then(function ($v) { |
||
402 | return [ |
||
403 | 32 | 'connections' => [$v], |
|
404 | ]; |
||
405 | 34 | }) |
|
406 | 34 | ->end() |
|
407 | 34 | ->children() |
|
408 | 34 | ->arrayNode('connections') |
|
409 | 34 | ->requiresAtLeastOneElement() |
|
410 | 34 | ->prototype('array') |
|
411 | 34 | ->fixXmlConfig('header') |
|
412 | 34 | ->children() |
|
413 | 34 | ->scalarNode('url') |
|
414 | 34 | ->validate() |
|
415 | ->ifTrue(function ($url) { |
||
416 | 18 | return $url && '/' !== substr($url, -1); |
|
417 | 34 | }) |
|
418 | ->then(function ($url) { |
||
419 | 18 | return $url.'/'; |
|
420 | 34 | }) |
|
421 | 34 | ->end() |
|
422 | 34 | ->end() |
|
423 | 34 | ->scalarNode('username')->end() |
|
424 | 34 | ->scalarNode('password')->end() |
|
425 | 34 | ->scalarNode('host')->end() |
|
426 | 34 | ->scalarNode('port')->end() |
|
427 | 34 | ->scalarNode('proxy')->end() |
|
428 | 34 | ->arrayNode('http_error_codes') |
|
429 | 34 | ->beforeNormalization() |
|
430 | ->ifTrue(function ($v) { return !is_array($v); }) |
||
431 | ->then(function ($v) { return array($v); }) |
||
432 | 34 | ->end() |
|
433 | 34 | ->requiresAtLeastOneElement() |
|
434 | 34 | ->defaultValue([400, 403, 404]) |
|
435 | 34 | ->prototype('scalar')->end() |
|
436 | 34 | ->end() |
|
437 | 34 | ->scalarNode('aws_access_key_id')->end() |
|
438 | 34 | ->scalarNode('aws_secret_access_key')->end() |
|
439 | 34 | ->scalarNode('aws_region')->end() |
|
440 | 34 | ->scalarNode('aws_session_token')->end() |
|
441 | 34 | ->booleanNode('ssl')->defaultValue(false)->end() |
|
442 | 34 | ->scalarNode('logger') |
|
443 | 34 | ->defaultValue($this->debug ? 'fos_elastica.logger' : false) |
|
444 | 34 | ->treatNullLike('fos_elastica.logger') |
|
445 | 34 | ->treatTrueLike('fos_elastica.logger') |
|
446 | 34 | ->end() |
|
447 | 34 | ->booleanNode('compression')->defaultValue(false)->end() |
|
448 | 34 | ->arrayNode('headers') |
|
449 | 34 | ->normalizeKeys(false) |
|
450 | 34 | ->useAttributeAsKey('name') |
|
451 | 34 | ->prototype('scalar')->end() |
|
452 | 34 | ->end() |
|
453 | 34 | ->arrayNode('curl') |
|
454 | 34 | ->useAttributeAsKey(CURLOPT_SSL_VERIFYPEER) |
|
455 | 34 | ->prototype('boolean')->end() |
|
456 | 34 | ->end() |
|
457 | 34 | ->scalarNode('transport')->end() |
|
458 | 34 | ->scalarNode('timeout')->end() |
|
459 | 34 | ->scalarNode('connectTimeout')->end() |
|
460 | 34 | ->scalarNode('retryOnConflict') |
|
461 | 34 | ->defaultValue(0) |
|
462 | 34 | ->end() |
|
463 | 34 | ->booleanNode('persistent')->defaultValue(true)->end() |
|
464 | 34 | ->end() |
|
465 | 34 | ->end() |
|
466 | 34 | ->end() |
|
467 | 34 | ->scalarNode('timeout')->end() |
|
468 | 34 | ->scalarNode('connectTimeout')->end() |
|
469 | 34 | ->scalarNode('headers')->end() |
|
470 | 34 | ->scalarNode('connectionStrategy')->defaultValue('Simple')->end() |
|
471 | 34 | ->end() |
|
472 | 34 | ->end() |
|
473 | 34 | ->end() |
|
474 | 34 | ->end() |
|
475 | ; |
||
476 | 34 | } |
|
477 | |||
478 | /** |
||
479 | * Adds the configuration for the "indexes" key. |
||
480 | */ |
||
481 | 34 | private function addIndexesSection(ArrayNodeDefinition $rootNode) |
|
482 | { |
||
483 | $rootNode |
||
484 | 34 | ->fixXmlConfig('index') |
|
485 | 34 | ->children() |
|
486 | 34 | ->arrayNode('indexes') |
|
487 | 34 | ->useAttributeAsKey('name') |
|
488 | 34 | ->prototype('array') |
|
489 | 34 | ->children() |
|
490 | 34 | ->scalarNode('index_name') |
|
491 | 34 | ->info('Defaults to the name of the index, but can be modified if the index name is different in ElasticSearch') |
|
492 | 34 | ->end() |
|
493 | 34 | ->variableNode('indexable_callback')->end() |
|
494 | 34 | ->booleanNode('use_alias')->defaultValue(false)->end() |
|
495 | 34 | ->scalarNode('client')->end() |
|
496 | 34 | ->scalarNode('finder') |
|
497 | 34 | ->treatNullLike(true) |
|
498 | 34 | ->defaultFalse() |
|
499 | 34 | ->end() |
|
500 | 34 | ->append($this->getPersistenceNode()) |
|
501 | 34 | ->append($this->getSerializerNode()) |
|
502 | 34 | ->arrayNode('type_prototype') |
|
503 | 34 | ->children() |
|
504 | 34 | ->scalarNode('analyzer')->end() |
|
505 | 34 | ->append($this->getPersistenceNode()) |
|
506 | 34 | ->append($this->getSerializerNode()) |
|
507 | 34 | ->end() |
|
508 | 34 | ->end() |
|
509 | 34 | ->variableNode('settings')->defaultValue([])->end() |
|
510 | 34 | ->end() |
|
511 | 34 | ->append($this->getTypesNode()) |
|
512 | 34 | ->end() |
|
513 | 34 | ->end() |
|
514 | 34 | ->end() |
|
515 | ; |
||
516 | 34 | } |
|
517 | |||
518 | /** |
||
519 | * @return ArrayNodeDefinition|\Symfony\Component\Config\Definition\Builder\NodeDefinition |
||
520 | */ |
||
521 | 34 | private function createTreeBuilderNode($name) |
|
525 | |||
526 | /** |
||
527 | * Adds the configuration for the "index_templates" key. |
||
528 | * |
||
529 | * @param ArrayNodeDefinition $rootNode |
||
530 | * |
||
531 | * @return void |
||
532 | */ |
||
533 | 34 | private function addIndexTemplatesSection(ArrayNodeDefinition $rootNode) |
|
555 | } |
||
556 |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.