@@ -18,157 +18,157 @@ |
||
18 | 18 | */ |
19 | 19 | class MockHandler implements \Countable |
20 | 20 | { |
21 | - /** |
|
22 | - * @var array |
|
23 | - */ |
|
24 | - private $queue = []; |
|
25 | - /** |
|
26 | - * @var RequestInterface|null |
|
27 | - */ |
|
28 | - private $lastRequest; |
|
29 | - /** |
|
30 | - * @var array |
|
31 | - */ |
|
32 | - private $lastOptions = []; |
|
33 | - /** |
|
34 | - * @var callable|null |
|
35 | - */ |
|
36 | - private $onFulfilled; |
|
37 | - /** |
|
38 | - * @var callable|null |
|
39 | - */ |
|
40 | - private $onRejected; |
|
41 | - /** |
|
42 | - * Creates a new MockHandler that uses the default handler stack list of |
|
43 | - * middlewares. |
|
44 | - * |
|
45 | - * @param array|null $queue Array of responses, callables, or exceptions. |
|
46 | - * @param callable|null $onFulfilled Callback to invoke when the return value is fulfilled. |
|
47 | - * @param callable|null $onRejected Callback to invoke when the return value is rejected. |
|
48 | - */ |
|
49 | - public static function createWithMiddleware(array $queue = null, callable $onFulfilled = null, callable $onRejected = null) : HandlerStack |
|
50 | - { |
|
51 | - return HandlerStack::create(new self($queue, $onFulfilled, $onRejected)); |
|
52 | - } |
|
53 | - /** |
|
54 | - * The passed in value must be an array of |
|
55 | - * {@see \Psr\Http\Message\ResponseInterface} objects, Exceptions, |
|
56 | - * callables, or Promises. |
|
57 | - * |
|
58 | - * @param array<int, mixed>|null $queue The parameters to be passed to the append function, as an indexed array. |
|
59 | - * @param callable|null $onFulfilled Callback to invoke when the return value is fulfilled. |
|
60 | - * @param callable|null $onRejected Callback to invoke when the return value is rejected. |
|
61 | - */ |
|
62 | - public function __construct(array $queue = null, callable $onFulfilled = null, callable $onRejected = null) |
|
63 | - { |
|
64 | - $this->onFulfilled = $onFulfilled; |
|
65 | - $this->onRejected = $onRejected; |
|
66 | - if ($queue) { |
|
67 | - // array_values included for BC |
|
68 | - $this->append(...\array_values($queue)); |
|
69 | - } |
|
70 | - } |
|
71 | - public function __invoke(RequestInterface $request, array $options) : PromiseInterface |
|
72 | - { |
|
73 | - if (!$this->queue) { |
|
74 | - throw new \OutOfBoundsException('Mock queue is empty'); |
|
75 | - } |
|
76 | - if (isset($options['delay']) && \is_numeric($options['delay'])) { |
|
77 | - \usleep((int) $options['delay'] * 1000); |
|
78 | - } |
|
79 | - $this->lastRequest = $request; |
|
80 | - $this->lastOptions = $options; |
|
81 | - $response = \array_shift($this->queue); |
|
82 | - if (isset($options['on_headers'])) { |
|
83 | - if (!\is_callable($options['on_headers'])) { |
|
84 | - throw new \InvalidArgumentException('on_headers must be callable'); |
|
85 | - } |
|
86 | - try { |
|
87 | - $options['on_headers']($response); |
|
88 | - } catch (\Exception $e) { |
|
89 | - $msg = 'An error was encountered during the on_headers event'; |
|
90 | - $response = new RequestException($msg, $request, $response, $e); |
|
91 | - } |
|
92 | - } |
|
93 | - if (\is_callable($response)) { |
|
94 | - $response = $response($request, $options); |
|
95 | - } |
|
96 | - $response = $response instanceof \Throwable ? P\Create::rejectionFor($response) : P\Create::promiseFor($response); |
|
97 | - return $response->then(function (?ResponseInterface $value) use($request, $options) { |
|
98 | - $this->invokeStats($request, $options, $value); |
|
99 | - if ($this->onFulfilled) { |
|
100 | - ($this->onFulfilled)($value); |
|
101 | - } |
|
102 | - if ($value !== null && isset($options['sink'])) { |
|
103 | - $contents = (string) $value->getBody(); |
|
104 | - $sink = $options['sink']; |
|
105 | - if (\is_resource($sink)) { |
|
106 | - \fwrite($sink, $contents); |
|
107 | - } elseif (\is_string($sink)) { |
|
108 | - \file_put_contents($sink, $contents); |
|
109 | - } elseif ($sink instanceof StreamInterface) { |
|
110 | - $sink->write($contents); |
|
111 | - } |
|
112 | - } |
|
113 | - return $value; |
|
114 | - }, function ($reason) use($request, $options) { |
|
115 | - $this->invokeStats($request, $options, null, $reason); |
|
116 | - if ($this->onRejected) { |
|
117 | - ($this->onRejected)($reason); |
|
118 | - } |
|
119 | - return P\Create::rejectionFor($reason); |
|
120 | - }); |
|
121 | - } |
|
122 | - /** |
|
123 | - * Adds one or more variadic requests, exceptions, callables, or promises |
|
124 | - * to the queue. |
|
125 | - * |
|
126 | - * @param mixed ...$values |
|
127 | - */ |
|
128 | - public function append(...$values) : void |
|
129 | - { |
|
130 | - foreach ($values as $value) { |
|
131 | - if ($value instanceof ResponseInterface || $value instanceof \Throwable || $value instanceof PromiseInterface || \is_callable($value)) { |
|
132 | - $this->queue[] = $value; |
|
133 | - } else { |
|
134 | - throw new \TypeError('Expected a Response, Promise, Throwable or callable. Found ' . Utils::describeType($value)); |
|
135 | - } |
|
136 | - } |
|
137 | - } |
|
138 | - /** |
|
139 | - * Get the last received request. |
|
140 | - */ |
|
141 | - public function getLastRequest() : ?RequestInterface |
|
142 | - { |
|
143 | - return $this->lastRequest; |
|
144 | - } |
|
145 | - /** |
|
146 | - * Get the last received request options. |
|
147 | - */ |
|
148 | - public function getLastOptions() : array |
|
149 | - { |
|
150 | - return $this->lastOptions; |
|
151 | - } |
|
152 | - /** |
|
153 | - * Returns the number of remaining items in the queue. |
|
154 | - */ |
|
155 | - public function count() : int |
|
156 | - { |
|
157 | - return \count($this->queue); |
|
158 | - } |
|
159 | - public function reset() : void |
|
160 | - { |
|
161 | - $this->queue = []; |
|
162 | - } |
|
163 | - /** |
|
164 | - * @param mixed $reason Promise or reason. |
|
165 | - */ |
|
166 | - private function invokeStats(RequestInterface $request, array $options, ResponseInterface $response = null, $reason = null) : void |
|
167 | - { |
|
168 | - if (isset($options['on_stats'])) { |
|
169 | - $transferTime = $options['transfer_time'] ?? 0; |
|
170 | - $stats = new TransferStats($request, $response, $transferTime, $reason); |
|
171 | - $options['on_stats']($stats); |
|
172 | - } |
|
173 | - } |
|
21 | + /** |
|
22 | + * @var array |
|
23 | + */ |
|
24 | + private $queue = []; |
|
25 | + /** |
|
26 | + * @var RequestInterface|null |
|
27 | + */ |
|
28 | + private $lastRequest; |
|
29 | + /** |
|
30 | + * @var array |
|
31 | + */ |
|
32 | + private $lastOptions = []; |
|
33 | + /** |
|
34 | + * @var callable|null |
|
35 | + */ |
|
36 | + private $onFulfilled; |
|
37 | + /** |
|
38 | + * @var callable|null |
|
39 | + */ |
|
40 | + private $onRejected; |
|
41 | + /** |
|
42 | + * Creates a new MockHandler that uses the default handler stack list of |
|
43 | + * middlewares. |
|
44 | + * |
|
45 | + * @param array|null $queue Array of responses, callables, or exceptions. |
|
46 | + * @param callable|null $onFulfilled Callback to invoke when the return value is fulfilled. |
|
47 | + * @param callable|null $onRejected Callback to invoke when the return value is rejected. |
|
48 | + */ |
|
49 | + public static function createWithMiddleware(array $queue = null, callable $onFulfilled = null, callable $onRejected = null) : HandlerStack |
|
50 | + { |
|
51 | + return HandlerStack::create(new self($queue, $onFulfilled, $onRejected)); |
|
52 | + } |
|
53 | + /** |
|
54 | + * The passed in value must be an array of |
|
55 | + * {@see \Psr\Http\Message\ResponseInterface} objects, Exceptions, |
|
56 | + * callables, or Promises. |
|
57 | + * |
|
58 | + * @param array<int, mixed>|null $queue The parameters to be passed to the append function, as an indexed array. |
|
59 | + * @param callable|null $onFulfilled Callback to invoke when the return value is fulfilled. |
|
60 | + * @param callable|null $onRejected Callback to invoke when the return value is rejected. |
|
61 | + */ |
|
62 | + public function __construct(array $queue = null, callable $onFulfilled = null, callable $onRejected = null) |
|
63 | + { |
|
64 | + $this->onFulfilled = $onFulfilled; |
|
65 | + $this->onRejected = $onRejected; |
|
66 | + if ($queue) { |
|
67 | + // array_values included for BC |
|
68 | + $this->append(...\array_values($queue)); |
|
69 | + } |
|
70 | + } |
|
71 | + public function __invoke(RequestInterface $request, array $options) : PromiseInterface |
|
72 | + { |
|
73 | + if (!$this->queue) { |
|
74 | + throw new \OutOfBoundsException('Mock queue is empty'); |
|
75 | + } |
|
76 | + if (isset($options['delay']) && \is_numeric($options['delay'])) { |
|
77 | + \usleep((int) $options['delay'] * 1000); |
|
78 | + } |
|
79 | + $this->lastRequest = $request; |
|
80 | + $this->lastOptions = $options; |
|
81 | + $response = \array_shift($this->queue); |
|
82 | + if (isset($options['on_headers'])) { |
|
83 | + if (!\is_callable($options['on_headers'])) { |
|
84 | + throw new \InvalidArgumentException('on_headers must be callable'); |
|
85 | + } |
|
86 | + try { |
|
87 | + $options['on_headers']($response); |
|
88 | + } catch (\Exception $e) { |
|
89 | + $msg = 'An error was encountered during the on_headers event'; |
|
90 | + $response = new RequestException($msg, $request, $response, $e); |
|
91 | + } |
|
92 | + } |
|
93 | + if (\is_callable($response)) { |
|
94 | + $response = $response($request, $options); |
|
95 | + } |
|
96 | + $response = $response instanceof \Throwable ? P\Create::rejectionFor($response) : P\Create::promiseFor($response); |
|
97 | + return $response->then(function (?ResponseInterface $value) use($request, $options) { |
|
98 | + $this->invokeStats($request, $options, $value); |
|
99 | + if ($this->onFulfilled) { |
|
100 | + ($this->onFulfilled)($value); |
|
101 | + } |
|
102 | + if ($value !== null && isset($options['sink'])) { |
|
103 | + $contents = (string) $value->getBody(); |
|
104 | + $sink = $options['sink']; |
|
105 | + if (\is_resource($sink)) { |
|
106 | + \fwrite($sink, $contents); |
|
107 | + } elseif (\is_string($sink)) { |
|
108 | + \file_put_contents($sink, $contents); |
|
109 | + } elseif ($sink instanceof StreamInterface) { |
|
110 | + $sink->write($contents); |
|
111 | + } |
|
112 | + } |
|
113 | + return $value; |
|
114 | + }, function ($reason) use($request, $options) { |
|
115 | + $this->invokeStats($request, $options, null, $reason); |
|
116 | + if ($this->onRejected) { |
|
117 | + ($this->onRejected)($reason); |
|
118 | + } |
|
119 | + return P\Create::rejectionFor($reason); |
|
120 | + }); |
|
121 | + } |
|
122 | + /** |
|
123 | + * Adds one or more variadic requests, exceptions, callables, or promises |
|
124 | + * to the queue. |
|
125 | + * |
|
126 | + * @param mixed ...$values |
|
127 | + */ |
|
128 | + public function append(...$values) : void |
|
129 | + { |
|
130 | + foreach ($values as $value) { |
|
131 | + if ($value instanceof ResponseInterface || $value instanceof \Throwable || $value instanceof PromiseInterface || \is_callable($value)) { |
|
132 | + $this->queue[] = $value; |
|
133 | + } else { |
|
134 | + throw new \TypeError('Expected a Response, Promise, Throwable or callable. Found ' . Utils::describeType($value)); |
|
135 | + } |
|
136 | + } |
|
137 | + } |
|
138 | + /** |
|
139 | + * Get the last received request. |
|
140 | + */ |
|
141 | + public function getLastRequest() : ?RequestInterface |
|
142 | + { |
|
143 | + return $this->lastRequest; |
|
144 | + } |
|
145 | + /** |
|
146 | + * Get the last received request options. |
|
147 | + */ |
|
148 | + public function getLastOptions() : array |
|
149 | + { |
|
150 | + return $this->lastOptions; |
|
151 | + } |
|
152 | + /** |
|
153 | + * Returns the number of remaining items in the queue. |
|
154 | + */ |
|
155 | + public function count() : int |
|
156 | + { |
|
157 | + return \count($this->queue); |
|
158 | + } |
|
159 | + public function reset() : void |
|
160 | + { |
|
161 | + $this->queue = []; |
|
162 | + } |
|
163 | + /** |
|
164 | + * @param mixed $reason Promise or reason. |
|
165 | + */ |
|
166 | + private function invokeStats(RequestInterface $request, array $options, ResponseInterface $response = null, $reason = null) : void |
|
167 | + { |
|
168 | + if (isset($options['on_stats'])) { |
|
169 | + $transferTime = $options['transfer_time'] ?? 0; |
|
170 | + $stats = new TransferStats($request, $response, $transferTime, $reason); |
|
171 | + $options['on_stats']($stats); |
|
172 | + } |
|
173 | + } |
|
174 | 174 | } |
@@ -74,7 +74,7 @@ discard block |
||
74 | 74 | throw new \OutOfBoundsException('Mock queue is empty'); |
75 | 75 | } |
76 | 76 | if (isset($options['delay']) && \is_numeric($options['delay'])) { |
77 | - \usleep((int) $options['delay'] * 1000); |
|
77 | + \usleep((int)$options['delay'] * 1000); |
|
78 | 78 | } |
79 | 79 | $this->lastRequest = $request; |
80 | 80 | $this->lastOptions = $options; |
@@ -94,13 +94,13 @@ discard block |
||
94 | 94 | $response = $response($request, $options); |
95 | 95 | } |
96 | 96 | $response = $response instanceof \Throwable ? P\Create::rejectionFor($response) : P\Create::promiseFor($response); |
97 | - return $response->then(function (?ResponseInterface $value) use($request, $options) { |
|
97 | + return $response->then(function(?ResponseInterface $value) use($request, $options) { |
|
98 | 98 | $this->invokeStats($request, $options, $value); |
99 | 99 | if ($this->onFulfilled) { |
100 | 100 | ($this->onFulfilled)($value); |
101 | 101 | } |
102 | 102 | if ($value !== null && isset($options['sink'])) { |
103 | - $contents = (string) $value->getBody(); |
|
103 | + $contents = (string)$value->getBody(); |
|
104 | 104 | $sink = $options['sink']; |
105 | 105 | if (\is_resource($sink)) { |
106 | 106 | \fwrite($sink, $contents); |
@@ -111,7 +111,7 @@ discard block |
||
111 | 111 | } |
112 | 112 | } |
113 | 113 | return $value; |
114 | - }, function ($reason) use($request, $options) { |
|
114 | + }, function($reason) use($request, $options) { |
|
115 | 115 | $this->invokeStats($request, $options, null, $reason); |
116 | 116 | if ($this->onRejected) { |
117 | 117 | ($this->onRejected)($reason); |
@@ -131,7 +131,7 @@ discard block |
||
131 | 131 | if ($value instanceof ResponseInterface || $value instanceof \Throwable || $value instanceof PromiseInterface || \is_callable($value)) { |
132 | 132 | $this->queue[] = $value; |
133 | 133 | } else { |
134 | - throw new \TypeError('Expected a Response, Promise, Throwable or callable. Found ' . Utils::describeType($value)); |
|
134 | + throw new \TypeError('Expected a Response, Promise, Throwable or callable. Found '.Utils::describeType($value)); |
|
135 | 135 | } |
136 | 136 | } |
137 | 137 | } |
@@ -15,7 +15,7 @@ discard block |
||
15 | 15 | */ |
16 | 16 | function describe_type($input) : string |
17 | 17 | { |
18 | - return Utils::describeType($input); |
|
18 | + return Utils::describeType($input); |
|
19 | 19 | } |
20 | 20 | /** |
21 | 21 | * Parses an array of header lines into an associative array of headers. |
@@ -27,7 +27,7 @@ discard block |
||
27 | 27 | */ |
28 | 28 | function headers_from_lines(iterable $lines) : array |
29 | 29 | { |
30 | - return Utils::headersFromLines($lines); |
|
30 | + return Utils::headersFromLines($lines); |
|
31 | 31 | } |
32 | 32 | /** |
33 | 33 | * Returns a debug stream based on the provided variable. |
@@ -40,7 +40,7 @@ discard block |
||
40 | 40 | */ |
41 | 41 | function debug_resource($value = null) |
42 | 42 | { |
43 | - return Utils::debugResource($value); |
|
43 | + return Utils::debugResource($value); |
|
44 | 44 | } |
45 | 45 | /** |
46 | 46 | * Chooses and creates a default handler to use based on the environment. |
@@ -55,7 +55,7 @@ discard block |
||
55 | 55 | */ |
56 | 56 | function choose_handler() : callable |
57 | 57 | { |
58 | - return Utils::chooseHandler(); |
|
58 | + return Utils::chooseHandler(); |
|
59 | 59 | } |
60 | 60 | /** |
61 | 61 | * Get the default User-Agent string to use with Guzzle. |
@@ -64,7 +64,7 @@ discard block |
||
64 | 64 | */ |
65 | 65 | function default_user_agent() : string |
66 | 66 | { |
67 | - return Utils::defaultUserAgent(); |
|
67 | + return Utils::defaultUserAgent(); |
|
68 | 68 | } |
69 | 69 | /** |
70 | 70 | * Returns the default cacert bundle for the current system. |
@@ -83,7 +83,7 @@ discard block |
||
83 | 83 | */ |
84 | 84 | function default_ca_bundle() : string |
85 | 85 | { |
86 | - return Utils::defaultCaBundle(); |
|
86 | + return Utils::defaultCaBundle(); |
|
87 | 87 | } |
88 | 88 | /** |
89 | 89 | * Creates an associative array of lowercase header names to the actual |
@@ -93,7 +93,7 @@ discard block |
||
93 | 93 | */ |
94 | 94 | function normalize_header_keys(array $headers) : array |
95 | 95 | { |
96 | - return Utils::normalizeHeaderKeys($headers); |
|
96 | + return Utils::normalizeHeaderKeys($headers); |
|
97 | 97 | } |
98 | 98 | /** |
99 | 99 | * Returns true if the provided host matches any of the no proxy areas. |
@@ -118,7 +118,7 @@ discard block |
||
118 | 118 | */ |
119 | 119 | function is_host_in_noproxy(string $host, array $noProxyArray) : bool |
120 | 120 | { |
121 | - return Utils::isHostInNoProxy($host, $noProxyArray); |
|
121 | + return Utils::isHostInNoProxy($host, $noProxyArray); |
|
122 | 122 | } |
123 | 123 | /** |
124 | 124 | * Wrapper for json_decode that throws when an error occurs. |
@@ -138,7 +138,7 @@ discard block |
||
138 | 138 | */ |
139 | 139 | function json_decode(string $json, bool $assoc = \false, int $depth = 512, int $options = 0) |
140 | 140 | { |
141 | - return Utils::jsonDecode($json, $assoc, $depth, $options); |
|
141 | + return Utils::jsonDecode($json, $assoc, $depth, $options); |
|
142 | 142 | } |
143 | 143 | /** |
144 | 144 | * Wrapper for JSON encoding that throws when an error occurs. |
@@ -154,5 +154,5 @@ discard block |
||
154 | 154 | */ |
155 | 155 | function json_encode($value, int $options = 0, int $depth = 512) : string |
156 | 156 | { |
157 | - return Utils::jsonEncode($value, $options, $depth); |
|
157 | + return Utils::jsonEncode($value, $options, $depth); |
|
158 | 158 | } |
@@ -5,17 +5,17 @@ |
||
5 | 5 | |
6 | 6 | interface TaskQueueInterface |
7 | 7 | { |
8 | - /** |
|
9 | - * Returns true if the queue is empty. |
|
10 | - */ |
|
11 | - public function isEmpty() : bool; |
|
12 | - /** |
|
13 | - * Adds a task to the queue that will be executed the next time run is |
|
14 | - * called. |
|
15 | - */ |
|
16 | - public function add(callable $task) : void; |
|
17 | - /** |
|
18 | - * Execute all of the pending task in the queue. |
|
19 | - */ |
|
20 | - public function run() : void; |
|
8 | + /** |
|
9 | + * Returns true if the queue is empty. |
|
10 | + */ |
|
11 | + public function isEmpty() : bool; |
|
12 | + /** |
|
13 | + * Adds a task to the queue that will be executed the next time run is |
|
14 | + * called. |
|
15 | + */ |
|
16 | + public function add(callable $task) : void; |
|
17 | + /** |
|
18 | + * Execute all of the pending task in the queue. |
|
19 | + */ |
|
20 | + public function run() : void; |
|
21 | 21 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare (strict_types=1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | namespace OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\Promise; |
5 | 5 | |
6 | 6 | interface TaskQueueInterface |
@@ -3,8 +3,7 @@ |
||
3 | 3 | declare (strict_types=1); |
4 | 4 | namespace OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\Promise; |
5 | 5 | |
6 | -interface TaskQueueInterface |
|
7 | -{ |
|
6 | +interface TaskQueueInterface { |
|
8 | 7 | /** |
9 | 8 | * Returns true if the queue is empty. |
10 | 9 | */ |
@@ -10,32 +10,32 @@ |
||
10 | 10 | */ |
11 | 11 | class RejectionException extends \RuntimeException |
12 | 12 | { |
13 | - /** @var mixed Rejection reason. */ |
|
14 | - private $reason; |
|
15 | - /** |
|
16 | - * @param mixed $reason Rejection reason. |
|
17 | - * @param string|null $description Optional description. |
|
18 | - */ |
|
19 | - public function __construct($reason, string $description = null) |
|
20 | - { |
|
21 | - $this->reason = $reason; |
|
22 | - $message = 'The promise was rejected'; |
|
23 | - if ($description) { |
|
24 | - $message .= ' with reason: ' . $description; |
|
25 | - } elseif (\is_string($reason) || \is_object($reason) && \method_exists($reason, '__toString')) { |
|
26 | - $message .= ' with reason: ' . $this->reason; |
|
27 | - } elseif ($reason instanceof \JsonSerializable) { |
|
28 | - $message .= ' with reason: ' . \json_encode($this->reason, \JSON_PRETTY_PRINT); |
|
29 | - } |
|
30 | - parent::__construct($message); |
|
31 | - } |
|
32 | - /** |
|
33 | - * Returns the rejection reason. |
|
34 | - * |
|
35 | - * @return mixed |
|
36 | - */ |
|
37 | - public function getReason() |
|
38 | - { |
|
39 | - return $this->reason; |
|
40 | - } |
|
13 | + /** @var mixed Rejection reason. */ |
|
14 | + private $reason; |
|
15 | + /** |
|
16 | + * @param mixed $reason Rejection reason. |
|
17 | + * @param string|null $description Optional description. |
|
18 | + */ |
|
19 | + public function __construct($reason, string $description = null) |
|
20 | + { |
|
21 | + $this->reason = $reason; |
|
22 | + $message = 'The promise was rejected'; |
|
23 | + if ($description) { |
|
24 | + $message .= ' with reason: ' . $description; |
|
25 | + } elseif (\is_string($reason) || \is_object($reason) && \method_exists($reason, '__toString')) { |
|
26 | + $message .= ' with reason: ' . $this->reason; |
|
27 | + } elseif ($reason instanceof \JsonSerializable) { |
|
28 | + $message .= ' with reason: ' . \json_encode($this->reason, \JSON_PRETTY_PRINT); |
|
29 | + } |
|
30 | + parent::__construct($message); |
|
31 | + } |
|
32 | + /** |
|
33 | + * Returns the rejection reason. |
|
34 | + * |
|
35 | + * @return mixed |
|
36 | + */ |
|
37 | + public function getReason() |
|
38 | + { |
|
39 | + return $this->reason; |
|
40 | + } |
|
41 | 41 | } |
@@ -1,6 +1,6 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare (strict_types=1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | namespace OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\Promise; |
5 | 5 | |
6 | 6 | /** |
@@ -21,11 +21,11 @@ discard block |
||
21 | 21 | $this->reason = $reason; |
22 | 22 | $message = 'The promise was rejected'; |
23 | 23 | if ($description) { |
24 | - $message .= ' with reason: ' . $description; |
|
24 | + $message .= ' with reason: '.$description; |
|
25 | 25 | } elseif (\is_string($reason) || \is_object($reason) && \method_exists($reason, '__toString')) { |
26 | - $message .= ' with reason: ' . $this->reason; |
|
26 | + $message .= ' with reason: '.$this->reason; |
|
27 | 27 | } elseif ($reason instanceof \JsonSerializable) { |
28 | - $message .= ' with reason: ' . \json_encode($this->reason, \JSON_PRETTY_PRINT); |
|
28 | + $message .= ' with reason: '.\json_encode($this->reason, \JSON_PRETTY_PRINT); |
|
29 | 29 | } |
30 | 30 | parent::__construct($message); |
31 | 31 | } |
@@ -16,50 +16,50 @@ |
||
16 | 16 | */ |
17 | 17 | class TaskQueue implements TaskQueueInterface |
18 | 18 | { |
19 | - private $enableShutdown = \true; |
|
20 | - private $queue = []; |
|
21 | - public function __construct(bool $withShutdown = \true) |
|
22 | - { |
|
23 | - if ($withShutdown) { |
|
24 | - \register_shutdown_function(function () : void { |
|
25 | - if ($this->enableShutdown) { |
|
26 | - // Only run the tasks if an E_ERROR didn't occur. |
|
27 | - $err = \error_get_last(); |
|
28 | - if (!$err || $err['type'] ^ \E_ERROR) { |
|
29 | - $this->run(); |
|
30 | - } |
|
31 | - } |
|
32 | - }); |
|
33 | - } |
|
34 | - } |
|
35 | - public function isEmpty() : bool |
|
36 | - { |
|
37 | - return !$this->queue; |
|
38 | - } |
|
39 | - public function add(callable $task) : void |
|
40 | - { |
|
41 | - $this->queue[] = $task; |
|
42 | - } |
|
43 | - public function run() : void |
|
44 | - { |
|
45 | - while ($task = \array_shift($this->queue)) { |
|
46 | - /** @var callable $task */ |
|
47 | - $task(); |
|
48 | - } |
|
49 | - } |
|
50 | - /** |
|
51 | - * The task queue will be run and exhausted by default when the process |
|
52 | - * exits IFF the exit is not the result of a PHP E_ERROR error. |
|
53 | - * |
|
54 | - * You can disable running the automatic shutdown of the queue by calling |
|
55 | - * this function. If you disable the task queue shutdown process, then you |
|
56 | - * MUST either run the task queue (as a result of running your event loop |
|
57 | - * or manually using the run() method) or wait on each outstanding promise. |
|
58 | - * |
|
59 | - * Note: This shutdown will occur before any destructors are triggered. |
|
60 | - */ |
|
61 | - public function disableShutdown() : void |
|
62 | - { |
|
63 | - $this->enableShutdown = \false; |
|
64 | - } |
|
19 | + private $enableShutdown = \true; |
|
20 | + private $queue = []; |
|
21 | + public function __construct(bool $withShutdown = \true) |
|
22 | + { |
|
23 | + if ($withShutdown) { |
|
24 | + \register_shutdown_function(function () : void { |
|
25 | + if ($this->enableShutdown) { |
|
26 | + // Only run the tasks if an E_ERROR didn't occur. |
|
27 | + $err = \error_get_last(); |
|
28 | + if (!$err || $err['type'] ^ \E_ERROR) { |
|
29 | + $this->run(); |
|
30 | + } |
|
31 | + } |
|
32 | + }); |
|
33 | + } |
|
34 | + } |
|
35 | + public function isEmpty() : bool |
|
36 | + { |
|
37 | + return !$this->queue; |
|
38 | + } |
|
39 | + public function add(callable $task) : void |
|
40 | + { |
|
41 | + $this->queue[] = $task; |
|
42 | + } |
|
43 | + public function run() : void |
|
44 | + { |
|
45 | + while ($task = \array_shift($this->queue)) { |
|
46 | + /** @var callable $task */ |
|
47 | + $task(); |
|
48 | + } |
|
49 | + } |
|
50 | + /** |
|
51 | + * The task queue will be run and exhausted by default when the process |
|
52 | + * exits IFF the exit is not the result of a PHP E_ERROR error. |
|
53 | + * |
|
54 | + * You can disable running the automatic shutdown of the queue by calling |
|
55 | + * this function. If you disable the task queue shutdown process, then you |
|
56 | + * MUST either run the task queue (as a result of running your event loop |
|
57 | + * or manually using the run() method) or wait on each outstanding promise. |
|
58 | + * |
|
59 | + * Note: This shutdown will occur before any destructors are triggered. |
|
60 | + */ |
|
61 | + public function disableShutdown() : void |
|
62 | + { |
|
63 | + $this->enableShutdown = \false; |
|
64 | + } |
|
65 | 65 | } |
@@ -1,6 +1,6 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare (strict_types=1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | namespace OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\Promise; |
5 | 5 | |
6 | 6 | /** |
@@ -21,7 +21,7 @@ discard block |
||
21 | 21 | public function __construct(bool $withShutdown = \true) |
22 | 22 | { |
23 | 23 | if ($withShutdown) { |
24 | - \register_shutdown_function(function () : void { |
|
24 | + \register_shutdown_function(function() : void { |
|
25 | 25 | if ($this->enableShutdown) { |
26 | 26 | // Only run the tasks if an E_ERROR didn't occur. |
27 | 27 | $err = \error_get_last(); |
@@ -14,8 +14,7 @@ |
||
14 | 14 | * |
15 | 15 | * @final |
16 | 16 | */ |
17 | -class TaskQueue implements TaskQueueInterface |
|
18 | -{ |
|
17 | +class TaskQueue implements TaskQueueInterface { |
|
19 | 18 | private $enableShutdown = \true; |
20 | 19 | private $queue = []; |
21 | 20 | public function __construct(bool $withShutdown = \true) |
@@ -13,61 +13,61 @@ |
||
13 | 13 | */ |
14 | 14 | class FulfilledPromise implements PromiseInterface |
15 | 15 | { |
16 | - private $value; |
|
17 | - /** |
|
18 | - * @param mixed $value |
|
19 | - */ |
|
20 | - public function __construct($value) |
|
21 | - { |
|
22 | - if (\is_object($value) && \method_exists($value, 'then')) { |
|
23 | - throw new \InvalidArgumentException('You cannot create a FulfilledPromise with a promise.'); |
|
24 | - } |
|
25 | - $this->value = $value; |
|
26 | - } |
|
27 | - public function then(callable $onFulfilled = null, callable $onRejected = null) : PromiseInterface |
|
28 | - { |
|
29 | - // Return itself if there is no onFulfilled function. |
|
30 | - if (!$onFulfilled) { |
|
31 | - return $this; |
|
32 | - } |
|
33 | - $queue = Utils::queue(); |
|
34 | - $p = new Promise([$queue, 'run']); |
|
35 | - $value = $this->value; |
|
36 | - $queue->add(static function () use($p, $value, $onFulfilled) : void { |
|
37 | - if (Is::pending($p)) { |
|
38 | - try { |
|
39 | - $p->resolve($onFulfilled($value)); |
|
40 | - } catch (\Throwable $e) { |
|
41 | - $p->reject($e); |
|
42 | - } |
|
43 | - } |
|
44 | - }); |
|
45 | - return $p; |
|
46 | - } |
|
47 | - public function otherwise(callable $onRejected) : PromiseInterface |
|
48 | - { |
|
49 | - return $this->then(null, $onRejected); |
|
50 | - } |
|
51 | - public function wait(bool $unwrap = \true) |
|
52 | - { |
|
53 | - return $unwrap ? $this->value : null; |
|
54 | - } |
|
55 | - public function getState() : string |
|
56 | - { |
|
57 | - return self::FULFILLED; |
|
58 | - } |
|
59 | - public function resolve($value) : void |
|
60 | - { |
|
61 | - if ($value !== $this->value) { |
|
62 | - throw new \LogicException('Cannot resolve a fulfilled promise'); |
|
63 | - } |
|
64 | - } |
|
65 | - public function reject($reason) : void |
|
66 | - { |
|
67 | - throw new \LogicException('Cannot reject a fulfilled promise'); |
|
68 | - } |
|
69 | - public function cancel() : void |
|
70 | - { |
|
71 | - // pass |
|
72 | - } |
|
16 | + private $value; |
|
17 | + /** |
|
18 | + * @param mixed $value |
|
19 | + */ |
|
20 | + public function __construct($value) |
|
21 | + { |
|
22 | + if (\is_object($value) && \method_exists($value, 'then')) { |
|
23 | + throw new \InvalidArgumentException('You cannot create a FulfilledPromise with a promise.'); |
|
24 | + } |
|
25 | + $this->value = $value; |
|
26 | + } |
|
27 | + public function then(callable $onFulfilled = null, callable $onRejected = null) : PromiseInterface |
|
28 | + { |
|
29 | + // Return itself if there is no onFulfilled function. |
|
30 | + if (!$onFulfilled) { |
|
31 | + return $this; |
|
32 | + } |
|
33 | + $queue = Utils::queue(); |
|
34 | + $p = new Promise([$queue, 'run']); |
|
35 | + $value = $this->value; |
|
36 | + $queue->add(static function () use($p, $value, $onFulfilled) : void { |
|
37 | + if (Is::pending($p)) { |
|
38 | + try { |
|
39 | + $p->resolve($onFulfilled($value)); |
|
40 | + } catch (\Throwable $e) { |
|
41 | + $p->reject($e); |
|
42 | + } |
|
43 | + } |
|
44 | + }); |
|
45 | + return $p; |
|
46 | + } |
|
47 | + public function otherwise(callable $onRejected) : PromiseInterface |
|
48 | + { |
|
49 | + return $this->then(null, $onRejected); |
|
50 | + } |
|
51 | + public function wait(bool $unwrap = \true) |
|
52 | + { |
|
53 | + return $unwrap ? $this->value : null; |
|
54 | + } |
|
55 | + public function getState() : string |
|
56 | + { |
|
57 | + return self::FULFILLED; |
|
58 | + } |
|
59 | + public function resolve($value) : void |
|
60 | + { |
|
61 | + if ($value !== $this->value) { |
|
62 | + throw new \LogicException('Cannot resolve a fulfilled promise'); |
|
63 | + } |
|
64 | + } |
|
65 | + public function reject($reason) : void |
|
66 | + { |
|
67 | + throw new \LogicException('Cannot reject a fulfilled promise'); |
|
68 | + } |
|
69 | + public function cancel() : void |
|
70 | + { |
|
71 | + // pass |
|
72 | + } |
|
73 | 73 | } |
@@ -1,6 +1,6 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare (strict_types=1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | namespace OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\Promise; |
5 | 5 | |
6 | 6 | /** |
@@ -33,7 +33,7 @@ discard block |
||
33 | 33 | $queue = Utils::queue(); |
34 | 34 | $p = new Promise([$queue, 'run']); |
35 | 35 | $value = $this->value; |
36 | - $queue->add(static function () use($p, $value, $onFulfilled) : void { |
|
36 | + $queue->add(static function() use($p, $value, $onFulfilled) : void { |
|
37 | 37 | if (Is::pending($p)) { |
38 | 38 | try { |
39 | 39 | $p->resolve($onFulfilled($value)); |
@@ -11,8 +11,7 @@ |
||
11 | 11 | * |
12 | 12 | * @final |
13 | 13 | */ |
14 | -class FulfilledPromise implements PromiseInterface |
|
15 | -{ |
|
14 | +class FulfilledPromise implements PromiseInterface { |
|
16 | 15 | private $value; |
17 | 16 | /** |
18 | 17 | * @param mixed $value |
@@ -11,186 +11,186 @@ |
||
11 | 11 | */ |
12 | 12 | class EachPromise implements PromisorInterface |
13 | 13 | { |
14 | - private $pending = []; |
|
15 | - private $nextPendingIndex = 0; |
|
16 | - /** @var \Iterator|null */ |
|
17 | - private $iterable; |
|
18 | - /** @var callable|int|null */ |
|
19 | - private $concurrency; |
|
20 | - /** @var callable|null */ |
|
21 | - private $onFulfilled; |
|
22 | - /** @var callable|null */ |
|
23 | - private $onRejected; |
|
24 | - /** @var Promise|null */ |
|
25 | - private $aggregate; |
|
26 | - /** @var bool|null */ |
|
27 | - private $mutex; |
|
28 | - /** |
|
29 | - * Configuration hash can include the following key value pairs: |
|
30 | - * |
|
31 | - * - fulfilled: (callable) Invoked when a promise fulfills. The function |
|
32 | - * is invoked with three arguments: the fulfillment value, the index |
|
33 | - * position from the iterable list of the promise, and the aggregate |
|
34 | - * promise that manages all of the promises. The aggregate promise may |
|
35 | - * be resolved from within the callback to short-circuit the promise. |
|
36 | - * - rejected: (callable) Invoked when a promise is rejected. The |
|
37 | - * function is invoked with three arguments: the rejection reason, the |
|
38 | - * index position from the iterable list of the promise, and the |
|
39 | - * aggregate promise that manages all of the promises. The aggregate |
|
40 | - * promise may be resolved from within the callback to short-circuit |
|
41 | - * the promise. |
|
42 | - * - concurrency: (integer) Pass this configuration option to limit the |
|
43 | - * allowed number of outstanding concurrently executing promises, |
|
44 | - * creating a capped pool of promises. There is no limit by default. |
|
45 | - * |
|
46 | - * @param mixed $iterable Promises or values to iterate. |
|
47 | - * @param array $config Configuration options |
|
48 | - */ |
|
49 | - public function __construct($iterable, array $config = []) |
|
50 | - { |
|
51 | - $this->iterable = Create::iterFor($iterable); |
|
52 | - if (isset($config['concurrency'])) { |
|
53 | - $this->concurrency = $config['concurrency']; |
|
54 | - } |
|
55 | - if (isset($config['fulfilled'])) { |
|
56 | - $this->onFulfilled = $config['fulfilled']; |
|
57 | - } |
|
58 | - if (isset($config['rejected'])) { |
|
59 | - $this->onRejected = $config['rejected']; |
|
60 | - } |
|
61 | - } |
|
62 | - /** @psalm-suppress InvalidNullableReturnType */ |
|
63 | - public function promise() : PromiseInterface |
|
64 | - { |
|
65 | - if ($this->aggregate) { |
|
66 | - return $this->aggregate; |
|
67 | - } |
|
68 | - try { |
|
69 | - $this->createPromise(); |
|
70 | - /** @psalm-assert Promise $this->aggregate */ |
|
71 | - $this->iterable->rewind(); |
|
72 | - $this->refillPending(); |
|
73 | - } catch (\Throwable $e) { |
|
74 | - $this->aggregate->reject($e); |
|
75 | - } |
|
76 | - /** |
|
77 | - * @psalm-suppress NullableReturnStatement |
|
78 | - */ |
|
79 | - return $this->aggregate; |
|
80 | - } |
|
81 | - private function createPromise() : void |
|
82 | - { |
|
83 | - $this->mutex = \false; |
|
84 | - $this->aggregate = new Promise(function () : void { |
|
85 | - if ($this->checkIfFinished()) { |
|
86 | - return; |
|
87 | - } |
|
88 | - \reset($this->pending); |
|
89 | - // Consume a potentially fluctuating list of promises while |
|
90 | - // ensuring that indexes are maintained (precluding array_shift). |
|
91 | - while ($promise = \current($this->pending)) { |
|
92 | - \next($this->pending); |
|
93 | - $promise->wait(); |
|
94 | - if (Is::settled($this->aggregate)) { |
|
95 | - return; |
|
96 | - } |
|
97 | - } |
|
98 | - }); |
|
99 | - // Clear the references when the promise is resolved. |
|
100 | - $clearFn = function () : void { |
|
101 | - $this->iterable = $this->concurrency = $this->pending = null; |
|
102 | - $this->onFulfilled = $this->onRejected = null; |
|
103 | - $this->nextPendingIndex = 0; |
|
104 | - }; |
|
105 | - $this->aggregate->then($clearFn, $clearFn); |
|
106 | - } |
|
107 | - private function refillPending() : void |
|
108 | - { |
|
109 | - if (!$this->concurrency) { |
|
110 | - // Add all pending promises. |
|
111 | - while ($this->addPending() && $this->advanceIterator()) { |
|
112 | - } |
|
113 | - return; |
|
114 | - } |
|
115 | - // Add only up to N pending promises. |
|
116 | - $concurrency = \is_callable($this->concurrency) ? ($this->concurrency)(\count($this->pending)) : $this->concurrency; |
|
117 | - $concurrency = \max($concurrency - \count($this->pending), 0); |
|
118 | - // Concurrency may be set to 0 to disallow new promises. |
|
119 | - if (!$concurrency) { |
|
120 | - return; |
|
121 | - } |
|
122 | - // Add the first pending promise. |
|
123 | - $this->addPending(); |
|
124 | - // Note this is special handling for concurrency=1 so that we do |
|
125 | - // not advance the iterator after adding the first promise. This |
|
126 | - // helps work around issues with generators that might not have the |
|
127 | - // next value to yield until promise callbacks are called. |
|
128 | - while (--$concurrency && $this->advanceIterator() && $this->addPending()) { |
|
129 | - } |
|
130 | - } |
|
131 | - private function addPending() : bool |
|
132 | - { |
|
133 | - if (!$this->iterable || !$this->iterable->valid()) { |
|
134 | - return \false; |
|
135 | - } |
|
136 | - $promise = Create::promiseFor($this->iterable->current()); |
|
137 | - $key = $this->iterable->key(); |
|
138 | - // Iterable keys may not be unique, so we use a counter to |
|
139 | - // guarantee uniqueness |
|
140 | - $idx = $this->nextPendingIndex++; |
|
141 | - $this->pending[$idx] = $promise->then(function ($value) use($idx, $key) : void { |
|
142 | - if ($this->onFulfilled) { |
|
143 | - ($this->onFulfilled)($value, $key, $this->aggregate); |
|
144 | - } |
|
145 | - $this->step($idx); |
|
146 | - }, function ($reason) use($idx, $key) : void { |
|
147 | - if ($this->onRejected) { |
|
148 | - ($this->onRejected)($reason, $key, $this->aggregate); |
|
149 | - } |
|
150 | - $this->step($idx); |
|
151 | - }); |
|
152 | - return \true; |
|
153 | - } |
|
154 | - private function advanceIterator() : bool |
|
155 | - { |
|
156 | - // Place a lock on the iterator so that we ensure to not recurse, |
|
157 | - // preventing fatal generator errors. |
|
158 | - if ($this->mutex) { |
|
159 | - return \false; |
|
160 | - } |
|
161 | - $this->mutex = \true; |
|
162 | - try { |
|
163 | - $this->iterable->next(); |
|
164 | - $this->mutex = \false; |
|
165 | - return \true; |
|
166 | - } catch (\Throwable $e) { |
|
167 | - $this->aggregate->reject($e); |
|
168 | - $this->mutex = \false; |
|
169 | - return \false; |
|
170 | - } |
|
171 | - } |
|
172 | - private function step(int $idx) : void |
|
173 | - { |
|
174 | - // If the promise was already resolved, then ignore this step. |
|
175 | - if (Is::settled($this->aggregate)) { |
|
176 | - return; |
|
177 | - } |
|
178 | - unset($this->pending[$idx]); |
|
179 | - // Only refill pending promises if we are not locked, preventing the |
|
180 | - // EachPromise to recursively invoke the provided iterator, which |
|
181 | - // cause a fatal error: "Cannot resume an already running generator" |
|
182 | - if ($this->advanceIterator() && !$this->checkIfFinished()) { |
|
183 | - // Add more pending promises if possible. |
|
184 | - $this->refillPending(); |
|
185 | - } |
|
186 | - } |
|
187 | - private function checkIfFinished() : bool |
|
188 | - { |
|
189 | - if (!$this->pending && !$this->iterable->valid()) { |
|
190 | - // Resolve the promise if there's nothing left to do. |
|
191 | - $this->aggregate->resolve(null); |
|
192 | - return \true; |
|
193 | - } |
|
194 | - return \false; |
|
195 | - } |
|
14 | + private $pending = []; |
|
15 | + private $nextPendingIndex = 0; |
|
16 | + /** @var \Iterator|null */ |
|
17 | + private $iterable; |
|
18 | + /** @var callable|int|null */ |
|
19 | + private $concurrency; |
|
20 | + /** @var callable|null */ |
|
21 | + private $onFulfilled; |
|
22 | + /** @var callable|null */ |
|
23 | + private $onRejected; |
|
24 | + /** @var Promise|null */ |
|
25 | + private $aggregate; |
|
26 | + /** @var bool|null */ |
|
27 | + private $mutex; |
|
28 | + /** |
|
29 | + * Configuration hash can include the following key value pairs: |
|
30 | + * |
|
31 | + * - fulfilled: (callable) Invoked when a promise fulfills. The function |
|
32 | + * is invoked with three arguments: the fulfillment value, the index |
|
33 | + * position from the iterable list of the promise, and the aggregate |
|
34 | + * promise that manages all of the promises. The aggregate promise may |
|
35 | + * be resolved from within the callback to short-circuit the promise. |
|
36 | + * - rejected: (callable) Invoked when a promise is rejected. The |
|
37 | + * function is invoked with three arguments: the rejection reason, the |
|
38 | + * index position from the iterable list of the promise, and the |
|
39 | + * aggregate promise that manages all of the promises. The aggregate |
|
40 | + * promise may be resolved from within the callback to short-circuit |
|
41 | + * the promise. |
|
42 | + * - concurrency: (integer) Pass this configuration option to limit the |
|
43 | + * allowed number of outstanding concurrently executing promises, |
|
44 | + * creating a capped pool of promises. There is no limit by default. |
|
45 | + * |
|
46 | + * @param mixed $iterable Promises or values to iterate. |
|
47 | + * @param array $config Configuration options |
|
48 | + */ |
|
49 | + public function __construct($iterable, array $config = []) |
|
50 | + { |
|
51 | + $this->iterable = Create::iterFor($iterable); |
|
52 | + if (isset($config['concurrency'])) { |
|
53 | + $this->concurrency = $config['concurrency']; |
|
54 | + } |
|
55 | + if (isset($config['fulfilled'])) { |
|
56 | + $this->onFulfilled = $config['fulfilled']; |
|
57 | + } |
|
58 | + if (isset($config['rejected'])) { |
|
59 | + $this->onRejected = $config['rejected']; |
|
60 | + } |
|
61 | + } |
|
62 | + /** @psalm-suppress InvalidNullableReturnType */ |
|
63 | + public function promise() : PromiseInterface |
|
64 | + { |
|
65 | + if ($this->aggregate) { |
|
66 | + return $this->aggregate; |
|
67 | + } |
|
68 | + try { |
|
69 | + $this->createPromise(); |
|
70 | + /** @psalm-assert Promise $this->aggregate */ |
|
71 | + $this->iterable->rewind(); |
|
72 | + $this->refillPending(); |
|
73 | + } catch (\Throwable $e) { |
|
74 | + $this->aggregate->reject($e); |
|
75 | + } |
|
76 | + /** |
|
77 | + * @psalm-suppress NullableReturnStatement |
|
78 | + */ |
|
79 | + return $this->aggregate; |
|
80 | + } |
|
81 | + private function createPromise() : void |
|
82 | + { |
|
83 | + $this->mutex = \false; |
|
84 | + $this->aggregate = new Promise(function () : void { |
|
85 | + if ($this->checkIfFinished()) { |
|
86 | + return; |
|
87 | + } |
|
88 | + \reset($this->pending); |
|
89 | + // Consume a potentially fluctuating list of promises while |
|
90 | + // ensuring that indexes are maintained (precluding array_shift). |
|
91 | + while ($promise = \current($this->pending)) { |
|
92 | + \next($this->pending); |
|
93 | + $promise->wait(); |
|
94 | + if (Is::settled($this->aggregate)) { |
|
95 | + return; |
|
96 | + } |
|
97 | + } |
|
98 | + }); |
|
99 | + // Clear the references when the promise is resolved. |
|
100 | + $clearFn = function () : void { |
|
101 | + $this->iterable = $this->concurrency = $this->pending = null; |
|
102 | + $this->onFulfilled = $this->onRejected = null; |
|
103 | + $this->nextPendingIndex = 0; |
|
104 | + }; |
|
105 | + $this->aggregate->then($clearFn, $clearFn); |
|
106 | + } |
|
107 | + private function refillPending() : void |
|
108 | + { |
|
109 | + if (!$this->concurrency) { |
|
110 | + // Add all pending promises. |
|
111 | + while ($this->addPending() && $this->advanceIterator()) { |
|
112 | + } |
|
113 | + return; |
|
114 | + } |
|
115 | + // Add only up to N pending promises. |
|
116 | + $concurrency = \is_callable($this->concurrency) ? ($this->concurrency)(\count($this->pending)) : $this->concurrency; |
|
117 | + $concurrency = \max($concurrency - \count($this->pending), 0); |
|
118 | + // Concurrency may be set to 0 to disallow new promises. |
|
119 | + if (!$concurrency) { |
|
120 | + return; |
|
121 | + } |
|
122 | + // Add the first pending promise. |
|
123 | + $this->addPending(); |
|
124 | + // Note this is special handling for concurrency=1 so that we do |
|
125 | + // not advance the iterator after adding the first promise. This |
|
126 | + // helps work around issues with generators that might not have the |
|
127 | + // next value to yield until promise callbacks are called. |
|
128 | + while (--$concurrency && $this->advanceIterator() && $this->addPending()) { |
|
129 | + } |
|
130 | + } |
|
131 | + private function addPending() : bool |
|
132 | + { |
|
133 | + if (!$this->iterable || !$this->iterable->valid()) { |
|
134 | + return \false; |
|
135 | + } |
|
136 | + $promise = Create::promiseFor($this->iterable->current()); |
|
137 | + $key = $this->iterable->key(); |
|
138 | + // Iterable keys may not be unique, so we use a counter to |
|
139 | + // guarantee uniqueness |
|
140 | + $idx = $this->nextPendingIndex++; |
|
141 | + $this->pending[$idx] = $promise->then(function ($value) use($idx, $key) : void { |
|
142 | + if ($this->onFulfilled) { |
|
143 | + ($this->onFulfilled)($value, $key, $this->aggregate); |
|
144 | + } |
|
145 | + $this->step($idx); |
|
146 | + }, function ($reason) use($idx, $key) : void { |
|
147 | + if ($this->onRejected) { |
|
148 | + ($this->onRejected)($reason, $key, $this->aggregate); |
|
149 | + } |
|
150 | + $this->step($idx); |
|
151 | + }); |
|
152 | + return \true; |
|
153 | + } |
|
154 | + private function advanceIterator() : bool |
|
155 | + { |
|
156 | + // Place a lock on the iterator so that we ensure to not recurse, |
|
157 | + // preventing fatal generator errors. |
|
158 | + if ($this->mutex) { |
|
159 | + return \false; |
|
160 | + } |
|
161 | + $this->mutex = \true; |
|
162 | + try { |
|
163 | + $this->iterable->next(); |
|
164 | + $this->mutex = \false; |
|
165 | + return \true; |
|
166 | + } catch (\Throwable $e) { |
|
167 | + $this->aggregate->reject($e); |
|
168 | + $this->mutex = \false; |
|
169 | + return \false; |
|
170 | + } |
|
171 | + } |
|
172 | + private function step(int $idx) : void |
|
173 | + { |
|
174 | + // If the promise was already resolved, then ignore this step. |
|
175 | + if (Is::settled($this->aggregate)) { |
|
176 | + return; |
|
177 | + } |
|
178 | + unset($this->pending[$idx]); |
|
179 | + // Only refill pending promises if we are not locked, preventing the |
|
180 | + // EachPromise to recursively invoke the provided iterator, which |
|
181 | + // cause a fatal error: "Cannot resume an already running generator" |
|
182 | + if ($this->advanceIterator() && !$this->checkIfFinished()) { |
|
183 | + // Add more pending promises if possible. |
|
184 | + $this->refillPending(); |
|
185 | + } |
|
186 | + } |
|
187 | + private function checkIfFinished() : bool |
|
188 | + { |
|
189 | + if (!$this->pending && !$this->iterable->valid()) { |
|
190 | + // Resolve the promise if there's nothing left to do. |
|
191 | + $this->aggregate->resolve(null); |
|
192 | + return \true; |
|
193 | + } |
|
194 | + return \false; |
|
195 | + } |
|
196 | 196 | } |
@@ -1,6 +1,6 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare (strict_types=1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | namespace OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\Promise; |
5 | 5 | |
6 | 6 | /** |
@@ -81,7 +81,7 @@ discard block |
||
81 | 81 | private function createPromise() : void |
82 | 82 | { |
83 | 83 | $this->mutex = \false; |
84 | - $this->aggregate = new Promise(function () : void { |
|
84 | + $this->aggregate = new Promise(function() : void { |
|
85 | 85 | if ($this->checkIfFinished()) { |
86 | 86 | return; |
87 | 87 | } |
@@ -97,7 +97,7 @@ discard block |
||
97 | 97 | } |
98 | 98 | }); |
99 | 99 | // Clear the references when the promise is resolved. |
100 | - $clearFn = function () : void { |
|
100 | + $clearFn = function() : void { |
|
101 | 101 | $this->iterable = $this->concurrency = $this->pending = null; |
102 | 102 | $this->onFulfilled = $this->onRejected = null; |
103 | 103 | $this->nextPendingIndex = 0; |
@@ -138,12 +138,12 @@ discard block |
||
138 | 138 | // Iterable keys may not be unique, so we use a counter to |
139 | 139 | // guarantee uniqueness |
140 | 140 | $idx = $this->nextPendingIndex++; |
141 | - $this->pending[$idx] = $promise->then(function ($value) use($idx, $key) : void { |
|
141 | + $this->pending[$idx] = $promise->then(function($value) use($idx, $key) : void { |
|
142 | 142 | if ($this->onFulfilled) { |
143 | 143 | ($this->onFulfilled)($value, $key, $this->aggregate); |
144 | 144 | } |
145 | 145 | $this->step($idx); |
146 | - }, function ($reason) use($idx, $key) : void { |
|
146 | + }, function($reason) use($idx, $key) : void { |
|
147 | 147 | if ($this->onRejected) { |
148 | 148 | ($this->onRejected)($reason, $key, $this->aggregate); |
149 | 149 | } |
@@ -9,8 +9,7 @@ |
||
9 | 9 | * |
10 | 10 | * @final |
11 | 11 | */ |
12 | -class EachPromise implements PromisorInterface |
|
13 | -{ |
|
12 | +class EachPromise implements PromisorInterface { |
|
14 | 13 | private $pending = []; |
15 | 14 | private $nextPendingIndex = 0; |
16 | 15 | /** @var \Iterator|null */ |
@@ -5,32 +5,32 @@ |
||
5 | 5 | |
6 | 6 | final class Is |
7 | 7 | { |
8 | - /** |
|
9 | - * Returns true if a promise is pending. |
|
10 | - */ |
|
11 | - public static function pending(PromiseInterface $promise) : bool |
|
12 | - { |
|
13 | - return $promise->getState() === PromiseInterface::PENDING; |
|
14 | - } |
|
15 | - /** |
|
16 | - * Returns true if a promise is fulfilled or rejected. |
|
17 | - */ |
|
18 | - public static function settled(PromiseInterface $promise) : bool |
|
19 | - { |
|
20 | - return $promise->getState() !== PromiseInterface::PENDING; |
|
21 | - } |
|
22 | - /** |
|
23 | - * Returns true if a promise is fulfilled. |
|
24 | - */ |
|
25 | - public static function fulfilled(PromiseInterface $promise) : bool |
|
26 | - { |
|
27 | - return $promise->getState() === PromiseInterface::FULFILLED; |
|
28 | - } |
|
29 | - /** |
|
30 | - * Returns true if a promise is rejected. |
|
31 | - */ |
|
32 | - public static function rejected(PromiseInterface $promise) : bool |
|
33 | - { |
|
34 | - return $promise->getState() === PromiseInterface::REJECTED; |
|
35 | - } |
|
8 | + /** |
|
9 | + * Returns true if a promise is pending. |
|
10 | + */ |
|
11 | + public static function pending(PromiseInterface $promise) : bool |
|
12 | + { |
|
13 | + return $promise->getState() === PromiseInterface::PENDING; |
|
14 | + } |
|
15 | + /** |
|
16 | + * Returns true if a promise is fulfilled or rejected. |
|
17 | + */ |
|
18 | + public static function settled(PromiseInterface $promise) : bool |
|
19 | + { |
|
20 | + return $promise->getState() !== PromiseInterface::PENDING; |
|
21 | + } |
|
22 | + /** |
|
23 | + * Returns true if a promise is fulfilled. |
|
24 | + */ |
|
25 | + public static function fulfilled(PromiseInterface $promise) : bool |
|
26 | + { |
|
27 | + return $promise->getState() === PromiseInterface::FULFILLED; |
|
28 | + } |
|
29 | + /** |
|
30 | + * Returns true if a promise is rejected. |
|
31 | + */ |
|
32 | + public static function rejected(PromiseInterface $promise) : bool |
|
33 | + { |
|
34 | + return $promise->getState() === PromiseInterface::REJECTED; |
|
35 | + } |
|
36 | 36 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare (strict_types=1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | namespace OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\Promise; |
5 | 5 | |
6 | 6 | final class Is |
@@ -3,8 +3,7 @@ |
||
3 | 3 | declare (strict_types=1); |
4 | 4 | namespace OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\Promise; |
5 | 5 | |
6 | -final class Is |
|
7 | -{ |
|
6 | +final class Is { |
|
8 | 7 | /** |
9 | 8 | * Returns true if a promise is pending. |
10 | 9 | */ |
@@ -13,66 +13,66 @@ |
||
13 | 13 | */ |
14 | 14 | class RejectedPromise implements PromiseInterface |
15 | 15 | { |
16 | - private $reason; |
|
17 | - /** |
|
18 | - * @param mixed $reason |
|
19 | - */ |
|
20 | - public function __construct($reason) |
|
21 | - { |
|
22 | - if (\is_object($reason) && \method_exists($reason, 'then')) { |
|
23 | - throw new \InvalidArgumentException('You cannot create a RejectedPromise with a promise.'); |
|
24 | - } |
|
25 | - $this->reason = $reason; |
|
26 | - } |
|
27 | - public function then(callable $onFulfilled = null, callable $onRejected = null) : PromiseInterface |
|
28 | - { |
|
29 | - // If there's no onRejected callback then just return self. |
|
30 | - if (!$onRejected) { |
|
31 | - return $this; |
|
32 | - } |
|
33 | - $queue = Utils::queue(); |
|
34 | - $reason = $this->reason; |
|
35 | - $p = new Promise([$queue, 'run']); |
|
36 | - $queue->add(static function () use($p, $reason, $onRejected) : void { |
|
37 | - if (Is::pending($p)) { |
|
38 | - try { |
|
39 | - // Return a resolved promise if onRejected does not throw. |
|
40 | - $p->resolve($onRejected($reason)); |
|
41 | - } catch (\Throwable $e) { |
|
42 | - // onRejected threw, so return a rejected promise. |
|
43 | - $p->reject($e); |
|
44 | - } |
|
45 | - } |
|
46 | - }); |
|
47 | - return $p; |
|
48 | - } |
|
49 | - public function otherwise(callable $onRejected) : PromiseInterface |
|
50 | - { |
|
51 | - return $this->then(null, $onRejected); |
|
52 | - } |
|
53 | - public function wait(bool $unwrap = \true) |
|
54 | - { |
|
55 | - if ($unwrap) { |
|
56 | - throw Create::exceptionFor($this->reason); |
|
57 | - } |
|
58 | - return null; |
|
59 | - } |
|
60 | - public function getState() : string |
|
61 | - { |
|
62 | - return self::REJECTED; |
|
63 | - } |
|
64 | - public function resolve($value) : void |
|
65 | - { |
|
66 | - throw new \LogicException('Cannot resolve a rejected promise'); |
|
67 | - } |
|
68 | - public function reject($reason) : void |
|
69 | - { |
|
70 | - if ($reason !== $this->reason) { |
|
71 | - throw new \LogicException('Cannot reject a rejected promise'); |
|
72 | - } |
|
73 | - } |
|
74 | - public function cancel() : void |
|
75 | - { |
|
76 | - // pass |
|
77 | - } |
|
16 | + private $reason; |
|
17 | + /** |
|
18 | + * @param mixed $reason |
|
19 | + */ |
|
20 | + public function __construct($reason) |
|
21 | + { |
|
22 | + if (\is_object($reason) && \method_exists($reason, 'then')) { |
|
23 | + throw new \InvalidArgumentException('You cannot create a RejectedPromise with a promise.'); |
|
24 | + } |
|
25 | + $this->reason = $reason; |
|
26 | + } |
|
27 | + public function then(callable $onFulfilled = null, callable $onRejected = null) : PromiseInterface |
|
28 | + { |
|
29 | + // If there's no onRejected callback then just return self. |
|
30 | + if (!$onRejected) { |
|
31 | + return $this; |
|
32 | + } |
|
33 | + $queue = Utils::queue(); |
|
34 | + $reason = $this->reason; |
|
35 | + $p = new Promise([$queue, 'run']); |
|
36 | + $queue->add(static function () use($p, $reason, $onRejected) : void { |
|
37 | + if (Is::pending($p)) { |
|
38 | + try { |
|
39 | + // Return a resolved promise if onRejected does not throw. |
|
40 | + $p->resolve($onRejected($reason)); |
|
41 | + } catch (\Throwable $e) { |
|
42 | + // onRejected threw, so return a rejected promise. |
|
43 | + $p->reject($e); |
|
44 | + } |
|
45 | + } |
|
46 | + }); |
|
47 | + return $p; |
|
48 | + } |
|
49 | + public function otherwise(callable $onRejected) : PromiseInterface |
|
50 | + { |
|
51 | + return $this->then(null, $onRejected); |
|
52 | + } |
|
53 | + public function wait(bool $unwrap = \true) |
|
54 | + { |
|
55 | + if ($unwrap) { |
|
56 | + throw Create::exceptionFor($this->reason); |
|
57 | + } |
|
58 | + return null; |
|
59 | + } |
|
60 | + public function getState() : string |
|
61 | + { |
|
62 | + return self::REJECTED; |
|
63 | + } |
|
64 | + public function resolve($value) : void |
|
65 | + { |
|
66 | + throw new \LogicException('Cannot resolve a rejected promise'); |
|
67 | + } |
|
68 | + public function reject($reason) : void |
|
69 | + { |
|
70 | + if ($reason !== $this->reason) { |
|
71 | + throw new \LogicException('Cannot reject a rejected promise'); |
|
72 | + } |
|
73 | + } |
|
74 | + public function cancel() : void |
|
75 | + { |
|
76 | + // pass |
|
77 | + } |
|
78 | 78 | } |
@@ -1,6 +1,6 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare (strict_types=1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | namespace OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\Promise; |
5 | 5 | |
6 | 6 | /** |
@@ -33,7 +33,7 @@ discard block |
||
33 | 33 | $queue = Utils::queue(); |
34 | 34 | $reason = $this->reason; |
35 | 35 | $p = new Promise([$queue, 'run']); |
36 | - $queue->add(static function () use($p, $reason, $onRejected) : void { |
|
36 | + $queue->add(static function() use($p, $reason, $onRejected) : void { |
|
37 | 37 | if (Is::pending($p)) { |
38 | 38 | try { |
39 | 39 | // Return a resolved promise if onRejected does not throw. |
@@ -11,8 +11,7 @@ |
||
11 | 11 | * |
12 | 12 | * @final |
13 | 13 | */ |
14 | -class RejectedPromise implements PromiseInterface |
|
15 | -{ |
|
14 | +class RejectedPromise implements PromiseInterface { |
|
16 | 15 | private $reason; |
17 | 16 | /** |
18 | 17 | * @param mixed $reason |