|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Elasticquent; |
| 4 | + |
| 5 | +use Aws\Credentials\Credentials; |
| 6 | +use Aws\Signature\SignatureV4; |
| 7 | +use GuzzleHttp\Psr7\Request; |
| 8 | +use GuzzleHttp\Psr7\Uri; |
| 9 | +use GuzzleHttp\Ring\Future\CompletedFutureArray; |
| 10 | +use Psr\Http\Message\ResponseInterface; |
| 11 | + |
| 12 | +final class ElasticSearchClientFactory |
| 13 | +{ |
| 14 | + use ElasticquentConfigTrait; |
| 15 | + |
| 16 | + /** |
| 17 | + * @var array |
| 18 | + */ |
| 19 | + private $config; |
| 20 | + |
| 21 | + /** |
| 22 | + * ElasticSearchClientFactory constructor. |
| 23 | + */ |
| 24 | + public function __construct() |
| 25 | + { |
| 26 | + /* @var array $config */ |
| 27 | + $this->config = $this->getElasticConfig(); |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * @return \Elasticsearch\Client |
| 32 | + */ |
| 33 | + public function getClient() |
| 34 | + { |
| 35 | + // elasticsearch v2.0 using builder |
| 36 | + if (class_exists('\Elasticsearch\ClientBuilder')) { |
| 37 | + // elasticsearch v2.0 using builder |
| 38 | + $awsConfig = $this->getElasticConfig('aws'); |
| 39 | + if (!empty($awsConfig) && array_get($this->getElasticConfig('aws'), 'iam', false)) { |
| 40 | + if ($handler = $this->getAwsESHandler()) { |
| 41 | + array_set($this->config, 'handler', $handler); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + return \Elasticsearch\ClientBuilder::fromConfig($this->config); |
| 46 | + } |
| 47 | + |
| 48 | + // elasticsearch v1 |
| 49 | + return new \Elasticsearch\Client($this->config); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * @return bool|\Closure |
| 54 | + */ |
| 55 | + private function getAwsESHandler() |
| 56 | + { |
| 57 | + $awsConfig = $this->getElasticConfig('aws'); |
| 58 | + if (empty($awsConfig)) { |
| 59 | + return false; |
| 60 | + } |
| 61 | + |
| 62 | + $key = array_get($awsConfig, 'key'); |
| 63 | + $secret = array_get($awsConfig, 'secret'); |
| 64 | + $region = array_get($awsConfig, 'region', 'us-west-2'); |
| 65 | + |
| 66 | + $psr7Handler = \Aws\default_http_handler(); |
| 67 | + $signer = new SignatureV4('es', $region); |
| 68 | + |
| 69 | + $handler = function (array $request) use ( |
| 70 | + $psr7Handler, |
| 71 | + $signer, |
| 72 | + $key, |
| 73 | + $secret |
| 74 | + ) { |
| 75 | + // Amazon ES listens on standard ports (443 for HTTPS, 80 for HTTP). |
| 76 | + $request['headers']['host'][0] = parse_url($request['headers']['host'][0], PHP_URL_HOST); |
| 77 | + |
| 78 | + $credentials = new Credentials($key, $secret); |
| 79 | + |
| 80 | + // Create a PSR-7 request from the array passed to the handler |
| 81 | + $psr7Request = new Request($request['http_method'], |
| 82 | + (new Uri($request['uri']))->withScheme($request['scheme'])->withHost($request['headers']['host'][0]), $request['headers'], |
| 83 | + $request['body']); |
| 84 | + |
| 85 | + // Sign the PSR-7 request with credentials from the environment |
| 86 | + $signedRequest = $signer->signRequest($psr7Request, $credentials); |
| 87 | + |
| 88 | + // Send the signed request to Amazon ES |
| 89 | + /** @var ResponseInterface $response */ |
| 90 | + $response = $psr7Handler($signedRequest)->wait(); |
| 91 | + |
| 92 | + // Convert the PSR-7 response to a RingPHP response |
| 93 | + return new CompletedFutureArray([ |
| 94 | + 'status' => $response->getStatusCode(), |
| 95 | + 'headers' => $response->getHeaders(), |
| 96 | + 'body' => $response->getBody()->detach(), |
| 97 | + 'transfer_stats' => ['total_time' => 0], |
| 98 | + 'effective_url' => (string) $psr7Request->getUri(), |
| 99 | + ]); |
| 100 | + }; |
| 101 | + |
| 102 | + return $handler; |
| 103 | + } |
| 104 | +} |
0 commit comments