[e3d4e0a] | 1 | <?php
|
---|
| 2 |
|
---|
| 3 | declare(strict_types=1);
|
---|
| 4 |
|
---|
| 5 | namespace GuzzleHttp\Promise;
|
---|
| 6 |
|
---|
| 7 | final class Utils
|
---|
| 8 | {
|
---|
| 9 | /**
|
---|
| 10 | * Get the global task queue used for promise resolution.
|
---|
| 11 | *
|
---|
| 12 | * This task queue MUST be run in an event loop in order for promises to be
|
---|
| 13 | * settled asynchronously. It will be automatically run when synchronously
|
---|
| 14 | * waiting on a promise.
|
---|
| 15 | *
|
---|
| 16 | * <code>
|
---|
| 17 | * while ($eventLoop->isRunning()) {
|
---|
| 18 | * GuzzleHttp\Promise\Utils::queue()->run();
|
---|
| 19 | * }
|
---|
| 20 | * </code>
|
---|
| 21 | *
|
---|
| 22 | * @param TaskQueueInterface|null $assign Optionally specify a new queue instance.
|
---|
| 23 | */
|
---|
| 24 | public static function queue(?TaskQueueInterface $assign = null): TaskQueueInterface
|
---|
| 25 | {
|
---|
| 26 | static $queue;
|
---|
| 27 |
|
---|
| 28 | if ($assign) {
|
---|
| 29 | $queue = $assign;
|
---|
| 30 | } elseif (!$queue) {
|
---|
| 31 | $queue = new TaskQueue();
|
---|
| 32 | }
|
---|
| 33 |
|
---|
| 34 | return $queue;
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | /**
|
---|
| 38 | * Adds a function to run in the task queue when it is next `run()` and
|
---|
| 39 | * returns a promise that is fulfilled or rejected with the result.
|
---|
| 40 | *
|
---|
| 41 | * @param callable $task Task function to run.
|
---|
| 42 | */
|
---|
| 43 | public static function task(callable $task): PromiseInterface
|
---|
| 44 | {
|
---|
| 45 | $queue = self::queue();
|
---|
| 46 | $promise = new Promise([$queue, 'run']);
|
---|
| 47 | $queue->add(function () use ($task, $promise): void {
|
---|
| 48 | try {
|
---|
| 49 | if (Is::pending($promise)) {
|
---|
| 50 | $promise->resolve($task());
|
---|
| 51 | }
|
---|
| 52 | } catch (\Throwable $e) {
|
---|
| 53 | $promise->reject($e);
|
---|
| 54 | }
|
---|
| 55 | });
|
---|
| 56 |
|
---|
| 57 | return $promise;
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | /**
|
---|
| 61 | * Synchronously waits on a promise to resolve and returns an inspection
|
---|
| 62 | * state array.
|
---|
| 63 | *
|
---|
| 64 | * Returns a state associative array containing a "state" key mapping to a
|
---|
| 65 | * valid promise state. If the state of the promise is "fulfilled", the
|
---|
| 66 | * array will contain a "value" key mapping to the fulfilled value of the
|
---|
| 67 | * promise. If the promise is rejected, the array will contain a "reason"
|
---|
| 68 | * key mapping to the rejection reason of the promise.
|
---|
| 69 | *
|
---|
| 70 | * @param PromiseInterface $promise Promise or value.
|
---|
| 71 | */
|
---|
| 72 | public static function inspect(PromiseInterface $promise): array
|
---|
| 73 | {
|
---|
| 74 | try {
|
---|
| 75 | return [
|
---|
| 76 | 'state' => PromiseInterface::FULFILLED,
|
---|
| 77 | 'value' => $promise->wait(),
|
---|
| 78 | ];
|
---|
| 79 | } catch (RejectionException $e) {
|
---|
| 80 | return ['state' => PromiseInterface::REJECTED, 'reason' => $e->getReason()];
|
---|
| 81 | } catch (\Throwable $e) {
|
---|
| 82 | return ['state' => PromiseInterface::REJECTED, 'reason' => $e];
|
---|
| 83 | }
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | /**
|
---|
| 87 | * Waits on all of the provided promises, but does not unwrap rejected
|
---|
| 88 | * promises as thrown exception.
|
---|
| 89 | *
|
---|
| 90 | * Returns an array of inspection state arrays.
|
---|
| 91 | *
|
---|
| 92 | * @see inspect for the inspection state array format.
|
---|
| 93 | *
|
---|
| 94 | * @param PromiseInterface[] $promises Traversable of promises to wait upon.
|
---|
| 95 | */
|
---|
| 96 | public static function inspectAll($promises): array
|
---|
| 97 | {
|
---|
| 98 | $results = [];
|
---|
| 99 | foreach ($promises as $key => $promise) {
|
---|
| 100 | $results[$key] = self::inspect($promise);
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | return $results;
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | /**
|
---|
| 107 | * Waits on all of the provided promises and returns the fulfilled values.
|
---|
| 108 | *
|
---|
| 109 | * Returns an array that contains the value of each promise (in the same
|
---|
| 110 | * order the promises were provided). An exception is thrown if any of the
|
---|
| 111 | * promises are rejected.
|
---|
| 112 | *
|
---|
| 113 | * @param iterable<PromiseInterface> $promises Iterable of PromiseInterface objects to wait on.
|
---|
| 114 | *
|
---|
| 115 | * @throws \Throwable on error
|
---|
| 116 | */
|
---|
| 117 | public static function unwrap($promises): array
|
---|
| 118 | {
|
---|
| 119 | $results = [];
|
---|
| 120 | foreach ($promises as $key => $promise) {
|
---|
| 121 | $results[$key] = $promise->wait();
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | return $results;
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | /**
|
---|
| 128 | * Given an array of promises, return a promise that is fulfilled when all
|
---|
| 129 | * the items in the array are fulfilled.
|
---|
| 130 | *
|
---|
| 131 | * The promise's fulfillment value is an array with fulfillment values at
|
---|
| 132 | * respective positions to the original array. If any promise in the array
|
---|
| 133 | * rejects, the returned promise is rejected with the rejection reason.
|
---|
| 134 | *
|
---|
| 135 | * @param mixed $promises Promises or values.
|
---|
| 136 | * @param bool $recursive If true, resolves new promises that might have been added to the stack during its own resolution.
|
---|
| 137 | */
|
---|
| 138 | public static function all($promises, bool $recursive = false): PromiseInterface
|
---|
| 139 | {
|
---|
| 140 | $results = [];
|
---|
| 141 | $promise = Each::of(
|
---|
| 142 | $promises,
|
---|
| 143 | function ($value, $idx) use (&$results): void {
|
---|
| 144 | $results[$idx] = $value;
|
---|
| 145 | },
|
---|
| 146 | function ($reason, $idx, Promise $aggregate): void {
|
---|
| 147 | if (Is::pending($aggregate)) {
|
---|
| 148 | $aggregate->reject($reason);
|
---|
| 149 | }
|
---|
| 150 | }
|
---|
| 151 | )->then(function () use (&$results) {
|
---|
| 152 | ksort($results);
|
---|
| 153 |
|
---|
| 154 | return $results;
|
---|
| 155 | });
|
---|
| 156 |
|
---|
| 157 | if (true === $recursive) {
|
---|
| 158 | $promise = $promise->then(function ($results) use ($recursive, &$promises) {
|
---|
| 159 | foreach ($promises as $promise) {
|
---|
| 160 | if (Is::pending($promise)) {
|
---|
| 161 | return self::all($promises, $recursive);
|
---|
| 162 | }
|
---|
| 163 | }
|
---|
| 164 |
|
---|
| 165 | return $results;
|
---|
| 166 | });
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | return $promise;
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | /**
|
---|
| 173 | * Initiate a competitive race between multiple promises or values (values
|
---|
| 174 | * will become immediately fulfilled promises).
|
---|
| 175 | *
|
---|
| 176 | * When count amount of promises have been fulfilled, the returned promise
|
---|
| 177 | * is fulfilled with an array that contains the fulfillment values of the
|
---|
| 178 | * winners in order of resolution.
|
---|
| 179 | *
|
---|
| 180 | * This promise is rejected with a {@see AggregateException} if the number
|
---|
| 181 | * of fulfilled promises is less than the desired $count.
|
---|
| 182 | *
|
---|
| 183 | * @param int $count Total number of promises.
|
---|
| 184 | * @param mixed $promises Promises or values.
|
---|
| 185 | */
|
---|
| 186 | public static function some(int $count, $promises): PromiseInterface
|
---|
| 187 | {
|
---|
| 188 | $results = [];
|
---|
| 189 | $rejections = [];
|
---|
| 190 |
|
---|
| 191 | return Each::of(
|
---|
| 192 | $promises,
|
---|
| 193 | function ($value, $idx, PromiseInterface $p) use (&$results, $count): void {
|
---|
| 194 | if (Is::settled($p)) {
|
---|
| 195 | return;
|
---|
| 196 | }
|
---|
| 197 | $results[$idx] = $value;
|
---|
| 198 | if (count($results) >= $count) {
|
---|
| 199 | $p->resolve(null);
|
---|
| 200 | }
|
---|
| 201 | },
|
---|
| 202 | function ($reason) use (&$rejections): void {
|
---|
| 203 | $rejections[] = $reason;
|
---|
| 204 | }
|
---|
| 205 | )->then(
|
---|
| 206 | function () use (&$results, &$rejections, $count) {
|
---|
| 207 | if (count($results) !== $count) {
|
---|
| 208 | throw new AggregateException(
|
---|
| 209 | 'Not enough promises to fulfill count',
|
---|
| 210 | $rejections
|
---|
| 211 | );
|
---|
| 212 | }
|
---|
| 213 | ksort($results);
|
---|
| 214 |
|
---|
| 215 | return array_values($results);
|
---|
| 216 | }
|
---|
| 217 | );
|
---|
| 218 | }
|
---|
| 219 |
|
---|
| 220 | /**
|
---|
| 221 | * Like some(), with 1 as count. However, if the promise fulfills, the
|
---|
| 222 | * fulfillment value is not an array of 1 but the value directly.
|
---|
| 223 | *
|
---|
| 224 | * @param mixed $promises Promises or values.
|
---|
| 225 | */
|
---|
| 226 | public static function any($promises): PromiseInterface
|
---|
| 227 | {
|
---|
| 228 | return self::some(1, $promises)->then(function ($values) {
|
---|
| 229 | return $values[0];
|
---|
| 230 | });
|
---|
| 231 | }
|
---|
| 232 |
|
---|
| 233 | /**
|
---|
| 234 | * Returns a promise that is fulfilled when all of the provided promises have
|
---|
| 235 | * been fulfilled or rejected.
|
---|
| 236 | *
|
---|
| 237 | * The returned promise is fulfilled with an array of inspection state arrays.
|
---|
| 238 | *
|
---|
| 239 | * @see inspect for the inspection state array format.
|
---|
| 240 | *
|
---|
| 241 | * @param mixed $promises Promises or values.
|
---|
| 242 | */
|
---|
| 243 | public static function settle($promises): PromiseInterface
|
---|
| 244 | {
|
---|
| 245 | $results = [];
|
---|
| 246 |
|
---|
| 247 | return Each::of(
|
---|
| 248 | $promises,
|
---|
| 249 | function ($value, $idx) use (&$results): void {
|
---|
| 250 | $results[$idx] = ['state' => PromiseInterface::FULFILLED, 'value' => $value];
|
---|
| 251 | },
|
---|
| 252 | function ($reason, $idx) use (&$results): void {
|
---|
| 253 | $results[$idx] = ['state' => PromiseInterface::REJECTED, 'reason' => $reason];
|
---|
| 254 | }
|
---|
| 255 | )->then(function () use (&$results) {
|
---|
| 256 | ksort($results);
|
---|
| 257 |
|
---|
| 258 | return $results;
|
---|
| 259 | });
|
---|
| 260 | }
|
---|
| 261 | }
|
---|