/
ClientBuilder.php
666 lines (564 loc) · 18.6 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
<?php
namespace Elasticsearch;
use Elasticsearch\Common\Exceptions\InvalidArgumentException;
use Elasticsearch\Common\Exceptions\RuntimeException;
use Elasticsearch\ConnectionPool\AbstractConnectionPool;
use Elasticsearch\ConnectionPool\Selectors\SelectorInterface;
use Elasticsearch\ConnectionPool\StaticNoPingConnectionPool;
use Elasticsearch\Connections\Connection;
use Elasticsearch\Connections\ConnectionFactory;
use Elasticsearch\Connections\ConnectionFactoryInterface;
use Elasticsearch\Namespaces\NamespaceBuilderInterface;
use Elasticsearch\Serializers\SerializerInterface;
use Elasticsearch\ConnectionPool\Selectors;
use Elasticsearch\Serializers\SmartSerializer;
use GuzzleHttp\Ring\Client\CurlHandler;
use GuzzleHttp\Ring\Client\CurlMultiHandler;
use GuzzleHttp\Ring\Client\Middleware;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use Monolog\Processor\IntrospectionProcessor;
/**
* Class ClientBuilder
*
* @category Elasticsearch
* @package Elasticsearch\Common\Exceptions
* @author Zachary Tong <zach@elastic.co>
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache2
* @link http://elastic.co
*/
class ClientBuilder
{
/** @var Transport */
private $transport;
/** @var callback */
private $endpoint;
/** @var NamespaceBuilderInterface[] */
private $registeredNamespacesBuilders = [];
/** @var ConnectionFactoryInterface */
private $connectionFactory;
private $handler;
/** @var LoggerInterface */
private $logger;
/** @var LoggerInterface */
private $tracer;
/** @var string */
private $connectionPool = '\Elasticsearch\ConnectionPool\StaticNoPingConnectionPool';
/** @var string */
private $serializer = '\Elasticsearch\Serializers\SmartSerializer';
/** @var string */
private $selector = '\Elasticsearch\ConnectionPool\Selectors\RoundRobinSelector';
/** @var array */
private $connectionPoolArgs = [
'randomizeHosts' => true
];
/** @var array */
private $hosts;
/** @var array */
private $connectionParams;
/** @var int */
private $retries;
/** @var bool */
private $sniffOnStart = false;
/** @var null|array */
private $sslCert = null;
/** @var null|array */
private $sslKey = null;
/** @var null|bool|string */
private $sslVerification = null;
/** @var bool */
private $allowBadJSON = false;
/**
* @return ClientBuilder
*/
public static function create()
{
return new static();
}
/**
* Build a new client from the provided config. Hash keys
* should correspond to the method name e.g. ['connectionPool']
* corresponds to setConnectionPool().
*
* Missing keys will use the default for that setting if applicable
*
* Unknown keys will throw an exception by default, but this can be silenced
* by setting `quiet` to true
*
* @param array $config hash of settings
* @param bool $quiet False if unknown settings throw exception, true to silently
* ignore unknown settings
* @throws Common\Exceptions\RuntimeException
* @return \Elasticsearch\Client
*/
public static function fromConfig($config, $quiet = false)
{
$builder = new self;
foreach ($config as $key => $value) {
$method = "set$key";
if (method_exists($builder, $method)) {
$builder->$method($value);
unset($config[$key]);
}
}
if ($quiet === false && count($config) > 0) {
$unknown = implode(array_keys($config));
throw new RuntimeException("Unknown parameters provided: $unknown");
}
return $builder->build();
}
/**
* @param array $singleParams
* @param array $multiParams
* @throws \RuntimeException
* @return callable
*/
public static function defaultHandler($multiParams = [], $singleParams = [])
{
$future = null;
if (extension_loaded('curl')) {
$config = array_merge([ 'mh' => curl_multi_init() ], $multiParams);
if (function_exists('curl_reset')) {
$default = new CurlHandler($singleParams);
$future = new CurlMultiHandler($config);
} else {
$default = new CurlMultiHandler($config);
}
} else {
throw new \RuntimeException('Elasticsearch-PHP requires cURL, or a custom HTTP handler.');
}
return $future ? Middleware::wrapFuture($default, $future) : $default;
}
/**
* @param array $params
* @throws \RuntimeException
* @return CurlMultiHandler
*/
public static function multiHandler($params = [])
{
if (function_exists('curl_multi_init')) {
return new CurlMultiHandler(array_merge([ 'mh' => curl_multi_init() ], $params));
} else {
throw new \RuntimeException('CurlMulti handler requires cURL.');
}
}
/**
* @return CurlHandler
* @throws \RuntimeException
*/
public static function singleHandler()
{
if (function_exists('curl_reset')) {
return new CurlHandler();
} else {
throw new \RuntimeException('CurlSingle handler requires cURL.');
}
}
/**
* @param $path string
* @return \Monolog\Logger\Logger
*/
public static function defaultLogger($path, $level = Logger::WARNING)
{
$log = new Logger('log');
$handler = new StreamHandler($path, $level);
$log->pushHandler($handler);
return $log;
}
/**
* @param \Elasticsearch\Connections\ConnectionFactoryInterface $connectionFactory
* @return $this
*/
public function setConnectionFactory(ConnectionFactoryInterface $connectionFactory)
{
$this->connectionFactory = $connectionFactory;
return $this;
}
/**
* @param \Elasticsearch\ConnectionPool\AbstractConnectionPool|string $connectionPool
* @param array $args
* @throws \InvalidArgumentException
* @return $this
*/
public function setConnectionPool($connectionPool, array $args = [])
{
if (is_string($connectionPool)) {
$this->connectionPool = $connectionPool;
$this->connectionPoolArgs = $args;
} elseif (is_object($connectionPool)) {
$this->connectionPool = $connectionPool;
} else {
throw new InvalidArgumentException("Serializer must be a class path or instantiated object extending AbstractConnectionPool");
}
return $this;
}
/**
* @param callable $endpoint
* @return $this
*/
public function setEndpoint($endpoint)
{
$this->endpoint = $endpoint;
return $this;
}
/**
* @param NamespaceBuilderInterface $namespaceBuilder
* @return $this
*/
public function registerNamespace(NamespaceBuilderInterface $namespaceBuilder)
{
$this->registeredNamespacesBuilders[] = $namespaceBuilder;
return $this;
}
/**
* @param \Elasticsearch\Transport $transport
* @return $this
*/
public function setTransport($transport)
{
$this->transport = $transport;
return $this;
}
/**
* @param mixed $handler
* @return $this
*/
public function setHandler($handler)
{
$this->handler = $handler;
return $this;
}
/**
* @param \Psr\Log\LoggerInterface $logger
* @return $this
*/
public function setLogger($logger)
{
$this->logger = $logger;
return $this;
}
/**
* @param \Psr\Log\LoggerInterface $tracer
* @return $this
*/
public function setTracer($tracer)
{
$this->tracer = $tracer;
return $this;
}
/**
* @param \Elasticsearch\Serializers\SerializerInterface|string $serializer
* @throws \InvalidArgumentException
* @return $this
*/
public function setSerializer($serializer)
{
$this->parseStringOrObject($serializer, $this->serializer, 'SerializerInterface');
return $this;
}
/**
* @param array $hosts
* @return $this
*/
public function setHosts($hosts)
{
$this->hosts = $hosts;
return $this;
}
/**
* @param array $params
* @return $this
*/
public function setConnectionParams(array $params)
{
$this->connectionParams = $params;
return $this;
}
/**
* @param int $retries
* @return $this
*/
public function setRetries($retries)
{
$this->retries = $retries;
return $this;
}
/**
* @param \Elasticsearch\ConnectionPool\Selectors\SelectorInterface|string $selector
* @throws \InvalidArgumentException
* @return $this
*/
public function setSelector($selector)
{
$this->parseStringOrObject($selector, $this->selector, 'SelectorInterface');
return $this;
}
/**
* @param boolean $sniffOnStart
* @return $this
*/
public function setSniffOnStart($sniffOnStart)
{
$this->sniffOnStart = $sniffOnStart;
return $this;
}
/**
* @param $cert
* @param null|string $password
* @return $this
*/
public function setSSLCert($cert, $password = null)
{
$this->sslCert = [$cert, $password];
return $this;
}
/**
* @param $key
* @param null|string $password
* @return $this
*/
public function setSSLKey($key, $password = null)
{
$this->sslKey = [$key, $password];
return $this;
}
/**
* @param bool|string $value
* @return $this
*/
public function setSSLVerification($value = true)
{
$this->sslVerification = $value;
return $this;
}
public function allowBadJSONSerialization()
{
$this->allowBadJSON = true;
return $this;
}
/**
* @return Client
*/
public function build()
{
if(!defined('JSON_PRESERVE_ZERO_FRACTION') && $this->allowBadJSON === false) {
throw new RuntimeException("Your version of PHP / json-ext does not support the constant 'JSON_PRESERVE_ZERO_FRACTION',".
" which is important for proper type mapping in Elasticsearch. Please upgrade your PHP or json-ext.\n".
"If you are unable to upgrade, and are willing to accept the consequences, you may use the allowBadJSONSerialization()".
" method on the ClientBuilder to bypass this limitation.");
}
$this->buildLoggers();
if (is_null($this->handler)) {
$this->handler = ClientBuilder::defaultHandler();
}
$sslOptions = null;
if (isset($this->sslKey)) {
$sslOptions['ssl_key'] = $this->sslKey;
}
if (isset($this->sslCert)) {
$sslOptions['cert'] = $this->sslCert;
}
if (isset($this->sslVerification)) {
$sslOptions['verify'] = $this->sslVerification;
}
if (!is_null($sslOptions)) {
$sslHandler = function (callable $handler, array $sslOptions) {
return function (array $request) use ($handler, $sslOptions) {
// Add our custom headers
foreach ($sslOptions as $key => $value) {
$request['client'][$key] = $value;
}
// Send the request using the handler and return the response.
return $handler($request);
};
};
$this->handler = $sslHandler($this->handler, $sslOptions);
}
if (is_null($this->serializer)) {
$this->serializer = new SmartSerializer();
} elseif (is_string($this->serializer)) {
$this->serializer = new $this->serializer;
}
if (is_null($this->connectionFactory)) {
if (is_null($this->connectionParams)) {
$this->connectionParams = [];
}
// Make sure we are setting Content-type and Accept (unless the user has explicitly
// overridden it
if (isset($this->connectionParams['client']['headers']) === false) {
$this->connectionParams['client']['headers'] = [
'Content-type' => ['application/json'],
'Accept' => ['application/json']
];
} else {
if (isset($this->connectionParams['client']['headers']['Content-type']) === false) {
$this->connectionParams['client']['headers']['Content-type'] = ['application/json'];
}
if (isset($this->connectionParams['client']['headers']['Accept']) === false) {
$this->connectionParams['client']['headers']['Accept'] = ['application/json'];
}
}
$this->connectionFactory = new ConnectionFactory($this->handler, $this->connectionParams, $this->serializer, $this->logger, $this->tracer);
}
if (is_null($this->hosts)) {
$this->hosts = $this->getDefaultHost();
}
if (is_null($this->selector)) {
$this->selector = new Selectors\RoundRobinSelector();
} elseif (is_string($this->selector)) {
$this->selector = new $this->selector;
}
$this->buildTransport();
if (is_null($this->endpoint)) {
$serializer = $this->serializer;
$this->endpoint = function ($class) use ($serializer) {
$fullPath = '\\Elasticsearch\\Endpoints\\' . $class;
if ($class === 'Bulk' || $class === 'Msearch' || $class === 'MPercolate') {
return new $fullPath($serializer);
} else {
return new $fullPath();
}
};
}
$registeredNamespaces = [];
foreach ($this->registeredNamespacesBuilders as $builder) {
/** @var $builder NamespaceBuilderInterface */
$registeredNamespaces[$builder->getName()] = $builder->getObject($this->transport, $this->serializer);
}
return $this->instantiate($this->transport, $this->endpoint, $registeredNamespaces);
}
/**
* @param Transport $transport
* @param callable $endpoint
* @param Object[] $registeredNamespaces
* @return Client
*/
protected function instantiate(Transport $transport, callable $endpoint, array $registeredNamespaces)
{
return new Client($transport, $endpoint, $registeredNamespaces);
}
private function buildLoggers()
{
if (is_null($this->logger)) {
$this->logger = new NullLogger();
}
if (is_null($this->tracer)) {
$this->tracer = new NullLogger();
}
}
private function buildTransport()
{
$connections = $this->buildConnectionsFromHosts($this->hosts);
if (is_string($this->connectionPool)) {
$this->connectionPool = new $this->connectionPool(
$connections,
$this->selector,
$this->connectionFactory,
$this->connectionPoolArgs);
} elseif (is_null($this->connectionPool)) {
$this->connectionPool = new StaticNoPingConnectionPool(
$connections,
$this->selector,
$this->connectionFactory,
$this->connectionPoolArgs);
}
if (is_null($this->retries)) {
$this->retries = count($connections);
}
if (is_null($this->transport)) {
$this->transport = new Transport($this->retries, $this->sniffOnStart, $this->connectionPool, $this->logger);
}
}
private function parseStringOrObject($arg, &$destination, $interface)
{
if (is_string($arg)) {
$destination = new $arg;
} elseif (is_object($arg)) {
$destination = $arg;
} else {
throw new InvalidArgumentException("Serializer must be a class path or instantiated object implementing $interface");
}
}
/**
* @return array
*/
private function getDefaultHost()
{
return ['localhost:9200'];
}
/**
* @param array $hosts
*
* @throws \InvalidArgumentException
* @return \Elasticsearch\Connections\Connection[]
*/
private function buildConnectionsFromHosts($hosts)
{
if (is_array($hosts) === false) {
$this->logger->error("Hosts parameter must be an array of strings, or an array of Connection hashes.");
throw new InvalidArgumentException('Hosts parameter must be an array of strings, or an array of Connection hashes.');
}
$connections = [];
foreach ($hosts as $host) {
if (is_string($host)) {
$host = $this->prependMissingScheme($host);
$host = $this->extractURIParts($host);
} else if (is_array($host)) {
$host = $this->normalizeExtendedHost($host);
} else {
$this->logger->error("Could not parse host: ".print_r($host, true));
throw new RuntimeException("Could not parse host: ".print_r($host, true));
}
$connections[] = $this->connectionFactory->create($host);
}
return $connections;
}
/**
* @param $host
* @return array
*/
private function normalizeExtendedHost($host) {
if (isset($host['host']) === false) {
$this->logger->error("Required 'host' was not defined in extended format: ".print_r($host, true));
throw new RuntimeException("Required 'host' was not defined in extended format: ".print_r($host, true));
}
if (isset($host['scheme']) === false) {
$host['scheme'] = 'http';
}
if (isset($host['port']) === false) {
$host['port'] = '9200';
}
return $host;
}
/**
* @param array $host
*
* @throws \InvalidArgumentException
* @return array
*/
private function extractURIParts($host)
{
$parts = parse_url($host);
if ($parts === false) {
throw new InvalidArgumentException("Could not parse URI");
}
if (isset($parts['port']) !== true) {
$parts['port'] = 9200;
}
return $parts;
}
/**
* @param string $host
*
* @return string
*/
private function prependMissingScheme($host)
{
if (!filter_var($host, FILTER_VALIDATE_URL, FILTER_FLAG_SCHEME_REQUIRED)) {
$host = 'http://' . $host;
}
return $host;
}
}