@@ -15,105 +15,105 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | class RetryMiddleware |
| 17 | 17 | { |
| 18 | - /** |
|
| 19 | - * @var callable(RequestInterface, array): PromiseInterface |
|
| 20 | - */ |
|
| 21 | - private $nextHandler; |
|
| 22 | - |
|
| 23 | - /** |
|
| 24 | - * @var callable |
|
| 25 | - */ |
|
| 26 | - private $decider; |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * @var callable(int) |
|
| 30 | - */ |
|
| 31 | - private $delay; |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * @param callable $decider Function that accepts the number of retries, |
|
| 35 | - * a request, [response], and [exception] and |
|
| 36 | - * returns true if the request is to be |
|
| 37 | - * retried. |
|
| 38 | - * @param callable(RequestInterface, array): PromiseInterface $nextHandler Next handler to invoke. |
|
| 39 | - * @param (callable(int): int)|null $delay Function that accepts the number of retries |
|
| 40 | - * and returns the number of |
|
| 41 | - * milliseconds to delay. |
|
| 42 | - */ |
|
| 43 | - public function __construct(callable $decider, callable $nextHandler, callable $delay = null) |
|
| 44 | - { |
|
| 45 | - $this->decider = $decider; |
|
| 46 | - $this->nextHandler = $nextHandler; |
|
| 47 | - $this->delay = $delay ?: __CLASS__.'::exponentialDelay'; |
|
| 48 | - } |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * Default exponential backoff delay function. |
|
| 52 | - * |
|
| 53 | - * @return int milliseconds. |
|
| 54 | - */ |
|
| 55 | - public static function exponentialDelay(int $retries): int |
|
| 56 | - { |
|
| 57 | - return (int) 2 ** ($retries - 1) * 1000; |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - public function __invoke(RequestInterface $request, array $options): PromiseInterface |
|
| 61 | - { |
|
| 62 | - if (!isset($options['retries'])) { |
|
| 63 | - $options['retries'] = 0; |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - $fn = $this->nextHandler; |
|
| 67 | - |
|
| 68 | - return $fn($request, $options) |
|
| 69 | - ->then( |
|
| 70 | - $this->onFulfilled($request, $options), |
|
| 71 | - $this->onRejected($request, $options) |
|
| 72 | - ); |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * Execute fulfilled closure |
|
| 77 | - */ |
|
| 78 | - private function onFulfilled(RequestInterface $request, array $options): callable |
|
| 79 | - { |
|
| 80 | - return function ($value) use ($request, $options) { |
|
| 81 | - if (!($this->decider)( |
|
| 82 | - $options['retries'], |
|
| 83 | - $request, |
|
| 84 | - $value, |
|
| 85 | - null |
|
| 86 | - )) { |
|
| 87 | - return $value; |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - return $this->doRetry($request, $options, $value); |
|
| 91 | - }; |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - /** |
|
| 95 | - * Execute rejected closure |
|
| 96 | - */ |
|
| 97 | - private function onRejected(RequestInterface $req, array $options): callable |
|
| 98 | - { |
|
| 99 | - return function ($reason) use ($req, $options) { |
|
| 100 | - if (!($this->decider)( |
|
| 101 | - $options['retries'], |
|
| 102 | - $req, |
|
| 103 | - null, |
|
| 104 | - $reason |
|
| 105 | - )) { |
|
| 106 | - return P\Create::rejectionFor($reason); |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - return $this->doRetry($req, $options); |
|
| 110 | - }; |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - private function doRetry(RequestInterface $request, array $options, ResponseInterface $response = null): PromiseInterface |
|
| 114 | - { |
|
| 115 | - $options['delay'] = ($this->delay)(++$options['retries'], $response, $request); |
|
| 116 | - |
|
| 117 | - return $this($request, $options); |
|
| 118 | - } |
|
| 18 | + /** |
|
| 19 | + * @var callable(RequestInterface, array): PromiseInterface |
|
| 20 | + */ |
|
| 21 | + private $nextHandler; |
|
| 22 | + |
|
| 23 | + /** |
|
| 24 | + * @var callable |
|
| 25 | + */ |
|
| 26 | + private $decider; |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * @var callable(int) |
|
| 30 | + */ |
|
| 31 | + private $delay; |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * @param callable $decider Function that accepts the number of retries, |
|
| 35 | + * a request, [response], and [exception] and |
|
| 36 | + * returns true if the request is to be |
|
| 37 | + * retried. |
|
| 38 | + * @param callable(RequestInterface, array): PromiseInterface $nextHandler Next handler to invoke. |
|
| 39 | + * @param (callable(int): int)|null $delay Function that accepts the number of retries |
|
| 40 | + * and returns the number of |
|
| 41 | + * milliseconds to delay. |
|
| 42 | + */ |
|
| 43 | + public function __construct(callable $decider, callable $nextHandler, callable $delay = null) |
|
| 44 | + { |
|
| 45 | + $this->decider = $decider; |
|
| 46 | + $this->nextHandler = $nextHandler; |
|
| 47 | + $this->delay = $delay ?: __CLASS__.'::exponentialDelay'; |
|
| 48 | + } |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * Default exponential backoff delay function. |
|
| 52 | + * |
|
| 53 | + * @return int milliseconds. |
|
| 54 | + */ |
|
| 55 | + public static function exponentialDelay(int $retries): int |
|
| 56 | + { |
|
| 57 | + return (int) 2 ** ($retries - 1) * 1000; |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + public function __invoke(RequestInterface $request, array $options): PromiseInterface |
|
| 61 | + { |
|
| 62 | + if (!isset($options['retries'])) { |
|
| 63 | + $options['retries'] = 0; |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + $fn = $this->nextHandler; |
|
| 67 | + |
|
| 68 | + return $fn($request, $options) |
|
| 69 | + ->then( |
|
| 70 | + $this->onFulfilled($request, $options), |
|
| 71 | + $this->onRejected($request, $options) |
|
| 72 | + ); |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * Execute fulfilled closure |
|
| 77 | + */ |
|
| 78 | + private function onFulfilled(RequestInterface $request, array $options): callable |
|
| 79 | + { |
|
| 80 | + return function ($value) use ($request, $options) { |
|
| 81 | + if (!($this->decider)( |
|
| 82 | + $options['retries'], |
|
| 83 | + $request, |
|
| 84 | + $value, |
|
| 85 | + null |
|
| 86 | + )) { |
|
| 87 | + return $value; |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + return $this->doRetry($request, $options, $value); |
|
| 91 | + }; |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + /** |
|
| 95 | + * Execute rejected closure |
|
| 96 | + */ |
|
| 97 | + private function onRejected(RequestInterface $req, array $options): callable |
|
| 98 | + { |
|
| 99 | + return function ($reason) use ($req, $options) { |
|
| 100 | + if (!($this->decider)( |
|
| 101 | + $options['retries'], |
|
| 102 | + $req, |
|
| 103 | + null, |
|
| 104 | + $reason |
|
| 105 | + )) { |
|
| 106 | + return P\Create::rejectionFor($reason); |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + return $this->doRetry($req, $options); |
|
| 110 | + }; |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + private function doRetry(RequestInterface $request, array $options, ResponseInterface $response = null): PromiseInterface |
|
| 114 | + { |
|
| 115 | + $options['delay'] = ($this->delay)(++$options['retries'], $response, $request); |
|
| 116 | + |
|
| 117 | + return $this($request, $options); |
|
| 118 | + } |
|
| 119 | 119 | } |
@@ -54,7 +54,7 @@ discard block |
||
| 54 | 54 | */ |
| 55 | 55 | public static function exponentialDelay(int $retries): int |
| 56 | 56 | { |
| 57 | - return (int) 2 ** ($retries - 1) * 1000; |
|
| 57 | + return (int)2 ** ($retries - 1) * 1000; |
|
| 58 | 58 | } |
| 59 | 59 | |
| 60 | 60 | public function __invoke(RequestInterface $request, array $options): PromiseInterface |
@@ -77,7 +77,7 @@ discard block |
||
| 77 | 77 | */ |
| 78 | 78 | private function onFulfilled(RequestInterface $request, array $options): callable |
| 79 | 79 | { |
| 80 | - return function ($value) use ($request, $options) { |
|
| 80 | + return function($value) use ($request, $options) { |
|
| 81 | 81 | if (!($this->decider)( |
| 82 | 82 | $options['retries'], |
| 83 | 83 | $request, |
@@ -96,7 +96,7 @@ discard block |
||
| 96 | 96 | */ |
| 97 | 97 | private function onRejected(RequestInterface $req, array $options): callable |
| 98 | 98 | { |
| 99 | - return function ($reason) use ($req, $options) { |
|
| 99 | + return function($reason) use ($req, $options) { |
|
| 100 | 100 | if (!($this->decider)( |
| 101 | 101 | $options['retries'], |
| 102 | 102 | $req, |
@@ -13,8 +13,7 @@ |
||
| 13 | 13 | * |
| 14 | 14 | * @final |
| 15 | 15 | */ |
| 16 | -class RetryMiddleware |
|
| 17 | -{ |
|
| 16 | +class RetryMiddleware { |
|
| 18 | 17 | /** |
| 19 | 18 | * @var callable(RequestInterface, array): PromiseInterface |
| 20 | 19 | */ |
@@ -7,12 +7,12 @@ |
||
| 7 | 7 | |
| 8 | 8 | interface MessageFormatterInterface |
| 9 | 9 | { |
| 10 | - /** |
|
| 11 | - * Returns a formatted message string. |
|
| 12 | - * |
|
| 13 | - * @param RequestInterface $request Request that was sent |
|
| 14 | - * @param ResponseInterface|null $response Response that was received |
|
| 15 | - * @param \Throwable|null $error Exception that was received |
|
| 16 | - */ |
|
| 17 | - public function format(RequestInterface $request, ResponseInterface $response = null, \Throwable $error = null): string; |
|
| 10 | + /** |
|
| 11 | + * Returns a formatted message string. |
|
| 12 | + * |
|
| 13 | + * @param RequestInterface $request Request that was sent |
|
| 14 | + * @param ResponseInterface|null $response Response that was received |
|
| 15 | + * @param \Throwable|null $error Exception that was received |
|
| 16 | + */ |
|
| 17 | + public function format(RequestInterface $request, ResponseInterface $response = null, \Throwable $error = null): string; |
|
| 18 | 18 | } |
@@ -5,8 +5,7 @@ |
||
| 5 | 5 | use OCA\FullTextSearch_Elasticsearch\Vendor\Psr\Http\Message\RequestInterface; |
| 6 | 6 | use OCA\FullTextSearch_Elasticsearch\Vendor\Psr\Http\Message\ResponseInterface; |
| 7 | 7 | |
| 8 | -interface MessageFormatterInterface |
|
| 9 | -{ |
|
| 8 | +interface MessageFormatterInterface { |
|
| 10 | 9 | /** |
| 11 | 10 | * Returns a formatted message string. |
| 12 | 11 | * |
@@ -23,103 +23,103 @@ |
||
| 23 | 23 | */ |
| 24 | 24 | class Pool implements PromisorInterface |
| 25 | 25 | { |
| 26 | - /** |
|
| 27 | - * @var EachPromise |
|
| 28 | - */ |
|
| 29 | - private $each; |
|
| 26 | + /** |
|
| 27 | + * @var EachPromise |
|
| 28 | + */ |
|
| 29 | + private $each; |
|
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * @param ClientInterface $client Client used to send the requests. |
|
| 33 | - * @param array|\Iterator $requests Requests or functions that return |
|
| 34 | - * requests to send concurrently. |
|
| 35 | - * @param array $config Associative array of options |
|
| 36 | - * - concurrency: (int) Maximum number of requests to send concurrently |
|
| 37 | - * - options: Array of request options to apply to each request. |
|
| 38 | - * - fulfilled: (callable) Function to invoke when a request completes. |
|
| 39 | - * - rejected: (callable) Function to invoke when a request is rejected. |
|
| 40 | - */ |
|
| 41 | - public function __construct(ClientInterface $client, $requests, array $config = []) |
|
| 42 | - { |
|
| 43 | - if (!isset($config['concurrency'])) { |
|
| 44 | - $config['concurrency'] = 25; |
|
| 45 | - } |
|
| 31 | + /** |
|
| 32 | + * @param ClientInterface $client Client used to send the requests. |
|
| 33 | + * @param array|\Iterator $requests Requests or functions that return |
|
| 34 | + * requests to send concurrently. |
|
| 35 | + * @param array $config Associative array of options |
|
| 36 | + * - concurrency: (int) Maximum number of requests to send concurrently |
|
| 37 | + * - options: Array of request options to apply to each request. |
|
| 38 | + * - fulfilled: (callable) Function to invoke when a request completes. |
|
| 39 | + * - rejected: (callable) Function to invoke when a request is rejected. |
|
| 40 | + */ |
|
| 41 | + public function __construct(ClientInterface $client, $requests, array $config = []) |
|
| 42 | + { |
|
| 43 | + if (!isset($config['concurrency'])) { |
|
| 44 | + $config['concurrency'] = 25; |
|
| 45 | + } |
|
| 46 | 46 | |
| 47 | - if (isset($config['options'])) { |
|
| 48 | - $opts = $config['options']; |
|
| 49 | - unset($config['options']); |
|
| 50 | - } else { |
|
| 51 | - $opts = []; |
|
| 52 | - } |
|
| 47 | + if (isset($config['options'])) { |
|
| 48 | + $opts = $config['options']; |
|
| 49 | + unset($config['options']); |
|
| 50 | + } else { |
|
| 51 | + $opts = []; |
|
| 52 | + } |
|
| 53 | 53 | |
| 54 | - $iterable = P\Create::iterFor($requests); |
|
| 55 | - $requests = static function () use ($iterable, $client, $opts) { |
|
| 56 | - foreach ($iterable as $key => $rfn) { |
|
| 57 | - if ($rfn instanceof RequestInterface) { |
|
| 58 | - yield $key => $client->sendAsync($rfn, $opts); |
|
| 59 | - } elseif (\is_callable($rfn)) { |
|
| 60 | - yield $key => $rfn($opts); |
|
| 61 | - } else { |
|
| 62 | - throw new \InvalidArgumentException('Each value yielded by the iterator must be a Psr7\Http\Message\RequestInterface or a callable that returns a promise that fulfills with a Psr7\Message\Http\ResponseInterface object.'); |
|
| 63 | - } |
|
| 64 | - } |
|
| 65 | - }; |
|
| 54 | + $iterable = P\Create::iterFor($requests); |
|
| 55 | + $requests = static function () use ($iterable, $client, $opts) { |
|
| 56 | + foreach ($iterable as $key => $rfn) { |
|
| 57 | + if ($rfn instanceof RequestInterface) { |
|
| 58 | + yield $key => $client->sendAsync($rfn, $opts); |
|
| 59 | + } elseif (\is_callable($rfn)) { |
|
| 60 | + yield $key => $rfn($opts); |
|
| 61 | + } else { |
|
| 62 | + throw new \InvalidArgumentException('Each value yielded by the iterator must be a Psr7\Http\Message\RequestInterface or a callable that returns a promise that fulfills with a Psr7\Message\Http\ResponseInterface object.'); |
|
| 63 | + } |
|
| 64 | + } |
|
| 65 | + }; |
|
| 66 | 66 | |
| 67 | - $this->each = new EachPromise($requests(), $config); |
|
| 68 | - } |
|
| 67 | + $this->each = new EachPromise($requests(), $config); |
|
| 68 | + } |
|
| 69 | 69 | |
| 70 | - /** |
|
| 71 | - * Get promise |
|
| 72 | - */ |
|
| 73 | - public function promise(): PromiseInterface |
|
| 74 | - { |
|
| 75 | - return $this->each->promise(); |
|
| 76 | - } |
|
| 70 | + /** |
|
| 71 | + * Get promise |
|
| 72 | + */ |
|
| 73 | + public function promise(): PromiseInterface |
|
| 74 | + { |
|
| 75 | + return $this->each->promise(); |
|
| 76 | + } |
|
| 77 | 77 | |
| 78 | - /** |
|
| 79 | - * Sends multiple requests concurrently and returns an array of responses |
|
| 80 | - * and exceptions that uses the same ordering as the provided requests. |
|
| 81 | - * |
|
| 82 | - * IMPORTANT: This method keeps every request and response in memory, and |
|
| 83 | - * as such, is NOT recommended when sending a large number or an |
|
| 84 | - * indeterminate number of requests concurrently. |
|
| 85 | - * |
|
| 86 | - * @param ClientInterface $client Client used to send the requests |
|
| 87 | - * @param array|\Iterator $requests Requests to send concurrently. |
|
| 88 | - * @param array $options Passes through the options available in |
|
| 89 | - * {@see \OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\Pool::__construct} |
|
| 90 | - * |
|
| 91 | - * @return array Returns an array containing the response or an exception |
|
| 92 | - * in the same order that the requests were sent. |
|
| 93 | - * |
|
| 94 | - * @throws \InvalidArgumentException if the event format is incorrect. |
|
| 95 | - */ |
|
| 96 | - public static function batch(ClientInterface $client, $requests, array $options = []): array |
|
| 97 | - { |
|
| 98 | - $res = []; |
|
| 99 | - self::cmpCallback($options, 'fulfilled', $res); |
|
| 100 | - self::cmpCallback($options, 'rejected', $res); |
|
| 101 | - $pool = new static($client, $requests, $options); |
|
| 102 | - $pool->promise()->wait(); |
|
| 103 | - \ksort($res); |
|
| 78 | + /** |
|
| 79 | + * Sends multiple requests concurrently and returns an array of responses |
|
| 80 | + * and exceptions that uses the same ordering as the provided requests. |
|
| 81 | + * |
|
| 82 | + * IMPORTANT: This method keeps every request and response in memory, and |
|
| 83 | + * as such, is NOT recommended when sending a large number or an |
|
| 84 | + * indeterminate number of requests concurrently. |
|
| 85 | + * |
|
| 86 | + * @param ClientInterface $client Client used to send the requests |
|
| 87 | + * @param array|\Iterator $requests Requests to send concurrently. |
|
| 88 | + * @param array $options Passes through the options available in |
|
| 89 | + * {@see \OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\Pool::__construct} |
|
| 90 | + * |
|
| 91 | + * @return array Returns an array containing the response or an exception |
|
| 92 | + * in the same order that the requests were sent. |
|
| 93 | + * |
|
| 94 | + * @throws \InvalidArgumentException if the event format is incorrect. |
|
| 95 | + */ |
|
| 96 | + public static function batch(ClientInterface $client, $requests, array $options = []): array |
|
| 97 | + { |
|
| 98 | + $res = []; |
|
| 99 | + self::cmpCallback($options, 'fulfilled', $res); |
|
| 100 | + self::cmpCallback($options, 'rejected', $res); |
|
| 101 | + $pool = new static($client, $requests, $options); |
|
| 102 | + $pool->promise()->wait(); |
|
| 103 | + \ksort($res); |
|
| 104 | 104 | |
| 105 | - return $res; |
|
| 106 | - } |
|
| 105 | + return $res; |
|
| 106 | + } |
|
| 107 | 107 | |
| 108 | - /** |
|
| 109 | - * Execute callback(s) |
|
| 110 | - */ |
|
| 111 | - private static function cmpCallback(array &$options, string $name, array &$results): void |
|
| 112 | - { |
|
| 113 | - if (!isset($options[$name])) { |
|
| 114 | - $options[$name] = static function ($v, $k) use (&$results) { |
|
| 115 | - $results[$k] = $v; |
|
| 116 | - }; |
|
| 117 | - } else { |
|
| 118 | - $currentFn = $options[$name]; |
|
| 119 | - $options[$name] = static function ($v, $k) use (&$results, $currentFn) { |
|
| 120 | - $currentFn($v, $k); |
|
| 121 | - $results[$k] = $v; |
|
| 122 | - }; |
|
| 123 | - } |
|
| 124 | - } |
|
| 108 | + /** |
|
| 109 | + * Execute callback(s) |
|
| 110 | + */ |
|
| 111 | + private static function cmpCallback(array &$options, string $name, array &$results): void |
|
| 112 | + { |
|
| 113 | + if (!isset($options[$name])) { |
|
| 114 | + $options[$name] = static function ($v, $k) use (&$results) { |
|
| 115 | + $results[$k] = $v; |
|
| 116 | + }; |
|
| 117 | + } else { |
|
| 118 | + $currentFn = $options[$name]; |
|
| 119 | + $options[$name] = static function ($v, $k) use (&$results, $currentFn) { |
|
| 120 | + $currentFn($v, $k); |
|
| 121 | + $results[$k] = $v; |
|
| 122 | + }; |
|
| 123 | + } |
|
| 124 | + } |
|
| 125 | 125 | } |
@@ -52,7 +52,7 @@ discard block |
||
| 52 | 52 | } |
| 53 | 53 | |
| 54 | 54 | $iterable = P\Create::iterFor($requests); |
| 55 | - $requests = static function () use ($iterable, $client, $opts) { |
|
| 55 | + $requests = static function() use ($iterable, $client, $opts) { |
|
| 56 | 56 | foreach ($iterable as $key => $rfn) { |
| 57 | 57 | if ($rfn instanceof RequestInterface) { |
| 58 | 58 | yield $key => $client->sendAsync($rfn, $opts); |
@@ -111,12 +111,12 @@ discard block |
||
| 111 | 111 | private static function cmpCallback(array &$options, string $name, array &$results): void |
| 112 | 112 | { |
| 113 | 113 | if (!isset($options[$name])) { |
| 114 | - $options[$name] = static function ($v, $k) use (&$results) { |
|
| 114 | + $options[$name] = static function($v, $k) use (&$results) { |
|
| 115 | 115 | $results[$k] = $v; |
| 116 | 116 | }; |
| 117 | 117 | } else { |
| 118 | 118 | $currentFn = $options[$name]; |
| 119 | - $options[$name] = static function ($v, $k) use (&$results, $currentFn) { |
|
| 119 | + $options[$name] = static function($v, $k) use (&$results, $currentFn) { |
|
| 120 | 120 | $currentFn($v, $k); |
| 121 | 121 | $results[$k] = $v; |
| 122 | 122 | }; |
@@ -21,8 +21,7 @@ |
||
| 21 | 21 | * |
| 22 | 22 | * @final |
| 23 | 23 | */ |
| 24 | -class Pool implements PromisorInterface |
|
| 25 | -{ |
|
| 24 | +class Pool implements PromisorInterface { |
|
| 26 | 25 | /** |
| 27 | 26 | * @var EachPromise |
| 28 | 27 | */ |
@@ -37,163 +37,163 @@ |
||
| 37 | 37 | */ |
| 38 | 38 | class MessageFormatter implements MessageFormatterInterface |
| 39 | 39 | { |
| 40 | - /** |
|
| 41 | - * Apache Common Log Format. |
|
| 42 | - * |
|
| 43 | - * @see https://httpd.apache.org/docs/2.4/logs.html#common |
|
| 44 | - * |
|
| 45 | - * @var string |
|
| 46 | - */ |
|
| 47 | - public const CLF = '{hostname} {req_header_User-Agent} - [{date_common_log}] "{method} {target} HTTP/{version}" {code} {res_header_Content-Length}'; |
|
| 48 | - public const DEBUG = ">>>>>>>>\n{request}\n<<<<<<<<\n{response}\n--------\n{error}"; |
|
| 49 | - public const SHORT = '[{ts}] "{method} {target} HTTP/{version}" {code}'; |
|
| 40 | + /** |
|
| 41 | + * Apache Common Log Format. |
|
| 42 | + * |
|
| 43 | + * @see https://httpd.apache.org/docs/2.4/logs.html#common |
|
| 44 | + * |
|
| 45 | + * @var string |
|
| 46 | + */ |
|
| 47 | + public const CLF = '{hostname} {req_header_User-Agent} - [{date_common_log}] "{method} {target} HTTP/{version}" {code} {res_header_Content-Length}'; |
|
| 48 | + public const DEBUG = ">>>>>>>>\n{request}\n<<<<<<<<\n{response}\n--------\n{error}"; |
|
| 49 | + public const SHORT = '[{ts}] "{method} {target} HTTP/{version}" {code}'; |
|
| 50 | 50 | |
| 51 | - /** |
|
| 52 | - * @var string Template used to format log messages |
|
| 53 | - */ |
|
| 54 | - private $template; |
|
| 51 | + /** |
|
| 52 | + * @var string Template used to format log messages |
|
| 53 | + */ |
|
| 54 | + private $template; |
|
| 55 | 55 | |
| 56 | - /** |
|
| 57 | - * @param string $template Log message template |
|
| 58 | - */ |
|
| 59 | - public function __construct(?string $template = self::CLF) |
|
| 60 | - { |
|
| 61 | - $this->template = $template ?: self::CLF; |
|
| 62 | - } |
|
| 56 | + /** |
|
| 57 | + * @param string $template Log message template |
|
| 58 | + */ |
|
| 59 | + public function __construct(?string $template = self::CLF) |
|
| 60 | + { |
|
| 61 | + $this->template = $template ?: self::CLF; |
|
| 62 | + } |
|
| 63 | 63 | |
| 64 | - /** |
|
| 65 | - * Returns a formatted message string. |
|
| 66 | - * |
|
| 67 | - * @param RequestInterface $request Request that was sent |
|
| 68 | - * @param ResponseInterface|null $response Response that was received |
|
| 69 | - * @param \Throwable|null $error Exception that was received |
|
| 70 | - */ |
|
| 71 | - public function format(RequestInterface $request, ResponseInterface $response = null, \Throwable $error = null): string |
|
| 72 | - { |
|
| 73 | - $cache = []; |
|
| 64 | + /** |
|
| 65 | + * Returns a formatted message string. |
|
| 66 | + * |
|
| 67 | + * @param RequestInterface $request Request that was sent |
|
| 68 | + * @param ResponseInterface|null $response Response that was received |
|
| 69 | + * @param \Throwable|null $error Exception that was received |
|
| 70 | + */ |
|
| 71 | + public function format(RequestInterface $request, ResponseInterface $response = null, \Throwable $error = null): string |
|
| 72 | + { |
|
| 73 | + $cache = []; |
|
| 74 | 74 | |
| 75 | - /** @var string */ |
|
| 76 | - return \preg_replace_callback( |
|
| 77 | - '/{\s*([A-Za-z_\-\.0-9]+)\s*}/', |
|
| 78 | - function (array $matches) use ($request, $response, $error, &$cache) { |
|
| 79 | - if (isset($cache[$matches[1]])) { |
|
| 80 | - return $cache[$matches[1]]; |
|
| 81 | - } |
|
| 75 | + /** @var string */ |
|
| 76 | + return \preg_replace_callback( |
|
| 77 | + '/{\s*([A-Za-z_\-\.0-9]+)\s*}/', |
|
| 78 | + function (array $matches) use ($request, $response, $error, &$cache) { |
|
| 79 | + if (isset($cache[$matches[1]])) { |
|
| 80 | + return $cache[$matches[1]]; |
|
| 81 | + } |
|
| 82 | 82 | |
| 83 | - $result = ''; |
|
| 84 | - switch ($matches[1]) { |
|
| 85 | - case 'request': |
|
| 86 | - $result = Psr7\Message::toString($request); |
|
| 87 | - break; |
|
| 88 | - case 'response': |
|
| 89 | - $result = $response ? Psr7\Message::toString($response) : ''; |
|
| 90 | - break; |
|
| 91 | - case 'req_headers': |
|
| 92 | - $result = \trim($request->getMethod() |
|
| 93 | - .' '.$request->getRequestTarget()) |
|
| 94 | - .' HTTP/'.$request->getProtocolVersion()."\r\n" |
|
| 95 | - .$this->headers($request); |
|
| 96 | - break; |
|
| 97 | - case 'res_headers': |
|
| 98 | - $result = $response ? |
|
| 99 | - \sprintf( |
|
| 100 | - 'HTTP/%s %d %s', |
|
| 101 | - $response->getProtocolVersion(), |
|
| 102 | - $response->getStatusCode(), |
|
| 103 | - $response->getReasonPhrase() |
|
| 104 | - )."\r\n".$this->headers($response) |
|
| 105 | - : 'NULL'; |
|
| 106 | - break; |
|
| 107 | - case 'req_body': |
|
| 108 | - $result = $request->getBody()->__toString(); |
|
| 109 | - break; |
|
| 110 | - case 'res_body': |
|
| 111 | - if (!$response instanceof ResponseInterface) { |
|
| 112 | - $result = 'NULL'; |
|
| 113 | - break; |
|
| 114 | - } |
|
| 83 | + $result = ''; |
|
| 84 | + switch ($matches[1]) { |
|
| 85 | + case 'request': |
|
| 86 | + $result = Psr7\Message::toString($request); |
|
| 87 | + break; |
|
| 88 | + case 'response': |
|
| 89 | + $result = $response ? Psr7\Message::toString($response) : ''; |
|
| 90 | + break; |
|
| 91 | + case 'req_headers': |
|
| 92 | + $result = \trim($request->getMethod() |
|
| 93 | + .' '.$request->getRequestTarget()) |
|
| 94 | + .' HTTP/'.$request->getProtocolVersion()."\r\n" |
|
| 95 | + .$this->headers($request); |
|
| 96 | + break; |
|
| 97 | + case 'res_headers': |
|
| 98 | + $result = $response ? |
|
| 99 | + \sprintf( |
|
| 100 | + 'HTTP/%s %d %s', |
|
| 101 | + $response->getProtocolVersion(), |
|
| 102 | + $response->getStatusCode(), |
|
| 103 | + $response->getReasonPhrase() |
|
| 104 | + )."\r\n".$this->headers($response) |
|
| 105 | + : 'NULL'; |
|
| 106 | + break; |
|
| 107 | + case 'req_body': |
|
| 108 | + $result = $request->getBody()->__toString(); |
|
| 109 | + break; |
|
| 110 | + case 'res_body': |
|
| 111 | + if (!$response instanceof ResponseInterface) { |
|
| 112 | + $result = 'NULL'; |
|
| 113 | + break; |
|
| 114 | + } |
|
| 115 | 115 | |
| 116 | - $body = $response->getBody(); |
|
| 116 | + $body = $response->getBody(); |
|
| 117 | 117 | |
| 118 | - if (!$body->isSeekable()) { |
|
| 119 | - $result = 'RESPONSE_NOT_LOGGEABLE'; |
|
| 120 | - break; |
|
| 121 | - } |
|
| 118 | + if (!$body->isSeekable()) { |
|
| 119 | + $result = 'RESPONSE_NOT_LOGGEABLE'; |
|
| 120 | + break; |
|
| 121 | + } |
|
| 122 | 122 | |
| 123 | - $result = $response->getBody()->__toString(); |
|
| 124 | - break; |
|
| 125 | - case 'ts': |
|
| 126 | - case 'date_iso_8601': |
|
| 127 | - $result = \gmdate('c'); |
|
| 128 | - break; |
|
| 129 | - case 'date_common_log': |
|
| 130 | - $result = \date('d/M/Y:H:i:s O'); |
|
| 131 | - break; |
|
| 132 | - case 'method': |
|
| 133 | - $result = $request->getMethod(); |
|
| 134 | - break; |
|
| 135 | - case 'version': |
|
| 136 | - $result = $request->getProtocolVersion(); |
|
| 137 | - break; |
|
| 138 | - case 'uri': |
|
| 139 | - case 'url': |
|
| 140 | - $result = $request->getUri()->__toString(); |
|
| 141 | - break; |
|
| 142 | - case 'target': |
|
| 143 | - $result = $request->getRequestTarget(); |
|
| 144 | - break; |
|
| 145 | - case 'req_version': |
|
| 146 | - $result = $request->getProtocolVersion(); |
|
| 147 | - break; |
|
| 148 | - case 'res_version': |
|
| 149 | - $result = $response |
|
| 150 | - ? $response->getProtocolVersion() |
|
| 151 | - : 'NULL'; |
|
| 152 | - break; |
|
| 153 | - case 'host': |
|
| 154 | - $result = $request->getHeaderLine('Host'); |
|
| 155 | - break; |
|
| 156 | - case 'hostname': |
|
| 157 | - $result = \gethostname(); |
|
| 158 | - break; |
|
| 159 | - case 'code': |
|
| 160 | - $result = $response ? $response->getStatusCode() : 'NULL'; |
|
| 161 | - break; |
|
| 162 | - case 'phrase': |
|
| 163 | - $result = $response ? $response->getReasonPhrase() : 'NULL'; |
|
| 164 | - break; |
|
| 165 | - case 'error': |
|
| 166 | - $result = $error ? $error->getMessage() : 'NULL'; |
|
| 167 | - break; |
|
| 168 | - default: |
|
| 169 | - // handle prefixed dynamic headers |
|
| 170 | - if (\strpos($matches[1], 'req_header_') === 0) { |
|
| 171 | - $result = $request->getHeaderLine(\substr($matches[1], 11)); |
|
| 172 | - } elseif (\strpos($matches[1], 'res_header_') === 0) { |
|
| 173 | - $result = $response |
|
| 174 | - ? $response->getHeaderLine(\substr($matches[1], 11)) |
|
| 175 | - : 'NULL'; |
|
| 176 | - } |
|
| 177 | - } |
|
| 123 | + $result = $response->getBody()->__toString(); |
|
| 124 | + break; |
|
| 125 | + case 'ts': |
|
| 126 | + case 'date_iso_8601': |
|
| 127 | + $result = \gmdate('c'); |
|
| 128 | + break; |
|
| 129 | + case 'date_common_log': |
|
| 130 | + $result = \date('d/M/Y:H:i:s O'); |
|
| 131 | + break; |
|
| 132 | + case 'method': |
|
| 133 | + $result = $request->getMethod(); |
|
| 134 | + break; |
|
| 135 | + case 'version': |
|
| 136 | + $result = $request->getProtocolVersion(); |
|
| 137 | + break; |
|
| 138 | + case 'uri': |
|
| 139 | + case 'url': |
|
| 140 | + $result = $request->getUri()->__toString(); |
|
| 141 | + break; |
|
| 142 | + case 'target': |
|
| 143 | + $result = $request->getRequestTarget(); |
|
| 144 | + break; |
|
| 145 | + case 'req_version': |
|
| 146 | + $result = $request->getProtocolVersion(); |
|
| 147 | + break; |
|
| 148 | + case 'res_version': |
|
| 149 | + $result = $response |
|
| 150 | + ? $response->getProtocolVersion() |
|
| 151 | + : 'NULL'; |
|
| 152 | + break; |
|
| 153 | + case 'host': |
|
| 154 | + $result = $request->getHeaderLine('Host'); |
|
| 155 | + break; |
|
| 156 | + case 'hostname': |
|
| 157 | + $result = \gethostname(); |
|
| 158 | + break; |
|
| 159 | + case 'code': |
|
| 160 | + $result = $response ? $response->getStatusCode() : 'NULL'; |
|
| 161 | + break; |
|
| 162 | + case 'phrase': |
|
| 163 | + $result = $response ? $response->getReasonPhrase() : 'NULL'; |
|
| 164 | + break; |
|
| 165 | + case 'error': |
|
| 166 | + $result = $error ? $error->getMessage() : 'NULL'; |
|
| 167 | + break; |
|
| 168 | + default: |
|
| 169 | + // handle prefixed dynamic headers |
|
| 170 | + if (\strpos($matches[1], 'req_header_') === 0) { |
|
| 171 | + $result = $request->getHeaderLine(\substr($matches[1], 11)); |
|
| 172 | + } elseif (\strpos($matches[1], 'res_header_') === 0) { |
|
| 173 | + $result = $response |
|
| 174 | + ? $response->getHeaderLine(\substr($matches[1], 11)) |
|
| 175 | + : 'NULL'; |
|
| 176 | + } |
|
| 177 | + } |
|
| 178 | 178 | |
| 179 | - $cache[$matches[1]] = $result; |
|
| 179 | + $cache[$matches[1]] = $result; |
|
| 180 | 180 | |
| 181 | - return $result; |
|
| 182 | - }, |
|
| 183 | - $this->template |
|
| 184 | - ); |
|
| 185 | - } |
|
| 181 | + return $result; |
|
| 182 | + }, |
|
| 183 | + $this->template |
|
| 184 | + ); |
|
| 185 | + } |
|
| 186 | 186 | |
| 187 | - /** |
|
| 188 | - * Get headers from message as string |
|
| 189 | - */ |
|
| 190 | - private function headers(MessageInterface $message): string |
|
| 191 | - { |
|
| 192 | - $result = ''; |
|
| 193 | - foreach ($message->getHeaders() as $name => $values) { |
|
| 194 | - $result .= $name.': '.\implode(', ', $values)."\r\n"; |
|
| 195 | - } |
|
| 187 | + /** |
|
| 188 | + * Get headers from message as string |
|
| 189 | + */ |
|
| 190 | + private function headers(MessageInterface $message): string |
|
| 191 | + { |
|
| 192 | + $result = ''; |
|
| 193 | + foreach ($message->getHeaders() as $name => $values) { |
|
| 194 | + $result .= $name.': '.\implode(', ', $values)."\r\n"; |
|
| 195 | + } |
|
| 196 | 196 | |
| 197 | - return \trim($result); |
|
| 198 | - } |
|
| 197 | + return \trim($result); |
|
| 198 | + } |
|
| 199 | 199 | } |
@@ -75,7 +75,7 @@ |
||
| 75 | 75 | /** @var string */ |
| 76 | 76 | return \preg_replace_callback( |
| 77 | 77 | '/{\s*([A-Za-z_\-\.0-9]+)\s*}/', |
| 78 | - function (array $matches) use ($request, $response, $error, &$cache) { |
|
| 78 | + function(array $matches) use ($request, $response, $error, &$cache) { |
|
| 79 | 79 | if (isset($cache[$matches[1]])) { |
| 80 | 80 | return $cache[$matches[1]]; |
| 81 | 81 | } |
@@ -35,8 +35,7 @@ |
||
| 35 | 35 | * |
| 36 | 36 | * @final |
| 37 | 37 | */ |
| 38 | -class MessageFormatter implements MessageFormatterInterface |
|
| 39 | -{ |
|
| 38 | +class MessageFormatter implements MessageFormatterInterface { |
|
| 40 | 39 | /** |
| 41 | 40 | * Apache Common Log Format. |
| 42 | 41 | * |
@@ -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 |