1
|
|
|
<?php namespace Cviebrock\LaravelElasticsearch; |
2
|
|
|
|
3
|
|
|
use Elasticsearch\Client; |
4
|
|
|
use Elasticsearch\ClientBuilder; |
5
|
|
|
use Illuminate\Support\Arr; |
6
|
|
|
use Psr\Log\LoggerInterface; |
7
|
|
|
use Monolog\Logger; |
8
|
|
|
use Monolog\Handler\StreamHandler; |
9
|
|
|
|
10
|
|
|
class Factory |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Map configuration array keys with ES ClientBuilder setters |
15
|
|
|
* |
16
|
|
|
* @var array |
17
|
|
|
*/ |
18
|
|
|
protected $configMappings = [ |
19
|
|
|
'sslVerification' => 'setSSLVerification', |
20
|
|
|
'sniffOnStart' => 'setSniffOnStart', |
21
|
|
|
'retries' => 'setRetries', |
22
|
|
|
'httpHandler' => 'setHandler', |
23
|
|
|
'connectionPool' => 'setConnectionPool', |
24
|
|
|
'connectionSelector' => 'setSelector', |
25
|
|
|
'serializer' => 'setSerializer', |
26
|
|
|
'connectionFactory' => 'setConnectionFactory', |
27
|
|
|
'endpoint' => 'setEndpoint', |
28
|
|
|
'namespaces' => 'registerNamespace', |
29
|
|
|
]; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Make the Elasticsearch client for the given named configuration, or |
33
|
|
|
* the default client. |
34
|
|
|
* |
35
|
|
|
* @param array $config |
36
|
|
|
* |
37
|
|
|
* @return \Elasticsearch\Client|mixed |
38
|
|
|
*/ |
39
|
|
|
public function make(array $config) |
40
|
|
|
{ |
41
|
|
|
// Build the client |
42
|
|
|
return $this->buildClient($config); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Build and configure an Elasticsearch client. |
47
|
|
|
* |
48
|
|
|
* @param array $config |
49
|
|
|
* |
50
|
|
|
* @return \Elasticsearch\Client |
51
|
|
|
*/ |
52
|
|
|
protected function buildClient(array $config): Client |
53
|
|
|
{ |
54
|
|
|
$clientBuilder = ClientBuilder::create(); |
55
|
|
|
|
56
|
|
|
// Configure hosts |
57
|
|
|
|
58
|
|
|
$clientBuilder->setHosts($config['hosts']); |
59
|
|
|
|
60
|
|
|
// Configure logging |
61
|
|
|
|
62
|
|
|
if (Arr::get($config, 'logging')) { |
63
|
|
|
$logObject = Arr::get($config, 'logObject'); |
64
|
|
|
$logPath = Arr::get($config, 'logPath'); |
65
|
|
|
$logLevel = Arr::get($config, 'logLevel'); |
66
|
|
|
if ($logObject && $logObject instanceof LoggerInterface) { |
67
|
|
|
$clientBuilder->setLogger($logObject); |
68
|
|
|
} elseif ($logPath && $logLevel) { |
69
|
|
|
$handler = new StreamHandler($logPath, $logLevel); |
70
|
|
|
$logObject = new Logger('log'); |
71
|
|
|
$logObject->pushHandler($handler); |
72
|
|
|
$clientBuilder->setLogger($logObject); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
// Configure tracer |
77
|
|
|
if ($tracer = Arr::get($config, 'tracer')) { |
78
|
|
|
$clientBuilder->setTracer(app($tracer)); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
// Set additional client configuration |
82
|
|
|
|
83
|
|
|
foreach ($this->configMappings as $key => $method) { |
84
|
|
|
$value = Arr::get($config, $key); |
85
|
|
|
if (is_array($value)) { |
86
|
|
|
foreach ($value as $vItem) { |
87
|
|
|
$clientBuilder->$method($vItem); |
88
|
|
|
} |
89
|
|
|
} elseif ($value !== null) { |
90
|
|
|
$clientBuilder->$method($value); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
// Configure handlers for any AWS hosts |
95
|
|
|
|
96
|
|
|
foreach ($config['hosts'] as $host) { |
97
|
|
|
if (isset($host['aws']) && $host['aws']) { |
98
|
|
|
$clientBuilder->setHandler(function(array $request) use ($host) { |
99
|
|
|
$psr7Handler = \Aws\default_http_handler(); |
100
|
|
|
$signer = new \Aws\Signature\SignatureV4('es', $host['aws_region']); |
101
|
|
|
$request['headers']['Host'][0] = parse_url($request['headers']['Host'][0])['host']; |
102
|
|
|
|
103
|
|
|
// Create a PSR-7 request from the array passed to the handler |
104
|
|
|
$psr7Request = new \GuzzleHttp\Psr7\Request( |
105
|
|
|
$request['http_method'], |
106
|
|
|
(new \GuzzleHttp\Psr7\Uri($request['uri'])) |
107
|
|
|
->withScheme($request['scheme']) |
108
|
|
|
->withHost($request['headers']['Host'][0]), |
109
|
|
|
$request['headers'], |
110
|
|
|
$request['body'] |
111
|
|
|
); |
112
|
|
|
|
113
|
|
|
// Create the Credentials instance with the credentials from the environment |
114
|
|
|
$credentials = new \Aws\Credentials\Credentials($host['aws_key'], $host['aws_secret']); |
115
|
|
|
// check if the aws_credentials from config is set and if it contains a Credentials instance |
116
|
|
|
if (!empty($host['aws_credentials']) && $host['aws_credentials'] instanceof \Aws\Credentials\Credentials) { |
|
|
|
|
117
|
|
|
// Set the credentials as in config |
118
|
|
|
$credentials = $host['aws_credentials']; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
// Sign the PSR-7 request |
122
|
|
|
$signedRequest = $signer->signRequest( |
123
|
|
|
$psr7Request, |
124
|
|
|
$credentials |
125
|
|
|
); |
126
|
|
|
|
127
|
|
|
// Send the signed request to Amazon ES |
128
|
|
|
/** @var \Psr\Http\Message\ResponseInterface $response */ |
129
|
|
|
$response = $psr7Handler($signedRequest) |
130
|
|
|
->then(function(\Psr\Http\Message\ResponseInterface $response) { |
131
|
|
|
return $response; |
132
|
|
|
}, function($error) { |
133
|
|
|
return $error['response']; |
134
|
|
|
}) |
135
|
|
|
->wait(); |
136
|
|
|
|
137
|
|
|
// Convert the PSR-7 response to a RingPHP response |
138
|
|
|
return new \GuzzleHttp\Ring\Future\CompletedFutureArray([ |
139
|
|
|
'status' => $response->getStatusCode(), |
140
|
|
|
'headers' => $response->getHeaders(), |
141
|
|
|
'body' => $response->getBody() |
142
|
|
|
->detach(), |
143
|
|
|
'transfer_stats' => ['total_time' => 0], |
144
|
|
|
'effective_url' => (string)$psr7Request->getUri(), |
145
|
|
|
]); |
146
|
|
|
}); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
// Build and return the client |
151
|
|
|
|
152
|
|
|
return $clientBuilder->build(); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.