forked from api-platform/core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathElasticsearchClientPass.php
More file actions
78 lines (64 loc) · 2.55 KB
/
ElasticsearchClientPass.php
File metadata and controls
78 lines (64 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php
/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace ApiPlatform\Symfony\Bundle\DependencyInjection\Compiler;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
/**
* Creates the Elasticsearch client.
*
* @author Baptiste Meyer <baptiste@les-tilleuls.coop>
*/
final class ElasticsearchClientPass implements CompilerPassInterface
{
/**
* {@inheritdoc}
*/
public function process(ContainerBuilder $container): void
{
if (!$container->getParameter('api_platform.elasticsearch.enabled')) {
return;
}
$clientConfiguration = [];
if ($hosts = $container->getParameter('api_platform.elasticsearch.hosts')) {
$clientConfiguration['hosts'] = $hosts;
}
if ('opensearch' === $container->getParameter('api_platform.elasticsearch.client')) {
$builderName = \OpenSearch\ClientBuilder::class; // @phpstan-ignore class.notFound
} elseif (class_exists(\Elasticsearch\ClientBuilder::class)) {
// ES v7
$builderName = \Elasticsearch\ClientBuilder::class;
} else {
// ES v8 and up
$builderName = \Elastic\Elasticsearch\ClientBuilder::class;
}
if ($container->has('logger')) {
$clientConfiguration['logger'] = new Reference('logger');
// @phpstan-ignore-next-line
if (\Elasticsearch\ClientBuilder::class === $builderName) {
$clientConfiguration['tracer'] = new Reference('logger');
}
}
if ($sslCaBundle = $container->getParameter('api_platform.elasticsearch.ssl_ca_bundle')) {
$clientConfiguration['CABundle'] = $sslCaBundle;
}
if (false === $container->getParameter('api_platform.elasticsearch.ssl_verification')) {
$clientConfiguration['SSLVerification'] = false;
}
$clientDefinition = $container->getDefinition('api_platform.elasticsearch.client');
if (!$clientConfiguration) {
$clientDefinition->setFactory([$builderName, 'build']);
} else {
$clientDefinition->setFactory([$builderName, 'fromConfig']);
$clientDefinition->setArguments([$clientConfiguration]);
}
}
}