@@ -15,15 +15,15 @@ |
||
15 | 15 | */ |
16 | 16 | class RequestException extends TransferException implements PsrRequestException |
17 | 17 | { |
18 | - use RequestAwareTrait; |
|
18 | + use RequestAwareTrait; |
|
19 | 19 | |
20 | - /** |
|
21 | - * @param string $message |
|
22 | - */ |
|
23 | - public function __construct($message, RequestInterface $request, \Exception $previous = null) |
|
24 | - { |
|
25 | - $this->setRequest($request); |
|
20 | + /** |
|
21 | + * @param string $message |
|
22 | + */ |
|
23 | + public function __construct($message, RequestInterface $request, \Exception $previous = null) |
|
24 | + { |
|
25 | + $this->setRequest($request); |
|
26 | 26 | |
27 | - parent::__construct($message, 0, $previous); |
|
28 | - } |
|
27 | + parent::__construct($message, 0, $previous); |
|
28 | + } |
|
29 | 29 | } |
@@ -13,8 +13,7 @@ |
||
13 | 13 | * |
14 | 14 | * @author Márk Sági-Kazár <[email protected]> |
15 | 15 | */ |
16 | -class RequestException extends TransferException implements PsrRequestException |
|
17 | -{ |
|
16 | +class RequestException extends TransferException implements PsrRequestException { |
|
18 | 17 | use RequestAwareTrait; |
19 | 18 | |
20 | 19 | /** |
@@ -6,21 +6,21 @@ |
||
6 | 6 | |
7 | 7 | trait RequestAwareTrait |
8 | 8 | { |
9 | - /** |
|
10 | - * @var RequestInterface |
|
11 | - */ |
|
12 | - private $request; |
|
9 | + /** |
|
10 | + * @var RequestInterface |
|
11 | + */ |
|
12 | + private $request; |
|
13 | 13 | |
14 | - private function setRequest(RequestInterface $request) |
|
15 | - { |
|
16 | - $this->request = $request; |
|
17 | - } |
|
14 | + private function setRequest(RequestInterface $request) |
|
15 | + { |
|
16 | + $this->request = $request; |
|
17 | + } |
|
18 | 18 | |
19 | - /** |
|
20 | - * {@inheritdoc} |
|
21 | - */ |
|
22 | - public function getRequest(): RequestInterface |
|
23 | - { |
|
24 | - return $this->request; |
|
25 | - } |
|
19 | + /** |
|
20 | + * {@inheritdoc} |
|
21 | + */ |
|
22 | + public function getRequest(): RequestInterface |
|
23 | + { |
|
24 | + return $this->request; |
|
25 | + } |
|
26 | 26 | } |
@@ -4,8 +4,7 @@ |
||
4 | 4 | |
5 | 5 | use OCA\FullTextSearch_Elasticsearch\Vendor\Psr\Http\Message\RequestInterface; |
6 | 6 | |
7 | -trait RequestAwareTrait |
|
8 | -{ |
|
7 | +trait RequestAwareTrait { |
|
9 | 8 | /** |
10 | 9 | * @var RequestInterface |
11 | 10 | */ |
@@ -8,47 +8,47 @@ |
||
8 | 8 | |
9 | 9 | final class HttpFulfilledPromise implements Promise |
10 | 10 | { |
11 | - /** |
|
12 | - * @var ResponseInterface |
|
13 | - */ |
|
14 | - private $response; |
|
15 | - |
|
16 | - public function __construct(ResponseInterface $response) |
|
17 | - { |
|
18 | - $this->response = $response; |
|
19 | - } |
|
20 | - |
|
21 | - /** |
|
22 | - * {@inheritdoc} |
|
23 | - */ |
|
24 | - public function then(callable $onFulfilled = null, callable $onRejected = null) |
|
25 | - { |
|
26 | - if (null === $onFulfilled) { |
|
27 | - return $this; |
|
28 | - } |
|
29 | - |
|
30 | - try { |
|
31 | - return new self($onFulfilled($this->response)); |
|
32 | - } catch (Exception $e) { |
|
33 | - return new HttpRejectedPromise($e); |
|
34 | - } |
|
35 | - } |
|
36 | - |
|
37 | - /** |
|
38 | - * {@inheritdoc} |
|
39 | - */ |
|
40 | - public function getState() |
|
41 | - { |
|
42 | - return Promise::FULFILLED; |
|
43 | - } |
|
44 | - |
|
45 | - /** |
|
46 | - * {@inheritdoc} |
|
47 | - */ |
|
48 | - public function wait($unwrap = true) |
|
49 | - { |
|
50 | - if ($unwrap) { |
|
51 | - return $this->response; |
|
52 | - } |
|
53 | - } |
|
11 | + /** |
|
12 | + * @var ResponseInterface |
|
13 | + */ |
|
14 | + private $response; |
|
15 | + |
|
16 | + public function __construct(ResponseInterface $response) |
|
17 | + { |
|
18 | + $this->response = $response; |
|
19 | + } |
|
20 | + |
|
21 | + /** |
|
22 | + * {@inheritdoc} |
|
23 | + */ |
|
24 | + public function then(callable $onFulfilled = null, callable $onRejected = null) |
|
25 | + { |
|
26 | + if (null === $onFulfilled) { |
|
27 | + return $this; |
|
28 | + } |
|
29 | + |
|
30 | + try { |
|
31 | + return new self($onFulfilled($this->response)); |
|
32 | + } catch (Exception $e) { |
|
33 | + return new HttpRejectedPromise($e); |
|
34 | + } |
|
35 | + } |
|
36 | + |
|
37 | + /** |
|
38 | + * {@inheritdoc} |
|
39 | + */ |
|
40 | + public function getState() |
|
41 | + { |
|
42 | + return Promise::FULFILLED; |
|
43 | + } |
|
44 | + |
|
45 | + /** |
|
46 | + * {@inheritdoc} |
|
47 | + */ |
|
48 | + public function wait($unwrap = true) |
|
49 | + { |
|
50 | + if ($unwrap) { |
|
51 | + return $this->response; |
|
52 | + } |
|
53 | + } |
|
54 | 54 | } |
@@ -6,8 +6,7 @@ |
||
6 | 6 | use OCA\FullTextSearch_Elasticsearch\Vendor\Http\Promise\Promise; |
7 | 7 | use OCA\FullTextSearch_Elasticsearch\Vendor\Psr\Http\Message\ResponseInterface; |
8 | 8 | |
9 | -final class HttpFulfilledPromise implements Promise |
|
10 | -{ |
|
9 | +final class HttpFulfilledPromise implements Promise { |
|
11 | 10 | /** |
12 | 11 | * @var ResponseInterface |
13 | 12 | */ |
@@ -7,52 +7,52 @@ |
||
7 | 7 | |
8 | 8 | final class HttpRejectedPromise implements Promise |
9 | 9 | { |
10 | - /** |
|
11 | - * @var Exception |
|
12 | - */ |
|
13 | - private $exception; |
|
14 | - |
|
15 | - public function __construct(Exception $exception) |
|
16 | - { |
|
17 | - $this->exception = $exception; |
|
18 | - } |
|
19 | - |
|
20 | - /** |
|
21 | - * {@inheritdoc} |
|
22 | - */ |
|
23 | - public function then(callable $onFulfilled = null, callable $onRejected = null) |
|
24 | - { |
|
25 | - if (null === $onRejected) { |
|
26 | - return $this; |
|
27 | - } |
|
28 | - |
|
29 | - try { |
|
30 | - $result = $onRejected($this->exception); |
|
31 | - if ($result instanceof Promise) { |
|
32 | - return $result; |
|
33 | - } |
|
34 | - |
|
35 | - return new HttpFulfilledPromise($result); |
|
36 | - } catch (Exception $e) { |
|
37 | - return new self($e); |
|
38 | - } |
|
39 | - } |
|
40 | - |
|
41 | - /** |
|
42 | - * {@inheritdoc} |
|
43 | - */ |
|
44 | - public function getState() |
|
45 | - { |
|
46 | - return Promise::REJECTED; |
|
47 | - } |
|
48 | - |
|
49 | - /** |
|
50 | - * {@inheritdoc} |
|
51 | - */ |
|
52 | - public function wait($unwrap = true) |
|
53 | - { |
|
54 | - if ($unwrap) { |
|
55 | - throw $this->exception; |
|
56 | - } |
|
57 | - } |
|
10 | + /** |
|
11 | + * @var Exception |
|
12 | + */ |
|
13 | + private $exception; |
|
14 | + |
|
15 | + public function __construct(Exception $exception) |
|
16 | + { |
|
17 | + $this->exception = $exception; |
|
18 | + } |
|
19 | + |
|
20 | + /** |
|
21 | + * {@inheritdoc} |
|
22 | + */ |
|
23 | + public function then(callable $onFulfilled = null, callable $onRejected = null) |
|
24 | + { |
|
25 | + if (null === $onRejected) { |
|
26 | + return $this; |
|
27 | + } |
|
28 | + |
|
29 | + try { |
|
30 | + $result = $onRejected($this->exception); |
|
31 | + if ($result instanceof Promise) { |
|
32 | + return $result; |
|
33 | + } |
|
34 | + |
|
35 | + return new HttpFulfilledPromise($result); |
|
36 | + } catch (Exception $e) { |
|
37 | + return new self($e); |
|
38 | + } |
|
39 | + } |
|
40 | + |
|
41 | + /** |
|
42 | + * {@inheritdoc} |
|
43 | + */ |
|
44 | + public function getState() |
|
45 | + { |
|
46 | + return Promise::REJECTED; |
|
47 | + } |
|
48 | + |
|
49 | + /** |
|
50 | + * {@inheritdoc} |
|
51 | + */ |
|
52 | + public function wait($unwrap = true) |
|
53 | + { |
|
54 | + if ($unwrap) { |
|
55 | + throw $this->exception; |
|
56 | + } |
|
57 | + } |
|
58 | 58 | } |
@@ -5,8 +5,7 @@ |
||
5 | 5 | use OCA\FullTextSearch_Elasticsearch\Vendor\Http\Client\Exception; |
6 | 6 | use OCA\FullTextSearch_Elasticsearch\Vendor\Http\Promise\Promise; |
7 | 7 | |
8 | -final class HttpRejectedPromise implements Promise |
|
9 | -{ |
|
8 | +final class HttpRejectedPromise implements Promise { |
|
10 | 9 | /** |
11 | 10 | * @var Exception |
12 | 11 | */ |
@@ -48,420 +48,420 @@ |
||
48 | 48 | |
49 | 49 | final class Transport implements ClientInterface, HttpAsyncClient |
50 | 50 | { |
51 | - const VERSION = "8.8.0"; |
|
52 | - |
|
53 | - private ClientInterface $client; |
|
54 | - private LoggerInterface $logger; |
|
55 | - private NodePoolInterface $nodePool; |
|
56 | - private array $headers = []; |
|
57 | - private string $user; |
|
58 | - private string $password; |
|
59 | - private RequestInterface $lastRequest; |
|
60 | - private ResponseInterface $lastResponse; |
|
61 | - private string $OSVersion; |
|
62 | - private int $retries = 0; |
|
63 | - private HttpAsyncClient $asyncClient; |
|
64 | - private OnSuccessInterface $onAsyncSuccess; |
|
65 | - private OnFailureInterface $onAsyncFailure; |
|
66 | - |
|
67 | - public function __construct( |
|
68 | - ClientInterface $client, |
|
69 | - NodePoolInterface $nodePool, |
|
70 | - LoggerInterface $logger |
|
71 | - ) { |
|
72 | - $this->client = $client; |
|
73 | - $this->nodePool = $nodePool; |
|
74 | - $this->logger = $logger; |
|
75 | - } |
|
76 | - |
|
77 | - public function getClient(): ClientInterface |
|
78 | - { |
|
79 | - return $this->client; |
|
80 | - } |
|
81 | - |
|
82 | - public function getNodePool(): NodePoolInterface |
|
83 | - { |
|
84 | - return $this->nodePool; |
|
85 | - } |
|
86 | - |
|
87 | - public function getLogger(): LoggerInterface |
|
88 | - { |
|
89 | - return $this->logger; |
|
90 | - } |
|
91 | - |
|
92 | - public function setHeader(string $name, string $value): self |
|
93 | - { |
|
94 | - $this->headers[$name] = $value; |
|
95 | - return $this; |
|
96 | - } |
|
97 | - |
|
98 | - /** |
|
99 | - * @throws InvalidArgumentException |
|
100 | - */ |
|
101 | - public function setRetries(int $num): self |
|
102 | - { |
|
103 | - if ($num < 0) { |
|
104 | - throw new InvalidArgumentException('The retries number must be a positive integer'); |
|
105 | - } |
|
106 | - $this->retries = $num; |
|
107 | - return $this; |
|
108 | - } |
|
109 | - |
|
110 | - public function getRetries(): int |
|
111 | - { |
|
112 | - return $this->retries; |
|
113 | - } |
|
114 | - |
|
115 | - public function getHeaders(): array |
|
116 | - { |
|
117 | - return $this->headers; |
|
118 | - } |
|
119 | - |
|
120 | - public function setUserInfo(string $user, string $password = ''): self |
|
121 | - { |
|
122 | - $this->user = $user; |
|
123 | - $this->password = $password; |
|
124 | - return $this; |
|
125 | - } |
|
126 | - |
|
127 | - public function setUserAgent(string $name, string $version): self |
|
128 | - { |
|
129 | - $this->headers['User-Agent'] = sprintf( |
|
130 | - "%s/%s (%s %s; PHP %s)", |
|
131 | - $name, |
|
132 | - $version, |
|
133 | - PHP_OS, |
|
134 | - $this->getOSVersion(), |
|
135 | - phpversion() |
|
136 | - ); |
|
137 | - return $this; |
|
138 | - } |
|
139 | - |
|
140 | - /** |
|
141 | - * Set the x-elastic-client-meta header |
|
142 | - * |
|
143 | - * The header format is specified by the following regex: |
|
144 | - * ^[a-z]{1,}=[a-z0-9\.\-]{1,}(?:,[a-z]{1,}=[a-z0-9\.\-]+)*$ |
|
145 | - */ |
|
146 | - public function setElasticMetaHeader(string $clientName, string $clientVersion, bool $async = false): self |
|
147 | - { |
|
148 | - $phpSemVersion = sprintf("%d.%d.%d", PHP_MAJOR_VERSION, PHP_MINOR_VERSION, PHP_RELEASE_VERSION); |
|
149 | - $meta = sprintf( |
|
150 | - "%s=%s,php=%s,t=%s,a=%d", |
|
151 | - $clientName, |
|
152 | - $this->purgePreReleaseTag($clientVersion), |
|
153 | - $phpSemVersion, |
|
154 | - $this->purgePreReleaseTag(self::VERSION), |
|
155 | - $async ? 1 : 0 // 0=syncronous, 1=asynchronous |
|
156 | - ); |
|
157 | - $lib = $this->getClientLibraryInfo(); |
|
158 | - if (!empty($lib)) { |
|
159 | - $meta .= sprintf(",%s=%s", $lib[0], $lib[1]); |
|
160 | - } |
|
161 | - $this->headers['x-elastic-client-meta'] = $meta; |
|
162 | - return $this; |
|
163 | - } |
|
164 | - |
|
165 | - /** |
|
166 | - * Remove pre-release suffix with a single 'p' letter |
|
167 | - */ |
|
168 | - private function purgePreReleaseTag(string $version): string |
|
169 | - { |
|
170 | - return str_replace(['alpha', 'beta', 'snapshot', 'rc', 'pre'], 'p', strtolower($version)); |
|
171 | - } |
|
172 | - |
|
173 | - public function getLastRequest(): RequestInterface |
|
174 | - { |
|
175 | - return $this->lastRequest; |
|
176 | - } |
|
177 | - |
|
178 | - public function getLastResponse(): ResponseInterface |
|
179 | - { |
|
180 | - return $this->lastResponse; |
|
181 | - } |
|
182 | - |
|
183 | - /** |
|
184 | - * Setup the headers, if not already present |
|
185 | - */ |
|
186 | - private function setupHeaders(RequestInterface $request): RequestInterface |
|
187 | - { |
|
188 | - foreach ($this->headers as $name => $value) { |
|
189 | - if (!$request->hasHeader($name)) { |
|
190 | - $request = $request->withHeader($name, $value); |
|
191 | - } |
|
192 | - } |
|
193 | - return $request; |
|
194 | - } |
|
195 | - |
|
196 | - /** |
|
197 | - * Setup the user info, if not already present |
|
198 | - */ |
|
199 | - private function setupUserInfo(RequestInterface $request): RequestInterface |
|
200 | - { |
|
201 | - $uri = $request->getUri(); |
|
202 | - if (empty($uri->getUserInfo())) { |
|
203 | - if (isset($this->user)) { |
|
204 | - $request = $request->withUri($uri->withUserInfo($this->user, $this->password)); |
|
205 | - } |
|
206 | - } |
|
207 | - return $request; |
|
208 | - } |
|
209 | - |
|
210 | - /** |
|
211 | - * Setup the connection Uri |
|
212 | - */ |
|
213 | - private function setupConnectionUri(Node $node, RequestInterface $request): RequestInterface |
|
214 | - { |
|
215 | - $uri = $node->getUri(); |
|
216 | - $path = $request->getUri()->getPath(); |
|
51 | + const VERSION = "8.8.0"; |
|
52 | + |
|
53 | + private ClientInterface $client; |
|
54 | + private LoggerInterface $logger; |
|
55 | + private NodePoolInterface $nodePool; |
|
56 | + private array $headers = []; |
|
57 | + private string $user; |
|
58 | + private string $password; |
|
59 | + private RequestInterface $lastRequest; |
|
60 | + private ResponseInterface $lastResponse; |
|
61 | + private string $OSVersion; |
|
62 | + private int $retries = 0; |
|
63 | + private HttpAsyncClient $asyncClient; |
|
64 | + private OnSuccessInterface $onAsyncSuccess; |
|
65 | + private OnFailureInterface $onAsyncFailure; |
|
66 | + |
|
67 | + public function __construct( |
|
68 | + ClientInterface $client, |
|
69 | + NodePoolInterface $nodePool, |
|
70 | + LoggerInterface $logger |
|
71 | + ) { |
|
72 | + $this->client = $client; |
|
73 | + $this->nodePool = $nodePool; |
|
74 | + $this->logger = $logger; |
|
75 | + } |
|
76 | + |
|
77 | + public function getClient(): ClientInterface |
|
78 | + { |
|
79 | + return $this->client; |
|
80 | + } |
|
81 | + |
|
82 | + public function getNodePool(): NodePoolInterface |
|
83 | + { |
|
84 | + return $this->nodePool; |
|
85 | + } |
|
86 | + |
|
87 | + public function getLogger(): LoggerInterface |
|
88 | + { |
|
89 | + return $this->logger; |
|
90 | + } |
|
91 | + |
|
92 | + public function setHeader(string $name, string $value): self |
|
93 | + { |
|
94 | + $this->headers[$name] = $value; |
|
95 | + return $this; |
|
96 | + } |
|
97 | + |
|
98 | + /** |
|
99 | + * @throws InvalidArgumentException |
|
100 | + */ |
|
101 | + public function setRetries(int $num): self |
|
102 | + { |
|
103 | + if ($num < 0) { |
|
104 | + throw new InvalidArgumentException('The retries number must be a positive integer'); |
|
105 | + } |
|
106 | + $this->retries = $num; |
|
107 | + return $this; |
|
108 | + } |
|
109 | + |
|
110 | + public function getRetries(): int |
|
111 | + { |
|
112 | + return $this->retries; |
|
113 | + } |
|
114 | + |
|
115 | + public function getHeaders(): array |
|
116 | + { |
|
117 | + return $this->headers; |
|
118 | + } |
|
119 | + |
|
120 | + public function setUserInfo(string $user, string $password = ''): self |
|
121 | + { |
|
122 | + $this->user = $user; |
|
123 | + $this->password = $password; |
|
124 | + return $this; |
|
125 | + } |
|
126 | + |
|
127 | + public function setUserAgent(string $name, string $version): self |
|
128 | + { |
|
129 | + $this->headers['User-Agent'] = sprintf( |
|
130 | + "%s/%s (%s %s; PHP %s)", |
|
131 | + $name, |
|
132 | + $version, |
|
133 | + PHP_OS, |
|
134 | + $this->getOSVersion(), |
|
135 | + phpversion() |
|
136 | + ); |
|
137 | + return $this; |
|
138 | + } |
|
139 | + |
|
140 | + /** |
|
141 | + * Set the x-elastic-client-meta header |
|
142 | + * |
|
143 | + * The header format is specified by the following regex: |
|
144 | + * ^[a-z]{1,}=[a-z0-9\.\-]{1,}(?:,[a-z]{1,}=[a-z0-9\.\-]+)*$ |
|
145 | + */ |
|
146 | + public function setElasticMetaHeader(string $clientName, string $clientVersion, bool $async = false): self |
|
147 | + { |
|
148 | + $phpSemVersion = sprintf("%d.%d.%d", PHP_MAJOR_VERSION, PHP_MINOR_VERSION, PHP_RELEASE_VERSION); |
|
149 | + $meta = sprintf( |
|
150 | + "%s=%s,php=%s,t=%s,a=%d", |
|
151 | + $clientName, |
|
152 | + $this->purgePreReleaseTag($clientVersion), |
|
153 | + $phpSemVersion, |
|
154 | + $this->purgePreReleaseTag(self::VERSION), |
|
155 | + $async ? 1 : 0 // 0=syncronous, 1=asynchronous |
|
156 | + ); |
|
157 | + $lib = $this->getClientLibraryInfo(); |
|
158 | + if (!empty($lib)) { |
|
159 | + $meta .= sprintf(",%s=%s", $lib[0], $lib[1]); |
|
160 | + } |
|
161 | + $this->headers['x-elastic-client-meta'] = $meta; |
|
162 | + return $this; |
|
163 | + } |
|
164 | + |
|
165 | + /** |
|
166 | + * Remove pre-release suffix with a single 'p' letter |
|
167 | + */ |
|
168 | + private function purgePreReleaseTag(string $version): string |
|
169 | + { |
|
170 | + return str_replace(['alpha', 'beta', 'snapshot', 'rc', 'pre'], 'p', strtolower($version)); |
|
171 | + } |
|
172 | + |
|
173 | + public function getLastRequest(): RequestInterface |
|
174 | + { |
|
175 | + return $this->lastRequest; |
|
176 | + } |
|
177 | + |
|
178 | + public function getLastResponse(): ResponseInterface |
|
179 | + { |
|
180 | + return $this->lastResponse; |
|
181 | + } |
|
182 | + |
|
183 | + /** |
|
184 | + * Setup the headers, if not already present |
|
185 | + */ |
|
186 | + private function setupHeaders(RequestInterface $request): RequestInterface |
|
187 | + { |
|
188 | + foreach ($this->headers as $name => $value) { |
|
189 | + if (!$request->hasHeader($name)) { |
|
190 | + $request = $request->withHeader($name, $value); |
|
191 | + } |
|
192 | + } |
|
193 | + return $request; |
|
194 | + } |
|
195 | + |
|
196 | + /** |
|
197 | + * Setup the user info, if not already present |
|
198 | + */ |
|
199 | + private function setupUserInfo(RequestInterface $request): RequestInterface |
|
200 | + { |
|
201 | + $uri = $request->getUri(); |
|
202 | + if (empty($uri->getUserInfo())) { |
|
203 | + if (isset($this->user)) { |
|
204 | + $request = $request->withUri($uri->withUserInfo($this->user, $this->password)); |
|
205 | + } |
|
206 | + } |
|
207 | + return $request; |
|
208 | + } |
|
209 | + |
|
210 | + /** |
|
211 | + * Setup the connection Uri |
|
212 | + */ |
|
213 | + private function setupConnectionUri(Node $node, RequestInterface $request): RequestInterface |
|
214 | + { |
|
215 | + $uri = $node->getUri(); |
|
216 | + $path = $request->getUri()->getPath(); |
|
217 | 217 | |
218 | - $nodePath = $uri->getPath(); |
|
219 | - // If the node has a path we need to use it as prefix for the existing path |
|
220 | - // @see https://github.com/elastic/elastic-transport-php/pull/20 |
|
221 | - if (!empty($nodePath)) { |
|
222 | - $path = sprintf("%s/%s", rtrim($nodePath, '/'), ltrim($path,'/')); |
|
223 | - } |
|
224 | - // If the user information is not in the request, we check if it is present in the node uri |
|
225 | - // @see https://github.com/elastic/elastic-transport-php/issues/18 |
|
226 | - if (empty($request->getUri()->getUserInfo()) && !empty($uri->getUserInfo())) { |
|
227 | - $userInfo = explode(':', $uri->getUserInfo()); |
|
228 | - $request = $request->withUri( |
|
229 | - $request->getUri() |
|
230 | - ->withUserInfo($userInfo[0], $userInfo[1] ?? null) |
|
231 | - ); |
|
232 | - } |
|
233 | - return $request->withUri( |
|
234 | - $request->getUri() |
|
235 | - ->withHost($uri->getHost()) |
|
236 | - ->withPort($uri->getPort()) |
|
237 | - ->withScheme($uri->getScheme()) |
|
238 | - ->withPath($path) |
|
239 | - ); |
|
240 | - } |
|
241 | - |
|
242 | - private function decorateRequest(RequestInterface $request): RequestInterface |
|
243 | - { |
|
244 | - $request = $this->setupHeaders($request); |
|
245 | - return $this->setupUserInfo($request); |
|
246 | - } |
|
247 | - |
|
248 | - private function logHeaders(MessageInterface $message): void |
|
249 | - { |
|
250 | - $this->logger->debug(sprintf( |
|
251 | - "Headers: %s\nBody: %s", |
|
252 | - json_encode($message->getHeaders()), |
|
253 | - (string) $message->getBody() |
|
254 | - )); |
|
255 | - } |
|
256 | - |
|
257 | - private function logRequest(string $title, RequestInterface $request): void |
|
258 | - { |
|
259 | - $this->logger->info(sprintf( |
|
260 | - "%s: %s %s", |
|
261 | - $title, |
|
262 | - $request->getMethod(), |
|
263 | - (string) $request->getUri() |
|
264 | - ), [ |
|
265 | - 'request' => $request |
|
266 | - ]); |
|
267 | - $this->logHeaders($request); |
|
268 | - } |
|
269 | - |
|
270 | - private function logResponse(string $title, ResponseInterface $response, int $retry): void |
|
271 | - { |
|
272 | - $this->logger->info(sprintf( |
|
273 | - "%s (retry %d): %d", |
|
274 | - $title, |
|
275 | - $retry, |
|
276 | - $response->getStatusCode() |
|
277 | - ), [ |
|
278 | - 'response' => $response, |
|
279 | - 'retry' => $retry |
|
280 | - ]); |
|
281 | - $this->logHeaders($response); |
|
282 | - } |
|
283 | - |
|
284 | - /** |
|
285 | - * @throws NoNodeAvailableException |
|
286 | - * @throws ClientExceptionInterface |
|
287 | - */ |
|
288 | - public function sendRequest(RequestInterface $request): ResponseInterface |
|
289 | - { |
|
290 | - if (empty($request->getUri()->getHost())) { |
|
291 | - $node = $this->nodePool->nextNode(); |
|
292 | - $request = $this->setupConnectionUri($node, $request); |
|
293 | - } |
|
294 | - $request = $this->decorateRequest($request); |
|
295 | - $this->lastRequest = $request; |
|
296 | - $this->logRequest("Request", $request); |
|
218 | + $nodePath = $uri->getPath(); |
|
219 | + // If the node has a path we need to use it as prefix for the existing path |
|
220 | + // @see https://github.com/elastic/elastic-transport-php/pull/20 |
|
221 | + if (!empty($nodePath)) { |
|
222 | + $path = sprintf("%s/%s", rtrim($nodePath, '/'), ltrim($path,'/')); |
|
223 | + } |
|
224 | + // If the user information is not in the request, we check if it is present in the node uri |
|
225 | + // @see https://github.com/elastic/elastic-transport-php/issues/18 |
|
226 | + if (empty($request->getUri()->getUserInfo()) && !empty($uri->getUserInfo())) { |
|
227 | + $userInfo = explode(':', $uri->getUserInfo()); |
|
228 | + $request = $request->withUri( |
|
229 | + $request->getUri() |
|
230 | + ->withUserInfo($userInfo[0], $userInfo[1] ?? null) |
|
231 | + ); |
|
232 | + } |
|
233 | + return $request->withUri( |
|
234 | + $request->getUri() |
|
235 | + ->withHost($uri->getHost()) |
|
236 | + ->withPort($uri->getPort()) |
|
237 | + ->withScheme($uri->getScheme()) |
|
238 | + ->withPath($path) |
|
239 | + ); |
|
240 | + } |
|
241 | + |
|
242 | + private function decorateRequest(RequestInterface $request): RequestInterface |
|
243 | + { |
|
244 | + $request = $this->setupHeaders($request); |
|
245 | + return $this->setupUserInfo($request); |
|
246 | + } |
|
247 | + |
|
248 | + private function logHeaders(MessageInterface $message): void |
|
249 | + { |
|
250 | + $this->logger->debug(sprintf( |
|
251 | + "Headers: %s\nBody: %s", |
|
252 | + json_encode($message->getHeaders()), |
|
253 | + (string) $message->getBody() |
|
254 | + )); |
|
255 | + } |
|
256 | + |
|
257 | + private function logRequest(string $title, RequestInterface $request): void |
|
258 | + { |
|
259 | + $this->logger->info(sprintf( |
|
260 | + "%s: %s %s", |
|
261 | + $title, |
|
262 | + $request->getMethod(), |
|
263 | + (string) $request->getUri() |
|
264 | + ), [ |
|
265 | + 'request' => $request |
|
266 | + ]); |
|
267 | + $this->logHeaders($request); |
|
268 | + } |
|
269 | + |
|
270 | + private function logResponse(string $title, ResponseInterface $response, int $retry): void |
|
271 | + { |
|
272 | + $this->logger->info(sprintf( |
|
273 | + "%s (retry %d): %d", |
|
274 | + $title, |
|
275 | + $retry, |
|
276 | + $response->getStatusCode() |
|
277 | + ), [ |
|
278 | + 'response' => $response, |
|
279 | + 'retry' => $retry |
|
280 | + ]); |
|
281 | + $this->logHeaders($response); |
|
282 | + } |
|
283 | + |
|
284 | + /** |
|
285 | + * @throws NoNodeAvailableException |
|
286 | + * @throws ClientExceptionInterface |
|
287 | + */ |
|
288 | + public function sendRequest(RequestInterface $request): ResponseInterface |
|
289 | + { |
|
290 | + if (empty($request->getUri()->getHost())) { |
|
291 | + $node = $this->nodePool->nextNode(); |
|
292 | + $request = $this->setupConnectionUri($node, $request); |
|
293 | + } |
|
294 | + $request = $this->decorateRequest($request); |
|
295 | + $this->lastRequest = $request; |
|
296 | + $this->logRequest("Request", $request); |
|
297 | 297 | |
298 | - $count = -1; |
|
299 | - while ($count < $this->getRetries()) { |
|
300 | - try { |
|
301 | - $count++; |
|
302 | - $response = $this->client->sendRequest($request); |
|
303 | - |
|
304 | - $this->lastResponse = $response; |
|
305 | - $this->logResponse("Response", $response, $count); |
|
306 | - |
|
307 | - return $response; |
|
308 | - } catch (NetworkExceptionInterface $e) { |
|
309 | - $this->logger->error(sprintf("Retry %d: %s", $count, $e->getMessage())); |
|
310 | - if (isset($node)) { |
|
311 | - $node->markAlive(false); |
|
312 | - $node = $this->nodePool->nextNode(); |
|
313 | - $request = $this->setupConnectionUri($node, $request); |
|
314 | - } |
|
315 | - } catch (ClientExceptionInterface $e) { |
|
316 | - $this->logger->error(sprintf("Retry %d: %s", $count, $e->getMessage())); |
|
317 | - throw $e; |
|
318 | - } |
|
319 | - } |
|
320 | - $exceededMsg = sprintf("Exceeded maximum number of retries (%d)", $this->getRetries()); |
|
321 | - $this->logger->error($exceededMsg); |
|
322 | - throw new NoNodeAvailableException($exceededMsg); |
|
323 | - } |
|
324 | - |
|
325 | - public function setAsyncClient(HttpAsyncClient $asyncClient): self |
|
326 | - { |
|
327 | - $this->asyncClient = $asyncClient; |
|
328 | - return $this; |
|
329 | - } |
|
330 | - |
|
331 | - /** |
|
332 | - * @throws NoAsyncClientException |
|
333 | - */ |
|
334 | - public function getAsyncClient(): HttpAsyncClient |
|
335 | - { |
|
336 | - if (!empty($this->asyncClient)) { |
|
337 | - return $this->asyncClient; |
|
338 | - } |
|
339 | - if ($this->client instanceof HttpAsyncClient) { |
|
340 | - return $this->client; |
|
341 | - } |
|
342 | - try { |
|
343 | - $this->asyncClient = HttpAsyncClientDiscovery::find(); |
|
344 | - } catch (Exception $e) { |
|
345 | - throw new NoAsyncClientException(sprintf( |
|
346 | - "I did not find any HTTP library with HttpAsyncClient interface. " . |
|
347 | - "Make sure to install a package providing \"php-http/async-client-implementation\". " . |
|
348 | - "You can also set a specific async library using %s::setAsyncClient()", |
|
349 | - self::class |
|
350 | - )); |
|
351 | - } |
|
352 | - return $this->asyncClient; |
|
353 | - } |
|
354 | - |
|
355 | - public function setAsyncOnSuccess(OnSuccessInterface $success): self |
|
356 | - { |
|
357 | - $this->onAsyncSuccess = $success; |
|
358 | - return $this; |
|
359 | - } |
|
360 | - |
|
361 | - public function getAsyncOnSuccess(): OnSuccessInterface |
|
362 | - { |
|
363 | - if (empty($this->onAsyncSuccess)) { |
|
364 | - $this->onAsyncSuccess = new OnSuccessDefault(); |
|
365 | - } |
|
366 | - return $this->onAsyncSuccess; |
|
367 | - } |
|
368 | - |
|
369 | - public function setAsyncOnFailure(OnFailureInterface $failure): self |
|
370 | - { |
|
371 | - $this->onAsyncFailure = $failure; |
|
372 | - return $this; |
|
373 | - } |
|
374 | - |
|
375 | - public function getAsyncOnFailure(): OnFailureInterface |
|
376 | - { |
|
377 | - if (empty($this->onAsyncFailure)) { |
|
378 | - $this->onAsyncFailure = new OnFailureDefault(); |
|
379 | - } |
|
380 | - return $this->onAsyncFailure; |
|
381 | - } |
|
382 | - |
|
383 | - /** |
|
384 | - * @throws Exception |
|
385 | - */ |
|
386 | - public function sendAsyncRequest(RequestInterface $request): Promise |
|
387 | - { |
|
388 | - $client = $this->getAsyncClient(); |
|
389 | - $node = null; |
|
390 | - if (empty($request->getUri()->getHost())) { |
|
391 | - $node = $this->nodePool->nextNode(); |
|
392 | - $request = $this->setupConnectionUri($node, $request); |
|
393 | - } |
|
394 | - $request = $this->decorateRequest($request); |
|
395 | - $this->lastRequest = $request; |
|
396 | - $this->logRequest("Async Request", $request); |
|
397 | - |
|
398 | - $count = 0; |
|
399 | - $promise = $client->sendAsyncRequest($request); |
|
400 | - |
|
401 | - // onFulfilled callable |
|
402 | - $onFulfilled = function (ResponseInterface $response) use (&$count) { |
|
403 | - $this->lastResponse = $response; |
|
404 | - $this->logResponse("Async Response", $response, $count); |
|
405 | - return $this->getAsyncOnSuccess()->success($response, $count); |
|
406 | - }; |
|
407 | - |
|
408 | - // onRejected callable |
|
409 | - $onRejected = function (Exception $e) use ($client, $request, &$count, $node) { |
|
410 | - $this->logger->error(sprintf("Retry %d: %s", $count, $e->getMessage())); |
|
411 | - $this->getAsyncOnFailure()->failure($e, $request, $count, $node ?? null); |
|
412 | - if (isset($node)) { |
|
413 | - $node->markAlive(false); |
|
414 | - $node = $this->nodePool->nextNode(); |
|
415 | - $request = $this->setupConnectionUri($node, $request); |
|
416 | - } |
|
417 | - $count++; |
|
418 | - return $client->sendAsyncRequest($request); |
|
419 | - }; |
|
298 | + $count = -1; |
|
299 | + while ($count < $this->getRetries()) { |
|
300 | + try { |
|
301 | + $count++; |
|
302 | + $response = $this->client->sendRequest($request); |
|
303 | + |
|
304 | + $this->lastResponse = $response; |
|
305 | + $this->logResponse("Response", $response, $count); |
|
306 | + |
|
307 | + return $response; |
|
308 | + } catch (NetworkExceptionInterface $e) { |
|
309 | + $this->logger->error(sprintf("Retry %d: %s", $count, $e->getMessage())); |
|
310 | + if (isset($node)) { |
|
311 | + $node->markAlive(false); |
|
312 | + $node = $this->nodePool->nextNode(); |
|
313 | + $request = $this->setupConnectionUri($node, $request); |
|
314 | + } |
|
315 | + } catch (ClientExceptionInterface $e) { |
|
316 | + $this->logger->error(sprintf("Retry %d: %s", $count, $e->getMessage())); |
|
317 | + throw $e; |
|
318 | + } |
|
319 | + } |
|
320 | + $exceededMsg = sprintf("Exceeded maximum number of retries (%d)", $this->getRetries()); |
|
321 | + $this->logger->error($exceededMsg); |
|
322 | + throw new NoNodeAvailableException($exceededMsg); |
|
323 | + } |
|
324 | + |
|
325 | + public function setAsyncClient(HttpAsyncClient $asyncClient): self |
|
326 | + { |
|
327 | + $this->asyncClient = $asyncClient; |
|
328 | + return $this; |
|
329 | + } |
|
330 | + |
|
331 | + /** |
|
332 | + * @throws NoAsyncClientException |
|
333 | + */ |
|
334 | + public function getAsyncClient(): HttpAsyncClient |
|
335 | + { |
|
336 | + if (!empty($this->asyncClient)) { |
|
337 | + return $this->asyncClient; |
|
338 | + } |
|
339 | + if ($this->client instanceof HttpAsyncClient) { |
|
340 | + return $this->client; |
|
341 | + } |
|
342 | + try { |
|
343 | + $this->asyncClient = HttpAsyncClientDiscovery::find(); |
|
344 | + } catch (Exception $e) { |
|
345 | + throw new NoAsyncClientException(sprintf( |
|
346 | + "I did not find any HTTP library with HttpAsyncClient interface. " . |
|
347 | + "Make sure to install a package providing \"php-http/async-client-implementation\". " . |
|
348 | + "You can also set a specific async library using %s::setAsyncClient()", |
|
349 | + self::class |
|
350 | + )); |
|
351 | + } |
|
352 | + return $this->asyncClient; |
|
353 | + } |
|
354 | + |
|
355 | + public function setAsyncOnSuccess(OnSuccessInterface $success): self |
|
356 | + { |
|
357 | + $this->onAsyncSuccess = $success; |
|
358 | + return $this; |
|
359 | + } |
|
360 | + |
|
361 | + public function getAsyncOnSuccess(): OnSuccessInterface |
|
362 | + { |
|
363 | + if (empty($this->onAsyncSuccess)) { |
|
364 | + $this->onAsyncSuccess = new OnSuccessDefault(); |
|
365 | + } |
|
366 | + return $this->onAsyncSuccess; |
|
367 | + } |
|
368 | + |
|
369 | + public function setAsyncOnFailure(OnFailureInterface $failure): self |
|
370 | + { |
|
371 | + $this->onAsyncFailure = $failure; |
|
372 | + return $this; |
|
373 | + } |
|
374 | + |
|
375 | + public function getAsyncOnFailure(): OnFailureInterface |
|
376 | + { |
|
377 | + if (empty($this->onAsyncFailure)) { |
|
378 | + $this->onAsyncFailure = new OnFailureDefault(); |
|
379 | + } |
|
380 | + return $this->onAsyncFailure; |
|
381 | + } |
|
382 | + |
|
383 | + /** |
|
384 | + * @throws Exception |
|
385 | + */ |
|
386 | + public function sendAsyncRequest(RequestInterface $request): Promise |
|
387 | + { |
|
388 | + $client = $this->getAsyncClient(); |
|
389 | + $node = null; |
|
390 | + if (empty($request->getUri()->getHost())) { |
|
391 | + $node = $this->nodePool->nextNode(); |
|
392 | + $request = $this->setupConnectionUri($node, $request); |
|
393 | + } |
|
394 | + $request = $this->decorateRequest($request); |
|
395 | + $this->lastRequest = $request; |
|
396 | + $this->logRequest("Async Request", $request); |
|
397 | + |
|
398 | + $count = 0; |
|
399 | + $promise = $client->sendAsyncRequest($request); |
|
400 | + |
|
401 | + // onFulfilled callable |
|
402 | + $onFulfilled = function (ResponseInterface $response) use (&$count) { |
|
403 | + $this->lastResponse = $response; |
|
404 | + $this->logResponse("Async Response", $response, $count); |
|
405 | + return $this->getAsyncOnSuccess()->success($response, $count); |
|
406 | + }; |
|
407 | + |
|
408 | + // onRejected callable |
|
409 | + $onRejected = function (Exception $e) use ($client, $request, &$count, $node) { |
|
410 | + $this->logger->error(sprintf("Retry %d: %s", $count, $e->getMessage())); |
|
411 | + $this->getAsyncOnFailure()->failure($e, $request, $count, $node ?? null); |
|
412 | + if (isset($node)) { |
|
413 | + $node->markAlive(false); |
|
414 | + $node = $this->nodePool->nextNode(); |
|
415 | + $request = $this->setupConnectionUri($node, $request); |
|
416 | + } |
|
417 | + $count++; |
|
418 | + return $client->sendAsyncRequest($request); |
|
419 | + }; |
|
420 | 420 | |
421 | - // Add getRetries() callables using then() |
|
422 | - for ($i=0; $i < $this->getRetries(); $i++) { |
|
423 | - $promise = $promise->then($onFulfilled, $onRejected); |
|
424 | - } |
|
425 | - // Add the last getRetries()+1 callable for managing the exceeded error |
|
426 | - $promise = $promise->then($onFulfilled, function(Exception $e) use (&$count) { |
|
427 | - $exceededMsg = sprintf("Exceeded maximum number of retries (%d)", $this->getRetries()); |
|
428 | - $this->logger->error(sprintf("Retry %d: %s", $count, $e->getMessage())); |
|
429 | - $this->logger->error($exceededMsg); |
|
430 | - throw new NoNodeAvailableException(sprintf("%s: %s", $exceededMsg, $e->getMessage())); |
|
431 | - }); |
|
432 | - return $promise; |
|
433 | - } |
|
434 | - |
|
435 | - /** |
|
436 | - * Get the OS version using php_uname if available |
|
437 | - * otherwise it returns an empty string |
|
438 | - */ |
|
439 | - private function getOSVersion(): string |
|
440 | - { |
|
441 | - if (!isset($this->OSVersion)) { |
|
442 | - $disable_functions = (string) ini_get('disable_functions'); |
|
443 | - $this->OSVersion = strpos(strtolower($disable_functions), 'php_uname') !== false |
|
444 | - ? '' |
|
445 | - : php_uname("r"); |
|
446 | - } |
|
447 | - return $this->OSVersion; |
|
448 | - } |
|
449 | - |
|
450 | - /** |
|
451 | - * Returns the name and the version of the Client HTTP library used |
|
452 | - * Here a list of supported libraries: |
|
453 | - * gu => guzzlehttp/guzzle |
|
454 | - * sy => symfony/http-client |
|
455 | - */ |
|
456 | - private function getClientLibraryInfo(): array |
|
457 | - { |
|
458 | - $clientClass = get_class($this->client); |
|
459 | - if (false !== strpos($clientClass, 'GuzzleHttp\Client')) { |
|
460 | - return ['gu', InstalledVersions::getPrettyVersion('guzzlehttp/guzzle')]; |
|
461 | - } |
|
462 | - if (false !== strpos($clientClass, 'Symfony\Component\HttpClient')) { |
|
463 | - return ['sy', InstalledVersions::getPrettyVersion('symfony/http-client')]; |
|
464 | - } |
|
465 | - return []; |
|
466 | - } |
|
421 | + // Add getRetries() callables using then() |
|
422 | + for ($i=0; $i < $this->getRetries(); $i++) { |
|
423 | + $promise = $promise->then($onFulfilled, $onRejected); |
|
424 | + } |
|
425 | + // Add the last getRetries()+1 callable for managing the exceeded error |
|
426 | + $promise = $promise->then($onFulfilled, function(Exception $e) use (&$count) { |
|
427 | + $exceededMsg = sprintf("Exceeded maximum number of retries (%d)", $this->getRetries()); |
|
428 | + $this->logger->error(sprintf("Retry %d: %s", $count, $e->getMessage())); |
|
429 | + $this->logger->error($exceededMsg); |
|
430 | + throw new NoNodeAvailableException(sprintf("%s: %s", $exceededMsg, $e->getMessage())); |
|
431 | + }); |
|
432 | + return $promise; |
|
433 | + } |
|
434 | + |
|
435 | + /** |
|
436 | + * Get the OS version using php_uname if available |
|
437 | + * otherwise it returns an empty string |
|
438 | + */ |
|
439 | + private function getOSVersion(): string |
|
440 | + { |
|
441 | + if (!isset($this->OSVersion)) { |
|
442 | + $disable_functions = (string) ini_get('disable_functions'); |
|
443 | + $this->OSVersion = strpos(strtolower($disable_functions), 'php_uname') !== false |
|
444 | + ? '' |
|
445 | + : php_uname("r"); |
|
446 | + } |
|
447 | + return $this->OSVersion; |
|
448 | + } |
|
449 | + |
|
450 | + /** |
|
451 | + * Returns the name and the version of the Client HTTP library used |
|
452 | + * Here a list of supported libraries: |
|
453 | + * gu => guzzlehttp/guzzle |
|
454 | + * sy => symfony/http-client |
|
455 | + */ |
|
456 | + private function getClientLibraryInfo(): array |
|
457 | + { |
|
458 | + $clientClass = get_class($this->client); |
|
459 | + if (false !== strpos($clientClass, 'GuzzleHttp\Client')) { |
|
460 | + return ['gu', InstalledVersions::getPrettyVersion('guzzlehttp/guzzle')]; |
|
461 | + } |
|
462 | + if (false !== strpos($clientClass, 'Symfony\Component\HttpClient')) { |
|
463 | + return ['sy', InstalledVersions::getPrettyVersion('symfony/http-client')]; |
|
464 | + } |
|
465 | + return []; |
|
466 | + } |
|
467 | 467 | } |
@@ -219,7 +219,7 @@ discard block |
||
219 | 219 | // If the node has a path we need to use it as prefix for the existing path |
220 | 220 | // @see https://github.com/elastic/elastic-transport-php/pull/20 |
221 | 221 | if (!empty($nodePath)) { |
222 | - $path = sprintf("%s/%s", rtrim($nodePath, '/'), ltrim($path,'/')); |
|
222 | + $path = sprintf("%s/%s", rtrim($nodePath, '/'), ltrim($path, '/')); |
|
223 | 223 | } |
224 | 224 | // If the user information is not in the request, we check if it is present in the node uri |
225 | 225 | // @see https://github.com/elastic/elastic-transport-php/issues/18 |
@@ -250,7 +250,7 @@ discard block |
||
250 | 250 | $this->logger->debug(sprintf( |
251 | 251 | "Headers: %s\nBody: %s", |
252 | 252 | json_encode($message->getHeaders()), |
253 | - (string) $message->getBody() |
|
253 | + (string)$message->getBody() |
|
254 | 254 | )); |
255 | 255 | } |
256 | 256 | |
@@ -260,7 +260,7 @@ discard block |
||
260 | 260 | "%s: %s %s", |
261 | 261 | $title, |
262 | 262 | $request->getMethod(), |
263 | - (string) $request->getUri() |
|
263 | + (string)$request->getUri() |
|
264 | 264 | ), [ |
265 | 265 | 'request' => $request |
266 | 266 | ]); |
@@ -343,8 +343,8 @@ discard block |
||
343 | 343 | $this->asyncClient = HttpAsyncClientDiscovery::find(); |
344 | 344 | } catch (Exception $e) { |
345 | 345 | throw new NoAsyncClientException(sprintf( |
346 | - "I did not find any HTTP library with HttpAsyncClient interface. " . |
|
347 | - "Make sure to install a package providing \"php-http/async-client-implementation\". " . |
|
346 | + "I did not find any HTTP library with HttpAsyncClient interface. ". |
|
347 | + "Make sure to install a package providing \"php-http/async-client-implementation\". ". |
|
348 | 348 | "You can also set a specific async library using %s::setAsyncClient()", |
349 | 349 | self::class |
350 | 350 | )); |
@@ -399,14 +399,14 @@ discard block |
||
399 | 399 | $promise = $client->sendAsyncRequest($request); |
400 | 400 | |
401 | 401 | // onFulfilled callable |
402 | - $onFulfilled = function (ResponseInterface $response) use (&$count) { |
|
402 | + $onFulfilled = function(ResponseInterface $response) use (&$count) { |
|
403 | 403 | $this->lastResponse = $response; |
404 | 404 | $this->logResponse("Async Response", $response, $count); |
405 | 405 | return $this->getAsyncOnSuccess()->success($response, $count); |
406 | 406 | }; |
407 | 407 | |
408 | 408 | // onRejected callable |
409 | - $onRejected = function (Exception $e) use ($client, $request, &$count, $node) { |
|
409 | + $onRejected = function(Exception $e) use ($client, $request, &$count, $node) { |
|
410 | 410 | $this->logger->error(sprintf("Retry %d: %s", $count, $e->getMessage())); |
411 | 411 | $this->getAsyncOnFailure()->failure($e, $request, $count, $node ?? null); |
412 | 412 | if (isset($node)) { |
@@ -419,7 +419,7 @@ discard block |
||
419 | 419 | }; |
420 | 420 | |
421 | 421 | // Add getRetries() callables using then() |
422 | - for ($i=0; $i < $this->getRetries(); $i++) { |
|
422 | + for ($i = 0; $i < $this->getRetries(); $i++) { |
|
423 | 423 | $promise = $promise->then($onFulfilled, $onRejected); |
424 | 424 | } |
425 | 425 | // Add the last getRetries()+1 callable for managing the exceeded error |
@@ -439,7 +439,7 @@ discard block |
||
439 | 439 | private function getOSVersion(): string |
440 | 440 | { |
441 | 441 | if (!isset($this->OSVersion)) { |
442 | - $disable_functions = (string) ini_get('disable_functions'); |
|
442 | + $disable_functions = (string)ini_get('disable_functions'); |
|
443 | 443 | $this->OSVersion = strpos(strtolower($disable_functions), 'php_uname') !== false |
444 | 444 | ? '' |
445 | 445 | : php_uname("r"); |
@@ -27,106 +27,106 @@ |
||
27 | 27 | |
28 | 28 | class TransportBuilder |
29 | 29 | { |
30 | - protected ClientInterface $client; |
|
31 | - protected NodePoolInterface $nodePool; |
|
32 | - protected LoggerInterface $logger; |
|
33 | - protected array $hosts = []; |
|
34 | - |
|
35 | - final public function __construct() |
|
36 | - { |
|
37 | - } |
|
38 | - |
|
39 | - public static function create(): TransportBuilder |
|
40 | - { |
|
41 | - return new static(); |
|
42 | - } |
|
43 | - |
|
44 | - public function setClient(ClientInterface $client): self |
|
45 | - { |
|
46 | - $this->client = $client; |
|
47 | - return $this; |
|
48 | - } |
|
49 | - |
|
50 | - public function getClient(): ClientInterface |
|
51 | - { |
|
52 | - if (empty($this->client)) { |
|
53 | - $this->client = Psr18ClientDiscovery::find(); |
|
54 | - } |
|
55 | - return $this->client; |
|
56 | - } |
|
57 | - |
|
58 | - public function setNodePool(NodePoolInterface $nodePool): self |
|
59 | - { |
|
60 | - $this->nodePool = $nodePool; |
|
61 | - return $this; |
|
62 | - } |
|
63 | - |
|
64 | - public function getNodePool(): NodePoolInterface |
|
65 | - { |
|
66 | - if (empty($this->nodePool)) { |
|
67 | - $this->nodePool = new SimpleNodePool( |
|
68 | - new RoundRobin(), |
|
69 | - new NoResurrect() |
|
70 | - ); |
|
71 | - } |
|
72 | - return $this->nodePool; |
|
73 | - } |
|
74 | - |
|
75 | - public function setLogger(LoggerInterface $logger): self |
|
76 | - { |
|
77 | - $this->logger = $logger; |
|
78 | - return $this; |
|
79 | - } |
|
80 | - |
|
81 | - public function getLogger(): LoggerInterface |
|
82 | - { |
|
83 | - if (empty($this->logger)) { |
|
84 | - $this->logger = new NullLogger(); |
|
85 | - } |
|
86 | - return $this->logger; |
|
87 | - } |
|
88 | - |
|
89 | - public function setHosts(array $hosts): self |
|
90 | - { |
|
91 | - $this->hosts = $hosts; |
|
92 | - return $this; |
|
93 | - } |
|
94 | - |
|
95 | - public function getHosts(): array |
|
96 | - { |
|
97 | - return $this->hosts; |
|
98 | - } |
|
99 | - |
|
100 | - public function setCloudId(string $cloudId): self |
|
101 | - { |
|
102 | - $this->hosts = [$this->parseElasticCloudId($cloudId)]; |
|
103 | - return $this; |
|
104 | - } |
|
105 | - |
|
106 | - public function build(): Transport |
|
107 | - { |
|
108 | - return new Transport( |
|
109 | - $this->getClient(), |
|
110 | - $this->getNodePool()->setHosts($this->hosts), |
|
111 | - $this->getLogger() |
|
112 | - ); |
|
113 | - } |
|
114 | - |
|
115 | - /** |
|
116 | - * Return the URL of Elastic Cloud from the Cloud ID |
|
117 | - */ |
|
118 | - private function parseElasticCloudId(string $cloudId): string |
|
119 | - { |
|
120 | - try { |
|
121 | - list($name, $encoded) = explode(':', $cloudId); |
|
122 | - list($uri, $uuids) = explode('$', base64_decode($encoded)); |
|
123 | - list($es,) = explode(':', $uuids); |
|
124 | - |
|
125 | - return sprintf("https://%s.%s", $es, $uri); |
|
126 | - } catch (Throwable $t) { |
|
127 | - throw new Exception\CloudIdParseException( |
|
128 | - 'Cloud ID not valid' |
|
129 | - ); |
|
130 | - } |
|
131 | - } |
|
30 | + protected ClientInterface $client; |
|
31 | + protected NodePoolInterface $nodePool; |
|
32 | + protected LoggerInterface $logger; |
|
33 | + protected array $hosts = []; |
|
34 | + |
|
35 | + final public function __construct() |
|
36 | + { |
|
37 | + } |
|
38 | + |
|
39 | + public static function create(): TransportBuilder |
|
40 | + { |
|
41 | + return new static(); |
|
42 | + } |
|
43 | + |
|
44 | + public function setClient(ClientInterface $client): self |
|
45 | + { |
|
46 | + $this->client = $client; |
|
47 | + return $this; |
|
48 | + } |
|
49 | + |
|
50 | + public function getClient(): ClientInterface |
|
51 | + { |
|
52 | + if (empty($this->client)) { |
|
53 | + $this->client = Psr18ClientDiscovery::find(); |
|
54 | + } |
|
55 | + return $this->client; |
|
56 | + } |
|
57 | + |
|
58 | + public function setNodePool(NodePoolInterface $nodePool): self |
|
59 | + { |
|
60 | + $this->nodePool = $nodePool; |
|
61 | + return $this; |
|
62 | + } |
|
63 | + |
|
64 | + public function getNodePool(): NodePoolInterface |
|
65 | + { |
|
66 | + if (empty($this->nodePool)) { |
|
67 | + $this->nodePool = new SimpleNodePool( |
|
68 | + new RoundRobin(), |
|
69 | + new NoResurrect() |
|
70 | + ); |
|
71 | + } |
|
72 | + return $this->nodePool; |
|
73 | + } |
|
74 | + |
|
75 | + public function setLogger(LoggerInterface $logger): self |
|
76 | + { |
|
77 | + $this->logger = $logger; |
|
78 | + return $this; |
|
79 | + } |
|
80 | + |
|
81 | + public function getLogger(): LoggerInterface |
|
82 | + { |
|
83 | + if (empty($this->logger)) { |
|
84 | + $this->logger = new NullLogger(); |
|
85 | + } |
|
86 | + return $this->logger; |
|
87 | + } |
|
88 | + |
|
89 | + public function setHosts(array $hosts): self |
|
90 | + { |
|
91 | + $this->hosts = $hosts; |
|
92 | + return $this; |
|
93 | + } |
|
94 | + |
|
95 | + public function getHosts(): array |
|
96 | + { |
|
97 | + return $this->hosts; |
|
98 | + } |
|
99 | + |
|
100 | + public function setCloudId(string $cloudId): self |
|
101 | + { |
|
102 | + $this->hosts = [$this->parseElasticCloudId($cloudId)]; |
|
103 | + return $this; |
|
104 | + } |
|
105 | + |
|
106 | + public function build(): Transport |
|
107 | + { |
|
108 | + return new Transport( |
|
109 | + $this->getClient(), |
|
110 | + $this->getNodePool()->setHosts($this->hosts), |
|
111 | + $this->getLogger() |
|
112 | + ); |
|
113 | + } |
|
114 | + |
|
115 | + /** |
|
116 | + * Return the URL of Elastic Cloud from the Cloud ID |
|
117 | + */ |
|
118 | + private function parseElasticCloudId(string $cloudId): string |
|
119 | + { |
|
120 | + try { |
|
121 | + list($name, $encoded) = explode(':', $cloudId); |
|
122 | + list($uri, $uuids) = explode('$', base64_decode($encoded)); |
|
123 | + list($es,) = explode(':', $uuids); |
|
124 | + |
|
125 | + return sprintf("https://%s.%s", $es, $uri); |
|
126 | + } catch (Throwable $t) { |
|
127 | + throw new Exception\CloudIdParseException( |
|
128 | + 'Cloud ID not valid' |
|
129 | + ); |
|
130 | + } |
|
131 | + } |
|
132 | 132 | } |
133 | 133 | \ No newline at end of file |
@@ -25,8 +25,7 @@ |
||
25 | 25 | use OCA\FullTextSearch_Elasticsearch\Vendor\Psr\Log\NullLogger; |
26 | 26 | use Throwable; |
27 | 27 | |
28 | -class TransportBuilder |
|
29 | -{ |
|
28 | +class TransportBuilder { |
|
30 | 29 | protected ClientInterface $client; |
31 | 30 | protected NodePoolInterface $nodePool; |
32 | 31 | protected LoggerInterface $logger; |
@@ -25,32 +25,32 @@ |
||
25 | 25 | |
26 | 26 | class XmlSerializer implements SerializerInterface |
27 | 27 | { |
28 | - public static function serialize($data, array $options = []): string |
|
29 | - { |
|
30 | - if ($data instanceof SimpleXMLElement) { |
|
31 | - $xml = $data->asXML(); |
|
32 | - return false === $xml ? '' : $xml; |
|
33 | - } |
|
34 | - throw new InvalidXmlException(sprintf( |
|
35 | - "Not a valid SimpleXMLElement: %s", |
|
36 | - serialize($data) |
|
37 | - )); |
|
38 | - } |
|
28 | + public static function serialize($data, array $options = []): string |
|
29 | + { |
|
30 | + if ($data instanceof SimpleXMLElement) { |
|
31 | + $xml = $data->asXML(); |
|
32 | + return false === $xml ? '' : $xml; |
|
33 | + } |
|
34 | + throw new InvalidXmlException(sprintf( |
|
35 | + "Not a valid SimpleXMLElement: %s", |
|
36 | + serialize($data) |
|
37 | + )); |
|
38 | + } |
|
39 | 39 | |
40 | - /** |
|
41 | - * @return SimpleXMLElement |
|
42 | - */ |
|
43 | - public static function unserialize(string $data, array $options = []): SimpleXMLElement |
|
44 | - { |
|
45 | - $result = simplexml_load_string($data); |
|
46 | - if (false === $result) { |
|
47 | - $errors = libxml_get_errors(); |
|
48 | - libxml_clear_errors(); |
|
49 | - throw new InvalidXmlException(sprintf( |
|
50 | - "Not a valid XML: %s", |
|
51 | - serialize($errors) |
|
52 | - )); |
|
53 | - } |
|
54 | - return $result; |
|
55 | - } |
|
40 | + /** |
|
41 | + * @return SimpleXMLElement |
|
42 | + */ |
|
43 | + public static function unserialize(string $data, array $options = []): SimpleXMLElement |
|
44 | + { |
|
45 | + $result = simplexml_load_string($data); |
|
46 | + if (false === $result) { |
|
47 | + $errors = libxml_get_errors(); |
|
48 | + libxml_clear_errors(); |
|
49 | + throw new InvalidXmlException(sprintf( |
|
50 | + "Not a valid XML: %s", |
|
51 | + serialize($errors) |
|
52 | + )); |
|
53 | + } |
|
54 | + return $result; |
|
55 | + } |
|
56 | 56 | } |
57 | 57 | \ No newline at end of file |
@@ -23,8 +23,7 @@ |
||
23 | 23 | use function simplexml_load_string; |
24 | 24 | use function sprintf; |
25 | 25 | |
26 | -class XmlSerializer implements SerializerInterface |
|
27 | -{ |
|
26 | +class XmlSerializer implements SerializerInterface { |
|
28 | 27 | public static function serialize($data, array $options = []): string |
29 | 28 | { |
30 | 29 | if ($data instanceof SimpleXMLElement) { |
@@ -26,54 +26,54 @@ |
||
26 | 26 | class JsonSerializer implements SerializerInterface |
27 | 27 | { |
28 | 28 | /** |
29 | - * The available $options are: |
|
30 | - * 'remove_null' => (bool) enable/disable the removing of |
|
31 | - * null values (default is true) |
|
32 | - * |
|
33 | - * @param mixed $data |
|
34 | - */ |
|
35 | - public static function serialize($data, array $options = []): string |
|
36 | - { |
|
37 | - if (empty($data)) { |
|
38 | - return '{}'; |
|
39 | - } |
|
40 | - if (is_string($data)) { |
|
41 | - return $data; |
|
42 | - } |
|
43 | - try { |
|
44 | - $removeNull = $options['remove_null'] ?? true; |
|
45 | - if ($removeNull) { |
|
46 | - Utility::removeNullValue($data); |
|
47 | - } |
|
48 | - return json_encode($data, JSON_PRESERVE_ZERO_FRACTION + JSON_INVALID_UTF8_SUBSTITUTE + JSON_THROW_ON_ERROR); |
|
49 | - } catch (JsonException $e) { |
|
50 | - throw new InvalidJsonException(sprintf( |
|
51 | - "I cannot serialize to Json: %s", |
|
52 | - $e->getMessage() |
|
53 | - )); |
|
54 | - } |
|
55 | - } |
|
29 | + * The available $options are: |
|
30 | + * 'remove_null' => (bool) enable/disable the removing of |
|
31 | + * null values (default is true) |
|
32 | + * |
|
33 | + * @param mixed $data |
|
34 | + */ |
|
35 | + public static function serialize($data, array $options = []): string |
|
36 | + { |
|
37 | + if (empty($data)) { |
|
38 | + return '{}'; |
|
39 | + } |
|
40 | + if (is_string($data)) { |
|
41 | + return $data; |
|
42 | + } |
|
43 | + try { |
|
44 | + $removeNull = $options['remove_null'] ?? true; |
|
45 | + if ($removeNull) { |
|
46 | + Utility::removeNullValue($data); |
|
47 | + } |
|
48 | + return json_encode($data, JSON_PRESERVE_ZERO_FRACTION + JSON_INVALID_UTF8_SUBSTITUTE + JSON_THROW_ON_ERROR); |
|
49 | + } catch (JsonException $e) { |
|
50 | + throw new InvalidJsonException(sprintf( |
|
51 | + "I cannot serialize to Json: %s", |
|
52 | + $e->getMessage() |
|
53 | + )); |
|
54 | + } |
|
55 | + } |
|
56 | 56 | |
57 | - /** |
|
58 | - * The available options are: |
|
59 | - * 'type' => (string) specify if the output should be an array |
|
60 | - * or an object (default is array) |
|
61 | - * |
|
62 | - * @inheritdoc |
|
63 | - */ |
|
64 | - public static function unserialize(string $data, array $options = []) |
|
65 | - { |
|
66 | - try { |
|
67 | - $type = $options['type'] ?? 'array'; |
|
68 | - if (!in_array($type, ['object', 'array'])) { |
|
69 | - throw new UndefinedPropertyException("The unserialize 'type' option must be object or array"); |
|
70 | - } |
|
71 | - return json_decode($data, $type === 'array', 512, JSON_THROW_ON_ERROR); |
|
72 | - } catch (JsonException $e) { |
|
73 | - throw new InvalidJsonException(sprintf( |
|
74 | - "Not a valid Json: %s", |
|
75 | - $e->getMessage() |
|
76 | - )); |
|
77 | - } |
|
78 | - } |
|
57 | + /** |
|
58 | + * The available options are: |
|
59 | + * 'type' => (string) specify if the output should be an array |
|
60 | + * or an object (default is array) |
|
61 | + * |
|
62 | + * @inheritdoc |
|
63 | + */ |
|
64 | + public static function unserialize(string $data, array $options = []) |
|
65 | + { |
|
66 | + try { |
|
67 | + $type = $options['type'] ?? 'array'; |
|
68 | + if (!in_array($type, ['object', 'array'])) { |
|
69 | + throw new UndefinedPropertyException("The unserialize 'type' option must be object or array"); |
|
70 | + } |
|
71 | + return json_decode($data, $type === 'array', 512, JSON_THROW_ON_ERROR); |
|
72 | + } catch (JsonException $e) { |
|
73 | + throw new InvalidJsonException(sprintf( |
|
74 | + "Not a valid Json: %s", |
|
75 | + $e->getMessage() |
|
76 | + )); |
|
77 | + } |
|
78 | + } |
|
79 | 79 | } |
80 | 80 | \ No newline at end of file |
@@ -23,8 +23,7 @@ |
||
23 | 23 | use function json_encode; |
24 | 24 | use function sprintf; |
25 | 25 | |
26 | -class JsonSerializer implements SerializerInterface |
|
27 | -{ |
|
26 | +class JsonSerializer implements SerializerInterface { |
|
28 | 27 | /** |
29 | 28 | * The available $options are: |
30 | 29 | * 'remove_null' => (bool) enable/disable the removing of |
@@ -20,24 +20,24 @@ |
||
20 | 20 | |
21 | 21 | class TextSerializer implements SerializerInterface |
22 | 22 | { |
23 | - /** |
|
24 | - * @throws SerializeException |
|
25 | - */ |
|
26 | - public static function serialize($data, array $options = []): string |
|
27 | - { |
|
28 | - if (is_string($data) || is_numeric($data) || (is_object($data) && method_exists($data, '__toString'))) { |
|
29 | - return (string) $data; |
|
30 | - } |
|
31 | - throw new SerializeException( |
|
32 | - sprintf("I cannot serialize %s in a text", serialize($data)) |
|
33 | - ); |
|
34 | - } |
|
23 | + /** |
|
24 | + * @throws SerializeException |
|
25 | + */ |
|
26 | + public static function serialize($data, array $options = []): string |
|
27 | + { |
|
28 | + if (is_string($data) || is_numeric($data) || (is_object($data) && method_exists($data, '__toString'))) { |
|
29 | + return (string) $data; |
|
30 | + } |
|
31 | + throw new SerializeException( |
|
32 | + sprintf("I cannot serialize %s in a text", serialize($data)) |
|
33 | + ); |
|
34 | + } |
|
35 | 35 | |
36 | - /** |
|
37 | - * @return string |
|
38 | - */ |
|
39 | - public static function unserialize(string $data, array $options = []): string |
|
40 | - { |
|
41 | - return $data; |
|
42 | - } |
|
36 | + /** |
|
37 | + * @return string |
|
38 | + */ |
|
39 | + public static function unserialize(string $data, array $options = []): string |
|
40 | + { |
|
41 | + return $data; |
|
42 | + } |
|
43 | 43 | } |
44 | 44 | \ No newline at end of file |
@@ -26,7 +26,7 @@ |
||
26 | 26 | public static function serialize($data, array $options = []): string |
27 | 27 | { |
28 | 28 | if (is_string($data) || is_numeric($data) || (is_object($data) && method_exists($data, '__toString'))) { |
29 | - return (string) $data; |
|
29 | + return (string)$data; |
|
30 | 30 | } |
31 | 31 | throw new SerializeException( |
32 | 32 | sprintf("I cannot serialize %s in a text", serialize($data)) |
@@ -18,8 +18,7 @@ |
||
18 | 18 | |
19 | 19 | use function serialize; |
20 | 20 | |
21 | -class TextSerializer implements SerializerInterface |
|
22 | -{ |
|
21 | +class TextSerializer implements SerializerInterface { |
|
23 | 22 | /** |
24 | 23 | * @throws SerializeException |
25 | 24 | */ |