@@ -7,6 +7,5 @@ |
||
| 7 | 7 | /** |
| 8 | 8 | * Exception that is set as the reason for a promise that has been cancelled. |
| 9 | 9 | */ |
| 10 | -class CancellationException extends RejectionException |
|
| 11 | -{ |
|
| 10 | +class CancellationException extends RejectionException { |
|
| 12 | 11 | } |
@@ -12,237 +12,237 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | class EachPromise implements PromisorInterface |
| 14 | 14 | { |
| 15 | - private $pending = []; |
|
| 16 | - |
|
| 17 | - private $nextPendingIndex = 0; |
|
| 18 | - |
|
| 19 | - /** @var \Iterator|null */ |
|
| 20 | - private $iterable; |
|
| 21 | - |
|
| 22 | - /** @var callable|int|null */ |
|
| 23 | - private $concurrency; |
|
| 24 | - |
|
| 25 | - /** @var callable|null */ |
|
| 26 | - private $onFulfilled; |
|
| 27 | - |
|
| 28 | - /** @var callable|null */ |
|
| 29 | - private $onRejected; |
|
| 30 | - |
|
| 31 | - /** @var Promise|null */ |
|
| 32 | - private $aggregate; |
|
| 33 | - |
|
| 34 | - /** @var bool|null */ |
|
| 35 | - private $mutex; |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * Configuration hash can include the following key value pairs: |
|
| 39 | - * |
|
| 40 | - * - fulfilled: (callable) Invoked when a promise fulfills. The function |
|
| 41 | - * is invoked with three arguments: the fulfillment value, the index |
|
| 42 | - * position from the iterable list of the promise, and the aggregate |
|
| 43 | - * promise that manages all of the promises. The aggregate promise may |
|
| 44 | - * be resolved from within the callback to short-circuit the promise. |
|
| 45 | - * - rejected: (callable) Invoked when a promise is rejected. The |
|
| 46 | - * function is invoked with three arguments: the rejection reason, the |
|
| 47 | - * index position from the iterable list of the promise, and the |
|
| 48 | - * aggregate promise that manages all of the promises. The aggregate |
|
| 49 | - * promise may be resolved from within the callback to short-circuit |
|
| 50 | - * the promise. |
|
| 51 | - * - concurrency: (integer) Pass this configuration option to limit the |
|
| 52 | - * allowed number of outstanding concurrently executing promises, |
|
| 53 | - * creating a capped pool of promises. There is no limit by default. |
|
| 54 | - * |
|
| 55 | - * @param mixed $iterable Promises or values to iterate. |
|
| 56 | - * @param array $config Configuration options |
|
| 57 | - */ |
|
| 58 | - public function __construct($iterable, array $config = []) |
|
| 59 | - { |
|
| 60 | - $this->iterable = Create::iterFor($iterable); |
|
| 61 | - |
|
| 62 | - if (isset($config['concurrency'])) { |
|
| 63 | - $this->concurrency = $config['concurrency']; |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - if (isset($config['fulfilled'])) { |
|
| 67 | - $this->onFulfilled = $config['fulfilled']; |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - if (isset($config['rejected'])) { |
|
| 71 | - $this->onRejected = $config['rejected']; |
|
| 72 | - } |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - /** @psalm-suppress InvalidNullableReturnType */ |
|
| 76 | - public function promise(): PromiseInterface |
|
| 77 | - { |
|
| 78 | - if ($this->aggregate) { |
|
| 79 | - return $this->aggregate; |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - try { |
|
| 83 | - $this->createPromise(); |
|
| 84 | - /** @psalm-assert Promise $this->aggregate */ |
|
| 85 | - $this->iterable->rewind(); |
|
| 86 | - $this->refillPending(); |
|
| 87 | - } catch (\Throwable $e) { |
|
| 88 | - $this->aggregate->reject($e); |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - /** |
|
| 92 | - * @psalm-suppress NullableReturnStatement |
|
| 93 | - */ |
|
| 94 | - return $this->aggregate; |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - private function createPromise(): void |
|
| 98 | - { |
|
| 99 | - $this->mutex = false; |
|
| 100 | - $this->aggregate = new Promise(function (): void { |
|
| 101 | - if ($this->checkIfFinished()) { |
|
| 102 | - return; |
|
| 103 | - } |
|
| 104 | - reset($this->pending); |
|
| 105 | - // Consume a potentially fluctuating list of promises while |
|
| 106 | - // ensuring that indexes are maintained (precluding array_shift). |
|
| 107 | - while ($promise = current($this->pending)) { |
|
| 108 | - next($this->pending); |
|
| 109 | - $promise->wait(); |
|
| 110 | - if (Is::settled($this->aggregate)) { |
|
| 111 | - return; |
|
| 112 | - } |
|
| 113 | - } |
|
| 114 | - }); |
|
| 115 | - |
|
| 116 | - // Clear the references when the promise is resolved. |
|
| 117 | - $clearFn = function (): void { |
|
| 118 | - $this->iterable = $this->concurrency = $this->pending = null; |
|
| 119 | - $this->onFulfilled = $this->onRejected = null; |
|
| 120 | - $this->nextPendingIndex = 0; |
|
| 121 | - }; |
|
| 122 | - |
|
| 123 | - $this->aggregate->then($clearFn, $clearFn); |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - private function refillPending(): void |
|
| 127 | - { |
|
| 128 | - if (!$this->concurrency) { |
|
| 129 | - // Add all pending promises. |
|
| 130 | - while ($this->addPending() && $this->advanceIterator()) { |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - return; |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - // Add only up to N pending promises. |
|
| 137 | - $concurrency = is_callable($this->concurrency) |
|
| 138 | - ? ($this->concurrency)(count($this->pending)) |
|
| 139 | - : $this->concurrency; |
|
| 140 | - $concurrency = max($concurrency - count($this->pending), 0); |
|
| 141 | - // Concurrency may be set to 0 to disallow new promises. |
|
| 142 | - if (!$concurrency) { |
|
| 143 | - return; |
|
| 144 | - } |
|
| 145 | - // Add the first pending promise. |
|
| 146 | - $this->addPending(); |
|
| 147 | - // Note this is special handling for concurrency=1 so that we do |
|
| 148 | - // not advance the iterator after adding the first promise. This |
|
| 149 | - // helps work around issues with generators that might not have the |
|
| 150 | - // next value to yield until promise callbacks are called. |
|
| 151 | - while (--$concurrency |
|
| 152 | - && $this->advanceIterator() |
|
| 153 | - && $this->addPending()) { |
|
| 154 | - } |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - private function addPending(): bool |
|
| 158 | - { |
|
| 159 | - if (!$this->iterable || !$this->iterable->valid()) { |
|
| 160 | - return false; |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - $promise = Create::promiseFor($this->iterable->current()); |
|
| 164 | - $key = $this->iterable->key(); |
|
| 165 | - |
|
| 166 | - // Iterable keys may not be unique, so we use a counter to |
|
| 167 | - // guarantee uniqueness |
|
| 168 | - $idx = $this->nextPendingIndex++; |
|
| 169 | - |
|
| 170 | - $this->pending[$idx] = $promise->then( |
|
| 171 | - function ($value) use ($idx, $key): void { |
|
| 172 | - if ($this->onFulfilled) { |
|
| 173 | - ($this->onFulfilled)( |
|
| 174 | - $value, |
|
| 175 | - $key, |
|
| 176 | - $this->aggregate |
|
| 177 | - ); |
|
| 178 | - } |
|
| 179 | - $this->step($idx); |
|
| 180 | - }, |
|
| 181 | - function ($reason) use ($idx, $key): void { |
|
| 182 | - if ($this->onRejected) { |
|
| 183 | - ($this->onRejected)( |
|
| 184 | - $reason, |
|
| 185 | - $key, |
|
| 186 | - $this->aggregate |
|
| 187 | - ); |
|
| 188 | - } |
|
| 189 | - $this->step($idx); |
|
| 190 | - } |
|
| 191 | - ); |
|
| 192 | - |
|
| 193 | - return true; |
|
| 194 | - } |
|
| 195 | - |
|
| 196 | - private function advanceIterator(): bool |
|
| 197 | - { |
|
| 198 | - // Place a lock on the iterator so that we ensure to not recurse, |
|
| 199 | - // preventing fatal generator errors. |
|
| 200 | - if ($this->mutex) { |
|
| 201 | - return false; |
|
| 202 | - } |
|
| 203 | - |
|
| 204 | - $this->mutex = true; |
|
| 205 | - |
|
| 206 | - try { |
|
| 207 | - $this->iterable->next(); |
|
| 208 | - $this->mutex = false; |
|
| 209 | - |
|
| 210 | - return true; |
|
| 211 | - } catch (\Throwable $e) { |
|
| 212 | - $this->aggregate->reject($e); |
|
| 213 | - $this->mutex = false; |
|
| 214 | - |
|
| 215 | - return false; |
|
| 216 | - } |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - private function step(int $idx): void |
|
| 220 | - { |
|
| 221 | - // If the promise was already resolved, then ignore this step. |
|
| 222 | - if (Is::settled($this->aggregate)) { |
|
| 223 | - return; |
|
| 224 | - } |
|
| 225 | - |
|
| 226 | - unset($this->pending[$idx]); |
|
| 227 | - |
|
| 228 | - // Only refill pending promises if we are not locked, preventing the |
|
| 229 | - // EachPromise to recursively invoke the provided iterator, which |
|
| 230 | - // cause a fatal error: "Cannot resume an already running generator" |
|
| 231 | - if ($this->advanceIterator() && !$this->checkIfFinished()) { |
|
| 232 | - // Add more pending promises if possible. |
|
| 233 | - $this->refillPending(); |
|
| 234 | - } |
|
| 235 | - } |
|
| 236 | - |
|
| 237 | - private function checkIfFinished(): bool |
|
| 238 | - { |
|
| 239 | - if (!$this->pending && !$this->iterable->valid()) { |
|
| 240 | - // Resolve the promise if there's nothing left to do. |
|
| 241 | - $this->aggregate->resolve(null); |
|
| 242 | - |
|
| 243 | - return true; |
|
| 244 | - } |
|
| 245 | - |
|
| 246 | - return false; |
|
| 247 | - } |
|
| 15 | + private $pending = []; |
|
| 16 | + |
|
| 17 | + private $nextPendingIndex = 0; |
|
| 18 | + |
|
| 19 | + /** @var \Iterator|null */ |
|
| 20 | + private $iterable; |
|
| 21 | + |
|
| 22 | + /** @var callable|int|null */ |
|
| 23 | + private $concurrency; |
|
| 24 | + |
|
| 25 | + /** @var callable|null */ |
|
| 26 | + private $onFulfilled; |
|
| 27 | + |
|
| 28 | + /** @var callable|null */ |
|
| 29 | + private $onRejected; |
|
| 30 | + |
|
| 31 | + /** @var Promise|null */ |
|
| 32 | + private $aggregate; |
|
| 33 | + |
|
| 34 | + /** @var bool|null */ |
|
| 35 | + private $mutex; |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * Configuration hash can include the following key value pairs: |
|
| 39 | + * |
|
| 40 | + * - fulfilled: (callable) Invoked when a promise fulfills. The function |
|
| 41 | + * is invoked with three arguments: the fulfillment value, the index |
|
| 42 | + * position from the iterable list of the promise, and the aggregate |
|
| 43 | + * promise that manages all of the promises. The aggregate promise may |
|
| 44 | + * be resolved from within the callback to short-circuit the promise. |
|
| 45 | + * - rejected: (callable) Invoked when a promise is rejected. The |
|
| 46 | + * function is invoked with three arguments: the rejection reason, the |
|
| 47 | + * index position from the iterable list of the promise, and the |
|
| 48 | + * aggregate promise that manages all of the promises. The aggregate |
|
| 49 | + * promise may be resolved from within the callback to short-circuit |
|
| 50 | + * the promise. |
|
| 51 | + * - concurrency: (integer) Pass this configuration option to limit the |
|
| 52 | + * allowed number of outstanding concurrently executing promises, |
|
| 53 | + * creating a capped pool of promises. There is no limit by default. |
|
| 54 | + * |
|
| 55 | + * @param mixed $iterable Promises or values to iterate. |
|
| 56 | + * @param array $config Configuration options |
|
| 57 | + */ |
|
| 58 | + public function __construct($iterable, array $config = []) |
|
| 59 | + { |
|
| 60 | + $this->iterable = Create::iterFor($iterable); |
|
| 61 | + |
|
| 62 | + if (isset($config['concurrency'])) { |
|
| 63 | + $this->concurrency = $config['concurrency']; |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + if (isset($config['fulfilled'])) { |
|
| 67 | + $this->onFulfilled = $config['fulfilled']; |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + if (isset($config['rejected'])) { |
|
| 71 | + $this->onRejected = $config['rejected']; |
|
| 72 | + } |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + /** @psalm-suppress InvalidNullableReturnType */ |
|
| 76 | + public function promise(): PromiseInterface |
|
| 77 | + { |
|
| 78 | + if ($this->aggregate) { |
|
| 79 | + return $this->aggregate; |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + try { |
|
| 83 | + $this->createPromise(); |
|
| 84 | + /** @psalm-assert Promise $this->aggregate */ |
|
| 85 | + $this->iterable->rewind(); |
|
| 86 | + $this->refillPending(); |
|
| 87 | + } catch (\Throwable $e) { |
|
| 88 | + $this->aggregate->reject($e); |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + /** |
|
| 92 | + * @psalm-suppress NullableReturnStatement |
|
| 93 | + */ |
|
| 94 | + return $this->aggregate; |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + private function createPromise(): void |
|
| 98 | + { |
|
| 99 | + $this->mutex = false; |
|
| 100 | + $this->aggregate = new Promise(function (): void { |
|
| 101 | + if ($this->checkIfFinished()) { |
|
| 102 | + return; |
|
| 103 | + } |
|
| 104 | + reset($this->pending); |
|
| 105 | + // Consume a potentially fluctuating list of promises while |
|
| 106 | + // ensuring that indexes are maintained (precluding array_shift). |
|
| 107 | + while ($promise = current($this->pending)) { |
|
| 108 | + next($this->pending); |
|
| 109 | + $promise->wait(); |
|
| 110 | + if (Is::settled($this->aggregate)) { |
|
| 111 | + return; |
|
| 112 | + } |
|
| 113 | + } |
|
| 114 | + }); |
|
| 115 | + |
|
| 116 | + // Clear the references when the promise is resolved. |
|
| 117 | + $clearFn = function (): void { |
|
| 118 | + $this->iterable = $this->concurrency = $this->pending = null; |
|
| 119 | + $this->onFulfilled = $this->onRejected = null; |
|
| 120 | + $this->nextPendingIndex = 0; |
|
| 121 | + }; |
|
| 122 | + |
|
| 123 | + $this->aggregate->then($clearFn, $clearFn); |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + private function refillPending(): void |
|
| 127 | + { |
|
| 128 | + if (!$this->concurrency) { |
|
| 129 | + // Add all pending promises. |
|
| 130 | + while ($this->addPending() && $this->advanceIterator()) { |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + return; |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + // Add only up to N pending promises. |
|
| 137 | + $concurrency = is_callable($this->concurrency) |
|
| 138 | + ? ($this->concurrency)(count($this->pending)) |
|
| 139 | + : $this->concurrency; |
|
| 140 | + $concurrency = max($concurrency - count($this->pending), 0); |
|
| 141 | + // Concurrency may be set to 0 to disallow new promises. |
|
| 142 | + if (!$concurrency) { |
|
| 143 | + return; |
|
| 144 | + } |
|
| 145 | + // Add the first pending promise. |
|
| 146 | + $this->addPending(); |
|
| 147 | + // Note this is special handling for concurrency=1 so that we do |
|
| 148 | + // not advance the iterator after adding the first promise. This |
|
| 149 | + // helps work around issues with generators that might not have the |
|
| 150 | + // next value to yield until promise callbacks are called. |
|
| 151 | + while (--$concurrency |
|
| 152 | + && $this->advanceIterator() |
|
| 153 | + && $this->addPending()) { |
|
| 154 | + } |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + private function addPending(): bool |
|
| 158 | + { |
|
| 159 | + if (!$this->iterable || !$this->iterable->valid()) { |
|
| 160 | + return false; |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + $promise = Create::promiseFor($this->iterable->current()); |
|
| 164 | + $key = $this->iterable->key(); |
|
| 165 | + |
|
| 166 | + // Iterable keys may not be unique, so we use a counter to |
|
| 167 | + // guarantee uniqueness |
|
| 168 | + $idx = $this->nextPendingIndex++; |
|
| 169 | + |
|
| 170 | + $this->pending[$idx] = $promise->then( |
|
| 171 | + function ($value) use ($idx, $key): void { |
|
| 172 | + if ($this->onFulfilled) { |
|
| 173 | + ($this->onFulfilled)( |
|
| 174 | + $value, |
|
| 175 | + $key, |
|
| 176 | + $this->aggregate |
|
| 177 | + ); |
|
| 178 | + } |
|
| 179 | + $this->step($idx); |
|
| 180 | + }, |
|
| 181 | + function ($reason) use ($idx, $key): void { |
|
| 182 | + if ($this->onRejected) { |
|
| 183 | + ($this->onRejected)( |
|
| 184 | + $reason, |
|
| 185 | + $key, |
|
| 186 | + $this->aggregate |
|
| 187 | + ); |
|
| 188 | + } |
|
| 189 | + $this->step($idx); |
|
| 190 | + } |
|
| 191 | + ); |
|
| 192 | + |
|
| 193 | + return true; |
|
| 194 | + } |
|
| 195 | + |
|
| 196 | + private function advanceIterator(): bool |
|
| 197 | + { |
|
| 198 | + // Place a lock on the iterator so that we ensure to not recurse, |
|
| 199 | + // preventing fatal generator errors. |
|
| 200 | + if ($this->mutex) { |
|
| 201 | + return false; |
|
| 202 | + } |
|
| 203 | + |
|
| 204 | + $this->mutex = true; |
|
| 205 | + |
|
| 206 | + try { |
|
| 207 | + $this->iterable->next(); |
|
| 208 | + $this->mutex = false; |
|
| 209 | + |
|
| 210 | + return true; |
|
| 211 | + } catch (\Throwable $e) { |
|
| 212 | + $this->aggregate->reject($e); |
|
| 213 | + $this->mutex = false; |
|
| 214 | + |
|
| 215 | + return false; |
|
| 216 | + } |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + private function step(int $idx): void |
|
| 220 | + { |
|
| 221 | + // If the promise was already resolved, then ignore this step. |
|
| 222 | + if (Is::settled($this->aggregate)) { |
|
| 223 | + return; |
|
| 224 | + } |
|
| 225 | + |
|
| 226 | + unset($this->pending[$idx]); |
|
| 227 | + |
|
| 228 | + // Only refill pending promises if we are not locked, preventing the |
|
| 229 | + // EachPromise to recursively invoke the provided iterator, which |
|
| 230 | + // cause a fatal error: "Cannot resume an already running generator" |
|
| 231 | + if ($this->advanceIterator() && !$this->checkIfFinished()) { |
|
| 232 | + // Add more pending promises if possible. |
|
| 233 | + $this->refillPending(); |
|
| 234 | + } |
|
| 235 | + } |
|
| 236 | + |
|
| 237 | + private function checkIfFinished(): bool |
|
| 238 | + { |
|
| 239 | + if (!$this->pending && !$this->iterable->valid()) { |
|
| 240 | + // Resolve the promise if there's nothing left to do. |
|
| 241 | + $this->aggregate->resolve(null); |
|
| 242 | + |
|
| 243 | + return true; |
|
| 244 | + } |
|
| 245 | + |
|
| 246 | + return false; |
|
| 247 | + } |
|
| 248 | 248 | } |
@@ -97,7 +97,7 @@ discard block |
||
| 97 | 97 | private function createPromise(): void |
| 98 | 98 | { |
| 99 | 99 | $this->mutex = false; |
| 100 | - $this->aggregate = new Promise(function (): void { |
|
| 100 | + $this->aggregate = new Promise(function(): void { |
|
| 101 | 101 | if ($this->checkIfFinished()) { |
| 102 | 102 | return; |
| 103 | 103 | } |
@@ -114,7 +114,7 @@ discard block |
||
| 114 | 114 | }); |
| 115 | 115 | |
| 116 | 116 | // Clear the references when the promise is resolved. |
| 117 | - $clearFn = function (): void { |
|
| 117 | + $clearFn = function(): void { |
|
| 118 | 118 | $this->iterable = $this->concurrency = $this->pending = null; |
| 119 | 119 | $this->onFulfilled = $this->onRejected = null; |
| 120 | 120 | $this->nextPendingIndex = 0; |
@@ -168,7 +168,7 @@ discard block |
||
| 168 | 168 | $idx = $this->nextPendingIndex++; |
| 169 | 169 | |
| 170 | 170 | $this->pending[$idx] = $promise->then( |
| 171 | - function ($value) use ($idx, $key): void { |
|
| 171 | + function($value) use ($idx, $key): void { |
|
| 172 | 172 | if ($this->onFulfilled) { |
| 173 | 173 | ($this->onFulfilled)( |
| 174 | 174 | $value, |
@@ -178,7 +178,7 @@ discard block |
||
| 178 | 178 | } |
| 179 | 179 | $this->step($idx); |
| 180 | 180 | }, |
| 181 | - function ($reason) use ($idx, $key): void { |
|
| 181 | + function($reason) use ($idx, $key): void { |
|
| 182 | 182 | if ($this->onRejected) { |
| 183 | 183 | ($this->onRejected)( |
| 184 | 184 | $reason, |
@@ -10,8 +10,7 @@ |
||
| 10 | 10 | * |
| 11 | 11 | * @final |
| 12 | 12 | */ |
| 13 | -class EachPromise implements PromisorInterface |
|
| 14 | -{ |
|
| 13 | +class EachPromise implements PromisorInterface { |
|
| 15 | 14 | private $pending = []; |
| 16 | 15 | |
| 17 | 16 | private $nextPendingIndex = 0; |
@@ -13,269 +13,269 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class Promise implements PromiseInterface |
| 15 | 15 | { |
| 16 | - private $state = self::PENDING; |
|
| 17 | - private $result; |
|
| 18 | - private $cancelFn; |
|
| 19 | - private $waitFn; |
|
| 20 | - private $waitList; |
|
| 21 | - private $handlers = []; |
|
| 22 | - |
|
| 23 | - /** |
|
| 24 | - * @param callable $waitFn Fn that when invoked resolves the promise. |
|
| 25 | - * @param callable $cancelFn Fn that when invoked cancels the promise. |
|
| 26 | - */ |
|
| 27 | - public function __construct( |
|
| 28 | - callable $waitFn = null, |
|
| 29 | - callable $cancelFn = null |
|
| 30 | - ) { |
|
| 31 | - $this->waitFn = $waitFn; |
|
| 32 | - $this->cancelFn = $cancelFn; |
|
| 33 | - } |
|
| 34 | - |
|
| 35 | - public function then( |
|
| 36 | - callable $onFulfilled = null, |
|
| 37 | - callable $onRejected = null |
|
| 38 | - ): PromiseInterface { |
|
| 39 | - if ($this->state === self::PENDING) { |
|
| 40 | - $p = new Promise(null, [$this, 'cancel']); |
|
| 41 | - $this->handlers[] = [$p, $onFulfilled, $onRejected]; |
|
| 42 | - $p->waitList = $this->waitList; |
|
| 43 | - $p->waitList[] = $this; |
|
| 44 | - |
|
| 45 | - return $p; |
|
| 46 | - } |
|
| 47 | - |
|
| 48 | - // Return a fulfilled promise and immediately invoke any callbacks. |
|
| 49 | - if ($this->state === self::FULFILLED) { |
|
| 50 | - $promise = Create::promiseFor($this->result); |
|
| 51 | - |
|
| 52 | - return $onFulfilled ? $promise->then($onFulfilled) : $promise; |
|
| 53 | - } |
|
| 54 | - |
|
| 55 | - // It's either cancelled or rejected, so return a rejected promise |
|
| 56 | - // and immediately invoke any callbacks. |
|
| 57 | - $rejection = Create::rejectionFor($this->result); |
|
| 58 | - |
|
| 59 | - return $onRejected ? $rejection->then(null, $onRejected) : $rejection; |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - public function otherwise(callable $onRejected): PromiseInterface |
|
| 63 | - { |
|
| 64 | - return $this->then(null, $onRejected); |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - public function wait(bool $unwrap = true) |
|
| 68 | - { |
|
| 69 | - $this->waitIfPending(); |
|
| 70 | - |
|
| 71 | - if ($this->result instanceof PromiseInterface) { |
|
| 72 | - return $this->result->wait($unwrap); |
|
| 73 | - } |
|
| 74 | - if ($unwrap) { |
|
| 75 | - if ($this->state === self::FULFILLED) { |
|
| 76 | - return $this->result; |
|
| 77 | - } |
|
| 78 | - // It's rejected so "unwrap" and throw an exception. |
|
| 79 | - throw Create::exceptionFor($this->result); |
|
| 80 | - } |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - public function getState(): string |
|
| 84 | - { |
|
| 85 | - return $this->state; |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - public function cancel(): void |
|
| 89 | - { |
|
| 90 | - if ($this->state !== self::PENDING) { |
|
| 91 | - return; |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - $this->waitFn = $this->waitList = null; |
|
| 95 | - |
|
| 96 | - if ($this->cancelFn) { |
|
| 97 | - $fn = $this->cancelFn; |
|
| 98 | - $this->cancelFn = null; |
|
| 99 | - try { |
|
| 100 | - $fn(); |
|
| 101 | - } catch (\Throwable $e) { |
|
| 102 | - $this->reject($e); |
|
| 103 | - } |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - // Reject the promise only if it wasn't rejected in a then callback. |
|
| 107 | - /** @psalm-suppress RedundantCondition */ |
|
| 108 | - if ($this->state === self::PENDING) { |
|
| 109 | - $this->reject(new CancellationException('Promise has been cancelled')); |
|
| 110 | - } |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - public function resolve($value): void |
|
| 114 | - { |
|
| 115 | - $this->settle(self::FULFILLED, $value); |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - public function reject($reason): void |
|
| 119 | - { |
|
| 120 | - $this->settle(self::REJECTED, $reason); |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - private function settle(string $state, $value): void |
|
| 124 | - { |
|
| 125 | - if ($this->state !== self::PENDING) { |
|
| 126 | - // Ignore calls with the same resolution. |
|
| 127 | - if ($state === $this->state && $value === $this->result) { |
|
| 128 | - return; |
|
| 129 | - } |
|
| 130 | - throw $this->state === $state |
|
| 131 | - ? new \LogicException("The promise is already {$state}.") |
|
| 132 | - : new \LogicException("Cannot change a {$this->state} promise to {$state}"); |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - if ($value === $this) { |
|
| 136 | - throw new \LogicException('Cannot fulfill or reject a promise with itself'); |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - // Clear out the state of the promise but stash the handlers. |
|
| 140 | - $this->state = $state; |
|
| 141 | - $this->result = $value; |
|
| 142 | - $handlers = $this->handlers; |
|
| 143 | - $this->handlers = null; |
|
| 144 | - $this->waitList = $this->waitFn = null; |
|
| 145 | - $this->cancelFn = null; |
|
| 146 | - |
|
| 147 | - if (!$handlers) { |
|
| 148 | - return; |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - // If the value was not a settled promise or a thenable, then resolve |
|
| 152 | - // it in the task queue using the correct ID. |
|
| 153 | - if (!is_object($value) || !method_exists($value, 'then')) { |
|
| 154 | - $id = $state === self::FULFILLED ? 1 : 2; |
|
| 155 | - // It's a success, so resolve the handlers in the queue. |
|
| 156 | - Utils::queue()->add(static function () use ($id, $value, $handlers): void { |
|
| 157 | - foreach ($handlers as $handler) { |
|
| 158 | - self::callHandler($id, $value, $handler); |
|
| 159 | - } |
|
| 160 | - }); |
|
| 161 | - } elseif ($value instanceof Promise && Is::pending($value)) { |
|
| 162 | - // We can just merge our handlers onto the next promise. |
|
| 163 | - $value->handlers = array_merge($value->handlers, $handlers); |
|
| 164 | - } else { |
|
| 165 | - // Resolve the handlers when the forwarded promise is resolved. |
|
| 166 | - $value->then( |
|
| 167 | - static function ($value) use ($handlers): void { |
|
| 168 | - foreach ($handlers as $handler) { |
|
| 169 | - self::callHandler(1, $value, $handler); |
|
| 170 | - } |
|
| 171 | - }, |
|
| 172 | - static function ($reason) use ($handlers): void { |
|
| 173 | - foreach ($handlers as $handler) { |
|
| 174 | - self::callHandler(2, $reason, $handler); |
|
| 175 | - } |
|
| 176 | - } |
|
| 177 | - ); |
|
| 178 | - } |
|
| 179 | - } |
|
| 180 | - |
|
| 181 | - /** |
|
| 182 | - * Call a stack of handlers using a specific callback index and value. |
|
| 183 | - * |
|
| 184 | - * @param int $index 1 (resolve) or 2 (reject). |
|
| 185 | - * @param mixed $value Value to pass to the callback. |
|
| 186 | - * @param array $handler Array of handler data (promise and callbacks). |
|
| 187 | - */ |
|
| 188 | - private static function callHandler(int $index, $value, array $handler): void |
|
| 189 | - { |
|
| 190 | - /** @var PromiseInterface $promise */ |
|
| 191 | - $promise = $handler[0]; |
|
| 192 | - |
|
| 193 | - // The promise may have been cancelled or resolved before placing |
|
| 194 | - // this thunk in the queue. |
|
| 195 | - if (Is::settled($promise)) { |
|
| 196 | - return; |
|
| 197 | - } |
|
| 198 | - |
|
| 199 | - try { |
|
| 200 | - if (isset($handler[$index])) { |
|
| 201 | - /* |
|
| 16 | + private $state = self::PENDING; |
|
| 17 | + private $result; |
|
| 18 | + private $cancelFn; |
|
| 19 | + private $waitFn; |
|
| 20 | + private $waitList; |
|
| 21 | + private $handlers = []; |
|
| 22 | + |
|
| 23 | + /** |
|
| 24 | + * @param callable $waitFn Fn that when invoked resolves the promise. |
|
| 25 | + * @param callable $cancelFn Fn that when invoked cancels the promise. |
|
| 26 | + */ |
|
| 27 | + public function __construct( |
|
| 28 | + callable $waitFn = null, |
|
| 29 | + callable $cancelFn = null |
|
| 30 | + ) { |
|
| 31 | + $this->waitFn = $waitFn; |
|
| 32 | + $this->cancelFn = $cancelFn; |
|
| 33 | + } |
|
| 34 | + |
|
| 35 | + public function then( |
|
| 36 | + callable $onFulfilled = null, |
|
| 37 | + callable $onRejected = null |
|
| 38 | + ): PromiseInterface { |
|
| 39 | + if ($this->state === self::PENDING) { |
|
| 40 | + $p = new Promise(null, [$this, 'cancel']); |
|
| 41 | + $this->handlers[] = [$p, $onFulfilled, $onRejected]; |
|
| 42 | + $p->waitList = $this->waitList; |
|
| 43 | + $p->waitList[] = $this; |
|
| 44 | + |
|
| 45 | + return $p; |
|
| 46 | + } |
|
| 47 | + |
|
| 48 | + // Return a fulfilled promise and immediately invoke any callbacks. |
|
| 49 | + if ($this->state === self::FULFILLED) { |
|
| 50 | + $promise = Create::promiseFor($this->result); |
|
| 51 | + |
|
| 52 | + return $onFulfilled ? $promise->then($onFulfilled) : $promise; |
|
| 53 | + } |
|
| 54 | + |
|
| 55 | + // It's either cancelled or rejected, so return a rejected promise |
|
| 56 | + // and immediately invoke any callbacks. |
|
| 57 | + $rejection = Create::rejectionFor($this->result); |
|
| 58 | + |
|
| 59 | + return $onRejected ? $rejection->then(null, $onRejected) : $rejection; |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + public function otherwise(callable $onRejected): PromiseInterface |
|
| 63 | + { |
|
| 64 | + return $this->then(null, $onRejected); |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + public function wait(bool $unwrap = true) |
|
| 68 | + { |
|
| 69 | + $this->waitIfPending(); |
|
| 70 | + |
|
| 71 | + if ($this->result instanceof PromiseInterface) { |
|
| 72 | + return $this->result->wait($unwrap); |
|
| 73 | + } |
|
| 74 | + if ($unwrap) { |
|
| 75 | + if ($this->state === self::FULFILLED) { |
|
| 76 | + return $this->result; |
|
| 77 | + } |
|
| 78 | + // It's rejected so "unwrap" and throw an exception. |
|
| 79 | + throw Create::exceptionFor($this->result); |
|
| 80 | + } |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + public function getState(): string |
|
| 84 | + { |
|
| 85 | + return $this->state; |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + public function cancel(): void |
|
| 89 | + { |
|
| 90 | + if ($this->state !== self::PENDING) { |
|
| 91 | + return; |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + $this->waitFn = $this->waitList = null; |
|
| 95 | + |
|
| 96 | + if ($this->cancelFn) { |
|
| 97 | + $fn = $this->cancelFn; |
|
| 98 | + $this->cancelFn = null; |
|
| 99 | + try { |
|
| 100 | + $fn(); |
|
| 101 | + } catch (\Throwable $e) { |
|
| 102 | + $this->reject($e); |
|
| 103 | + } |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + // Reject the promise only if it wasn't rejected in a then callback. |
|
| 107 | + /** @psalm-suppress RedundantCondition */ |
|
| 108 | + if ($this->state === self::PENDING) { |
|
| 109 | + $this->reject(new CancellationException('Promise has been cancelled')); |
|
| 110 | + } |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + public function resolve($value): void |
|
| 114 | + { |
|
| 115 | + $this->settle(self::FULFILLED, $value); |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + public function reject($reason): void |
|
| 119 | + { |
|
| 120 | + $this->settle(self::REJECTED, $reason); |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + private function settle(string $state, $value): void |
|
| 124 | + { |
|
| 125 | + if ($this->state !== self::PENDING) { |
|
| 126 | + // Ignore calls with the same resolution. |
|
| 127 | + if ($state === $this->state && $value === $this->result) { |
|
| 128 | + return; |
|
| 129 | + } |
|
| 130 | + throw $this->state === $state |
|
| 131 | + ? new \LogicException("The promise is already {$state}.") |
|
| 132 | + : new \LogicException("Cannot change a {$this->state} promise to {$state}"); |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + if ($value === $this) { |
|
| 136 | + throw new \LogicException('Cannot fulfill or reject a promise with itself'); |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + // Clear out the state of the promise but stash the handlers. |
|
| 140 | + $this->state = $state; |
|
| 141 | + $this->result = $value; |
|
| 142 | + $handlers = $this->handlers; |
|
| 143 | + $this->handlers = null; |
|
| 144 | + $this->waitList = $this->waitFn = null; |
|
| 145 | + $this->cancelFn = null; |
|
| 146 | + |
|
| 147 | + if (!$handlers) { |
|
| 148 | + return; |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + // If the value was not a settled promise or a thenable, then resolve |
|
| 152 | + // it in the task queue using the correct ID. |
|
| 153 | + if (!is_object($value) || !method_exists($value, 'then')) { |
|
| 154 | + $id = $state === self::FULFILLED ? 1 : 2; |
|
| 155 | + // It's a success, so resolve the handlers in the queue. |
|
| 156 | + Utils::queue()->add(static function () use ($id, $value, $handlers): void { |
|
| 157 | + foreach ($handlers as $handler) { |
|
| 158 | + self::callHandler($id, $value, $handler); |
|
| 159 | + } |
|
| 160 | + }); |
|
| 161 | + } elseif ($value instanceof Promise && Is::pending($value)) { |
|
| 162 | + // We can just merge our handlers onto the next promise. |
|
| 163 | + $value->handlers = array_merge($value->handlers, $handlers); |
|
| 164 | + } else { |
|
| 165 | + // Resolve the handlers when the forwarded promise is resolved. |
|
| 166 | + $value->then( |
|
| 167 | + static function ($value) use ($handlers): void { |
|
| 168 | + foreach ($handlers as $handler) { |
|
| 169 | + self::callHandler(1, $value, $handler); |
|
| 170 | + } |
|
| 171 | + }, |
|
| 172 | + static function ($reason) use ($handlers): void { |
|
| 173 | + foreach ($handlers as $handler) { |
|
| 174 | + self::callHandler(2, $reason, $handler); |
|
| 175 | + } |
|
| 176 | + } |
|
| 177 | + ); |
|
| 178 | + } |
|
| 179 | + } |
|
| 180 | + |
|
| 181 | + /** |
|
| 182 | + * Call a stack of handlers using a specific callback index and value. |
|
| 183 | + * |
|
| 184 | + * @param int $index 1 (resolve) or 2 (reject). |
|
| 185 | + * @param mixed $value Value to pass to the callback. |
|
| 186 | + * @param array $handler Array of handler data (promise and callbacks). |
|
| 187 | + */ |
|
| 188 | + private static function callHandler(int $index, $value, array $handler): void |
|
| 189 | + { |
|
| 190 | + /** @var PromiseInterface $promise */ |
|
| 191 | + $promise = $handler[0]; |
|
| 192 | + |
|
| 193 | + // The promise may have been cancelled or resolved before placing |
|
| 194 | + // this thunk in the queue. |
|
| 195 | + if (Is::settled($promise)) { |
|
| 196 | + return; |
|
| 197 | + } |
|
| 198 | + |
|
| 199 | + try { |
|
| 200 | + if (isset($handler[$index])) { |
|
| 201 | + /* |
|
| 202 | 202 | * If $f throws an exception, then $handler will be in the exception |
| 203 | 203 | * stack trace. Since $handler contains a reference to the callable |
| 204 | 204 | * itself we get a circular reference. We clear the $handler |
| 205 | 205 | * here to avoid that memory leak. |
| 206 | 206 | */ |
| 207 | - $f = $handler[$index]; |
|
| 208 | - unset($handler); |
|
| 209 | - $promise->resolve($f($value)); |
|
| 210 | - } elseif ($index === 1) { |
|
| 211 | - // Forward resolution values as-is. |
|
| 212 | - $promise->resolve($value); |
|
| 213 | - } else { |
|
| 214 | - // Forward rejections down the chain. |
|
| 215 | - $promise->reject($value); |
|
| 216 | - } |
|
| 217 | - } catch (\Throwable $reason) { |
|
| 218 | - $promise->reject($reason); |
|
| 219 | - } |
|
| 220 | - } |
|
| 221 | - |
|
| 222 | - private function waitIfPending(): void |
|
| 223 | - { |
|
| 224 | - if ($this->state !== self::PENDING) { |
|
| 225 | - return; |
|
| 226 | - } elseif ($this->waitFn) { |
|
| 227 | - $this->invokeWaitFn(); |
|
| 228 | - } elseif ($this->waitList) { |
|
| 229 | - $this->invokeWaitList(); |
|
| 230 | - } else { |
|
| 231 | - // If there's no wait function, then reject the promise. |
|
| 232 | - $this->reject('Cannot wait on a promise that has ' |
|
| 233 | - .'no internal wait function. You must provide a wait ' |
|
| 234 | - .'function when constructing the promise to be able to ' |
|
| 235 | - .'wait on a promise.'); |
|
| 236 | - } |
|
| 237 | - |
|
| 238 | - Utils::queue()->run(); |
|
| 239 | - |
|
| 240 | - /** @psalm-suppress RedundantCondition */ |
|
| 241 | - if ($this->state === self::PENDING) { |
|
| 242 | - $this->reject('Invoking the wait callback did not resolve the promise'); |
|
| 243 | - } |
|
| 244 | - } |
|
| 245 | - |
|
| 246 | - private function invokeWaitFn(): void |
|
| 247 | - { |
|
| 248 | - try { |
|
| 249 | - $wfn = $this->waitFn; |
|
| 250 | - $this->waitFn = null; |
|
| 251 | - $wfn(true); |
|
| 252 | - } catch (\Throwable $reason) { |
|
| 253 | - if ($this->state === self::PENDING) { |
|
| 254 | - // The promise has not been resolved yet, so reject the promise |
|
| 255 | - // with the exception. |
|
| 256 | - $this->reject($reason); |
|
| 257 | - } else { |
|
| 258 | - // The promise was already resolved, so there's a problem in |
|
| 259 | - // the application. |
|
| 260 | - throw $reason; |
|
| 261 | - } |
|
| 262 | - } |
|
| 263 | - } |
|
| 264 | - |
|
| 265 | - private function invokeWaitList(): void |
|
| 266 | - { |
|
| 267 | - $waitList = $this->waitList; |
|
| 268 | - $this->waitList = null; |
|
| 269 | - |
|
| 270 | - foreach ($waitList as $result) { |
|
| 271 | - do { |
|
| 272 | - $result->waitIfPending(); |
|
| 273 | - $result = $result->result; |
|
| 274 | - } while ($result instanceof Promise); |
|
| 275 | - |
|
| 276 | - if ($result instanceof PromiseInterface) { |
|
| 277 | - $result->wait(false); |
|
| 278 | - } |
|
| 279 | - } |
|
| 280 | - } |
|
| 207 | + $f = $handler[$index]; |
|
| 208 | + unset($handler); |
|
| 209 | + $promise->resolve($f($value)); |
|
| 210 | + } elseif ($index === 1) { |
|
| 211 | + // Forward resolution values as-is. |
|
| 212 | + $promise->resolve($value); |
|
| 213 | + } else { |
|
| 214 | + // Forward rejections down the chain. |
|
| 215 | + $promise->reject($value); |
|
| 216 | + } |
|
| 217 | + } catch (\Throwable $reason) { |
|
| 218 | + $promise->reject($reason); |
|
| 219 | + } |
|
| 220 | + } |
|
| 221 | + |
|
| 222 | + private function waitIfPending(): void |
|
| 223 | + { |
|
| 224 | + if ($this->state !== self::PENDING) { |
|
| 225 | + return; |
|
| 226 | + } elseif ($this->waitFn) { |
|
| 227 | + $this->invokeWaitFn(); |
|
| 228 | + } elseif ($this->waitList) { |
|
| 229 | + $this->invokeWaitList(); |
|
| 230 | + } else { |
|
| 231 | + // If there's no wait function, then reject the promise. |
|
| 232 | + $this->reject('Cannot wait on a promise that has ' |
|
| 233 | + .'no internal wait function. You must provide a wait ' |
|
| 234 | + .'function when constructing the promise to be able to ' |
|
| 235 | + .'wait on a promise.'); |
|
| 236 | + } |
|
| 237 | + |
|
| 238 | + Utils::queue()->run(); |
|
| 239 | + |
|
| 240 | + /** @psalm-suppress RedundantCondition */ |
|
| 241 | + if ($this->state === self::PENDING) { |
|
| 242 | + $this->reject('Invoking the wait callback did not resolve the promise'); |
|
| 243 | + } |
|
| 244 | + } |
|
| 245 | + |
|
| 246 | + private function invokeWaitFn(): void |
|
| 247 | + { |
|
| 248 | + try { |
|
| 249 | + $wfn = $this->waitFn; |
|
| 250 | + $this->waitFn = null; |
|
| 251 | + $wfn(true); |
|
| 252 | + } catch (\Throwable $reason) { |
|
| 253 | + if ($this->state === self::PENDING) { |
|
| 254 | + // The promise has not been resolved yet, so reject the promise |
|
| 255 | + // with the exception. |
|
| 256 | + $this->reject($reason); |
|
| 257 | + } else { |
|
| 258 | + // The promise was already resolved, so there's a problem in |
|
| 259 | + // the application. |
|
| 260 | + throw $reason; |
|
| 261 | + } |
|
| 262 | + } |
|
| 263 | + } |
|
| 264 | + |
|
| 265 | + private function invokeWaitList(): void |
|
| 266 | + { |
|
| 267 | + $waitList = $this->waitList; |
|
| 268 | + $this->waitList = null; |
|
| 269 | + |
|
| 270 | + foreach ($waitList as $result) { |
|
| 271 | + do { |
|
| 272 | + $result->waitIfPending(); |
|
| 273 | + $result = $result->result; |
|
| 274 | + } while ($result instanceof Promise); |
|
| 275 | + |
|
| 276 | + if ($result instanceof PromiseInterface) { |
|
| 277 | + $result->wait(false); |
|
| 278 | + } |
|
| 279 | + } |
|
| 280 | + } |
|
| 281 | 281 | } |
@@ -153,7 +153,7 @@ discard block |
||
| 153 | 153 | if (!is_object($value) || !method_exists($value, 'then')) { |
| 154 | 154 | $id = $state === self::FULFILLED ? 1 : 2; |
| 155 | 155 | // It's a success, so resolve the handlers in the queue. |
| 156 | - Utils::queue()->add(static function () use ($id, $value, $handlers): void { |
|
| 156 | + Utils::queue()->add(static function() use ($id, $value, $handlers): void { |
|
| 157 | 157 | foreach ($handlers as $handler) { |
| 158 | 158 | self::callHandler($id, $value, $handler); |
| 159 | 159 | } |
@@ -164,12 +164,12 @@ discard block |
||
| 164 | 164 | } else { |
| 165 | 165 | // Resolve the handlers when the forwarded promise is resolved. |
| 166 | 166 | $value->then( |
| 167 | - static function ($value) use ($handlers): void { |
|
| 167 | + static function($value) use ($handlers): void { |
|
| 168 | 168 | foreach ($handlers as $handler) { |
| 169 | 169 | self::callHandler(1, $value, $handler); |
| 170 | 170 | } |
| 171 | 171 | }, |
| 172 | - static function ($reason) use ($handlers): void { |
|
| 172 | + static function($reason) use ($handlers): void { |
|
| 173 | 173 | foreach ($handlers as $handler) { |
| 174 | 174 | self::callHandler(2, $reason, $handler); |
| 175 | 175 | } |
@@ -11,8 +11,7 @@ |
||
| 11 | 11 | * |
| 12 | 12 | * @final |
| 13 | 13 | */ |
| 14 | -class Promise implements PromiseInterface |
|
| 15 | -{ |
|
| 14 | +class Promise implements PromiseInterface { |
|
| 16 | 15 | private $state = self::PENDING; |
| 17 | 16 | private $result; |
| 18 | 17 | private $cancelFn; |
@@ -9,8 +9,8 @@ |
||
| 9 | 9 | */ |
| 10 | 10 | interface PromisorInterface |
| 11 | 11 | { |
| 12 | - /** |
|
| 13 | - * Returns a promise. |
|
| 14 | - */ |
|
| 15 | - public function promise(): PromiseInterface; |
|
| 12 | + /** |
|
| 13 | + * Returns a promise. |
|
| 14 | + */ |
|
| 15 | + public function promise(): PromiseInterface; |
|
| 16 | 16 | } |
@@ -7,8 +7,7 @@ |
||
| 7 | 7 | /** |
| 8 | 8 | * Interface used with classes that return a promise. |
| 9 | 9 | */ |
| 10 | -interface PromisorInterface |
|
| 11 | -{ |
|
| 10 | +interface PromisorInterface { |
|
| 12 | 11 | /** |
| 13 | 12 | * Returns a promise. |
| 14 | 13 | */ |
@@ -9,11 +9,11 @@ |
||
| 9 | 9 | */ |
| 10 | 10 | class AggregateException extends RejectionException |
| 11 | 11 | { |
| 12 | - public function __construct(string $msg, array $reasons) |
|
| 13 | - { |
|
| 14 | - parent::__construct( |
|
| 15 | - $reasons, |
|
| 16 | - sprintf('%s; %d rejected promises', $msg, count($reasons)) |
|
| 17 | - ); |
|
| 18 | - } |
|
| 12 | + public function __construct(string $msg, array $reasons) |
|
| 13 | + { |
|
| 14 | + parent::__construct( |
|
| 15 | + $reasons, |
|
| 16 | + sprintf('%s; %d rejected promises', $msg, count($reasons)) |
|
| 17 | + ); |
|
| 18 | + } |
|
| 19 | 19 | } |
@@ -7,8 +7,7 @@ |
||
| 7 | 7 | /** |
| 8 | 8 | * Exception thrown when too many errors occur in the some() or any() methods. |
| 9 | 9 | */ |
| 10 | -class AggregateException extends RejectionException |
|
| 11 | -{ |
|
| 10 | +class AggregateException extends RejectionException { |
|
| 12 | 11 | public function __construct(string $msg, array $reasons) |
| 13 | 12 | { |
| 14 | 13 | parent::__construct( |
@@ -14,82 +14,82 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | class RejectedPromise implements PromiseInterface |
| 16 | 16 | { |
| 17 | - private $reason; |
|
| 17 | + private $reason; |
|
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * @param mixed $reason |
|
| 21 | - */ |
|
| 22 | - public function __construct($reason) |
|
| 23 | - { |
|
| 24 | - if (is_object($reason) && method_exists($reason, 'then')) { |
|
| 25 | - throw new \InvalidArgumentException( |
|
| 26 | - 'You cannot create a RejectedPromise with a promise.' |
|
| 27 | - ); |
|
| 28 | - } |
|
| 19 | + /** |
|
| 20 | + * @param mixed $reason |
|
| 21 | + */ |
|
| 22 | + public function __construct($reason) |
|
| 23 | + { |
|
| 24 | + if (is_object($reason) && method_exists($reason, 'then')) { |
|
| 25 | + throw new \InvalidArgumentException( |
|
| 26 | + 'You cannot create a RejectedPromise with a promise.' |
|
| 27 | + ); |
|
| 28 | + } |
|
| 29 | 29 | |
| 30 | - $this->reason = $reason; |
|
| 31 | - } |
|
| 30 | + $this->reason = $reason; |
|
| 31 | + } |
|
| 32 | 32 | |
| 33 | - public function then( |
|
| 34 | - callable $onFulfilled = null, |
|
| 35 | - callable $onRejected = null |
|
| 36 | - ): PromiseInterface { |
|
| 37 | - // If there's no onRejected callback then just return self. |
|
| 38 | - if (!$onRejected) { |
|
| 39 | - return $this; |
|
| 40 | - } |
|
| 33 | + public function then( |
|
| 34 | + callable $onFulfilled = null, |
|
| 35 | + callable $onRejected = null |
|
| 36 | + ): PromiseInterface { |
|
| 37 | + // If there's no onRejected callback then just return self. |
|
| 38 | + if (!$onRejected) { |
|
| 39 | + return $this; |
|
| 40 | + } |
|
| 41 | 41 | |
| 42 | - $queue = Utils::queue(); |
|
| 43 | - $reason = $this->reason; |
|
| 44 | - $p = new Promise([$queue, 'run']); |
|
| 45 | - $queue->add(static function () use ($p, $reason, $onRejected): void { |
|
| 46 | - if (Is::pending($p)) { |
|
| 47 | - try { |
|
| 48 | - // Return a resolved promise if onRejected does not throw. |
|
| 49 | - $p->resolve($onRejected($reason)); |
|
| 50 | - } catch (\Throwable $e) { |
|
| 51 | - // onRejected threw, so return a rejected promise. |
|
| 52 | - $p->reject($e); |
|
| 53 | - } |
|
| 54 | - } |
|
| 55 | - }); |
|
| 42 | + $queue = Utils::queue(); |
|
| 43 | + $reason = $this->reason; |
|
| 44 | + $p = new Promise([$queue, 'run']); |
|
| 45 | + $queue->add(static function () use ($p, $reason, $onRejected): void { |
|
| 46 | + if (Is::pending($p)) { |
|
| 47 | + try { |
|
| 48 | + // Return a resolved promise if onRejected does not throw. |
|
| 49 | + $p->resolve($onRejected($reason)); |
|
| 50 | + } catch (\Throwable $e) { |
|
| 51 | + // onRejected threw, so return a rejected promise. |
|
| 52 | + $p->reject($e); |
|
| 53 | + } |
|
| 54 | + } |
|
| 55 | + }); |
|
| 56 | 56 | |
| 57 | - return $p; |
|
| 58 | - } |
|
| 57 | + return $p; |
|
| 58 | + } |
|
| 59 | 59 | |
| 60 | - public function otherwise(callable $onRejected): PromiseInterface |
|
| 61 | - { |
|
| 62 | - return $this->then(null, $onRejected); |
|
| 63 | - } |
|
| 60 | + public function otherwise(callable $onRejected): PromiseInterface |
|
| 61 | + { |
|
| 62 | + return $this->then(null, $onRejected); |
|
| 63 | + } |
|
| 64 | 64 | |
| 65 | - public function wait(bool $unwrap = true) |
|
| 66 | - { |
|
| 67 | - if ($unwrap) { |
|
| 68 | - throw Create::exceptionFor($this->reason); |
|
| 69 | - } |
|
| 65 | + public function wait(bool $unwrap = true) |
|
| 66 | + { |
|
| 67 | + if ($unwrap) { |
|
| 68 | + throw Create::exceptionFor($this->reason); |
|
| 69 | + } |
|
| 70 | 70 | |
| 71 | - return null; |
|
| 72 | - } |
|
| 71 | + return null; |
|
| 72 | + } |
|
| 73 | 73 | |
| 74 | - public function getState(): string |
|
| 75 | - { |
|
| 76 | - return self::REJECTED; |
|
| 77 | - } |
|
| 74 | + public function getState(): string |
|
| 75 | + { |
|
| 76 | + return self::REJECTED; |
|
| 77 | + } |
|
| 78 | 78 | |
| 79 | - public function resolve($value): void |
|
| 80 | - { |
|
| 81 | - throw new \LogicException('Cannot resolve a rejected promise'); |
|
| 82 | - } |
|
| 79 | + public function resolve($value): void |
|
| 80 | + { |
|
| 81 | + throw new \LogicException('Cannot resolve a rejected promise'); |
|
| 82 | + } |
|
| 83 | 83 | |
| 84 | - public function reject($reason): void |
|
| 85 | - { |
|
| 86 | - if ($reason !== $this->reason) { |
|
| 87 | - throw new \LogicException('Cannot reject a rejected promise'); |
|
| 88 | - } |
|
| 89 | - } |
|
| 84 | + public function reject($reason): void |
|
| 85 | + { |
|
| 86 | + if ($reason !== $this->reason) { |
|
| 87 | + throw new \LogicException('Cannot reject a rejected promise'); |
|
| 88 | + } |
|
| 89 | + } |
|
| 90 | 90 | |
| 91 | - public function cancel(): void |
|
| 92 | - { |
|
| 93 | - // pass |
|
| 94 | - } |
|
| 91 | + public function cancel(): void |
|
| 92 | + { |
|
| 93 | + // pass |
|
| 94 | + } |
|
| 95 | 95 | } |
@@ -42,7 +42,7 @@ |
||
| 42 | 42 | $queue = Utils::queue(); |
| 43 | 43 | $reason = $this->reason; |
| 44 | 44 | $p = new Promise([$queue, 'run']); |
| 45 | - $queue->add(static function () use ($p, $reason, $onRejected): void { |
|
| 45 | + $queue->add(static function() use ($p, $reason, $onRejected): void { |
|
| 46 | 46 | if (Is::pending($p)) { |
| 47 | 47 | try { |
| 48 | 48 | // Return a resolved promise if onRejected does not throw. |
@@ -12,8 +12,7 @@ |
||
| 12 | 12 | * |
| 13 | 13 | * @final |
| 14 | 14 | */ |
| 15 | -class RejectedPromise implements PromiseInterface |
|
| 16 | -{ |
|
| 15 | +class RejectedPromise implements PromiseInterface { |
|
| 17 | 16 | private $reason; |
| 18 | 17 | |
| 19 | 18 | /** |
@@ -6,35 +6,35 @@ |
||
| 6 | 6 | |
| 7 | 7 | final class Is |
| 8 | 8 | { |
| 9 | - /** |
|
| 10 | - * Returns true if a promise is pending. |
|
| 11 | - */ |
|
| 12 | - public static function pending(PromiseInterface $promise): bool |
|
| 13 | - { |
|
| 14 | - return $promise->getState() === PromiseInterface::PENDING; |
|
| 15 | - } |
|
| 9 | + /** |
|
| 10 | + * Returns true if a promise is pending. |
|
| 11 | + */ |
|
| 12 | + public static function pending(PromiseInterface $promise): bool |
|
| 13 | + { |
|
| 14 | + return $promise->getState() === PromiseInterface::PENDING; |
|
| 15 | + } |
|
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * Returns true if a promise is fulfilled or rejected. |
|
| 19 | - */ |
|
| 20 | - public static function settled(PromiseInterface $promise): bool |
|
| 21 | - { |
|
| 22 | - return $promise->getState() !== PromiseInterface::PENDING; |
|
| 23 | - } |
|
| 17 | + /** |
|
| 18 | + * Returns true if a promise is fulfilled or rejected. |
|
| 19 | + */ |
|
| 20 | + public static function settled(PromiseInterface $promise): bool |
|
| 21 | + { |
|
| 22 | + return $promise->getState() !== PromiseInterface::PENDING; |
|
| 23 | + } |
|
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * Returns true if a promise is fulfilled. |
|
| 27 | - */ |
|
| 28 | - public static function fulfilled(PromiseInterface $promise): bool |
|
| 29 | - { |
|
| 30 | - return $promise->getState() === PromiseInterface::FULFILLED; |
|
| 31 | - } |
|
| 25 | + /** |
|
| 26 | + * Returns true if a promise is fulfilled. |
|
| 27 | + */ |
|
| 28 | + public static function fulfilled(PromiseInterface $promise): bool |
|
| 29 | + { |
|
| 30 | + return $promise->getState() === PromiseInterface::FULFILLED; |
|
| 31 | + } |
|
| 32 | 32 | |
| 33 | - /** |
|
| 34 | - * Returns true if a promise is rejected. |
|
| 35 | - */ |
|
| 36 | - public static function rejected(PromiseInterface $promise): bool |
|
| 37 | - { |
|
| 38 | - return $promise->getState() === PromiseInterface::REJECTED; |
|
| 39 | - } |
|
| 33 | + /** |
|
| 34 | + * Returns true if a promise is rejected. |
|
| 35 | + */ |
|
| 36 | + public static function rejected(PromiseInterface $promise): bool |
|
| 37 | + { |
|
| 38 | + return $promise->getState() === PromiseInterface::REJECTED; |
|
| 39 | + } |
|
| 40 | 40 | } |
@@ -4,8 +4,7 @@ |
||
| 4 | 4 | |
| 5 | 5 | namespace OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\Promise; |
| 6 | 6 | |
| 7 | -final class Is |
|
| 8 | -{ |
|
| 7 | +final class Is { |
|
| 9 | 8 | /** |
| 10 | 9 | * Returns true if a promise is pending. |
| 11 | 10 | */ |
@@ -15,254 +15,254 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | final class Middleware |
| 17 | 17 | { |
| 18 | - /** |
|
| 19 | - * Middleware that adds cookies to requests. |
|
| 20 | - * |
|
| 21 | - * The options array must be set to a CookieJarInterface in order to use |
|
| 22 | - * cookies. This is typically handled for you by a client. |
|
| 23 | - * |
|
| 24 | - * @return callable Returns a function that accepts the next handler. |
|
| 25 | - */ |
|
| 26 | - public static function cookies(): callable |
|
| 27 | - { |
|
| 28 | - return static function (callable $handler): callable { |
|
| 29 | - return static function ($request, array $options) use ($handler) { |
|
| 30 | - if (empty($options['cookies'])) { |
|
| 31 | - return $handler($request, $options); |
|
| 32 | - } elseif (!($options['cookies'] instanceof CookieJarInterface)) { |
|
| 33 | - throw new \InvalidArgumentException('cookies must be an instance of OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\Cookie\CookieJarInterface'); |
|
| 34 | - } |
|
| 35 | - $cookieJar = $options['cookies']; |
|
| 36 | - $request = $cookieJar->withCookieHeader($request); |
|
| 18 | + /** |
|
| 19 | + * Middleware that adds cookies to requests. |
|
| 20 | + * |
|
| 21 | + * The options array must be set to a CookieJarInterface in order to use |
|
| 22 | + * cookies. This is typically handled for you by a client. |
|
| 23 | + * |
|
| 24 | + * @return callable Returns a function that accepts the next handler. |
|
| 25 | + */ |
|
| 26 | + public static function cookies(): callable |
|
| 27 | + { |
|
| 28 | + return static function (callable $handler): callable { |
|
| 29 | + return static function ($request, array $options) use ($handler) { |
|
| 30 | + if (empty($options['cookies'])) { |
|
| 31 | + return $handler($request, $options); |
|
| 32 | + } elseif (!($options['cookies'] instanceof CookieJarInterface)) { |
|
| 33 | + throw new \InvalidArgumentException('cookies must be an instance of OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\Cookie\CookieJarInterface'); |
|
| 34 | + } |
|
| 35 | + $cookieJar = $options['cookies']; |
|
| 36 | + $request = $cookieJar->withCookieHeader($request); |
|
| 37 | 37 | |
| 38 | - return $handler($request, $options) |
|
| 39 | - ->then( |
|
| 40 | - static function (ResponseInterface $response) use ($cookieJar, $request): ResponseInterface { |
|
| 41 | - $cookieJar->extractCookies($request, $response); |
|
| 38 | + return $handler($request, $options) |
|
| 39 | + ->then( |
|
| 40 | + static function (ResponseInterface $response) use ($cookieJar, $request): ResponseInterface { |
|
| 41 | + $cookieJar->extractCookies($request, $response); |
|
| 42 | 42 | |
| 43 | - return $response; |
|
| 44 | - } |
|
| 45 | - ); |
|
| 46 | - }; |
|
| 47 | - }; |
|
| 48 | - } |
|
| 43 | + return $response; |
|
| 44 | + } |
|
| 45 | + ); |
|
| 46 | + }; |
|
| 47 | + }; |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - /** |
|
| 51 | - * Middleware that throws exceptions for 4xx or 5xx responses when the |
|
| 52 | - * "http_errors" request option is set to true. |
|
| 53 | - * |
|
| 54 | - * @param BodySummarizerInterface|null $bodySummarizer The body summarizer to use in exception messages. |
|
| 55 | - * |
|
| 56 | - * @return callable(callable): callable Returns a function that accepts the next handler. |
|
| 57 | - */ |
|
| 58 | - public static function httpErrors(BodySummarizerInterface $bodySummarizer = null): callable |
|
| 59 | - { |
|
| 60 | - return static function (callable $handler) use ($bodySummarizer): callable { |
|
| 61 | - return static function ($request, array $options) use ($handler, $bodySummarizer) { |
|
| 62 | - if (empty($options['http_errors'])) { |
|
| 63 | - return $handler($request, $options); |
|
| 64 | - } |
|
| 50 | + /** |
|
| 51 | + * Middleware that throws exceptions for 4xx or 5xx responses when the |
|
| 52 | + * "http_errors" request option is set to true. |
|
| 53 | + * |
|
| 54 | + * @param BodySummarizerInterface|null $bodySummarizer The body summarizer to use in exception messages. |
|
| 55 | + * |
|
| 56 | + * @return callable(callable): callable Returns a function that accepts the next handler. |
|
| 57 | + */ |
|
| 58 | + public static function httpErrors(BodySummarizerInterface $bodySummarizer = null): callable |
|
| 59 | + { |
|
| 60 | + return static function (callable $handler) use ($bodySummarizer): callable { |
|
| 61 | + return static function ($request, array $options) use ($handler, $bodySummarizer) { |
|
| 62 | + if (empty($options['http_errors'])) { |
|
| 63 | + return $handler($request, $options); |
|
| 64 | + } |
|
| 65 | 65 | |
| 66 | - return $handler($request, $options)->then( |
|
| 67 | - static function (ResponseInterface $response) use ($request, $bodySummarizer) { |
|
| 68 | - $code = $response->getStatusCode(); |
|
| 69 | - if ($code < 400) { |
|
| 70 | - return $response; |
|
| 71 | - } |
|
| 72 | - throw RequestException::create($request, $response, null, [], $bodySummarizer); |
|
| 73 | - } |
|
| 74 | - ); |
|
| 75 | - }; |
|
| 76 | - }; |
|
| 77 | - } |
|
| 66 | + return $handler($request, $options)->then( |
|
| 67 | + static function (ResponseInterface $response) use ($request, $bodySummarizer) { |
|
| 68 | + $code = $response->getStatusCode(); |
|
| 69 | + if ($code < 400) { |
|
| 70 | + return $response; |
|
| 71 | + } |
|
| 72 | + throw RequestException::create($request, $response, null, [], $bodySummarizer); |
|
| 73 | + } |
|
| 74 | + ); |
|
| 75 | + }; |
|
| 76 | + }; |
|
| 77 | + } |
|
| 78 | 78 | |
| 79 | - /** |
|
| 80 | - * Middleware that pushes history data to an ArrayAccess container. |
|
| 81 | - * |
|
| 82 | - * @param array|\ArrayAccess<int, array> $container Container to hold the history (by reference). |
|
| 83 | - * |
|
| 84 | - * @return callable(callable): callable Returns a function that accepts the next handler. |
|
| 85 | - * |
|
| 86 | - * @throws \InvalidArgumentException if container is not an array or ArrayAccess. |
|
| 87 | - */ |
|
| 88 | - public static function history(&$container): callable |
|
| 89 | - { |
|
| 90 | - if (!\is_array($container) && !$container instanceof \ArrayAccess) { |
|
| 91 | - throw new \InvalidArgumentException('history container must be an array or object implementing ArrayAccess'); |
|
| 92 | - } |
|
| 79 | + /** |
|
| 80 | + * Middleware that pushes history data to an ArrayAccess container. |
|
| 81 | + * |
|
| 82 | + * @param array|\ArrayAccess<int, array> $container Container to hold the history (by reference). |
|
| 83 | + * |
|
| 84 | + * @return callable(callable): callable Returns a function that accepts the next handler. |
|
| 85 | + * |
|
| 86 | + * @throws \InvalidArgumentException if container is not an array or ArrayAccess. |
|
| 87 | + */ |
|
| 88 | + public static function history(&$container): callable |
|
| 89 | + { |
|
| 90 | + if (!\is_array($container) && !$container instanceof \ArrayAccess) { |
|
| 91 | + throw new \InvalidArgumentException('history container must be an array or object implementing ArrayAccess'); |
|
| 92 | + } |
|
| 93 | 93 | |
| 94 | - return static function (callable $handler) use (&$container): callable { |
|
| 95 | - return static function (RequestInterface $request, array $options) use ($handler, &$container) { |
|
| 96 | - return $handler($request, $options)->then( |
|
| 97 | - static function ($value) use ($request, &$container, $options) { |
|
| 98 | - $container[] = [ |
|
| 99 | - 'request' => $request, |
|
| 100 | - 'response' => $value, |
|
| 101 | - 'error' => null, |
|
| 102 | - 'options' => $options, |
|
| 103 | - ]; |
|
| 94 | + return static function (callable $handler) use (&$container): callable { |
|
| 95 | + return static function (RequestInterface $request, array $options) use ($handler, &$container) { |
|
| 96 | + return $handler($request, $options)->then( |
|
| 97 | + static function ($value) use ($request, &$container, $options) { |
|
| 98 | + $container[] = [ |
|
| 99 | + 'request' => $request, |
|
| 100 | + 'response' => $value, |
|
| 101 | + 'error' => null, |
|
| 102 | + 'options' => $options, |
|
| 103 | + ]; |
|
| 104 | 104 | |
| 105 | - return $value; |
|
| 106 | - }, |
|
| 107 | - static function ($reason) use ($request, &$container, $options) { |
|
| 108 | - $container[] = [ |
|
| 109 | - 'request' => $request, |
|
| 110 | - 'response' => null, |
|
| 111 | - 'error' => $reason, |
|
| 112 | - 'options' => $options, |
|
| 113 | - ]; |
|
| 105 | + return $value; |
|
| 106 | + }, |
|
| 107 | + static function ($reason) use ($request, &$container, $options) { |
|
| 108 | + $container[] = [ |
|
| 109 | + 'request' => $request, |
|
| 110 | + 'response' => null, |
|
| 111 | + 'error' => $reason, |
|
| 112 | + 'options' => $options, |
|
| 113 | + ]; |
|
| 114 | 114 | |
| 115 | - return P\Create::rejectionFor($reason); |
|
| 116 | - } |
|
| 117 | - ); |
|
| 118 | - }; |
|
| 119 | - }; |
|
| 120 | - } |
|
| 115 | + return P\Create::rejectionFor($reason); |
|
| 116 | + } |
|
| 117 | + ); |
|
| 118 | + }; |
|
| 119 | + }; |
|
| 120 | + } |
|
| 121 | 121 | |
| 122 | - /** |
|
| 123 | - * Middleware that invokes a callback before and after sending a request. |
|
| 124 | - * |
|
| 125 | - * The provided listener cannot modify or alter the response. It simply |
|
| 126 | - * "taps" into the chain to be notified before returning the promise. The |
|
| 127 | - * before listener accepts a request and options array, and the after |
|
| 128 | - * listener accepts a request, options array, and response promise. |
|
| 129 | - * |
|
| 130 | - * @param callable $before Function to invoke before forwarding the request. |
|
| 131 | - * @param callable $after Function invoked after forwarding. |
|
| 132 | - * |
|
| 133 | - * @return callable Returns a function that accepts the next handler. |
|
| 134 | - */ |
|
| 135 | - public static function tap(callable $before = null, callable $after = null): callable |
|
| 136 | - { |
|
| 137 | - return static function (callable $handler) use ($before, $after): callable { |
|
| 138 | - return static function (RequestInterface $request, array $options) use ($handler, $before, $after) { |
|
| 139 | - if ($before) { |
|
| 140 | - $before($request, $options); |
|
| 141 | - } |
|
| 142 | - $response = $handler($request, $options); |
|
| 143 | - if ($after) { |
|
| 144 | - $after($request, $options, $response); |
|
| 145 | - } |
|
| 122 | + /** |
|
| 123 | + * Middleware that invokes a callback before and after sending a request. |
|
| 124 | + * |
|
| 125 | + * The provided listener cannot modify or alter the response. It simply |
|
| 126 | + * "taps" into the chain to be notified before returning the promise. The |
|
| 127 | + * before listener accepts a request and options array, and the after |
|
| 128 | + * listener accepts a request, options array, and response promise. |
|
| 129 | + * |
|
| 130 | + * @param callable $before Function to invoke before forwarding the request. |
|
| 131 | + * @param callable $after Function invoked after forwarding. |
|
| 132 | + * |
|
| 133 | + * @return callable Returns a function that accepts the next handler. |
|
| 134 | + */ |
|
| 135 | + public static function tap(callable $before = null, callable $after = null): callable |
|
| 136 | + { |
|
| 137 | + return static function (callable $handler) use ($before, $after): callable { |
|
| 138 | + return static function (RequestInterface $request, array $options) use ($handler, $before, $after) { |
|
| 139 | + if ($before) { |
|
| 140 | + $before($request, $options); |
|
| 141 | + } |
|
| 142 | + $response = $handler($request, $options); |
|
| 143 | + if ($after) { |
|
| 144 | + $after($request, $options, $response); |
|
| 145 | + } |
|
| 146 | 146 | |
| 147 | - return $response; |
|
| 148 | - }; |
|
| 149 | - }; |
|
| 150 | - } |
|
| 147 | + return $response; |
|
| 148 | + }; |
|
| 149 | + }; |
|
| 150 | + } |
|
| 151 | 151 | |
| 152 | - /** |
|
| 153 | - * Middleware that handles request redirects. |
|
| 154 | - * |
|
| 155 | - * @return callable Returns a function that accepts the next handler. |
|
| 156 | - */ |
|
| 157 | - public static function redirect(): callable |
|
| 158 | - { |
|
| 159 | - return static function (callable $handler): RedirectMiddleware { |
|
| 160 | - return new RedirectMiddleware($handler); |
|
| 161 | - }; |
|
| 162 | - } |
|
| 152 | + /** |
|
| 153 | + * Middleware that handles request redirects. |
|
| 154 | + * |
|
| 155 | + * @return callable Returns a function that accepts the next handler. |
|
| 156 | + */ |
|
| 157 | + public static function redirect(): callable |
|
| 158 | + { |
|
| 159 | + return static function (callable $handler): RedirectMiddleware { |
|
| 160 | + return new RedirectMiddleware($handler); |
|
| 161 | + }; |
|
| 162 | + } |
|
| 163 | 163 | |
| 164 | - /** |
|
| 165 | - * Middleware that retries requests based on the boolean result of |
|
| 166 | - * invoking the provided "decider" function. |
|
| 167 | - * |
|
| 168 | - * If no delay function is provided, a simple implementation of exponential |
|
| 169 | - * backoff will be utilized. |
|
| 170 | - * |
|
| 171 | - * @param callable $decider Function that accepts the number of retries, |
|
| 172 | - * a request, [response], and [exception] and |
|
| 173 | - * returns true if the request is to be retried. |
|
| 174 | - * @param callable $delay Function that accepts the number of retries and |
|
| 175 | - * returns the number of milliseconds to delay. |
|
| 176 | - * |
|
| 177 | - * @return callable Returns a function that accepts the next handler. |
|
| 178 | - */ |
|
| 179 | - public static function retry(callable $decider, callable $delay = null): callable |
|
| 180 | - { |
|
| 181 | - return static function (callable $handler) use ($decider, $delay): RetryMiddleware { |
|
| 182 | - return new RetryMiddleware($decider, $handler, $delay); |
|
| 183 | - }; |
|
| 184 | - } |
|
| 164 | + /** |
|
| 165 | + * Middleware that retries requests based on the boolean result of |
|
| 166 | + * invoking the provided "decider" function. |
|
| 167 | + * |
|
| 168 | + * If no delay function is provided, a simple implementation of exponential |
|
| 169 | + * backoff will be utilized. |
|
| 170 | + * |
|
| 171 | + * @param callable $decider Function that accepts the number of retries, |
|
| 172 | + * a request, [response], and [exception] and |
|
| 173 | + * returns true if the request is to be retried. |
|
| 174 | + * @param callable $delay Function that accepts the number of retries and |
|
| 175 | + * returns the number of milliseconds to delay. |
|
| 176 | + * |
|
| 177 | + * @return callable Returns a function that accepts the next handler. |
|
| 178 | + */ |
|
| 179 | + public static function retry(callable $decider, callable $delay = null): callable |
|
| 180 | + { |
|
| 181 | + return static function (callable $handler) use ($decider, $delay): RetryMiddleware { |
|
| 182 | + return new RetryMiddleware($decider, $handler, $delay); |
|
| 183 | + }; |
|
| 184 | + } |
|
| 185 | 185 | |
| 186 | - /** |
|
| 187 | - * Middleware that logs requests, responses, and errors using a message |
|
| 188 | - * formatter. |
|
| 189 | - * |
|
| 190 | - * @phpstan-param \Psr\Log\LogLevel::* $logLevel Level at which to log requests. |
|
| 191 | - * |
|
| 192 | - * @param LoggerInterface $logger Logs messages. |
|
| 193 | - * @param MessageFormatterInterface|MessageFormatter $formatter Formatter used to create message strings. |
|
| 194 | - * @param string $logLevel Level at which to log requests. |
|
| 195 | - * |
|
| 196 | - * @return callable Returns a function that accepts the next handler. |
|
| 197 | - */ |
|
| 198 | - public static function log(LoggerInterface $logger, $formatter, string $logLevel = 'info'): callable |
|
| 199 | - { |
|
| 200 | - // To be compatible with Guzzle 7.1.x we need to allow users to pass a MessageFormatter |
|
| 201 | - if (!$formatter instanceof MessageFormatter && !$formatter instanceof MessageFormatterInterface) { |
|
| 202 | - throw new \LogicException(sprintf('Argument 2 to %s::log() must be of type %s', self::class, MessageFormatterInterface::class)); |
|
| 203 | - } |
|
| 186 | + /** |
|
| 187 | + * Middleware that logs requests, responses, and errors using a message |
|
| 188 | + * formatter. |
|
| 189 | + * |
|
| 190 | + * @phpstan-param \Psr\Log\LogLevel::* $logLevel Level at which to log requests. |
|
| 191 | + * |
|
| 192 | + * @param LoggerInterface $logger Logs messages. |
|
| 193 | + * @param MessageFormatterInterface|MessageFormatter $formatter Formatter used to create message strings. |
|
| 194 | + * @param string $logLevel Level at which to log requests. |
|
| 195 | + * |
|
| 196 | + * @return callable Returns a function that accepts the next handler. |
|
| 197 | + */ |
|
| 198 | + public static function log(LoggerInterface $logger, $formatter, string $logLevel = 'info'): callable |
|
| 199 | + { |
|
| 200 | + // To be compatible with Guzzle 7.1.x we need to allow users to pass a MessageFormatter |
|
| 201 | + if (!$formatter instanceof MessageFormatter && !$formatter instanceof MessageFormatterInterface) { |
|
| 202 | + throw new \LogicException(sprintf('Argument 2 to %s::log() must be of type %s', self::class, MessageFormatterInterface::class)); |
|
| 203 | + } |
|
| 204 | 204 | |
| 205 | - return static function (callable $handler) use ($logger, $formatter, $logLevel): callable { |
|
| 206 | - return static function (RequestInterface $request, array $options = []) use ($handler, $logger, $formatter, $logLevel) { |
|
| 207 | - return $handler($request, $options)->then( |
|
| 208 | - static function ($response) use ($logger, $request, $formatter, $logLevel): ResponseInterface { |
|
| 209 | - $message = $formatter->format($request, $response); |
|
| 210 | - $logger->log($logLevel, $message); |
|
| 205 | + return static function (callable $handler) use ($logger, $formatter, $logLevel): callable { |
|
| 206 | + return static function (RequestInterface $request, array $options = []) use ($handler, $logger, $formatter, $logLevel) { |
|
| 207 | + return $handler($request, $options)->then( |
|
| 208 | + static function ($response) use ($logger, $request, $formatter, $logLevel): ResponseInterface { |
|
| 209 | + $message = $formatter->format($request, $response); |
|
| 210 | + $logger->log($logLevel, $message); |
|
| 211 | 211 | |
| 212 | - return $response; |
|
| 213 | - }, |
|
| 214 | - static function ($reason) use ($logger, $request, $formatter): PromiseInterface { |
|
| 215 | - $response = $reason instanceof RequestException ? $reason->getResponse() : null; |
|
| 216 | - $message = $formatter->format($request, $response, P\Create::exceptionFor($reason)); |
|
| 217 | - $logger->error($message); |
|
| 212 | + return $response; |
|
| 213 | + }, |
|
| 214 | + static function ($reason) use ($logger, $request, $formatter): PromiseInterface { |
|
| 215 | + $response = $reason instanceof RequestException ? $reason->getResponse() : null; |
|
| 216 | + $message = $formatter->format($request, $response, P\Create::exceptionFor($reason)); |
|
| 217 | + $logger->error($message); |
|
| 218 | 218 | |
| 219 | - return P\Create::rejectionFor($reason); |
|
| 220 | - } |
|
| 221 | - ); |
|
| 222 | - }; |
|
| 223 | - }; |
|
| 224 | - } |
|
| 219 | + return P\Create::rejectionFor($reason); |
|
| 220 | + } |
|
| 221 | + ); |
|
| 222 | + }; |
|
| 223 | + }; |
|
| 224 | + } |
|
| 225 | 225 | |
| 226 | - /** |
|
| 227 | - * This middleware adds a default content-type if possible, a default |
|
| 228 | - * content-length or transfer-encoding header, and the expect header. |
|
| 229 | - */ |
|
| 230 | - public static function prepareBody(): callable |
|
| 231 | - { |
|
| 232 | - return static function (callable $handler): PrepareBodyMiddleware { |
|
| 233 | - return new PrepareBodyMiddleware($handler); |
|
| 234 | - }; |
|
| 235 | - } |
|
| 226 | + /** |
|
| 227 | + * This middleware adds a default content-type if possible, a default |
|
| 228 | + * content-length or transfer-encoding header, and the expect header. |
|
| 229 | + */ |
|
| 230 | + public static function prepareBody(): callable |
|
| 231 | + { |
|
| 232 | + return static function (callable $handler): PrepareBodyMiddleware { |
|
| 233 | + return new PrepareBodyMiddleware($handler); |
|
| 234 | + }; |
|
| 235 | + } |
|
| 236 | 236 | |
| 237 | - /** |
|
| 238 | - * Middleware that applies a map function to the request before passing to |
|
| 239 | - * the next handler. |
|
| 240 | - * |
|
| 241 | - * @param callable $fn Function that accepts a RequestInterface and returns |
|
| 242 | - * a RequestInterface. |
|
| 243 | - */ |
|
| 244 | - public static function mapRequest(callable $fn): callable |
|
| 245 | - { |
|
| 246 | - return static function (callable $handler) use ($fn): callable { |
|
| 247 | - return static function (RequestInterface $request, array $options) use ($handler, $fn) { |
|
| 248 | - return $handler($fn($request), $options); |
|
| 249 | - }; |
|
| 250 | - }; |
|
| 251 | - } |
|
| 237 | + /** |
|
| 238 | + * Middleware that applies a map function to the request before passing to |
|
| 239 | + * the next handler. |
|
| 240 | + * |
|
| 241 | + * @param callable $fn Function that accepts a RequestInterface and returns |
|
| 242 | + * a RequestInterface. |
|
| 243 | + */ |
|
| 244 | + public static function mapRequest(callable $fn): callable |
|
| 245 | + { |
|
| 246 | + return static function (callable $handler) use ($fn): callable { |
|
| 247 | + return static function (RequestInterface $request, array $options) use ($handler, $fn) { |
|
| 248 | + return $handler($fn($request), $options); |
|
| 249 | + }; |
|
| 250 | + }; |
|
| 251 | + } |
|
| 252 | 252 | |
| 253 | - /** |
|
| 254 | - * Middleware that applies a map function to the resolved promise's |
|
| 255 | - * response. |
|
| 256 | - * |
|
| 257 | - * @param callable $fn Function that accepts a ResponseInterface and |
|
| 258 | - * returns a ResponseInterface. |
|
| 259 | - */ |
|
| 260 | - public static function mapResponse(callable $fn): callable |
|
| 261 | - { |
|
| 262 | - return static function (callable $handler) use ($fn): callable { |
|
| 263 | - return static function (RequestInterface $request, array $options) use ($handler, $fn) { |
|
| 264 | - return $handler($request, $options)->then($fn); |
|
| 265 | - }; |
|
| 266 | - }; |
|
| 267 | - } |
|
| 253 | + /** |
|
| 254 | + * Middleware that applies a map function to the resolved promise's |
|
| 255 | + * response. |
|
| 256 | + * |
|
| 257 | + * @param callable $fn Function that accepts a ResponseInterface and |
|
| 258 | + * returns a ResponseInterface. |
|
| 259 | + */ |
|
| 260 | + public static function mapResponse(callable $fn): callable |
|
| 261 | + { |
|
| 262 | + return static function (callable $handler) use ($fn): callable { |
|
| 263 | + return static function (RequestInterface $request, array $options) use ($handler, $fn) { |
|
| 264 | + return $handler($request, $options)->then($fn); |
|
| 265 | + }; |
|
| 266 | + }; |
|
| 267 | + } |
|
| 268 | 268 | } |
@@ -25,8 +25,8 @@ discard block |
||
| 25 | 25 | */ |
| 26 | 26 | public static function cookies(): callable |
| 27 | 27 | { |
| 28 | - return static function (callable $handler): callable { |
|
| 29 | - return static function ($request, array $options) use ($handler) { |
|
| 28 | + return static function(callable $handler): callable { |
|
| 29 | + return static function($request, array $options) use ($handler) { |
|
| 30 | 30 | if (empty($options['cookies'])) { |
| 31 | 31 | return $handler($request, $options); |
| 32 | 32 | } elseif (!($options['cookies'] instanceof CookieJarInterface)) { |
@@ -37,7 +37,7 @@ discard block |
||
| 37 | 37 | |
| 38 | 38 | return $handler($request, $options) |
| 39 | 39 | ->then( |
| 40 | - static function (ResponseInterface $response) use ($cookieJar, $request): ResponseInterface { |
|
| 40 | + static function(ResponseInterface $response) use ($cookieJar, $request): ResponseInterface { |
|
| 41 | 41 | $cookieJar->extractCookies($request, $response); |
| 42 | 42 | |
| 43 | 43 | return $response; |
@@ -57,14 +57,14 @@ discard block |
||
| 57 | 57 | */ |
| 58 | 58 | public static function httpErrors(BodySummarizerInterface $bodySummarizer = null): callable |
| 59 | 59 | { |
| 60 | - return static function (callable $handler) use ($bodySummarizer): callable { |
|
| 61 | - return static function ($request, array $options) use ($handler, $bodySummarizer) { |
|
| 60 | + return static function(callable $handler) use ($bodySummarizer): callable { |
|
| 61 | + return static function($request, array $options) use ($handler, $bodySummarizer) { |
|
| 62 | 62 | if (empty($options['http_errors'])) { |
| 63 | 63 | return $handler($request, $options); |
| 64 | 64 | } |
| 65 | 65 | |
| 66 | 66 | return $handler($request, $options)->then( |
| 67 | - static function (ResponseInterface $response) use ($request, $bodySummarizer) { |
|
| 67 | + static function(ResponseInterface $response) use ($request, $bodySummarizer) { |
|
| 68 | 68 | $code = $response->getStatusCode(); |
| 69 | 69 | if ($code < 400) { |
| 70 | 70 | return $response; |
@@ -91,10 +91,10 @@ discard block |
||
| 91 | 91 | throw new \InvalidArgumentException('history container must be an array or object implementing ArrayAccess'); |
| 92 | 92 | } |
| 93 | 93 | |
| 94 | - return static function (callable $handler) use (&$container): callable { |
|
| 95 | - return static function (RequestInterface $request, array $options) use ($handler, &$container) { |
|
| 94 | + return static function(callable $handler) use (&$container): callable { |
|
| 95 | + return static function(RequestInterface $request, array $options) use ($handler, &$container) { |
|
| 96 | 96 | return $handler($request, $options)->then( |
| 97 | - static function ($value) use ($request, &$container, $options) { |
|
| 97 | + static function($value) use ($request, &$container, $options) { |
|
| 98 | 98 | $container[] = [ |
| 99 | 99 | 'request' => $request, |
| 100 | 100 | 'response' => $value, |
@@ -104,7 +104,7 @@ discard block |
||
| 104 | 104 | |
| 105 | 105 | return $value; |
| 106 | 106 | }, |
| 107 | - static function ($reason) use ($request, &$container, $options) { |
|
| 107 | + static function($reason) use ($request, &$container, $options) { |
|
| 108 | 108 | $container[] = [ |
| 109 | 109 | 'request' => $request, |
| 110 | 110 | 'response' => null, |
@@ -134,8 +134,8 @@ discard block |
||
| 134 | 134 | */ |
| 135 | 135 | public static function tap(callable $before = null, callable $after = null): callable |
| 136 | 136 | { |
| 137 | - return static function (callable $handler) use ($before, $after): callable { |
|
| 138 | - return static function (RequestInterface $request, array $options) use ($handler, $before, $after) { |
|
| 137 | + return static function(callable $handler) use ($before, $after): callable { |
|
| 138 | + return static function(RequestInterface $request, array $options) use ($handler, $before, $after) { |
|
| 139 | 139 | if ($before) { |
| 140 | 140 | $before($request, $options); |
| 141 | 141 | } |
@@ -156,7 +156,7 @@ discard block |
||
| 156 | 156 | */ |
| 157 | 157 | public static function redirect(): callable |
| 158 | 158 | { |
| 159 | - return static function (callable $handler): RedirectMiddleware { |
|
| 159 | + return static function(callable $handler): RedirectMiddleware { |
|
| 160 | 160 | return new RedirectMiddleware($handler); |
| 161 | 161 | }; |
| 162 | 162 | } |
@@ -178,7 +178,7 @@ discard block |
||
| 178 | 178 | */ |
| 179 | 179 | public static function retry(callable $decider, callable $delay = null): callable |
| 180 | 180 | { |
| 181 | - return static function (callable $handler) use ($decider, $delay): RetryMiddleware { |
|
| 181 | + return static function(callable $handler) use ($decider, $delay): RetryMiddleware { |
|
| 182 | 182 | return new RetryMiddleware($decider, $handler, $delay); |
| 183 | 183 | }; |
| 184 | 184 | } |
@@ -202,16 +202,16 @@ discard block |
||
| 202 | 202 | throw new \LogicException(sprintf('Argument 2 to %s::log() must be of type %s', self::class, MessageFormatterInterface::class)); |
| 203 | 203 | } |
| 204 | 204 | |
| 205 | - return static function (callable $handler) use ($logger, $formatter, $logLevel): callable { |
|
| 206 | - return static function (RequestInterface $request, array $options = []) use ($handler, $logger, $formatter, $logLevel) { |
|
| 205 | + return static function(callable $handler) use ($logger, $formatter, $logLevel): callable { |
|
| 206 | + return static function(RequestInterface $request, array $options = []) use ($handler, $logger, $formatter, $logLevel) { |
|
| 207 | 207 | return $handler($request, $options)->then( |
| 208 | - static function ($response) use ($logger, $request, $formatter, $logLevel): ResponseInterface { |
|
| 208 | + static function($response) use ($logger, $request, $formatter, $logLevel): ResponseInterface { |
|
| 209 | 209 | $message = $formatter->format($request, $response); |
| 210 | 210 | $logger->log($logLevel, $message); |
| 211 | 211 | |
| 212 | 212 | return $response; |
| 213 | 213 | }, |
| 214 | - static function ($reason) use ($logger, $request, $formatter): PromiseInterface { |
|
| 214 | + static function($reason) use ($logger, $request, $formatter): PromiseInterface { |
|
| 215 | 215 | $response = $reason instanceof RequestException ? $reason->getResponse() : null; |
| 216 | 216 | $message = $formatter->format($request, $response, P\Create::exceptionFor($reason)); |
| 217 | 217 | $logger->error($message); |
@@ -229,7 +229,7 @@ discard block |
||
| 229 | 229 | */ |
| 230 | 230 | public static function prepareBody(): callable |
| 231 | 231 | { |
| 232 | - return static function (callable $handler): PrepareBodyMiddleware { |
|
| 232 | + return static function(callable $handler): PrepareBodyMiddleware { |
|
| 233 | 233 | return new PrepareBodyMiddleware($handler); |
| 234 | 234 | }; |
| 235 | 235 | } |
@@ -243,8 +243,8 @@ discard block |
||
| 243 | 243 | */ |
| 244 | 244 | public static function mapRequest(callable $fn): callable |
| 245 | 245 | { |
| 246 | - return static function (callable $handler) use ($fn): callable { |
|
| 247 | - return static function (RequestInterface $request, array $options) use ($handler, $fn) { |
|
| 246 | + return static function(callable $handler) use ($fn): callable { |
|
| 247 | + return static function(RequestInterface $request, array $options) use ($handler, $fn) { |
|
| 248 | 248 | return $handler($fn($request), $options); |
| 249 | 249 | }; |
| 250 | 250 | }; |
@@ -259,8 +259,8 @@ discard block |
||
| 259 | 259 | */ |
| 260 | 260 | public static function mapResponse(callable $fn): callable |
| 261 | 261 | { |
| 262 | - return static function (callable $handler) use ($fn): callable { |
|
| 263 | - return static function (RequestInterface $request, array $options) use ($handler, $fn) { |
|
| 262 | + return static function(callable $handler) use ($fn): callable { |
|
| 263 | + return static function(RequestInterface $request, array $options) use ($handler, $fn) { |
|
| 264 | 264 | return $handler($request, $options)->then($fn); |
| 265 | 265 | }; |
| 266 | 266 | }; |
@@ -13,8 +13,7 @@ |
||
| 13 | 13 | /** |
| 14 | 14 | * Functions used to create and wrap handlers with handler middleware. |
| 15 | 15 | */ |
| 16 | -final class Middleware |
|
| 17 | -{ |
|
| 16 | +final class Middleware { |
|
| 18 | 17 | /** |
| 19 | 18 | * Middleware that adds cookies to requests. |
| 20 | 19 | * |
@@ -2,5 +2,5 @@ |
||
| 2 | 2 | |
| 3 | 3 | // Don't redefine the functions if included multiple times. |
| 4 | 4 | if (!\function_exists('OCA\FullTextSearch_Elasticsearch\Vendor\GuzzleHttp\describe_type')) { |
| 5 | - require __DIR__.'/functions.php'; |
|
| 5 | + require __DIR__.'/functions.php'; |
|
| 6 | 6 | } |