|
1
|
|
|
<?php namespace Cviebrock\LaravelElasticsearch; |
|
2
|
|
|
|
|
3
|
|
|
use Elasticsearch\Client; |
|
4
|
|
|
use Elasticsearch\ClientBuilder; |
|
5
|
|
|
use Psr\Log\LoggerInterface; |
|
6
|
|
|
|
|
7
|
|
|
|
|
8
|
|
|
class Factory |
|
9
|
|
|
{ |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Map configuration array keys with ES ClientBuilder setters |
|
13
|
|
|
* |
|
14
|
|
|
* @var array |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $configMappings = [ |
|
17
|
|
|
'sslVerification' => 'setSSLVerification', |
|
18
|
|
|
'sniffOnStart' => 'setSniffOnStart', |
|
19
|
|
|
'retries' => 'setRetries', |
|
20
|
|
|
'httpHandler' => 'setHandler', |
|
21
|
|
|
'connectionPool' => 'setConnectionPool', |
|
22
|
|
|
'connectionSelector' => 'setSelector', |
|
23
|
|
|
'serializer' => 'setSerializer', |
|
24
|
|
|
'connectionFactory' => 'setConnectionFactory', |
|
25
|
|
|
'endpoint' => 'setEndpoint', |
|
26
|
|
|
]; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Make the Elasticsearch client for the given named configuration, or |
|
30
|
|
|
* the default client. |
|
31
|
|
|
* |
|
32
|
|
|
* @param array $config |
|
33
|
|
|
* |
|
34
|
|
|
* @return \Elasticsearch\Client|mixed |
|
35
|
|
|
*/ |
|
36
|
|
|
public function make(array $config) |
|
37
|
|
|
{ |
|
38
|
|
|
// Build the client |
|
39
|
|
|
return $this->buildClient($config); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Build and configure an Elasticsearch client. |
|
44
|
|
|
* |
|
45
|
|
|
* @param array $config |
|
46
|
|
|
* |
|
47
|
|
|
* @return \Elasticsearch\Client |
|
48
|
|
|
*/ |
|
49
|
|
|
protected function buildClient(array $config): Client |
|
50
|
|
|
{ |
|
51
|
|
|
|
|
52
|
|
|
$clientBuilder = ClientBuilder::create(); |
|
53
|
|
|
|
|
54
|
|
|
// Configure hosts |
|
55
|
|
|
|
|
56
|
|
|
$clientBuilder->setHosts($config['hosts']); |
|
57
|
|
|
|
|
58
|
|
|
// Configure logging |
|
59
|
|
|
|
|
60
|
|
|
if (array_get($config, 'logging')) { |
|
61
|
|
|
$logObject = array_get($config, 'logObject'); |
|
62
|
|
|
$logPath = array_get($config, 'logPath'); |
|
63
|
|
|
$logLevel = array_get($config, 'logLevel'); |
|
64
|
|
|
if ($logObject && $logObject instanceof LoggerInterface) { |
|
65
|
|
|
$clientBuilder->setLogger($logObject); |
|
66
|
|
|
} else if ($logPath && $logLevel) { |
|
67
|
|
|
$logObject = ClientBuilder::defaultLogger($logPath, $logLevel); |
|
68
|
|
|
$clientBuilder->setLogger($logObject); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
// Set additional client configuration |
|
73
|
|
|
|
|
74
|
|
|
foreach ($this->configMappings as $key => $method) { |
|
75
|
|
|
$value = array_get($config, $key); |
|
76
|
|
|
if ($value !== null) { |
|
77
|
|
|
call_user_func([$clientBuilder, $method], $value); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
foreach($config['hosts'] as $c) { |
|
82
|
|
|
if( $c['aws'] ) { |
|
83
|
|
|
$clientBuilder->setHandler( function(array $request) use($c) { |
|
84
|
|
|
$psr7Handler = \Aws\default_http_handler(); |
|
85
|
|
|
$signer = new \Aws\Signature\SignatureV4('es', $c['aws_region']); |
|
86
|
|
|
$request['headers']['Host'][0] = parse_url($request['headers']['Host'][0])['host']; |
|
87
|
|
|
// Create a PSR-7 request from the array passed to the handler |
|
88
|
|
|
$psr7Request = new \GuzzleHttp\Psr7\Request( |
|
89
|
|
|
$request['http_method'], |
|
90
|
|
|
(new \GuzzleHttp\Psr7\Uri($request['uri'])) |
|
91
|
|
|
->withScheme($request['scheme']) |
|
92
|
|
|
->withHost($request['headers']['Host'][0]), |
|
93
|
|
|
$request['headers'], |
|
94
|
|
|
$request['body'] |
|
95
|
|
|
); |
|
96
|
|
|
// Sign the PSR-7 request with credentials from the environment |
|
97
|
|
|
$signedRequest = $signer->signRequest( |
|
98
|
|
|
$psr7Request, |
|
99
|
|
|
new \Aws\Credentials\Credentials($c['aws_key'], $c['aws_secret']) |
|
100
|
|
|
); |
|
101
|
|
|
// Send the signed request to Amazon ES |
|
102
|
|
|
/** @var \Psr\Http\Message\ResponseInterface $response */ |
|
103
|
|
|
$response = $psr7Handler($signedRequest)->then(function (\Psr\Http\Message\ResponseInterface $response) { |
|
104
|
|
|
return $response; |
|
105
|
|
|
}, function ($error) { |
|
106
|
|
|
return $error['response']; |
|
107
|
|
|
})->wait(); |
|
108
|
|
|
// Convert the PSR-7 response to a RingPHP response |
|
109
|
|
|
return new \GuzzleHttp\Ring\Future\CompletedFutureArray([ |
|
110
|
|
|
'status' => $response->getStatusCode(), |
|
111
|
|
|
'headers' => $response->getHeaders(), |
|
112
|
|
|
'body' => $response->getBody()->detach(), |
|
113
|
|
|
'transfer_stats' => ['total_time' => 0], |
|
114
|
|
|
'effective_url' => (string)$psr7Request->getUri(), |
|
115
|
|
|
]); |
|
116
|
|
|
}); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
// Build and return the client |
|
121
|
|
|
|
|
122
|
|
|
return $clientBuilder->build(); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|