-
Notifications
You must be signed in to change notification settings - Fork 341
/
ClientBuilder.php
187 lines (161 loc) · 5.4 KB
/
ClientBuilder.php
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?php
/**
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Smile ElasticSuite to newer
* versions in the future.
*
* @category Smile
* @package Smile\ElasticsuiteCore
* @author Aurelien FOUCRET <[email protected]>
* @copyright 2020 Smile
* @license Open Software License ("OSL") v. 3.0
*/
namespace Smile\ElasticsuiteCore\Client;
use Elasticsearch\ConnectionPool\Selectors\StickyRoundRobinSelector;
/**
* ElasticSearch client builder.
*
* @category Smile
* @package Smile\ElasticsuiteCore
* @author Aurelien FOUCRET <[email protected]>
*/
class ClientBuilder
{
/**
* @var \Elasticsearch\ClientBuilder
*/
private $clientBuilder;
/**
* @var \Psr\Log\LoggerInterface
*/
private $logger;
/**
* @var array
*/
private $defaultOptions = [
'servers' => 'localhost:9200',
'enable_http_auth' => false,
'http_auth_user' => null,
'http_auth_pwd' => null,
'http_auth_encoded' => false,
'is_debug_mode_enabled' => false,
'max_parallel_handles' => 100, // As per default Elasticsearch Handler configuration.
'max_retries' => 2,
'verify' => true,
];
/**
* @var string
*/
private $selector;
/**
* Constructor.
*
* @param \Elasticsearch\ClientBuilder $clientBuilder Client builder.
* @param \Psr\Log\LoggerInterface $logger Logger.
* @param string $selector Node Selector.
*/
public function __construct(
\Elasticsearch\ClientBuilder $clientBuilder,
\Psr\Log\LoggerInterface $logger,
$selector = StickyRoundRobinSelector::class
) {
$this->clientBuilder = $clientBuilder;
$this->logger = $logger;
$this->selector = $selector;
}
/**
* Build an ES client from options.
*
* @SuppressWarnings(PHPMD.StaticAccess)
*
* @param array $options Client options. See self::defaultOptions for available options.
*
* @return \Elasticsearch\Client
*/
public function build($options = [])
{
$options = array_merge($this->defaultOptions, $options);
$clientBuilder = $this->clientBuilder->create();
$hosts = $this->getHosts($options);
if (!empty($hosts)) {
$clientBuilder->setHosts($hosts);
}
if ($options['is_debug_mode_enabled']) {
$clientBuilder->setLogger($this->logger);
$clientBuilder->setTracer($this->logger);
}
if ($options['max_parallel_handles']) {
$handlerParams = ['max_handles' => (int) $options['max_parallel_handles']];
$handler = \Elasticsearch\ClientBuilder::defaultHandler($handlerParams);
$clientBuilder->setHandler($handler);
}
$connectionParams = $this->getConnectionParams($options);
if (!empty($connectionParams)) {
$this->clientBuilder->setConnectionParams($connectionParams);
}
if ($options['max_retries'] > 0) {
$clientBuilder->setRetries((int) $options['max_retries']);
}
if (array_key_exists('verify', $options)) {
$clientBuilder->setSSLVerification($options['verify']);
}
if (null !== $this->selector) {
$selector = (count($hosts) > 1) ? $this->selector : StickyRoundRobinSelector::class;
$clientBuilder->setSelector($selector);
}
return $clientBuilder->build();
}
/**
* Return hosts config used to connect to the cluster.
*
* @param array $options Client options. See self::defaultOptions for available options.
*
* @return array
*/
private function getHosts($options)
{
$hosts = [];
if (is_string($options['servers'])) {
$options['servers'] = explode(',', $options['servers']);
}
foreach ($options['servers'] as $host) {
if (!empty($host)) {
[$hostname, $port] = array_pad(explode(':', trim($host), 2), 2, 9200);
$currentHostConfig = [
'host' => $hostname,
'port' => $port,
'scheme' => isset($options['enable_https_mode']) ? 'https' : $options['scheme'] ?? 'http',
];
if ($options['enable_http_auth']) {
$currentHostConfig['user'] = $options['http_auth_user'];
$currentHostConfig['pass'] = $options['http_auth_pwd'];
}
$hosts[] = $currentHostConfig;
}
}
return $hosts;
}
/**
* Return HTTP Authentication parameters used to connect to the cluster if any
*
* @param array $options Client options. See self::defaultOptions for available options.
* @return array
*/
private function getConnectionParams($options)
{
return (
!empty($options['http_auth_user']) &&
!empty($options['http_auth_pwd']) &&
$options['http_auth_encoded']
) ? [
'client' => [
'headers' => [
'Authorization' => [
'Basic ' . base64_encode($options['http_auth_user'] . ':' . $options['http_auth_pwd']),
],
],
],
] : [];
}
}