@@ -16,468 +16,468 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | class Client implements ClientInterface, \OCA\FullTextSearch_Elasticsearch\Vendor\Psr\Http\Client\ClientInterface |
| 18 | 18 | { |
| 19 | - use ClientTrait; |
|
| 20 | - |
|
| 21 | - /** |
|
| 22 | - * @var array Default request options |
|
| 23 | - */ |
|
| 24 | - private $config; |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * Clients accept an array of constructor parameters. |
|
| 28 | - * |
|
| 29 | - * Here's an example of creating a client using a base_uri and an array of |
|
| 30 | - * default request options to apply to each request: |
|
| 31 | - * |
|
| 32 | - * $client = new Client([ |
|
| 33 | - * 'base_uri' => 'http://www.foo.com/1.0/', |
|
| 34 | - * 'timeout' => 0, |
|
| 35 | - * 'allow_redirects' => false, |
|
| 36 | - * 'proxy' => '192.168.16.1:10' |
|
| 37 | - * ]); |
|
| 38 | - * |
|
| 39 | - * Client configuration settings include the following options: |
|
| 40 | - * |
|
| 41 | - * - handler: (callable) Function that transfers HTTP requests over the |
|
| 42 | - * wire. The function is called with a Psr7\Http\Message\RequestInterface |
|
| 43 | - * and array of transfer options, and must return a |
|
| 44 | - * OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\Promise\PromiseInterface that is fulfilled with a |
|
| 45 | - * Psr7\Http\Message\ResponseInterface on success. |
|
| 46 | - * If no handler is provided, a default handler will be created |
|
| 47 | - * that enables all of the request options below by attaching all of the |
|
| 48 | - * default middleware to the handler. |
|
| 49 | - * - base_uri: (string|UriInterface) Base URI of the client that is merged |
|
| 50 | - * into relative URIs. Can be a string or instance of UriInterface. |
|
| 51 | - * - **: any request option |
|
| 52 | - * |
|
| 53 | - * @param array $config Client configuration settings. |
|
| 54 | - * |
|
| 55 | - * @see \OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\RequestOptions for a list of available request options. |
|
| 56 | - */ |
|
| 57 | - public function __construct(array $config = []) |
|
| 58 | - { |
|
| 59 | - if (!isset($config['handler'])) { |
|
| 60 | - $config['handler'] = HandlerStack::create(); |
|
| 61 | - } elseif (!\is_callable($config['handler'])) { |
|
| 62 | - throw new InvalidArgumentException('handler must be a callable'); |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - // Convert the base_uri to a UriInterface |
|
| 66 | - if (isset($config['base_uri'])) { |
|
| 67 | - $config['base_uri'] = Psr7\Utils::uriFor($config['base_uri']); |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - $this->configureDefaults($config); |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * @param string $method |
|
| 75 | - * @param array $args |
|
| 76 | - * |
|
| 77 | - * @return PromiseInterface|ResponseInterface |
|
| 78 | - * |
|
| 79 | - * @deprecated Client::__call will be removed in guzzlehttp/guzzle:8.0. |
|
| 80 | - */ |
|
| 81 | - public function __call($method, $args) |
|
| 82 | - { |
|
| 83 | - if (\count($args) < 1) { |
|
| 84 | - throw new InvalidArgumentException('Magic request methods require a URI and optional options array'); |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - $uri = $args[0]; |
|
| 88 | - $opts = $args[1] ?? []; |
|
| 89 | - |
|
| 90 | - return \substr($method, -5) === 'Async' |
|
| 91 | - ? $this->requestAsync(\substr($method, 0, -5), $uri, $opts) |
|
| 92 | - : $this->request($method, $uri, $opts); |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - /** |
|
| 96 | - * Asynchronously send an HTTP request. |
|
| 97 | - * |
|
| 98 | - * @param array $options Request options to apply to the given |
|
| 99 | - * request and to the transfer. See \OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\RequestOptions. |
|
| 100 | - */ |
|
| 101 | - public function sendAsync(RequestInterface $request, array $options = []): PromiseInterface |
|
| 102 | - { |
|
| 103 | - // Merge the base URI into the request URI if needed. |
|
| 104 | - $options = $this->prepareDefaults($options); |
|
| 105 | - |
|
| 106 | - return $this->transfer( |
|
| 107 | - $request->withUri($this->buildUri($request->getUri(), $options), $request->hasHeader('Host')), |
|
| 108 | - $options |
|
| 109 | - ); |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - /** |
|
| 113 | - * Send an HTTP request. |
|
| 114 | - * |
|
| 115 | - * @param array $options Request options to apply to the given |
|
| 116 | - * request and to the transfer. See \OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\RequestOptions. |
|
| 117 | - * |
|
| 118 | - * @throws GuzzleException |
|
| 119 | - */ |
|
| 120 | - public function send(RequestInterface $request, array $options = []): ResponseInterface |
|
| 121 | - { |
|
| 122 | - $options[RequestOptions::SYNCHRONOUS] = true; |
|
| 123 | - |
|
| 124 | - return $this->sendAsync($request, $options)->wait(); |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - /** |
|
| 128 | - * The HttpClient PSR (PSR-18) specify this method. |
|
| 129 | - * |
|
| 130 | - * {@inheritDoc} |
|
| 131 | - */ |
|
| 132 | - public function sendRequest(RequestInterface $request): ResponseInterface |
|
| 133 | - { |
|
| 134 | - $options[RequestOptions::SYNCHRONOUS] = true; |
|
| 135 | - $options[RequestOptions::ALLOW_REDIRECTS] = false; |
|
| 136 | - $options[RequestOptions::HTTP_ERRORS] = false; |
|
| 137 | - |
|
| 138 | - return $this->sendAsync($request, $options)->wait(); |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - /** |
|
| 142 | - * Create and send an asynchronous HTTP request. |
|
| 143 | - * |
|
| 144 | - * Use an absolute path to override the base path of the client, or a |
|
| 145 | - * relative path to append to the base path of the client. The URL can |
|
| 146 | - * contain the query string as well. Use an array to provide a URL |
|
| 147 | - * template and additional variables to use in the URL template expansion. |
|
| 148 | - * |
|
| 149 | - * @param string $method HTTP method |
|
| 150 | - * @param string|UriInterface $uri URI object or string. |
|
| 151 | - * @param array $options Request options to apply. See \OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\RequestOptions. |
|
| 152 | - */ |
|
| 153 | - public function requestAsync(string $method, $uri = '', array $options = []): PromiseInterface |
|
| 154 | - { |
|
| 155 | - $options = $this->prepareDefaults($options); |
|
| 156 | - // Remove request modifying parameter because it can be done up-front. |
|
| 157 | - $headers = $options['headers'] ?? []; |
|
| 158 | - $body = $options['body'] ?? null; |
|
| 159 | - $version = $options['version'] ?? '1.1'; |
|
| 160 | - // Merge the URI into the base URI. |
|
| 161 | - $uri = $this->buildUri(Psr7\Utils::uriFor($uri), $options); |
|
| 162 | - if (\is_array($body)) { |
|
| 163 | - throw $this->invalidBody(); |
|
| 164 | - } |
|
| 165 | - $request = new Psr7\Request($method, $uri, $headers, $body, $version); |
|
| 166 | - // Remove the option so that they are not doubly-applied. |
|
| 167 | - unset($options['headers'], $options['body'], $options['version']); |
|
| 168 | - |
|
| 169 | - return $this->transfer($request, $options); |
|
| 170 | - } |
|
| 171 | - |
|
| 172 | - /** |
|
| 173 | - * Create and send an HTTP request. |
|
| 174 | - * |
|
| 175 | - * Use an absolute path to override the base path of the client, or a |
|
| 176 | - * relative path to append to the base path of the client. The URL can |
|
| 177 | - * contain the query string as well. |
|
| 178 | - * |
|
| 179 | - * @param string $method HTTP method. |
|
| 180 | - * @param string|UriInterface $uri URI object or string. |
|
| 181 | - * @param array $options Request options to apply. See \OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\RequestOptions. |
|
| 182 | - * |
|
| 183 | - * @throws GuzzleException |
|
| 184 | - */ |
|
| 185 | - public function request(string $method, $uri = '', array $options = []): ResponseInterface |
|
| 186 | - { |
|
| 187 | - $options[RequestOptions::SYNCHRONOUS] = true; |
|
| 188 | - |
|
| 189 | - return $this->requestAsync($method, $uri, $options)->wait(); |
|
| 190 | - } |
|
| 191 | - |
|
| 192 | - /** |
|
| 193 | - * Get a client configuration option. |
|
| 194 | - * |
|
| 195 | - * These options include default request options of the client, a "handler" |
|
| 196 | - * (if utilized by the concrete client), and a "base_uri" if utilized by |
|
| 197 | - * the concrete client. |
|
| 198 | - * |
|
| 199 | - * @param string|null $option The config option to retrieve. |
|
| 200 | - * |
|
| 201 | - * @return mixed |
|
| 202 | - * |
|
| 203 | - * @deprecated Client::getConfig will be removed in guzzlehttp/guzzle:8.0. |
|
| 204 | - */ |
|
| 205 | - public function getConfig(string $option = null) |
|
| 206 | - { |
|
| 207 | - return $option === null |
|
| 208 | - ? $this->config |
|
| 209 | - : ($this->config[$option] ?? null); |
|
| 210 | - } |
|
| 211 | - |
|
| 212 | - private function buildUri(UriInterface $uri, array $config): UriInterface |
|
| 213 | - { |
|
| 214 | - if (isset($config['base_uri'])) { |
|
| 215 | - $uri = Psr7\UriResolver::resolve(Psr7\Utils::uriFor($config['base_uri']), $uri); |
|
| 216 | - } |
|
| 217 | - |
|
| 218 | - if (isset($config['idn_conversion']) && ($config['idn_conversion'] !== false)) { |
|
| 219 | - $idnOptions = ($config['idn_conversion'] === true) ? \IDNA_DEFAULT : $config['idn_conversion']; |
|
| 220 | - $uri = Utils::idnUriConvert($uri, $idnOptions); |
|
| 221 | - } |
|
| 222 | - |
|
| 223 | - return $uri->getScheme() === '' && $uri->getHost() !== '' ? $uri->withScheme('http') : $uri; |
|
| 224 | - } |
|
| 225 | - |
|
| 226 | - /** |
|
| 227 | - * Configures the default options for a client. |
|
| 228 | - */ |
|
| 229 | - private function configureDefaults(array $config): void |
|
| 230 | - { |
|
| 231 | - $defaults = [ |
|
| 232 | - 'allow_redirects' => RedirectMiddleware::$defaultSettings, |
|
| 233 | - 'http_errors' => true, |
|
| 234 | - 'decode_content' => true, |
|
| 235 | - 'verify' => true, |
|
| 236 | - 'cookies' => false, |
|
| 237 | - 'idn_conversion' => false, |
|
| 238 | - ]; |
|
| 239 | - |
|
| 240 | - // Use the standard Linux HTTP_PROXY and HTTPS_PROXY if set. |
|
| 241 | - |
|
| 242 | - // We can only trust the HTTP_PROXY environment variable in a CLI |
|
| 243 | - // process due to the fact that PHP has no reliable mechanism to |
|
| 244 | - // get environment variables that start with "HTTP_". |
|
| 245 | - if (\PHP_SAPI === 'cli' && ($proxy = Utils::getenv('HTTP_PROXY'))) { |
|
| 246 | - $defaults['proxy']['http'] = $proxy; |
|
| 247 | - } |
|
| 248 | - |
|
| 249 | - if ($proxy = Utils::getenv('HTTPS_PROXY')) { |
|
| 250 | - $defaults['proxy']['https'] = $proxy; |
|
| 251 | - } |
|
| 252 | - |
|
| 253 | - if ($noProxy = Utils::getenv('NO_PROXY')) { |
|
| 254 | - $cleanedNoProxy = \str_replace(' ', '', $noProxy); |
|
| 255 | - $defaults['proxy']['no'] = \explode(',', $cleanedNoProxy); |
|
| 256 | - } |
|
| 257 | - |
|
| 258 | - $this->config = $config + $defaults; |
|
| 259 | - |
|
| 260 | - if (!empty($config['cookies']) && $config['cookies'] === true) { |
|
| 261 | - $this->config['cookies'] = new CookieJar(); |
|
| 262 | - } |
|
| 263 | - |
|
| 264 | - // Add the default user-agent header. |
|
| 265 | - if (!isset($this->config['headers'])) { |
|
| 266 | - $this->config['headers'] = ['User-Agent' => Utils::defaultUserAgent()]; |
|
| 267 | - } else { |
|
| 268 | - // Add the User-Agent header if one was not already set. |
|
| 269 | - foreach (\array_keys($this->config['headers']) as $name) { |
|
| 270 | - if (\strtolower($name) === 'user-agent') { |
|
| 271 | - return; |
|
| 272 | - } |
|
| 273 | - } |
|
| 274 | - $this->config['headers']['User-Agent'] = Utils::defaultUserAgent(); |
|
| 275 | - } |
|
| 276 | - } |
|
| 277 | - |
|
| 278 | - /** |
|
| 279 | - * Merges default options into the array. |
|
| 280 | - * |
|
| 281 | - * @param array $options Options to modify by reference |
|
| 282 | - */ |
|
| 283 | - private function prepareDefaults(array $options): array |
|
| 284 | - { |
|
| 285 | - $defaults = $this->config; |
|
| 286 | - |
|
| 287 | - if (!empty($defaults['headers'])) { |
|
| 288 | - // Default headers are only added if they are not present. |
|
| 289 | - $defaults['_conditional'] = $defaults['headers']; |
|
| 290 | - unset($defaults['headers']); |
|
| 291 | - } |
|
| 292 | - |
|
| 293 | - // Special handling for headers is required as they are added as |
|
| 294 | - // conditional headers and as headers passed to a request ctor. |
|
| 295 | - if (\array_key_exists('headers', $options)) { |
|
| 296 | - // Allows default headers to be unset. |
|
| 297 | - if ($options['headers'] === null) { |
|
| 298 | - $defaults['_conditional'] = []; |
|
| 299 | - unset($options['headers']); |
|
| 300 | - } elseif (!\is_array($options['headers'])) { |
|
| 301 | - throw new InvalidArgumentException('headers must be an array'); |
|
| 302 | - } |
|
| 303 | - } |
|
| 304 | - |
|
| 305 | - // Shallow merge defaults underneath options. |
|
| 306 | - $result = $options + $defaults; |
|
| 307 | - |
|
| 308 | - // Remove null values. |
|
| 309 | - foreach ($result as $k => $v) { |
|
| 310 | - if ($v === null) { |
|
| 311 | - unset($result[$k]); |
|
| 312 | - } |
|
| 313 | - } |
|
| 314 | - |
|
| 315 | - return $result; |
|
| 316 | - } |
|
| 317 | - |
|
| 318 | - /** |
|
| 319 | - * Transfers the given request and applies request options. |
|
| 320 | - * |
|
| 321 | - * The URI of the request is not modified and the request options are used |
|
| 322 | - * as-is without merging in default options. |
|
| 323 | - * |
|
| 324 | - * @param array $options See \OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\RequestOptions. |
|
| 325 | - */ |
|
| 326 | - private function transfer(RequestInterface $request, array $options): PromiseInterface |
|
| 327 | - { |
|
| 328 | - $request = $this->applyOptions($request, $options); |
|
| 329 | - /** @var HandlerStack $handler */ |
|
| 330 | - $handler = $options['handler']; |
|
| 331 | - |
|
| 332 | - try { |
|
| 333 | - return P\Create::promiseFor($handler($request, $options)); |
|
| 334 | - } catch (\Exception $e) { |
|
| 335 | - return P\Create::rejectionFor($e); |
|
| 336 | - } |
|
| 337 | - } |
|
| 338 | - |
|
| 339 | - /** |
|
| 340 | - * Applies the array of request options to a request. |
|
| 341 | - */ |
|
| 342 | - private function applyOptions(RequestInterface $request, array &$options): RequestInterface |
|
| 343 | - { |
|
| 344 | - $modify = [ |
|
| 345 | - 'set_headers' => [], |
|
| 346 | - ]; |
|
| 347 | - |
|
| 348 | - if (isset($options['headers'])) { |
|
| 349 | - if (array_keys($options['headers']) === range(0, count($options['headers']) - 1)) { |
|
| 350 | - throw new InvalidArgumentException('The headers array must have header name as keys.'); |
|
| 351 | - } |
|
| 352 | - $modify['set_headers'] = $options['headers']; |
|
| 353 | - unset($options['headers']); |
|
| 354 | - } |
|
| 355 | - |
|
| 356 | - if (isset($options['form_params'])) { |
|
| 357 | - if (isset($options['multipart'])) { |
|
| 358 | - throw new InvalidArgumentException('You cannot use ' |
|
| 359 | - .'form_params and multipart at the same time. Use the ' |
|
| 360 | - .'form_params option if you want to send application/' |
|
| 361 | - .'x-www-form-urlencoded requests, and the multipart ' |
|
| 362 | - .'option to send multipart/form-data requests.'); |
|
| 363 | - } |
|
| 364 | - $options['body'] = \http_build_query($options['form_params'], '', '&'); |
|
| 365 | - unset($options['form_params']); |
|
| 366 | - // Ensure that we don't have the header in different case and set the new value. |
|
| 367 | - $options['_conditional'] = Psr7\Utils::caselessRemove(['Content-Type'], $options['_conditional']); |
|
| 368 | - $options['_conditional']['Content-Type'] = 'application/x-www-form-urlencoded'; |
|
| 369 | - } |
|
| 370 | - |
|
| 371 | - if (isset($options['multipart'])) { |
|
| 372 | - $options['body'] = new Psr7\MultipartStream($options['multipart']); |
|
| 373 | - unset($options['multipart']); |
|
| 374 | - } |
|
| 375 | - |
|
| 376 | - if (isset($options['json'])) { |
|
| 377 | - $options['body'] = Utils::jsonEncode($options['json']); |
|
| 378 | - unset($options['json']); |
|
| 379 | - // Ensure that we don't have the header in different case and set the new value. |
|
| 380 | - $options['_conditional'] = Psr7\Utils::caselessRemove(['Content-Type'], $options['_conditional']); |
|
| 381 | - $options['_conditional']['Content-Type'] = 'application/json'; |
|
| 382 | - } |
|
| 383 | - |
|
| 384 | - if (!empty($options['decode_content']) |
|
| 385 | - && $options['decode_content'] !== true |
|
| 386 | - ) { |
|
| 387 | - // Ensure that we don't have the header in different case and set the new value. |
|
| 388 | - $options['_conditional'] = Psr7\Utils::caselessRemove(['Accept-Encoding'], $options['_conditional']); |
|
| 389 | - $modify['set_headers']['Accept-Encoding'] = $options['decode_content']; |
|
| 390 | - } |
|
| 391 | - |
|
| 392 | - if (isset($options['body'])) { |
|
| 393 | - if (\is_array($options['body'])) { |
|
| 394 | - throw $this->invalidBody(); |
|
| 395 | - } |
|
| 396 | - $modify['body'] = Psr7\Utils::streamFor($options['body']); |
|
| 397 | - unset($options['body']); |
|
| 398 | - } |
|
| 399 | - |
|
| 400 | - if (!empty($options['auth']) && \is_array($options['auth'])) { |
|
| 401 | - $value = $options['auth']; |
|
| 402 | - $type = isset($value[2]) ? \strtolower($value[2]) : 'basic'; |
|
| 403 | - switch ($type) { |
|
| 404 | - case 'basic': |
|
| 405 | - // Ensure that we don't have the header in different case and set the new value. |
|
| 406 | - $modify['set_headers'] = Psr7\Utils::caselessRemove(['Authorization'], $modify['set_headers']); |
|
| 407 | - $modify['set_headers']['Authorization'] = 'Basic ' |
|
| 408 | - .\base64_encode("$value[0]:$value[1]"); |
|
| 409 | - break; |
|
| 410 | - case 'digest': |
|
| 411 | - // @todo: Do not rely on curl |
|
| 412 | - $options['curl'][\CURLOPT_HTTPAUTH] = \CURLAUTH_DIGEST; |
|
| 413 | - $options['curl'][\CURLOPT_USERPWD] = "$value[0]:$value[1]"; |
|
| 414 | - break; |
|
| 415 | - case 'ntlm': |
|
| 416 | - $options['curl'][\CURLOPT_HTTPAUTH] = \CURLAUTH_NTLM; |
|
| 417 | - $options['curl'][\CURLOPT_USERPWD] = "$value[0]:$value[1]"; |
|
| 418 | - break; |
|
| 419 | - } |
|
| 420 | - } |
|
| 421 | - |
|
| 422 | - if (isset($options['query'])) { |
|
| 423 | - $value = $options['query']; |
|
| 424 | - if (\is_array($value)) { |
|
| 425 | - $value = \http_build_query($value, '', '&', \PHP_QUERY_RFC3986); |
|
| 426 | - } |
|
| 427 | - if (!\is_string($value)) { |
|
| 428 | - throw new InvalidArgumentException('query must be a string or array'); |
|
| 429 | - } |
|
| 430 | - $modify['query'] = $value; |
|
| 431 | - unset($options['query']); |
|
| 432 | - } |
|
| 433 | - |
|
| 434 | - // Ensure that sink is not an invalid value. |
|
| 435 | - if (isset($options['sink'])) { |
|
| 436 | - // TODO: Add more sink validation? |
|
| 437 | - if (\is_bool($options['sink'])) { |
|
| 438 | - throw new InvalidArgumentException('sink must not be a boolean'); |
|
| 439 | - } |
|
| 440 | - } |
|
| 441 | - |
|
| 442 | - if (isset($options['version'])) { |
|
| 443 | - $modify['version'] = $options['version']; |
|
| 444 | - } |
|
| 445 | - |
|
| 446 | - $request = Psr7\Utils::modifyRequest($request, $modify); |
|
| 447 | - if ($request->getBody() instanceof Psr7\MultipartStream) { |
|
| 448 | - // Use a multipart/form-data POST if a Content-Type is not set. |
|
| 449 | - // Ensure that we don't have the header in different case and set the new value. |
|
| 450 | - $options['_conditional'] = Psr7\Utils::caselessRemove(['Content-Type'], $options['_conditional']); |
|
| 451 | - $options['_conditional']['Content-Type'] = 'multipart/form-data; boundary=' |
|
| 452 | - .$request->getBody()->getBoundary(); |
|
| 453 | - } |
|
| 454 | - |
|
| 455 | - // Merge in conditional headers if they are not present. |
|
| 456 | - if (isset($options['_conditional'])) { |
|
| 457 | - // Build up the changes so it's in a single clone of the message. |
|
| 458 | - $modify = []; |
|
| 459 | - foreach ($options['_conditional'] as $k => $v) { |
|
| 460 | - if (!$request->hasHeader($k)) { |
|
| 461 | - $modify['set_headers'][$k] = $v; |
|
| 462 | - } |
|
| 463 | - } |
|
| 464 | - $request = Psr7\Utils::modifyRequest($request, $modify); |
|
| 465 | - // Don't pass this internal value along to middleware/handlers. |
|
| 466 | - unset($options['_conditional']); |
|
| 467 | - } |
|
| 468 | - |
|
| 469 | - return $request; |
|
| 470 | - } |
|
| 471 | - |
|
| 472 | - /** |
|
| 473 | - * Return an InvalidArgumentException with pre-set message. |
|
| 474 | - */ |
|
| 475 | - private function invalidBody(): InvalidArgumentException |
|
| 476 | - { |
|
| 477 | - return new InvalidArgumentException('Passing in the "body" request ' |
|
| 478 | - .'option as an array to send a request is not supported. ' |
|
| 479 | - .'Please use the "form_params" request option to send a ' |
|
| 480 | - .'application/x-www-form-urlencoded request, or the "multipart" ' |
|
| 481 | - .'request option to send a multipart/form-data request.'); |
|
| 482 | - } |
|
| 19 | + use ClientTrait; |
|
| 20 | + |
|
| 21 | + /** |
|
| 22 | + * @var array Default request options |
|
| 23 | + */ |
|
| 24 | + private $config; |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * Clients accept an array of constructor parameters. |
|
| 28 | + * |
|
| 29 | + * Here's an example of creating a client using a base_uri and an array of |
|
| 30 | + * default request options to apply to each request: |
|
| 31 | + * |
|
| 32 | + * $client = new Client([ |
|
| 33 | + * 'base_uri' => 'http://www.foo.com/1.0/', |
|
| 34 | + * 'timeout' => 0, |
|
| 35 | + * 'allow_redirects' => false, |
|
| 36 | + * 'proxy' => '192.168.16.1:10' |
|
| 37 | + * ]); |
|
| 38 | + * |
|
| 39 | + * Client configuration settings include the following options: |
|
| 40 | + * |
|
| 41 | + * - handler: (callable) Function that transfers HTTP requests over the |
|
| 42 | + * wire. The function is called with a Psr7\Http\Message\RequestInterface |
|
| 43 | + * and array of transfer options, and must return a |
|
| 44 | + * OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\Promise\PromiseInterface that is fulfilled with a |
|
| 45 | + * Psr7\Http\Message\ResponseInterface on success. |
|
| 46 | + * If no handler is provided, a default handler will be created |
|
| 47 | + * that enables all of the request options below by attaching all of the |
|
| 48 | + * default middleware to the handler. |
|
| 49 | + * - base_uri: (string|UriInterface) Base URI of the client that is merged |
|
| 50 | + * into relative URIs. Can be a string or instance of UriInterface. |
|
| 51 | + * - **: any request option |
|
| 52 | + * |
|
| 53 | + * @param array $config Client configuration settings. |
|
| 54 | + * |
|
| 55 | + * @see \OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\RequestOptions for a list of available request options. |
|
| 56 | + */ |
|
| 57 | + public function __construct(array $config = []) |
|
| 58 | + { |
|
| 59 | + if (!isset($config['handler'])) { |
|
| 60 | + $config['handler'] = HandlerStack::create(); |
|
| 61 | + } elseif (!\is_callable($config['handler'])) { |
|
| 62 | + throw new InvalidArgumentException('handler must be a callable'); |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + // Convert the base_uri to a UriInterface |
|
| 66 | + if (isset($config['base_uri'])) { |
|
| 67 | + $config['base_uri'] = Psr7\Utils::uriFor($config['base_uri']); |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + $this->configureDefaults($config); |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * @param string $method |
|
| 75 | + * @param array $args |
|
| 76 | + * |
|
| 77 | + * @return PromiseInterface|ResponseInterface |
|
| 78 | + * |
|
| 79 | + * @deprecated Client::__call will be removed in guzzlehttp/guzzle:8.0. |
|
| 80 | + */ |
|
| 81 | + public function __call($method, $args) |
|
| 82 | + { |
|
| 83 | + if (\count($args) < 1) { |
|
| 84 | + throw new InvalidArgumentException('Magic request methods require a URI and optional options array'); |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + $uri = $args[0]; |
|
| 88 | + $opts = $args[1] ?? []; |
|
| 89 | + |
|
| 90 | + return \substr($method, -5) === 'Async' |
|
| 91 | + ? $this->requestAsync(\substr($method, 0, -5), $uri, $opts) |
|
| 92 | + : $this->request($method, $uri, $opts); |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + /** |
|
| 96 | + * Asynchronously send an HTTP request. |
|
| 97 | + * |
|
| 98 | + * @param array $options Request options to apply to the given |
|
| 99 | + * request and to the transfer. See \OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\RequestOptions. |
|
| 100 | + */ |
|
| 101 | + public function sendAsync(RequestInterface $request, array $options = []): PromiseInterface |
|
| 102 | + { |
|
| 103 | + // Merge the base URI into the request URI if needed. |
|
| 104 | + $options = $this->prepareDefaults($options); |
|
| 105 | + |
|
| 106 | + return $this->transfer( |
|
| 107 | + $request->withUri($this->buildUri($request->getUri(), $options), $request->hasHeader('Host')), |
|
| 108 | + $options |
|
| 109 | + ); |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + /** |
|
| 113 | + * Send an HTTP request. |
|
| 114 | + * |
|
| 115 | + * @param array $options Request options to apply to the given |
|
| 116 | + * request and to the transfer. See \OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\RequestOptions. |
|
| 117 | + * |
|
| 118 | + * @throws GuzzleException |
|
| 119 | + */ |
|
| 120 | + public function send(RequestInterface $request, array $options = []): ResponseInterface |
|
| 121 | + { |
|
| 122 | + $options[RequestOptions::SYNCHRONOUS] = true; |
|
| 123 | + |
|
| 124 | + return $this->sendAsync($request, $options)->wait(); |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + /** |
|
| 128 | + * The HttpClient PSR (PSR-18) specify this method. |
|
| 129 | + * |
|
| 130 | + * {@inheritDoc} |
|
| 131 | + */ |
|
| 132 | + public function sendRequest(RequestInterface $request): ResponseInterface |
|
| 133 | + { |
|
| 134 | + $options[RequestOptions::SYNCHRONOUS] = true; |
|
| 135 | + $options[RequestOptions::ALLOW_REDIRECTS] = false; |
|
| 136 | + $options[RequestOptions::HTTP_ERRORS] = false; |
|
| 137 | + |
|
| 138 | + return $this->sendAsync($request, $options)->wait(); |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + /** |
|
| 142 | + * Create and send an asynchronous HTTP request. |
|
| 143 | + * |
|
| 144 | + * Use an absolute path to override the base path of the client, or a |
|
| 145 | + * relative path to append to the base path of the client. The URL can |
|
| 146 | + * contain the query string as well. Use an array to provide a URL |
|
| 147 | + * template and additional variables to use in the URL template expansion. |
|
| 148 | + * |
|
| 149 | + * @param string $method HTTP method |
|
| 150 | + * @param string|UriInterface $uri URI object or string. |
|
| 151 | + * @param array $options Request options to apply. See \OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\RequestOptions. |
|
| 152 | + */ |
|
| 153 | + public function requestAsync(string $method, $uri = '', array $options = []): PromiseInterface |
|
| 154 | + { |
|
| 155 | + $options = $this->prepareDefaults($options); |
|
| 156 | + // Remove request modifying parameter because it can be done up-front. |
|
| 157 | + $headers = $options['headers'] ?? []; |
|
| 158 | + $body = $options['body'] ?? null; |
|
| 159 | + $version = $options['version'] ?? '1.1'; |
|
| 160 | + // Merge the URI into the base URI. |
|
| 161 | + $uri = $this->buildUri(Psr7\Utils::uriFor($uri), $options); |
|
| 162 | + if (\is_array($body)) { |
|
| 163 | + throw $this->invalidBody(); |
|
| 164 | + } |
|
| 165 | + $request = new Psr7\Request($method, $uri, $headers, $body, $version); |
|
| 166 | + // Remove the option so that they are not doubly-applied. |
|
| 167 | + unset($options['headers'], $options['body'], $options['version']); |
|
| 168 | + |
|
| 169 | + return $this->transfer($request, $options); |
|
| 170 | + } |
|
| 171 | + |
|
| 172 | + /** |
|
| 173 | + * Create and send an HTTP request. |
|
| 174 | + * |
|
| 175 | + * Use an absolute path to override the base path of the client, or a |
|
| 176 | + * relative path to append to the base path of the client. The URL can |
|
| 177 | + * contain the query string as well. |
|
| 178 | + * |
|
| 179 | + * @param string $method HTTP method. |
|
| 180 | + * @param string|UriInterface $uri URI object or string. |
|
| 181 | + * @param array $options Request options to apply. See \OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\RequestOptions. |
|
| 182 | + * |
|
| 183 | + * @throws GuzzleException |
|
| 184 | + */ |
|
| 185 | + public function request(string $method, $uri = '', array $options = []): ResponseInterface |
|
| 186 | + { |
|
| 187 | + $options[RequestOptions::SYNCHRONOUS] = true; |
|
| 188 | + |
|
| 189 | + return $this->requestAsync($method, $uri, $options)->wait(); |
|
| 190 | + } |
|
| 191 | + |
|
| 192 | + /** |
|
| 193 | + * Get a client configuration option. |
|
| 194 | + * |
|
| 195 | + * These options include default request options of the client, a "handler" |
|
| 196 | + * (if utilized by the concrete client), and a "base_uri" if utilized by |
|
| 197 | + * the concrete client. |
|
| 198 | + * |
|
| 199 | + * @param string|null $option The config option to retrieve. |
|
| 200 | + * |
|
| 201 | + * @return mixed |
|
| 202 | + * |
|
| 203 | + * @deprecated Client::getConfig will be removed in guzzlehttp/guzzle:8.0. |
|
| 204 | + */ |
|
| 205 | + public function getConfig(string $option = null) |
|
| 206 | + { |
|
| 207 | + return $option === null |
|
| 208 | + ? $this->config |
|
| 209 | + : ($this->config[$option] ?? null); |
|
| 210 | + } |
|
| 211 | + |
|
| 212 | + private function buildUri(UriInterface $uri, array $config): UriInterface |
|
| 213 | + { |
|
| 214 | + if (isset($config['base_uri'])) { |
|
| 215 | + $uri = Psr7\UriResolver::resolve(Psr7\Utils::uriFor($config['base_uri']), $uri); |
|
| 216 | + } |
|
| 217 | + |
|
| 218 | + if (isset($config['idn_conversion']) && ($config['idn_conversion'] !== false)) { |
|
| 219 | + $idnOptions = ($config['idn_conversion'] === true) ? \IDNA_DEFAULT : $config['idn_conversion']; |
|
| 220 | + $uri = Utils::idnUriConvert($uri, $idnOptions); |
|
| 221 | + } |
|
| 222 | + |
|
| 223 | + return $uri->getScheme() === '' && $uri->getHost() !== '' ? $uri->withScheme('http') : $uri; |
|
| 224 | + } |
|
| 225 | + |
|
| 226 | + /** |
|
| 227 | + * Configures the default options for a client. |
|
| 228 | + */ |
|
| 229 | + private function configureDefaults(array $config): void |
|
| 230 | + { |
|
| 231 | + $defaults = [ |
|
| 232 | + 'allow_redirects' => RedirectMiddleware::$defaultSettings, |
|
| 233 | + 'http_errors' => true, |
|
| 234 | + 'decode_content' => true, |
|
| 235 | + 'verify' => true, |
|
| 236 | + 'cookies' => false, |
|
| 237 | + 'idn_conversion' => false, |
|
| 238 | + ]; |
|
| 239 | + |
|
| 240 | + // Use the standard Linux HTTP_PROXY and HTTPS_PROXY if set. |
|
| 241 | + |
|
| 242 | + // We can only trust the HTTP_PROXY environment variable in a CLI |
|
| 243 | + // process due to the fact that PHP has no reliable mechanism to |
|
| 244 | + // get environment variables that start with "HTTP_". |
|
| 245 | + if (\PHP_SAPI === 'cli' && ($proxy = Utils::getenv('HTTP_PROXY'))) { |
|
| 246 | + $defaults['proxy']['http'] = $proxy; |
|
| 247 | + } |
|
| 248 | + |
|
| 249 | + if ($proxy = Utils::getenv('HTTPS_PROXY')) { |
|
| 250 | + $defaults['proxy']['https'] = $proxy; |
|
| 251 | + } |
|
| 252 | + |
|
| 253 | + if ($noProxy = Utils::getenv('NO_PROXY')) { |
|
| 254 | + $cleanedNoProxy = \str_replace(' ', '', $noProxy); |
|
| 255 | + $defaults['proxy']['no'] = \explode(',', $cleanedNoProxy); |
|
| 256 | + } |
|
| 257 | + |
|
| 258 | + $this->config = $config + $defaults; |
|
| 259 | + |
|
| 260 | + if (!empty($config['cookies']) && $config['cookies'] === true) { |
|
| 261 | + $this->config['cookies'] = new CookieJar(); |
|
| 262 | + } |
|
| 263 | + |
|
| 264 | + // Add the default user-agent header. |
|
| 265 | + if (!isset($this->config['headers'])) { |
|
| 266 | + $this->config['headers'] = ['User-Agent' => Utils::defaultUserAgent()]; |
|
| 267 | + } else { |
|
| 268 | + // Add the User-Agent header if one was not already set. |
|
| 269 | + foreach (\array_keys($this->config['headers']) as $name) { |
|
| 270 | + if (\strtolower($name) === 'user-agent') { |
|
| 271 | + return; |
|
| 272 | + } |
|
| 273 | + } |
|
| 274 | + $this->config['headers']['User-Agent'] = Utils::defaultUserAgent(); |
|
| 275 | + } |
|
| 276 | + } |
|
| 277 | + |
|
| 278 | + /** |
|
| 279 | + * Merges default options into the array. |
|
| 280 | + * |
|
| 281 | + * @param array $options Options to modify by reference |
|
| 282 | + */ |
|
| 283 | + private function prepareDefaults(array $options): array |
|
| 284 | + { |
|
| 285 | + $defaults = $this->config; |
|
| 286 | + |
|
| 287 | + if (!empty($defaults['headers'])) { |
|
| 288 | + // Default headers are only added if they are not present. |
|
| 289 | + $defaults['_conditional'] = $defaults['headers']; |
|
| 290 | + unset($defaults['headers']); |
|
| 291 | + } |
|
| 292 | + |
|
| 293 | + // Special handling for headers is required as they are added as |
|
| 294 | + // conditional headers and as headers passed to a request ctor. |
|
| 295 | + if (\array_key_exists('headers', $options)) { |
|
| 296 | + // Allows default headers to be unset. |
|
| 297 | + if ($options['headers'] === null) { |
|
| 298 | + $defaults['_conditional'] = []; |
|
| 299 | + unset($options['headers']); |
|
| 300 | + } elseif (!\is_array($options['headers'])) { |
|
| 301 | + throw new InvalidArgumentException('headers must be an array'); |
|
| 302 | + } |
|
| 303 | + } |
|
| 304 | + |
|
| 305 | + // Shallow merge defaults underneath options. |
|
| 306 | + $result = $options + $defaults; |
|
| 307 | + |
|
| 308 | + // Remove null values. |
|
| 309 | + foreach ($result as $k => $v) { |
|
| 310 | + if ($v === null) { |
|
| 311 | + unset($result[$k]); |
|
| 312 | + } |
|
| 313 | + } |
|
| 314 | + |
|
| 315 | + return $result; |
|
| 316 | + } |
|
| 317 | + |
|
| 318 | + /** |
|
| 319 | + * Transfers the given request and applies request options. |
|
| 320 | + * |
|
| 321 | + * The URI of the request is not modified and the request options are used |
|
| 322 | + * as-is without merging in default options. |
|
| 323 | + * |
|
| 324 | + * @param array $options See \OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\RequestOptions. |
|
| 325 | + */ |
|
| 326 | + private function transfer(RequestInterface $request, array $options): PromiseInterface |
|
| 327 | + { |
|
| 328 | + $request = $this->applyOptions($request, $options); |
|
| 329 | + /** @var HandlerStack $handler */ |
|
| 330 | + $handler = $options['handler']; |
|
| 331 | + |
|
| 332 | + try { |
|
| 333 | + return P\Create::promiseFor($handler($request, $options)); |
|
| 334 | + } catch (\Exception $e) { |
|
| 335 | + return P\Create::rejectionFor($e); |
|
| 336 | + } |
|
| 337 | + } |
|
| 338 | + |
|
| 339 | + /** |
|
| 340 | + * Applies the array of request options to a request. |
|
| 341 | + */ |
|
| 342 | + private function applyOptions(RequestInterface $request, array &$options): RequestInterface |
|
| 343 | + { |
|
| 344 | + $modify = [ |
|
| 345 | + 'set_headers' => [], |
|
| 346 | + ]; |
|
| 347 | + |
|
| 348 | + if (isset($options['headers'])) { |
|
| 349 | + if (array_keys($options['headers']) === range(0, count($options['headers']) - 1)) { |
|
| 350 | + throw new InvalidArgumentException('The headers array must have header name as keys.'); |
|
| 351 | + } |
|
| 352 | + $modify['set_headers'] = $options['headers']; |
|
| 353 | + unset($options['headers']); |
|
| 354 | + } |
|
| 355 | + |
|
| 356 | + if (isset($options['form_params'])) { |
|
| 357 | + if (isset($options['multipart'])) { |
|
| 358 | + throw new InvalidArgumentException('You cannot use ' |
|
| 359 | + .'form_params and multipart at the same time. Use the ' |
|
| 360 | + .'form_params option if you want to send application/' |
|
| 361 | + .'x-www-form-urlencoded requests, and the multipart ' |
|
| 362 | + .'option to send multipart/form-data requests.'); |
|
| 363 | + } |
|
| 364 | + $options['body'] = \http_build_query($options['form_params'], '', '&'); |
|
| 365 | + unset($options['form_params']); |
|
| 366 | + // Ensure that we don't have the header in different case and set the new value. |
|
| 367 | + $options['_conditional'] = Psr7\Utils::caselessRemove(['Content-Type'], $options['_conditional']); |
|
| 368 | + $options['_conditional']['Content-Type'] = 'application/x-www-form-urlencoded'; |
|
| 369 | + } |
|
| 370 | + |
|
| 371 | + if (isset($options['multipart'])) { |
|
| 372 | + $options['body'] = new Psr7\MultipartStream($options['multipart']); |
|
| 373 | + unset($options['multipart']); |
|
| 374 | + } |
|
| 375 | + |
|
| 376 | + if (isset($options['json'])) { |
|
| 377 | + $options['body'] = Utils::jsonEncode($options['json']); |
|
| 378 | + unset($options['json']); |
|
| 379 | + // Ensure that we don't have the header in different case and set the new value. |
|
| 380 | + $options['_conditional'] = Psr7\Utils::caselessRemove(['Content-Type'], $options['_conditional']); |
|
| 381 | + $options['_conditional']['Content-Type'] = 'application/json'; |
|
| 382 | + } |
|
| 383 | + |
|
| 384 | + if (!empty($options['decode_content']) |
|
| 385 | + && $options['decode_content'] !== true |
|
| 386 | + ) { |
|
| 387 | + // Ensure that we don't have the header in different case and set the new value. |
|
| 388 | + $options['_conditional'] = Psr7\Utils::caselessRemove(['Accept-Encoding'], $options['_conditional']); |
|
| 389 | + $modify['set_headers']['Accept-Encoding'] = $options['decode_content']; |
|
| 390 | + } |
|
| 391 | + |
|
| 392 | + if (isset($options['body'])) { |
|
| 393 | + if (\is_array($options['body'])) { |
|
| 394 | + throw $this->invalidBody(); |
|
| 395 | + } |
|
| 396 | + $modify['body'] = Psr7\Utils::streamFor($options['body']); |
|
| 397 | + unset($options['body']); |
|
| 398 | + } |
|
| 399 | + |
|
| 400 | + if (!empty($options['auth']) && \is_array($options['auth'])) { |
|
| 401 | + $value = $options['auth']; |
|
| 402 | + $type = isset($value[2]) ? \strtolower($value[2]) : 'basic'; |
|
| 403 | + switch ($type) { |
|
| 404 | + case 'basic': |
|
| 405 | + // Ensure that we don't have the header in different case and set the new value. |
|
| 406 | + $modify['set_headers'] = Psr7\Utils::caselessRemove(['Authorization'], $modify['set_headers']); |
|
| 407 | + $modify['set_headers']['Authorization'] = 'Basic ' |
|
| 408 | + .\base64_encode("$value[0]:$value[1]"); |
|
| 409 | + break; |
|
| 410 | + case 'digest': |
|
| 411 | + // @todo: Do not rely on curl |
|
| 412 | + $options['curl'][\CURLOPT_HTTPAUTH] = \CURLAUTH_DIGEST; |
|
| 413 | + $options['curl'][\CURLOPT_USERPWD] = "$value[0]:$value[1]"; |
|
| 414 | + break; |
|
| 415 | + case 'ntlm': |
|
| 416 | + $options['curl'][\CURLOPT_HTTPAUTH] = \CURLAUTH_NTLM; |
|
| 417 | + $options['curl'][\CURLOPT_USERPWD] = "$value[0]:$value[1]"; |
|
| 418 | + break; |
|
| 419 | + } |
|
| 420 | + } |
|
| 421 | + |
|
| 422 | + if (isset($options['query'])) { |
|
| 423 | + $value = $options['query']; |
|
| 424 | + if (\is_array($value)) { |
|
| 425 | + $value = \http_build_query($value, '', '&', \PHP_QUERY_RFC3986); |
|
| 426 | + } |
|
| 427 | + if (!\is_string($value)) { |
|
| 428 | + throw new InvalidArgumentException('query must be a string or array'); |
|
| 429 | + } |
|
| 430 | + $modify['query'] = $value; |
|
| 431 | + unset($options['query']); |
|
| 432 | + } |
|
| 433 | + |
|
| 434 | + // Ensure that sink is not an invalid value. |
|
| 435 | + if (isset($options['sink'])) { |
|
| 436 | + // TODO: Add more sink validation? |
|
| 437 | + if (\is_bool($options['sink'])) { |
|
| 438 | + throw new InvalidArgumentException('sink must not be a boolean'); |
|
| 439 | + } |
|
| 440 | + } |
|
| 441 | + |
|
| 442 | + if (isset($options['version'])) { |
|
| 443 | + $modify['version'] = $options['version']; |
|
| 444 | + } |
|
| 445 | + |
|
| 446 | + $request = Psr7\Utils::modifyRequest($request, $modify); |
|
| 447 | + if ($request->getBody() instanceof Psr7\MultipartStream) { |
|
| 448 | + // Use a multipart/form-data POST if a Content-Type is not set. |
|
| 449 | + // Ensure that we don't have the header in different case and set the new value. |
|
| 450 | + $options['_conditional'] = Psr7\Utils::caselessRemove(['Content-Type'], $options['_conditional']); |
|
| 451 | + $options['_conditional']['Content-Type'] = 'multipart/form-data; boundary=' |
|
| 452 | + .$request->getBody()->getBoundary(); |
|
| 453 | + } |
|
| 454 | + |
|
| 455 | + // Merge in conditional headers if they are not present. |
|
| 456 | + if (isset($options['_conditional'])) { |
|
| 457 | + // Build up the changes so it's in a single clone of the message. |
|
| 458 | + $modify = []; |
|
| 459 | + foreach ($options['_conditional'] as $k => $v) { |
|
| 460 | + if (!$request->hasHeader($k)) { |
|
| 461 | + $modify['set_headers'][$k] = $v; |
|
| 462 | + } |
|
| 463 | + } |
|
| 464 | + $request = Psr7\Utils::modifyRequest($request, $modify); |
|
| 465 | + // Don't pass this internal value along to middleware/handlers. |
|
| 466 | + unset($options['_conditional']); |
|
| 467 | + } |
|
| 468 | + |
|
| 469 | + return $request; |
|
| 470 | + } |
|
| 471 | + |
|
| 472 | + /** |
|
| 473 | + * Return an InvalidArgumentException with pre-set message. |
|
| 474 | + */ |
|
| 475 | + private function invalidBody(): InvalidArgumentException |
|
| 476 | + { |
|
| 477 | + return new InvalidArgumentException('Passing in the "body" request ' |
|
| 478 | + .'option as an array to send a request is not supported. ' |
|
| 479 | + .'Please use the "form_params" request option to send a ' |
|
| 480 | + .'application/x-www-form-urlencoded request, or the "multipart" ' |
|
| 481 | + .'request option to send a multipart/form-data request.'); |
|
| 482 | + } |
|
| 483 | 483 | } |
@@ -6,254 +6,254 @@ |
||
| 6 | 6 | |
| 7 | 7 | final class Utils |
| 8 | 8 | { |
| 9 | - /** |
|
| 10 | - * Get the global task queue used for promise resolution. |
|
| 11 | - * |
|
| 12 | - * This task queue MUST be run in an event loop in order for promises to be |
|
| 13 | - * settled asynchronously. It will be automatically run when synchronously |
|
| 14 | - * waiting on a promise. |
|
| 15 | - * |
|
| 16 | - * <code> |
|
| 17 | - * while ($eventLoop->isRunning()) { |
|
| 18 | - * OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\Promise\Utils::queue()->run(); |
|
| 19 | - * } |
|
| 20 | - * </code> |
|
| 21 | - * |
|
| 22 | - * @param TaskQueueInterface|null $assign Optionally specify a new queue instance. |
|
| 23 | - */ |
|
| 24 | - public static function queue(TaskQueueInterface $assign = null): TaskQueueInterface |
|
| 25 | - { |
|
| 26 | - static $queue; |
|
| 9 | + /** |
|
| 10 | + * Get the global task queue used for promise resolution. |
|
| 11 | + * |
|
| 12 | + * This task queue MUST be run in an event loop in order for promises to be |
|
| 13 | + * settled asynchronously. It will be automatically run when synchronously |
|
| 14 | + * waiting on a promise. |
|
| 15 | + * |
|
| 16 | + * <code> |
|
| 17 | + * while ($eventLoop->isRunning()) { |
|
| 18 | + * OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\Promise\Utils::queue()->run(); |
|
| 19 | + * } |
|
| 20 | + * </code> |
|
| 21 | + * |
|
| 22 | + * @param TaskQueueInterface|null $assign Optionally specify a new queue instance. |
|
| 23 | + */ |
|
| 24 | + public static function queue(TaskQueueInterface $assign = null): TaskQueueInterface |
|
| 25 | + { |
|
| 26 | + static $queue; |
|
| 27 | 27 | |
| 28 | - if ($assign) { |
|
| 29 | - $queue = $assign; |
|
| 30 | - } elseif (!$queue) { |
|
| 31 | - $queue = new TaskQueue(); |
|
| 32 | - } |
|
| 28 | + if ($assign) { |
|
| 29 | + $queue = $assign; |
|
| 30 | + } elseif (!$queue) { |
|
| 31 | + $queue = new TaskQueue(); |
|
| 32 | + } |
|
| 33 | 33 | |
| 34 | - return $queue; |
|
| 35 | - } |
|
| 34 | + return $queue; |
|
| 35 | + } |
|
| 36 | 36 | |
| 37 | - /** |
|
| 38 | - * Adds a function to run in the task queue when it is next `run()` and |
|
| 39 | - * returns a promise that is fulfilled or rejected with the result. |
|
| 40 | - * |
|
| 41 | - * @param callable $task Task function to run. |
|
| 42 | - */ |
|
| 43 | - public static function task(callable $task): PromiseInterface |
|
| 44 | - { |
|
| 45 | - $queue = self::queue(); |
|
| 46 | - $promise = new Promise([$queue, 'run']); |
|
| 47 | - $queue->add(function () use ($task, $promise): void { |
|
| 48 | - try { |
|
| 49 | - if (Is::pending($promise)) { |
|
| 50 | - $promise->resolve($task()); |
|
| 51 | - } |
|
| 52 | - } catch (\Throwable $e) { |
|
| 53 | - $promise->reject($e); |
|
| 54 | - } |
|
| 55 | - }); |
|
| 37 | + /** |
|
| 38 | + * Adds a function to run in the task queue when it is next `run()` and |
|
| 39 | + * returns a promise that is fulfilled or rejected with the result. |
|
| 40 | + * |
|
| 41 | + * @param callable $task Task function to run. |
|
| 42 | + */ |
|
| 43 | + public static function task(callable $task): PromiseInterface |
|
| 44 | + { |
|
| 45 | + $queue = self::queue(); |
|
| 46 | + $promise = new Promise([$queue, 'run']); |
|
| 47 | + $queue->add(function () use ($task, $promise): void { |
|
| 48 | + try { |
|
| 49 | + if (Is::pending($promise)) { |
|
| 50 | + $promise->resolve($task()); |
|
| 51 | + } |
|
| 52 | + } catch (\Throwable $e) { |
|
| 53 | + $promise->reject($e); |
|
| 54 | + } |
|
| 55 | + }); |
|
| 56 | 56 | |
| 57 | - return $promise; |
|
| 58 | - } |
|
| 57 | + return $promise; |
|
| 58 | + } |
|
| 59 | 59 | |
| 60 | - /** |
|
| 61 | - * Synchronously waits on a promise to resolve and returns an inspection |
|
| 62 | - * state array. |
|
| 63 | - * |
|
| 64 | - * Returns a state associative array containing a "state" key mapping to a |
|
| 65 | - * valid promise state. If the state of the promise is "fulfilled", the |
|
| 66 | - * array will contain a "value" key mapping to the fulfilled value of the |
|
| 67 | - * promise. If the promise is rejected, the array will contain a "reason" |
|
| 68 | - * key mapping to the rejection reason of the promise. |
|
| 69 | - * |
|
| 70 | - * @param PromiseInterface $promise Promise or value. |
|
| 71 | - */ |
|
| 72 | - public static function inspect(PromiseInterface $promise): array |
|
| 73 | - { |
|
| 74 | - try { |
|
| 75 | - return [ |
|
| 76 | - 'state' => PromiseInterface::FULFILLED, |
|
| 77 | - 'value' => $promise->wait(), |
|
| 78 | - ]; |
|
| 79 | - } catch (RejectionException $e) { |
|
| 80 | - return ['state' => PromiseInterface::REJECTED, 'reason' => $e->getReason()]; |
|
| 81 | - } catch (\Throwable $e) { |
|
| 82 | - return ['state' => PromiseInterface::REJECTED, 'reason' => $e]; |
|
| 83 | - } |
|
| 84 | - } |
|
| 60 | + /** |
|
| 61 | + * Synchronously waits on a promise to resolve and returns an inspection |
|
| 62 | + * state array. |
|
| 63 | + * |
|
| 64 | + * Returns a state associative array containing a "state" key mapping to a |
|
| 65 | + * valid promise state. If the state of the promise is "fulfilled", the |
|
| 66 | + * array will contain a "value" key mapping to the fulfilled value of the |
|
| 67 | + * promise. If the promise is rejected, the array will contain a "reason" |
|
| 68 | + * key mapping to the rejection reason of the promise. |
|
| 69 | + * |
|
| 70 | + * @param PromiseInterface $promise Promise or value. |
|
| 71 | + */ |
|
| 72 | + public static function inspect(PromiseInterface $promise): array |
|
| 73 | + { |
|
| 74 | + try { |
|
| 75 | + return [ |
|
| 76 | + 'state' => PromiseInterface::FULFILLED, |
|
| 77 | + 'value' => $promise->wait(), |
|
| 78 | + ]; |
|
| 79 | + } catch (RejectionException $e) { |
|
| 80 | + return ['state' => PromiseInterface::REJECTED, 'reason' => $e->getReason()]; |
|
| 81 | + } catch (\Throwable $e) { |
|
| 82 | + return ['state' => PromiseInterface::REJECTED, 'reason' => $e]; |
|
| 83 | + } |
|
| 84 | + } |
|
| 85 | 85 | |
| 86 | - /** |
|
| 87 | - * Waits on all of the provided promises, but does not unwrap rejected |
|
| 88 | - * promises as thrown exception. |
|
| 89 | - * |
|
| 90 | - * Returns an array of inspection state arrays. |
|
| 91 | - * |
|
| 92 | - * @see inspect for the inspection state array format. |
|
| 93 | - * |
|
| 94 | - * @param PromiseInterface[] $promises Traversable of promises to wait upon. |
|
| 95 | - */ |
|
| 96 | - public static function inspectAll($promises): array |
|
| 97 | - { |
|
| 98 | - $results = []; |
|
| 99 | - foreach ($promises as $key => $promise) { |
|
| 100 | - $results[$key] = self::inspect($promise); |
|
| 101 | - } |
|
| 86 | + /** |
|
| 87 | + * Waits on all of the provided promises, but does not unwrap rejected |
|
| 88 | + * promises as thrown exception. |
|
| 89 | + * |
|
| 90 | + * Returns an array of inspection state arrays. |
|
| 91 | + * |
|
| 92 | + * @see inspect for the inspection state array format. |
|
| 93 | + * |
|
| 94 | + * @param PromiseInterface[] $promises Traversable of promises to wait upon. |
|
| 95 | + */ |
|
| 96 | + public static function inspectAll($promises): array |
|
| 97 | + { |
|
| 98 | + $results = []; |
|
| 99 | + foreach ($promises as $key => $promise) { |
|
| 100 | + $results[$key] = self::inspect($promise); |
|
| 101 | + } |
|
| 102 | 102 | |
| 103 | - return $results; |
|
| 104 | - } |
|
| 103 | + return $results; |
|
| 104 | + } |
|
| 105 | 105 | |
| 106 | - /** |
|
| 107 | - * Waits on all of the provided promises and returns the fulfilled values. |
|
| 108 | - * |
|
| 109 | - * Returns an array that contains the value of each promise (in the same |
|
| 110 | - * order the promises were provided). An exception is thrown if any of the |
|
| 111 | - * promises are rejected. |
|
| 112 | - * |
|
| 113 | - * @param iterable<PromiseInterface> $promises Iterable of PromiseInterface objects to wait on. |
|
| 114 | - * |
|
| 115 | - * @throws \Throwable on error |
|
| 116 | - */ |
|
| 117 | - public static function unwrap($promises): array |
|
| 118 | - { |
|
| 119 | - $results = []; |
|
| 120 | - foreach ($promises as $key => $promise) { |
|
| 121 | - $results[$key] = $promise->wait(); |
|
| 122 | - } |
|
| 106 | + /** |
|
| 107 | + * Waits on all of the provided promises and returns the fulfilled values. |
|
| 108 | + * |
|
| 109 | + * Returns an array that contains the value of each promise (in the same |
|
| 110 | + * order the promises were provided). An exception is thrown if any of the |
|
| 111 | + * promises are rejected. |
|
| 112 | + * |
|
| 113 | + * @param iterable<PromiseInterface> $promises Iterable of PromiseInterface objects to wait on. |
|
| 114 | + * |
|
| 115 | + * @throws \Throwable on error |
|
| 116 | + */ |
|
| 117 | + public static function unwrap($promises): array |
|
| 118 | + { |
|
| 119 | + $results = []; |
|
| 120 | + foreach ($promises as $key => $promise) { |
|
| 121 | + $results[$key] = $promise->wait(); |
|
| 122 | + } |
|
| 123 | 123 | |
| 124 | - return $results; |
|
| 125 | - } |
|
| 124 | + return $results; |
|
| 125 | + } |
|
| 126 | 126 | |
| 127 | - /** |
|
| 128 | - * Given an array of promises, return a promise that is fulfilled when all |
|
| 129 | - * the items in the array are fulfilled. |
|
| 130 | - * |
|
| 131 | - * The promise's fulfillment value is an array with fulfillment values at |
|
| 132 | - * respective positions to the original array. If any promise in the array |
|
| 133 | - * rejects, the returned promise is rejected with the rejection reason. |
|
| 134 | - * |
|
| 135 | - * @param mixed $promises Promises or values. |
|
| 136 | - * @param bool $recursive If true, resolves new promises that might have been added to the stack during its own resolution. |
|
| 137 | - */ |
|
| 138 | - public static function all($promises, bool $recursive = false): PromiseInterface |
|
| 139 | - { |
|
| 140 | - $results = []; |
|
| 141 | - $promise = Each::of( |
|
| 142 | - $promises, |
|
| 143 | - function ($value, $idx) use (&$results): void { |
|
| 144 | - $results[$idx] = $value; |
|
| 145 | - }, |
|
| 146 | - function ($reason, $idx, Promise $aggregate): void { |
|
| 147 | - $aggregate->reject($reason); |
|
| 148 | - } |
|
| 149 | - )->then(function () use (&$results) { |
|
| 150 | - ksort($results); |
|
| 127 | + /** |
|
| 128 | + * Given an array of promises, return a promise that is fulfilled when all |
|
| 129 | + * the items in the array are fulfilled. |
|
| 130 | + * |
|
| 131 | + * The promise's fulfillment value is an array with fulfillment values at |
|
| 132 | + * respective positions to the original array. If any promise in the array |
|
| 133 | + * rejects, the returned promise is rejected with the rejection reason. |
|
| 134 | + * |
|
| 135 | + * @param mixed $promises Promises or values. |
|
| 136 | + * @param bool $recursive If true, resolves new promises that might have been added to the stack during its own resolution. |
|
| 137 | + */ |
|
| 138 | + public static function all($promises, bool $recursive = false): PromiseInterface |
|
| 139 | + { |
|
| 140 | + $results = []; |
|
| 141 | + $promise = Each::of( |
|
| 142 | + $promises, |
|
| 143 | + function ($value, $idx) use (&$results): void { |
|
| 144 | + $results[$idx] = $value; |
|
| 145 | + }, |
|
| 146 | + function ($reason, $idx, Promise $aggregate): void { |
|
| 147 | + $aggregate->reject($reason); |
|
| 148 | + } |
|
| 149 | + )->then(function () use (&$results) { |
|
| 150 | + ksort($results); |
|
| 151 | 151 | |
| 152 | - return $results; |
|
| 153 | - }); |
|
| 152 | + return $results; |
|
| 153 | + }); |
|
| 154 | 154 | |
| 155 | - if (true === $recursive) { |
|
| 156 | - $promise = $promise->then(function ($results) use ($recursive, &$promises) { |
|
| 157 | - foreach ($promises as $promise) { |
|
| 158 | - if (Is::pending($promise)) { |
|
| 159 | - return self::all($promises, $recursive); |
|
| 160 | - } |
|
| 161 | - } |
|
| 155 | + if (true === $recursive) { |
|
| 156 | + $promise = $promise->then(function ($results) use ($recursive, &$promises) { |
|
| 157 | + foreach ($promises as $promise) { |
|
| 158 | + if (Is::pending($promise)) { |
|
| 159 | + return self::all($promises, $recursive); |
|
| 160 | + } |
|
| 161 | + } |
|
| 162 | 162 | |
| 163 | - return $results; |
|
| 164 | - }); |
|
| 165 | - } |
|
| 163 | + return $results; |
|
| 164 | + }); |
|
| 165 | + } |
|
| 166 | 166 | |
| 167 | - return $promise; |
|
| 168 | - } |
|
| 167 | + return $promise; |
|
| 168 | + } |
|
| 169 | 169 | |
| 170 | - /** |
|
| 171 | - * Initiate a competitive race between multiple promises or values (values |
|
| 172 | - * will become immediately fulfilled promises). |
|
| 173 | - * |
|
| 174 | - * When count amount of promises have been fulfilled, the returned promise |
|
| 175 | - * is fulfilled with an array that contains the fulfillment values of the |
|
| 176 | - * winners in order of resolution. |
|
| 177 | - * |
|
| 178 | - * This promise is rejected with a {@see AggregateException} if the number |
|
| 179 | - * of fulfilled promises is less than the desired $count. |
|
| 180 | - * |
|
| 181 | - * @param int $count Total number of promises. |
|
| 182 | - * @param mixed $promises Promises or values. |
|
| 183 | - */ |
|
| 184 | - public static function some(int $count, $promises): PromiseInterface |
|
| 185 | - { |
|
| 186 | - $results = []; |
|
| 187 | - $rejections = []; |
|
| 170 | + /** |
|
| 171 | + * Initiate a competitive race between multiple promises or values (values |
|
| 172 | + * will become immediately fulfilled promises). |
|
| 173 | + * |
|
| 174 | + * When count amount of promises have been fulfilled, the returned promise |
|
| 175 | + * is fulfilled with an array that contains the fulfillment values of the |
|
| 176 | + * winners in order of resolution. |
|
| 177 | + * |
|
| 178 | + * This promise is rejected with a {@see AggregateException} if the number |
|
| 179 | + * of fulfilled promises is less than the desired $count. |
|
| 180 | + * |
|
| 181 | + * @param int $count Total number of promises. |
|
| 182 | + * @param mixed $promises Promises or values. |
|
| 183 | + */ |
|
| 184 | + public static function some(int $count, $promises): PromiseInterface |
|
| 185 | + { |
|
| 186 | + $results = []; |
|
| 187 | + $rejections = []; |
|
| 188 | 188 | |
| 189 | - return Each::of( |
|
| 190 | - $promises, |
|
| 191 | - function ($value, $idx, PromiseInterface $p) use (&$results, $count): void { |
|
| 192 | - if (Is::settled($p)) { |
|
| 193 | - return; |
|
| 194 | - } |
|
| 195 | - $results[$idx] = $value; |
|
| 196 | - if (count($results) >= $count) { |
|
| 197 | - $p->resolve(null); |
|
| 198 | - } |
|
| 199 | - }, |
|
| 200 | - function ($reason) use (&$rejections): void { |
|
| 201 | - $rejections[] = $reason; |
|
| 202 | - } |
|
| 203 | - )->then( |
|
| 204 | - function () use (&$results, &$rejections, $count) { |
|
| 205 | - if (count($results) !== $count) { |
|
| 206 | - throw new AggregateException( |
|
| 207 | - 'Not enough promises to fulfill count', |
|
| 208 | - $rejections |
|
| 209 | - ); |
|
| 210 | - } |
|
| 211 | - ksort($results); |
|
| 189 | + return Each::of( |
|
| 190 | + $promises, |
|
| 191 | + function ($value, $idx, PromiseInterface $p) use (&$results, $count): void { |
|
| 192 | + if (Is::settled($p)) { |
|
| 193 | + return; |
|
| 194 | + } |
|
| 195 | + $results[$idx] = $value; |
|
| 196 | + if (count($results) >= $count) { |
|
| 197 | + $p->resolve(null); |
|
| 198 | + } |
|
| 199 | + }, |
|
| 200 | + function ($reason) use (&$rejections): void { |
|
| 201 | + $rejections[] = $reason; |
|
| 202 | + } |
|
| 203 | + )->then( |
|
| 204 | + function () use (&$results, &$rejections, $count) { |
|
| 205 | + if (count($results) !== $count) { |
|
| 206 | + throw new AggregateException( |
|
| 207 | + 'Not enough promises to fulfill count', |
|
| 208 | + $rejections |
|
| 209 | + ); |
|
| 210 | + } |
|
| 211 | + ksort($results); |
|
| 212 | 212 | |
| 213 | - return array_values($results); |
|
| 214 | - } |
|
| 215 | - ); |
|
| 216 | - } |
|
| 213 | + return array_values($results); |
|
| 214 | + } |
|
| 215 | + ); |
|
| 216 | + } |
|
| 217 | 217 | |
| 218 | - /** |
|
| 219 | - * Like some(), with 1 as count. However, if the promise fulfills, the |
|
| 220 | - * fulfillment value is not an array of 1 but the value directly. |
|
| 221 | - * |
|
| 222 | - * @param mixed $promises Promises or values. |
|
| 223 | - */ |
|
| 224 | - public static function any($promises): PromiseInterface |
|
| 225 | - { |
|
| 226 | - return self::some(1, $promises)->then(function ($values) { |
|
| 227 | - return $values[0]; |
|
| 228 | - }); |
|
| 229 | - } |
|
| 218 | + /** |
|
| 219 | + * Like some(), with 1 as count. However, if the promise fulfills, the |
|
| 220 | + * fulfillment value is not an array of 1 but the value directly. |
|
| 221 | + * |
|
| 222 | + * @param mixed $promises Promises or values. |
|
| 223 | + */ |
|
| 224 | + public static function any($promises): PromiseInterface |
|
| 225 | + { |
|
| 226 | + return self::some(1, $promises)->then(function ($values) { |
|
| 227 | + return $values[0]; |
|
| 228 | + }); |
|
| 229 | + } |
|
| 230 | 230 | |
| 231 | - /** |
|
| 232 | - * Returns a promise that is fulfilled when all of the provided promises have |
|
| 233 | - * been fulfilled or rejected. |
|
| 234 | - * |
|
| 235 | - * The returned promise is fulfilled with an array of inspection state arrays. |
|
| 236 | - * |
|
| 237 | - * @see inspect for the inspection state array format. |
|
| 238 | - * |
|
| 239 | - * @param mixed $promises Promises or values. |
|
| 240 | - */ |
|
| 241 | - public static function settle($promises): PromiseInterface |
|
| 242 | - { |
|
| 243 | - $results = []; |
|
| 231 | + /** |
|
| 232 | + * Returns a promise that is fulfilled when all of the provided promises have |
|
| 233 | + * been fulfilled or rejected. |
|
| 234 | + * |
|
| 235 | + * The returned promise is fulfilled with an array of inspection state arrays. |
|
| 236 | + * |
|
| 237 | + * @see inspect for the inspection state array format. |
|
| 238 | + * |
|
| 239 | + * @param mixed $promises Promises or values. |
|
| 240 | + */ |
|
| 241 | + public static function settle($promises): PromiseInterface |
|
| 242 | + { |
|
| 243 | + $results = []; |
|
| 244 | 244 | |
| 245 | - return Each::of( |
|
| 246 | - $promises, |
|
| 247 | - function ($value, $idx) use (&$results): void { |
|
| 248 | - $results[$idx] = ['state' => PromiseInterface::FULFILLED, 'value' => $value]; |
|
| 249 | - }, |
|
| 250 | - function ($reason, $idx) use (&$results): void { |
|
| 251 | - $results[$idx] = ['state' => PromiseInterface::REJECTED, 'reason' => $reason]; |
|
| 252 | - } |
|
| 253 | - )->then(function () use (&$results) { |
|
| 254 | - ksort($results); |
|
| 245 | + return Each::of( |
|
| 246 | + $promises, |
|
| 247 | + function ($value, $idx) use (&$results): void { |
|
| 248 | + $results[$idx] = ['state' => PromiseInterface::FULFILLED, 'value' => $value]; |
|
| 249 | + }, |
|
| 250 | + function ($reason, $idx) use (&$results): void { |
|
| 251 | + $results[$idx] = ['state' => PromiseInterface::REJECTED, 'reason' => $reason]; |
|
| 252 | + } |
|
| 253 | + )->then(function () use (&$results) { |
|
| 254 | + ksort($results); |
|
| 255 | 255 | |
| 256 | - return $results; |
|
| 257 | - }); |
|
| 258 | - } |
|
| 256 | + return $results; |
|
| 257 | + }); |
|
| 258 | + } |
|
| 259 | 259 | } |
@@ -44,7 +44,7 @@ discard block |
||
| 44 | 44 | { |
| 45 | 45 | $queue = self::queue(); |
| 46 | 46 | $promise = new Promise([$queue, 'run']); |
| 47 | - $queue->add(function () use ($task, $promise): void { |
|
| 47 | + $queue->add(function() use ($task, $promise): void { |
|
| 48 | 48 | try { |
| 49 | 49 | if (Is::pending($promise)) { |
| 50 | 50 | $promise->resolve($task()); |
@@ -140,20 +140,20 @@ discard block |
||
| 140 | 140 | $results = []; |
| 141 | 141 | $promise = Each::of( |
| 142 | 142 | $promises, |
| 143 | - function ($value, $idx) use (&$results): void { |
|
| 143 | + function($value, $idx) use (&$results): void { |
|
| 144 | 144 | $results[$idx] = $value; |
| 145 | 145 | }, |
| 146 | - function ($reason, $idx, Promise $aggregate): void { |
|
| 146 | + function($reason, $idx, Promise $aggregate): void { |
|
| 147 | 147 | $aggregate->reject($reason); |
| 148 | 148 | } |
| 149 | - )->then(function () use (&$results) { |
|
| 149 | + )->then(function() use (&$results) { |
|
| 150 | 150 | ksort($results); |
| 151 | 151 | |
| 152 | 152 | return $results; |
| 153 | 153 | }); |
| 154 | 154 | |
| 155 | 155 | if (true === $recursive) { |
| 156 | - $promise = $promise->then(function ($results) use ($recursive, &$promises) { |
|
| 156 | + $promise = $promise->then(function($results) use ($recursive, &$promises) { |
|
| 157 | 157 | foreach ($promises as $promise) { |
| 158 | 158 | if (Is::pending($promise)) { |
| 159 | 159 | return self::all($promises, $recursive); |
@@ -188,7 +188,7 @@ discard block |
||
| 188 | 188 | |
| 189 | 189 | return Each::of( |
| 190 | 190 | $promises, |
| 191 | - function ($value, $idx, PromiseInterface $p) use (&$results, $count): void { |
|
| 191 | + function($value, $idx, PromiseInterface $p) use (&$results, $count): void { |
|
| 192 | 192 | if (Is::settled($p)) { |
| 193 | 193 | return; |
| 194 | 194 | } |
@@ -197,11 +197,11 @@ discard block |
||
| 197 | 197 | $p->resolve(null); |
| 198 | 198 | } |
| 199 | 199 | }, |
| 200 | - function ($reason) use (&$rejections): void { |
|
| 200 | + function($reason) use (&$rejections): void { |
|
| 201 | 201 | $rejections[] = $reason; |
| 202 | 202 | } |
| 203 | 203 | )->then( |
| 204 | - function () use (&$results, &$rejections, $count) { |
|
| 204 | + function() use (&$results, &$rejections, $count) { |
|
| 205 | 205 | if (count($results) !== $count) { |
| 206 | 206 | throw new AggregateException( |
| 207 | 207 | 'Not enough promises to fulfill count', |
@@ -223,7 +223,7 @@ discard block |
||
| 223 | 223 | */ |
| 224 | 224 | public static function any($promises): PromiseInterface |
| 225 | 225 | { |
| 226 | - return self::some(1, $promises)->then(function ($values) { |
|
| 226 | + return self::some(1, $promises)->then(function($values) { |
|
| 227 | 227 | return $values[0]; |
| 228 | 228 | }); |
| 229 | 229 | } |
@@ -244,13 +244,13 @@ discard block |
||
| 244 | 244 | |
| 245 | 245 | return Each::of( |
| 246 | 246 | $promises, |
| 247 | - function ($value, $idx) use (&$results): void { |
|
| 247 | + function($value, $idx) use (&$results): void { |
|
| 248 | 248 | $results[$idx] = ['state' => PromiseInterface::FULFILLED, 'value' => $value]; |
| 249 | 249 | }, |
| 250 | - function ($reason, $idx) use (&$results): void { |
|
| 250 | + function($reason, $idx) use (&$results): void { |
|
| 251 | 251 | $results[$idx] = ['state' => PromiseInterface::REJECTED, 'reason' => $reason]; |
| 252 | 252 | } |
| 253 | - )->then(function () use (&$results) { |
|
| 253 | + )->then(function() use (&$results) { |
|
| 254 | 254 | ksort($results); |
| 255 | 255 | |
| 256 | 256 | return $results; |
@@ -4,8 +4,7 @@ |
||
| 4 | 4 | |
| 5 | 5 | namespace OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\Promise; |
| 6 | 6 | |
| 7 | -final class Utils |
|
| 8 | -{ |
|
| 7 | +final class Utils { |
|
| 9 | 8 | /** |
| 10 | 9 | * Get the global task queue used for promise resolution. |
| 11 | 10 | * |
@@ -45,118 +45,118 @@ |
||
| 45 | 45 | */ |
| 46 | 46 | final class Coroutine implements PromiseInterface |
| 47 | 47 | { |
| 48 | - /** |
|
| 49 | - * @var PromiseInterface|null |
|
| 50 | - */ |
|
| 51 | - private $currentPromise; |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * @var Generator |
|
| 55 | - */ |
|
| 56 | - private $generator; |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * @var Promise |
|
| 60 | - */ |
|
| 61 | - private $result; |
|
| 62 | - |
|
| 63 | - public function __construct(callable $generatorFn) |
|
| 64 | - { |
|
| 65 | - $this->generator = $generatorFn(); |
|
| 66 | - $this->result = new Promise(function (): void { |
|
| 67 | - while (isset($this->currentPromise)) { |
|
| 68 | - $this->currentPromise->wait(); |
|
| 69 | - } |
|
| 70 | - }); |
|
| 71 | - try { |
|
| 72 | - $this->nextCoroutine($this->generator->current()); |
|
| 73 | - } catch (Throwable $throwable) { |
|
| 74 | - $this->result->reject($throwable); |
|
| 75 | - } |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - /** |
|
| 79 | - * Create a new coroutine. |
|
| 80 | - */ |
|
| 81 | - public static function of(callable $generatorFn): self |
|
| 82 | - { |
|
| 83 | - return new self($generatorFn); |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - public function then( |
|
| 87 | - callable $onFulfilled = null, |
|
| 88 | - callable $onRejected = null |
|
| 89 | - ): PromiseInterface { |
|
| 90 | - return $this->result->then($onFulfilled, $onRejected); |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - public function otherwise(callable $onRejected): PromiseInterface |
|
| 94 | - { |
|
| 95 | - return $this->result->otherwise($onRejected); |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - public function wait(bool $unwrap = true) |
|
| 99 | - { |
|
| 100 | - return $this->result->wait($unwrap); |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - public function getState(): string |
|
| 104 | - { |
|
| 105 | - return $this->result->getState(); |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - public function resolve($value): void |
|
| 109 | - { |
|
| 110 | - $this->result->resolve($value); |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - public function reject($reason): void |
|
| 114 | - { |
|
| 115 | - $this->result->reject($reason); |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - public function cancel(): void |
|
| 119 | - { |
|
| 120 | - $this->currentPromise->cancel(); |
|
| 121 | - $this->result->cancel(); |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - private function nextCoroutine($yielded): void |
|
| 125 | - { |
|
| 126 | - $this->currentPromise = Create::promiseFor($yielded) |
|
| 127 | - ->then([$this, '_handleSuccess'], [$this, '_handleFailure']); |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - /** |
|
| 131 | - * @internal |
|
| 132 | - */ |
|
| 133 | - public function _handleSuccess($value): void |
|
| 134 | - { |
|
| 135 | - unset($this->currentPromise); |
|
| 136 | - try { |
|
| 137 | - $next = $this->generator->send($value); |
|
| 138 | - if ($this->generator->valid()) { |
|
| 139 | - $this->nextCoroutine($next); |
|
| 140 | - } else { |
|
| 141 | - $this->result->resolve($value); |
|
| 142 | - } |
|
| 143 | - } catch (Throwable $throwable) { |
|
| 144 | - $this->result->reject($throwable); |
|
| 145 | - } |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - /** |
|
| 149 | - * @internal |
|
| 150 | - */ |
|
| 151 | - public function _handleFailure($reason): void |
|
| 152 | - { |
|
| 153 | - unset($this->currentPromise); |
|
| 154 | - try { |
|
| 155 | - $nextYield = $this->generator->throw(Create::exceptionFor($reason)); |
|
| 156 | - // The throw was caught, so keep iterating on the coroutine |
|
| 157 | - $this->nextCoroutine($nextYield); |
|
| 158 | - } catch (Throwable $throwable) { |
|
| 159 | - $this->result->reject($throwable); |
|
| 160 | - } |
|
| 161 | - } |
|
| 48 | + /** |
|
| 49 | + * @var PromiseInterface|null |
|
| 50 | + */ |
|
| 51 | + private $currentPromise; |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * @var Generator |
|
| 55 | + */ |
|
| 56 | + private $generator; |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * @var Promise |
|
| 60 | + */ |
|
| 61 | + private $result; |
|
| 62 | + |
|
| 63 | + public function __construct(callable $generatorFn) |
|
| 64 | + { |
|
| 65 | + $this->generator = $generatorFn(); |
|
| 66 | + $this->result = new Promise(function (): void { |
|
| 67 | + while (isset($this->currentPromise)) { |
|
| 68 | + $this->currentPromise->wait(); |
|
| 69 | + } |
|
| 70 | + }); |
|
| 71 | + try { |
|
| 72 | + $this->nextCoroutine($this->generator->current()); |
|
| 73 | + } catch (Throwable $throwable) { |
|
| 74 | + $this->result->reject($throwable); |
|
| 75 | + } |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + /** |
|
| 79 | + * Create a new coroutine. |
|
| 80 | + */ |
|
| 81 | + public static function of(callable $generatorFn): self |
|
| 82 | + { |
|
| 83 | + return new self($generatorFn); |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + public function then( |
|
| 87 | + callable $onFulfilled = null, |
|
| 88 | + callable $onRejected = null |
|
| 89 | + ): PromiseInterface { |
|
| 90 | + return $this->result->then($onFulfilled, $onRejected); |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + public function otherwise(callable $onRejected): PromiseInterface |
|
| 94 | + { |
|
| 95 | + return $this->result->otherwise($onRejected); |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + public function wait(bool $unwrap = true) |
|
| 99 | + { |
|
| 100 | + return $this->result->wait($unwrap); |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + public function getState(): string |
|
| 104 | + { |
|
| 105 | + return $this->result->getState(); |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + public function resolve($value): void |
|
| 109 | + { |
|
| 110 | + $this->result->resolve($value); |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + public function reject($reason): void |
|
| 114 | + { |
|
| 115 | + $this->result->reject($reason); |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + public function cancel(): void |
|
| 119 | + { |
|
| 120 | + $this->currentPromise->cancel(); |
|
| 121 | + $this->result->cancel(); |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + private function nextCoroutine($yielded): void |
|
| 125 | + { |
|
| 126 | + $this->currentPromise = Create::promiseFor($yielded) |
|
| 127 | + ->then([$this, '_handleSuccess'], [$this, '_handleFailure']); |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + /** |
|
| 131 | + * @internal |
|
| 132 | + */ |
|
| 133 | + public function _handleSuccess($value): void |
|
| 134 | + { |
|
| 135 | + unset($this->currentPromise); |
|
| 136 | + try { |
|
| 137 | + $next = $this->generator->send($value); |
|
| 138 | + if ($this->generator->valid()) { |
|
| 139 | + $this->nextCoroutine($next); |
|
| 140 | + } else { |
|
| 141 | + $this->result->resolve($value); |
|
| 142 | + } |
|
| 143 | + } catch (Throwable $throwable) { |
|
| 144 | + $this->result->reject($throwable); |
|
| 145 | + } |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + /** |
|
| 149 | + * @internal |
|
| 150 | + */ |
|
| 151 | + public function _handleFailure($reason): void |
|
| 152 | + { |
|
| 153 | + unset($this->currentPromise); |
|
| 154 | + try { |
|
| 155 | + $nextYield = $this->generator->throw(Create::exceptionFor($reason)); |
|
| 156 | + // The throw was caught, so keep iterating on the coroutine |
|
| 157 | + $this->nextCoroutine($nextYield); |
|
| 158 | + } catch (Throwable $throwable) { |
|
| 159 | + $this->result->reject($throwable); |
|
| 160 | + } |
|
| 161 | + } |
|
| 162 | 162 | } |
@@ -63,7 +63,7 @@ |
||
| 63 | 63 | public function __construct(callable $generatorFn) |
| 64 | 64 | { |
| 65 | 65 | $this->generator = $generatorFn(); |
| 66 | - $this->result = new Promise(function (): void { |
|
| 66 | + $this->result = new Promise(function(): void { |
|
| 67 | 67 | while (isset($this->currentPromise)) { |
| 68 | 68 | $this->currentPromise->wait(); |
| 69 | 69 | } |
@@ -43,8 +43,7 @@ |
||
| 43 | 43 | * |
| 44 | 44 | * @see https://github.com/petkaantonov/bluebird/blob/master/API.md#generators inspiration |
| 45 | 45 | */ |
| 46 | -final class Coroutine implements PromiseInterface |
|
| 47 | -{ |
|
| 46 | +final class Coroutine implements PromiseInterface { |
|
| 48 | 47 | /** |
| 49 | 48 | * @var PromiseInterface|null |
| 50 | 49 | */ |
@@ -11,39 +11,39 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | class RejectionException extends \RuntimeException |
| 13 | 13 | { |
| 14 | - /** @var mixed Rejection reason. */ |
|
| 15 | - private $reason; |
|
| 16 | - |
|
| 17 | - /** |
|
| 18 | - * @param mixed $reason Rejection reason. |
|
| 19 | - * @param string|null $description Optional description. |
|
| 20 | - */ |
|
| 21 | - public function __construct($reason, string $description = null) |
|
| 22 | - { |
|
| 23 | - $this->reason = $reason; |
|
| 24 | - |
|
| 25 | - $message = 'The promise was rejected'; |
|
| 26 | - |
|
| 27 | - if ($description) { |
|
| 28 | - $message .= ' with reason: '.$description; |
|
| 29 | - } elseif (is_string($reason) |
|
| 30 | - || (is_object($reason) && method_exists($reason, '__toString')) |
|
| 31 | - ) { |
|
| 32 | - $message .= ' with reason: '.$this->reason; |
|
| 33 | - } elseif ($reason instanceof \JsonSerializable) { |
|
| 34 | - $message .= ' with reason: '.json_encode($this->reason, JSON_PRETTY_PRINT); |
|
| 35 | - } |
|
| 36 | - |
|
| 37 | - parent::__construct($message); |
|
| 38 | - } |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * Returns the rejection reason. |
|
| 42 | - * |
|
| 43 | - * @return mixed |
|
| 44 | - */ |
|
| 45 | - public function getReason() |
|
| 46 | - { |
|
| 47 | - return $this->reason; |
|
| 48 | - } |
|
| 14 | + /** @var mixed Rejection reason. */ |
|
| 15 | + private $reason; |
|
| 16 | + |
|
| 17 | + /** |
|
| 18 | + * @param mixed $reason Rejection reason. |
|
| 19 | + * @param string|null $description Optional description. |
|
| 20 | + */ |
|
| 21 | + public function __construct($reason, string $description = null) |
|
| 22 | + { |
|
| 23 | + $this->reason = $reason; |
|
| 24 | + |
|
| 25 | + $message = 'The promise was rejected'; |
|
| 26 | + |
|
| 27 | + if ($description) { |
|
| 28 | + $message .= ' with reason: '.$description; |
|
| 29 | + } elseif (is_string($reason) |
|
| 30 | + || (is_object($reason) && method_exists($reason, '__toString')) |
|
| 31 | + ) { |
|
| 32 | + $message .= ' with reason: '.$this->reason; |
|
| 33 | + } elseif ($reason instanceof \JsonSerializable) { |
|
| 34 | + $message .= ' with reason: '.json_encode($this->reason, JSON_PRETTY_PRINT); |
|
| 35 | + } |
|
| 36 | + |
|
| 37 | + parent::__construct($message); |
|
| 38 | + } |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * Returns the rejection reason. |
|
| 42 | + * |
|
| 43 | + * @return mixed |
|
| 44 | + */ |
|
| 45 | + public function getReason() |
|
| 46 | + { |
|
| 47 | + return $this->reason; |
|
| 48 | + } |
|
| 49 | 49 | } |
@@ -6,76 +6,76 @@ |
||
| 6 | 6 | |
| 7 | 7 | final class Each |
| 8 | 8 | { |
| 9 | - /** |
|
| 10 | - * Given an iterator that yields promises or values, returns a promise that |
|
| 11 | - * is fulfilled with a null value when the iterator has been consumed or |
|
| 12 | - * the aggregate promise has been fulfilled or rejected. |
|
| 13 | - * |
|
| 14 | - * $onFulfilled is a function that accepts the fulfilled value, iterator |
|
| 15 | - * index, and the aggregate promise. The callback can invoke any necessary |
|
| 16 | - * side effects and choose to resolve or reject the aggregate if needed. |
|
| 17 | - * |
|
| 18 | - * $onRejected is a function that accepts the rejection reason, iterator |
|
| 19 | - * index, and the aggregate promise. The callback can invoke any necessary |
|
| 20 | - * side effects and choose to resolve or reject the aggregate if needed. |
|
| 21 | - * |
|
| 22 | - * @param mixed $iterable Iterator or array to iterate over. |
|
| 23 | - */ |
|
| 24 | - public static function of( |
|
| 25 | - $iterable, |
|
| 26 | - callable $onFulfilled = null, |
|
| 27 | - callable $onRejected = null |
|
| 28 | - ): PromiseInterface { |
|
| 29 | - return (new EachPromise($iterable, [ |
|
| 30 | - 'fulfilled' => $onFulfilled, |
|
| 31 | - 'rejected' => $onRejected, |
|
| 32 | - ]))->promise(); |
|
| 33 | - } |
|
| 9 | + /** |
|
| 10 | + * Given an iterator that yields promises or values, returns a promise that |
|
| 11 | + * is fulfilled with a null value when the iterator has been consumed or |
|
| 12 | + * the aggregate promise has been fulfilled or rejected. |
|
| 13 | + * |
|
| 14 | + * $onFulfilled is a function that accepts the fulfilled value, iterator |
|
| 15 | + * index, and the aggregate promise. The callback can invoke any necessary |
|
| 16 | + * side effects and choose to resolve or reject the aggregate if needed. |
|
| 17 | + * |
|
| 18 | + * $onRejected is a function that accepts the rejection reason, iterator |
|
| 19 | + * index, and the aggregate promise. The callback can invoke any necessary |
|
| 20 | + * side effects and choose to resolve or reject the aggregate if needed. |
|
| 21 | + * |
|
| 22 | + * @param mixed $iterable Iterator or array to iterate over. |
|
| 23 | + */ |
|
| 24 | + public static function of( |
|
| 25 | + $iterable, |
|
| 26 | + callable $onFulfilled = null, |
|
| 27 | + callable $onRejected = null |
|
| 28 | + ): PromiseInterface { |
|
| 29 | + return (new EachPromise($iterable, [ |
|
| 30 | + 'fulfilled' => $onFulfilled, |
|
| 31 | + 'rejected' => $onRejected, |
|
| 32 | + ]))->promise(); |
|
| 33 | + } |
|
| 34 | 34 | |
| 35 | - /** |
|
| 36 | - * Like of, but only allows a certain number of outstanding promises at any |
|
| 37 | - * given time. |
|
| 38 | - * |
|
| 39 | - * $concurrency may be an integer or a function that accepts the number of |
|
| 40 | - * pending promises and returns a numeric concurrency limit value to allow |
|
| 41 | - * for dynamic a concurrency size. |
|
| 42 | - * |
|
| 43 | - * @param mixed $iterable |
|
| 44 | - * @param int|callable $concurrency |
|
| 45 | - */ |
|
| 46 | - public static function ofLimit( |
|
| 47 | - $iterable, |
|
| 48 | - $concurrency, |
|
| 49 | - callable $onFulfilled = null, |
|
| 50 | - callable $onRejected = null |
|
| 51 | - ): PromiseInterface { |
|
| 52 | - return (new EachPromise($iterable, [ |
|
| 53 | - 'fulfilled' => $onFulfilled, |
|
| 54 | - 'rejected' => $onRejected, |
|
| 55 | - 'concurrency' => $concurrency, |
|
| 56 | - ]))->promise(); |
|
| 57 | - } |
|
| 35 | + /** |
|
| 36 | + * Like of, but only allows a certain number of outstanding promises at any |
|
| 37 | + * given time. |
|
| 38 | + * |
|
| 39 | + * $concurrency may be an integer or a function that accepts the number of |
|
| 40 | + * pending promises and returns a numeric concurrency limit value to allow |
|
| 41 | + * for dynamic a concurrency size. |
|
| 42 | + * |
|
| 43 | + * @param mixed $iterable |
|
| 44 | + * @param int|callable $concurrency |
|
| 45 | + */ |
|
| 46 | + public static function ofLimit( |
|
| 47 | + $iterable, |
|
| 48 | + $concurrency, |
|
| 49 | + callable $onFulfilled = null, |
|
| 50 | + callable $onRejected = null |
|
| 51 | + ): PromiseInterface { |
|
| 52 | + return (new EachPromise($iterable, [ |
|
| 53 | + 'fulfilled' => $onFulfilled, |
|
| 54 | + 'rejected' => $onRejected, |
|
| 55 | + 'concurrency' => $concurrency, |
|
| 56 | + ]))->promise(); |
|
| 57 | + } |
|
| 58 | 58 | |
| 59 | - /** |
|
| 60 | - * Like limit, but ensures that no promise in the given $iterable argument |
|
| 61 | - * is rejected. If any promise is rejected, then the aggregate promise is |
|
| 62 | - * rejected with the encountered rejection. |
|
| 63 | - * |
|
| 64 | - * @param mixed $iterable |
|
| 65 | - * @param int|callable $concurrency |
|
| 66 | - */ |
|
| 67 | - public static function ofLimitAll( |
|
| 68 | - $iterable, |
|
| 69 | - $concurrency, |
|
| 70 | - callable $onFulfilled = null |
|
| 71 | - ): PromiseInterface { |
|
| 72 | - return self::ofLimit( |
|
| 73 | - $iterable, |
|
| 74 | - $concurrency, |
|
| 75 | - $onFulfilled, |
|
| 76 | - function ($reason, $idx, PromiseInterface $aggregate): void { |
|
| 77 | - $aggregate->reject($reason); |
|
| 78 | - } |
|
| 79 | - ); |
|
| 80 | - } |
|
| 59 | + /** |
|
| 60 | + * Like limit, but ensures that no promise in the given $iterable argument |
|
| 61 | + * is rejected. If any promise is rejected, then the aggregate promise is |
|
| 62 | + * rejected with the encountered rejection. |
|
| 63 | + * |
|
| 64 | + * @param mixed $iterable |
|
| 65 | + * @param int|callable $concurrency |
|
| 66 | + */ |
|
| 67 | + public static function ofLimitAll( |
|
| 68 | + $iterable, |
|
| 69 | + $concurrency, |
|
| 70 | + callable $onFulfilled = null |
|
| 71 | + ): PromiseInterface { |
|
| 72 | + return self::ofLimit( |
|
| 73 | + $iterable, |
|
| 74 | + $concurrency, |
|
| 75 | + $onFulfilled, |
|
| 76 | + function ($reason, $idx, PromiseInterface $aggregate): void { |
|
| 77 | + $aggregate->reject($reason); |
|
| 78 | + } |
|
| 79 | + ); |
|
| 80 | + } |
|
| 81 | 81 | } |
@@ -73,7 +73,7 @@ |
||
| 73 | 73 | $iterable, |
| 74 | 74 | $concurrency, |
| 75 | 75 | $onFulfilled, |
| 76 | - function ($reason, $idx, PromiseInterface $aggregate): void { |
|
| 76 | + function($reason, $idx, PromiseInterface $aggregate): void { |
|
| 77 | 77 | $aggregate->reject($reason); |
| 78 | 78 | } |
| 79 | 79 | ); |
@@ -4,8 +4,7 @@ |
||
| 4 | 4 | |
| 5 | 5 | namespace OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\Promise; |
| 6 | 6 | |
| 7 | -final class Each |
|
| 8 | -{ |
|
| 7 | +final class Each { |
|
| 9 | 8 | /** |
| 10 | 9 | * Given an iterator that yields promises or values, returns a promise that |
| 11 | 10 | * is fulfilled with a null value when the iterator has been consumed or |
@@ -17,55 +17,55 @@ |
||
| 17 | 17 | */ |
| 18 | 18 | class TaskQueue implements TaskQueueInterface |
| 19 | 19 | { |
| 20 | - private $enableShutdown = true; |
|
| 21 | - private $queue = []; |
|
| 20 | + private $enableShutdown = true; |
|
| 21 | + private $queue = []; |
|
| 22 | 22 | |
| 23 | - public function __construct(bool $withShutdown = true) |
|
| 24 | - { |
|
| 25 | - if ($withShutdown) { |
|
| 26 | - register_shutdown_function(function (): void { |
|
| 27 | - if ($this->enableShutdown) { |
|
| 28 | - // Only run the tasks if an E_ERROR didn't occur. |
|
| 29 | - $err = error_get_last(); |
|
| 30 | - if (!$err || ($err['type'] ^ E_ERROR)) { |
|
| 31 | - $this->run(); |
|
| 32 | - } |
|
| 33 | - } |
|
| 34 | - }); |
|
| 35 | - } |
|
| 36 | - } |
|
| 23 | + public function __construct(bool $withShutdown = true) |
|
| 24 | + { |
|
| 25 | + if ($withShutdown) { |
|
| 26 | + register_shutdown_function(function (): void { |
|
| 27 | + if ($this->enableShutdown) { |
|
| 28 | + // Only run the tasks if an E_ERROR didn't occur. |
|
| 29 | + $err = error_get_last(); |
|
| 30 | + if (!$err || ($err['type'] ^ E_ERROR)) { |
|
| 31 | + $this->run(); |
|
| 32 | + } |
|
| 33 | + } |
|
| 34 | + }); |
|
| 35 | + } |
|
| 36 | + } |
|
| 37 | 37 | |
| 38 | - public function isEmpty(): bool |
|
| 39 | - { |
|
| 40 | - return !$this->queue; |
|
| 41 | - } |
|
| 38 | + public function isEmpty(): bool |
|
| 39 | + { |
|
| 40 | + return !$this->queue; |
|
| 41 | + } |
|
| 42 | 42 | |
| 43 | - public function add(callable $task): void |
|
| 44 | - { |
|
| 45 | - $this->queue[] = $task; |
|
| 46 | - } |
|
| 43 | + public function add(callable $task): void |
|
| 44 | + { |
|
| 45 | + $this->queue[] = $task; |
|
| 46 | + } |
|
| 47 | 47 | |
| 48 | - public function run(): void |
|
| 49 | - { |
|
| 50 | - while ($task = array_shift($this->queue)) { |
|
| 51 | - /** @var callable $task */ |
|
| 52 | - $task(); |
|
| 53 | - } |
|
| 54 | - } |
|
| 48 | + public function run(): void |
|
| 49 | + { |
|
| 50 | + while ($task = array_shift($this->queue)) { |
|
| 51 | + /** @var callable $task */ |
|
| 52 | + $task(); |
|
| 53 | + } |
|
| 54 | + } |
|
| 55 | 55 | |
| 56 | - /** |
|
| 57 | - * The task queue will be run and exhausted by default when the process |
|
| 58 | - * exits IFF the exit is not the result of a PHP E_ERROR error. |
|
| 59 | - * |
|
| 60 | - * You can disable running the automatic shutdown of the queue by calling |
|
| 61 | - * this function. If you disable the task queue shutdown process, then you |
|
| 62 | - * MUST either run the task queue (as a result of running your event loop |
|
| 63 | - * or manually using the run() method) or wait on each outstanding promise. |
|
| 64 | - * |
|
| 65 | - * Note: This shutdown will occur before any destructors are triggered. |
|
| 66 | - */ |
|
| 67 | - public function disableShutdown(): void |
|
| 68 | - { |
|
| 69 | - $this->enableShutdown = false; |
|
| 70 | - } |
|
| 56 | + /** |
|
| 57 | + * The task queue will be run and exhausted by default when the process |
|
| 58 | + * exits IFF the exit is not the result of a PHP E_ERROR error. |
|
| 59 | + * |
|
| 60 | + * You can disable running the automatic shutdown of the queue by calling |
|
| 61 | + * this function. If you disable the task queue shutdown process, then you |
|
| 62 | + * MUST either run the task queue (as a result of running your event loop |
|
| 63 | + * or manually using the run() method) or wait on each outstanding promise. |
|
| 64 | + * |
|
| 65 | + * Note: This shutdown will occur before any destructors are triggered. |
|
| 66 | + */ |
|
| 67 | + public function disableShutdown(): void |
|
| 68 | + { |
|
| 69 | + $this->enableShutdown = false; |
|
| 70 | + } |
|
| 71 | 71 | } |
@@ -23,7 +23,7 @@ |
||
| 23 | 23 | public function __construct(bool $withShutdown = true) |
| 24 | 24 | { |
| 25 | 25 | if ($withShutdown) { |
| 26 | - register_shutdown_function(function (): void { |
|
| 26 | + register_shutdown_function(function(): void { |
|
| 27 | 27 | if ($this->enableShutdown) { |
| 28 | 28 | // Only run the tasks if an E_ERROR didn't occur. |
| 29 | 29 | $err = error_get_last(); |
@@ -15,8 +15,7 @@ |
||
| 15 | 15 | * |
| 16 | 16 | * @final |
| 17 | 17 | */ |
| 18 | -class TaskQueue implements TaskQueueInterface |
|
| 19 | -{ |
|
| 18 | +class TaskQueue implements TaskQueueInterface { |
|
| 20 | 19 | private $enableShutdown = true; |
| 21 | 20 | private $queue = []; |
| 22 | 21 | |
@@ -6,19 +6,19 @@ |
||
| 6 | 6 | |
| 7 | 7 | interface TaskQueueInterface |
| 8 | 8 | { |
| 9 | - /** |
|
| 10 | - * Returns true if the queue is empty. |
|
| 11 | - */ |
|
| 12 | - public function isEmpty(): bool; |
|
| 9 | + /** |
|
| 10 | + * Returns true if the queue is empty. |
|
| 11 | + */ |
|
| 12 | + public function isEmpty(): bool; |
|
| 13 | 13 | |
| 14 | - /** |
|
| 15 | - * Adds a task to the queue that will be executed the next time run is |
|
| 16 | - * called. |
|
| 17 | - */ |
|
| 18 | - public function add(callable $task): void; |
|
| 14 | + /** |
|
| 15 | + * Adds a task to the queue that will be executed the next time run is |
|
| 16 | + * called. |
|
| 17 | + */ |
|
| 18 | + public function add(callable $task): void; |
|
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * Execute all of the pending task in the queue. |
|
| 22 | - */ |
|
| 23 | - public function run(): void; |
|
| 20 | + /** |
|
| 21 | + * Execute all of the pending task in the queue. |
|
| 22 | + */ |
|
| 23 | + public function run(): void; |
|
| 24 | 24 | } |
@@ -4,8 +4,7 @@ |
||
| 4 | 4 | |
| 5 | 5 | namespace OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\Promise; |
| 6 | 6 | |
| 7 | -interface TaskQueueInterface |
|
| 8 | -{ |
|
| 7 | +interface TaskQueueInterface { |
|
| 9 | 8 | /** |
| 10 | 9 | * Returns true if the queue is empty. |
| 11 | 10 | */ |
@@ -14,76 +14,76 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | class FulfilledPromise implements PromiseInterface |
| 16 | 16 | { |
| 17 | - private $value; |
|
| 17 | + private $value; |
|
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * @param mixed $value |
|
| 21 | - */ |
|
| 22 | - public function __construct($value) |
|
| 23 | - { |
|
| 24 | - if (is_object($value) && method_exists($value, 'then')) { |
|
| 25 | - throw new \InvalidArgumentException( |
|
| 26 | - 'You cannot create a FulfilledPromise with a promise.' |
|
| 27 | - ); |
|
| 28 | - } |
|
| 19 | + /** |
|
| 20 | + * @param mixed $value |
|
| 21 | + */ |
|
| 22 | + public function __construct($value) |
|
| 23 | + { |
|
| 24 | + if (is_object($value) && method_exists($value, 'then')) { |
|
| 25 | + throw new \InvalidArgumentException( |
|
| 26 | + 'You cannot create a FulfilledPromise with a promise.' |
|
| 27 | + ); |
|
| 28 | + } |
|
| 29 | 29 | |
| 30 | - $this->value = $value; |
|
| 31 | - } |
|
| 30 | + $this->value = $value; |
|
| 31 | + } |
|
| 32 | 32 | |
| 33 | - public function then( |
|
| 34 | - callable $onFulfilled = null, |
|
| 35 | - callable $onRejected = null |
|
| 36 | - ): PromiseInterface { |
|
| 37 | - // Return itself if there is no onFulfilled function. |
|
| 38 | - if (!$onFulfilled) { |
|
| 39 | - return $this; |
|
| 40 | - } |
|
| 33 | + public function then( |
|
| 34 | + callable $onFulfilled = null, |
|
| 35 | + callable $onRejected = null |
|
| 36 | + ): PromiseInterface { |
|
| 37 | + // Return itself if there is no onFulfilled function. |
|
| 38 | + if (!$onFulfilled) { |
|
| 39 | + return $this; |
|
| 40 | + } |
|
| 41 | 41 | |
| 42 | - $queue = Utils::queue(); |
|
| 43 | - $p = new Promise([$queue, 'run']); |
|
| 44 | - $value = $this->value; |
|
| 45 | - $queue->add(static function () use ($p, $value, $onFulfilled): void { |
|
| 46 | - if (Is::pending($p)) { |
|
| 47 | - try { |
|
| 48 | - $p->resolve($onFulfilled($value)); |
|
| 49 | - } catch (\Throwable $e) { |
|
| 50 | - $p->reject($e); |
|
| 51 | - } |
|
| 52 | - } |
|
| 53 | - }); |
|
| 42 | + $queue = Utils::queue(); |
|
| 43 | + $p = new Promise([$queue, 'run']); |
|
| 44 | + $value = $this->value; |
|
| 45 | + $queue->add(static function () use ($p, $value, $onFulfilled): void { |
|
| 46 | + if (Is::pending($p)) { |
|
| 47 | + try { |
|
| 48 | + $p->resolve($onFulfilled($value)); |
|
| 49 | + } catch (\Throwable $e) { |
|
| 50 | + $p->reject($e); |
|
| 51 | + } |
|
| 52 | + } |
|
| 53 | + }); |
|
| 54 | 54 | |
| 55 | - return $p; |
|
| 56 | - } |
|
| 55 | + return $p; |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | - public function otherwise(callable $onRejected): PromiseInterface |
|
| 59 | - { |
|
| 60 | - return $this->then(null, $onRejected); |
|
| 61 | - } |
|
| 58 | + public function otherwise(callable $onRejected): PromiseInterface |
|
| 59 | + { |
|
| 60 | + return $this->then(null, $onRejected); |
|
| 61 | + } |
|
| 62 | 62 | |
| 63 | - public function wait(bool $unwrap = true) |
|
| 64 | - { |
|
| 65 | - return $unwrap ? $this->value : null; |
|
| 66 | - } |
|
| 63 | + public function wait(bool $unwrap = true) |
|
| 64 | + { |
|
| 65 | + return $unwrap ? $this->value : null; |
|
| 66 | + } |
|
| 67 | 67 | |
| 68 | - public function getState(): string |
|
| 69 | - { |
|
| 70 | - return self::FULFILLED; |
|
| 71 | - } |
|
| 68 | + public function getState(): string |
|
| 69 | + { |
|
| 70 | + return self::FULFILLED; |
|
| 71 | + } |
|
| 72 | 72 | |
| 73 | - public function resolve($value): void |
|
| 74 | - { |
|
| 75 | - if ($value !== $this->value) { |
|
| 76 | - throw new \LogicException('Cannot resolve a fulfilled promise'); |
|
| 77 | - } |
|
| 78 | - } |
|
| 73 | + public function resolve($value): void |
|
| 74 | + { |
|
| 75 | + if ($value !== $this->value) { |
|
| 76 | + throw new \LogicException('Cannot resolve a fulfilled promise'); |
|
| 77 | + } |
|
| 78 | + } |
|
| 79 | 79 | |
| 80 | - public function reject($reason): void |
|
| 81 | - { |
|
| 82 | - throw new \LogicException('Cannot reject a fulfilled promise'); |
|
| 83 | - } |
|
| 80 | + public function reject($reason): void |
|
| 81 | + { |
|
| 82 | + throw new \LogicException('Cannot reject a fulfilled promise'); |
|
| 83 | + } |
|
| 84 | 84 | |
| 85 | - public function cancel(): void |
|
| 86 | - { |
|
| 87 | - // pass |
|
| 88 | - } |
|
| 85 | + public function cancel(): void |
|
| 86 | + { |
|
| 87 | + // pass |
|
| 88 | + } |
|
| 89 | 89 | } |
@@ -42,7 +42,7 @@ |
||
| 42 | 42 | $queue = Utils::queue(); |
| 43 | 43 | $p = new Promise([$queue, 'run']); |
| 44 | 44 | $value = $this->value; |
| 45 | - $queue->add(static function () use ($p, $value, $onFulfilled): void { |
|
| 45 | + $queue->add(static function() use ($p, $value, $onFulfilled): void { |
|
| 46 | 46 | if (Is::pending($p)) { |
| 47 | 47 | try { |
| 48 | 48 | $p->resolve($onFulfilled($value)); |
@@ -12,8 +12,7 @@ |
||
| 12 | 12 | * |
| 13 | 13 | * @final |
| 14 | 14 | */ |
| 15 | -class FulfilledPromise implements PromiseInterface |
|
| 16 | -{ |
|
| 15 | +class FulfilledPromise implements PromiseInterface { |
|
| 17 | 16 | private $value; |
| 18 | 17 | |
| 19 | 18 | /** |
@@ -6,74 +6,74 @@ |
||
| 6 | 6 | |
| 7 | 7 | final class Create |
| 8 | 8 | { |
| 9 | - /** |
|
| 10 | - * Creates a promise for a value if the value is not a promise. |
|
| 11 | - * |
|
| 12 | - * @param mixed $value Promise or value. |
|
| 13 | - */ |
|
| 14 | - public static function promiseFor($value): PromiseInterface |
|
| 15 | - { |
|
| 16 | - if ($value instanceof PromiseInterface) { |
|
| 17 | - return $value; |
|
| 18 | - } |
|
| 9 | + /** |
|
| 10 | + * Creates a promise for a value if the value is not a promise. |
|
| 11 | + * |
|
| 12 | + * @param mixed $value Promise or value. |
|
| 13 | + */ |
|
| 14 | + public static function promiseFor($value): PromiseInterface |
|
| 15 | + { |
|
| 16 | + if ($value instanceof PromiseInterface) { |
|
| 17 | + return $value; |
|
| 18 | + } |
|
| 19 | 19 | |
| 20 | - // Return a Guzzle promise that shadows the given promise. |
|
| 21 | - if (is_object($value) && method_exists($value, 'then')) { |
|
| 22 | - $wfn = method_exists($value, 'wait') ? [$value, 'wait'] : null; |
|
| 23 | - $cfn = method_exists($value, 'cancel') ? [$value, 'cancel'] : null; |
|
| 24 | - $promise = new Promise($wfn, $cfn); |
|
| 25 | - $value->then([$promise, 'resolve'], [$promise, 'reject']); |
|
| 20 | + // Return a Guzzle promise that shadows the given promise. |
|
| 21 | + if (is_object($value) && method_exists($value, 'then')) { |
|
| 22 | + $wfn = method_exists($value, 'wait') ? [$value, 'wait'] : null; |
|
| 23 | + $cfn = method_exists($value, 'cancel') ? [$value, 'cancel'] : null; |
|
| 24 | + $promise = new Promise($wfn, $cfn); |
|
| 25 | + $value->then([$promise, 'resolve'], [$promise, 'reject']); |
|
| 26 | 26 | |
| 27 | - return $promise; |
|
| 28 | - } |
|
| 27 | + return $promise; |
|
| 28 | + } |
|
| 29 | 29 | |
| 30 | - return new FulfilledPromise($value); |
|
| 31 | - } |
|
| 30 | + return new FulfilledPromise($value); |
|
| 31 | + } |
|
| 32 | 32 | |
| 33 | - /** |
|
| 34 | - * Creates a rejected promise for a reason if the reason is not a promise. |
|
| 35 | - * If the provided reason is a promise, then it is returned as-is. |
|
| 36 | - * |
|
| 37 | - * @param mixed $reason Promise or reason. |
|
| 38 | - */ |
|
| 39 | - public static function rejectionFor($reason): PromiseInterface |
|
| 40 | - { |
|
| 41 | - if ($reason instanceof PromiseInterface) { |
|
| 42 | - return $reason; |
|
| 43 | - } |
|
| 33 | + /** |
|
| 34 | + * Creates a rejected promise for a reason if the reason is not a promise. |
|
| 35 | + * If the provided reason is a promise, then it is returned as-is. |
|
| 36 | + * |
|
| 37 | + * @param mixed $reason Promise or reason. |
|
| 38 | + */ |
|
| 39 | + public static function rejectionFor($reason): PromiseInterface |
|
| 40 | + { |
|
| 41 | + if ($reason instanceof PromiseInterface) { |
|
| 42 | + return $reason; |
|
| 43 | + } |
|
| 44 | 44 | |
| 45 | - return new RejectedPromise($reason); |
|
| 46 | - } |
|
| 45 | + return new RejectedPromise($reason); |
|
| 46 | + } |
|
| 47 | 47 | |
| 48 | - /** |
|
| 49 | - * Create an exception for a rejected promise value. |
|
| 50 | - * |
|
| 51 | - * @param mixed $reason |
|
| 52 | - */ |
|
| 53 | - public static function exceptionFor($reason): \Throwable |
|
| 54 | - { |
|
| 55 | - if ($reason instanceof \Throwable) { |
|
| 56 | - return $reason; |
|
| 57 | - } |
|
| 48 | + /** |
|
| 49 | + * Create an exception for a rejected promise value. |
|
| 50 | + * |
|
| 51 | + * @param mixed $reason |
|
| 52 | + */ |
|
| 53 | + public static function exceptionFor($reason): \Throwable |
|
| 54 | + { |
|
| 55 | + if ($reason instanceof \Throwable) { |
|
| 56 | + return $reason; |
|
| 57 | + } |
|
| 58 | 58 | |
| 59 | - return new RejectionException($reason); |
|
| 60 | - } |
|
| 59 | + return new RejectionException($reason); |
|
| 60 | + } |
|
| 61 | 61 | |
| 62 | - /** |
|
| 63 | - * Returns an iterator for the given value. |
|
| 64 | - * |
|
| 65 | - * @param mixed $value |
|
| 66 | - */ |
|
| 67 | - public static function iterFor($value): \Iterator |
|
| 68 | - { |
|
| 69 | - if ($value instanceof \Iterator) { |
|
| 70 | - return $value; |
|
| 71 | - } |
|
| 62 | + /** |
|
| 63 | + * Returns an iterator for the given value. |
|
| 64 | + * |
|
| 65 | + * @param mixed $value |
|
| 66 | + */ |
|
| 67 | + public static function iterFor($value): \Iterator |
|
| 68 | + { |
|
| 69 | + if ($value instanceof \Iterator) { |
|
| 70 | + return $value; |
|
| 71 | + } |
|
| 72 | 72 | |
| 73 | - if (is_array($value)) { |
|
| 74 | - return new \ArrayIterator($value); |
|
| 75 | - } |
|
| 73 | + if (is_array($value)) { |
|
| 74 | + return new \ArrayIterator($value); |
|
| 75 | + } |
|
| 76 | 76 | |
| 77 | - return new \ArrayIterator([$value]); |
|
| 78 | - } |
|
| 77 | + return new \ArrayIterator([$value]); |
|
| 78 | + } |
|
| 79 | 79 | } |
@@ -4,8 +4,7 @@ |
||
| 4 | 4 | |
| 5 | 5 | namespace OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\Promise; |
| 6 | 6 | |
| 7 | -final class Create |
|
| 8 | -{ |
|
| 7 | +final class Create { |
|
| 9 | 8 | /** |
| 10 | 9 | * Creates a promise for a value if the value is not a promise. |
| 11 | 10 | * |