| [01a620f] | 1 | <?php
|
|---|
| 2 | namespace GuzzleHttp\Promise;
|
|---|
| 3 |
|
|---|
| 4 | /**
|
|---|
| 5 | * Get the global task queue used for promise resolution.
|
|---|
| 6 | *
|
|---|
| 7 | * This task queue MUST be run in an event loop in order for promises to be
|
|---|
| 8 | * settled asynchronously. It will be automatically run when synchronously
|
|---|
| 9 | * waiting on a promise.
|
|---|
| 10 | *
|
|---|
| 11 | * <code>
|
|---|
| 12 | * while ($eventLoop->isRunning()) {
|
|---|
| 13 | * GuzzleHttp\Promise\queue()->run();
|
|---|
| 14 | * }
|
|---|
| 15 | * </code>
|
|---|
| 16 | *
|
|---|
| 17 | * @return TaskQueue
|
|---|
| 18 | */
|
|---|
| 19 | function queue()
|
|---|
| 20 | {
|
|---|
| 21 | static $queue;
|
|---|
| 22 |
|
|---|
| 23 | if (!$queue) {
|
|---|
| 24 | $queue = new TaskQueue();
|
|---|
| 25 | }
|
|---|
| 26 |
|
|---|
| 27 | return $queue;
|
|---|
| 28 | }
|
|---|
| 29 |
|
|---|
| 30 | /**
|
|---|
| 31 | * Adds a function to run in the task queue when it is next `run()` and returns
|
|---|
| 32 | * a promise that is fulfilled or rejected with the result.
|
|---|
| 33 | *
|
|---|
| 34 | * @param callable $task Task function to run.
|
|---|
| 35 | *
|
|---|
| 36 | * @return PromiseInterface
|
|---|
| 37 | */
|
|---|
| 38 | function task(callable $task)
|
|---|
| 39 | {
|
|---|
| 40 | $queue = queue();
|
|---|
| 41 | $promise = new Promise([$queue, 'run']);
|
|---|
| 42 | $queue->add(function () use ($task, $promise) {
|
|---|
| 43 | try {
|
|---|
| 44 | $promise->resolve($task());
|
|---|
| 45 | } catch (\Exception $e) {
|
|---|
| 46 | $promise->reject($e);
|
|---|
| 47 | }
|
|---|
| 48 | });
|
|---|
| 49 |
|
|---|
| 50 | return $promise;
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | /**
|
|---|
| 54 | * Creates a promise for a value if the value is not a promise.
|
|---|
| 55 | *
|
|---|
| 56 | * @param mixed $value Promise or value.
|
|---|
| 57 | *
|
|---|
| 58 | * @return PromiseInterface
|
|---|
| 59 | */
|
|---|
| 60 | function promise_for($value)
|
|---|
| 61 | {
|
|---|
| 62 | if ($value instanceof PromiseInterface) {
|
|---|
| 63 | return $value;
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | // Return a Guzzle promise that shadows the given promise.
|
|---|
| 67 | if (method_exists($value, 'then')) {
|
|---|
| 68 | $wfn = method_exists($value, 'wait') ? [$value, 'wait'] : null;
|
|---|
| 69 | $cfn = method_exists($value, 'cancel') ? [$value, 'cancel'] : null;
|
|---|
| 70 | $promise = new Promise($wfn, $cfn);
|
|---|
| 71 | $value->then([$promise, 'resolve'], [$promise, 'reject']);
|
|---|
| 72 | return $promise;
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | return new FulfilledPromise($value);
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 | /**
|
|---|
| 79 | * Creates a rejected promise for a reason if the reason is not a promise. If
|
|---|
| 80 | * the provided reason is a promise, then it is returned as-is.
|
|---|
| 81 | *
|
|---|
| 82 | * @param mixed $reason Promise or reason.
|
|---|
| 83 | *
|
|---|
| 84 | * @return PromiseInterface
|
|---|
| 85 | */
|
|---|
| 86 | function rejection_for($reason)
|
|---|
| 87 | {
|
|---|
| 88 | if ($reason instanceof PromiseInterface) {
|
|---|
| 89 | return $reason;
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | return new RejectedPromise($reason);
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | /**
|
|---|
| 96 | * Create an exception for a rejected promise value.
|
|---|
| 97 | *
|
|---|
| 98 | * @param mixed $reason
|
|---|
| 99 | *
|
|---|
| 100 | * @return \Exception
|
|---|
| 101 | */
|
|---|
| 102 | function exception_for($reason)
|
|---|
| 103 | {
|
|---|
| 104 | return $reason instanceof \Exception
|
|---|
| 105 | ? $reason
|
|---|
| 106 | : new RejectionException($reason);
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | /**
|
|---|
| 110 | * Returns an iterator for the given value.
|
|---|
| 111 | *
|
|---|
| 112 | * @param mixed $value
|
|---|
| 113 | *
|
|---|
| 114 | * @return \Iterator
|
|---|
| 115 | */
|
|---|
| 116 | function iter_for($value)
|
|---|
| 117 | {
|
|---|
| 118 | if ($value instanceof \Iterator) {
|
|---|
| 119 | return $value;
|
|---|
| 120 | } elseif (is_array($value)) {
|
|---|
| 121 | return new \ArrayIterator($value);
|
|---|
| 122 | } else {
|
|---|
| 123 | return new \ArrayIterator([$value]);
|
|---|
| 124 | }
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | /**
|
|---|
| 128 | * Synchronously waits on a promise to resolve and returns an inspection state
|
|---|
| 129 | * array.
|
|---|
| 130 | *
|
|---|
| 131 | * Returns a state associative array containing a "state" key mapping to a
|
|---|
| 132 | * valid promise state. If the state of the promise is "fulfilled", the array
|
|---|
| 133 | * will contain a "value" key mapping to the fulfilled value of the promise. If
|
|---|
| 134 | * the promise is rejected, the array will contain a "reason" key mapping to
|
|---|
| 135 | * the rejection reason of the promise.
|
|---|
| 136 | *
|
|---|
| 137 | * @param PromiseInterface $promise Promise or value.
|
|---|
| 138 | *
|
|---|
| 139 | * @return array
|
|---|
| 140 | */
|
|---|
| 141 | function inspect(PromiseInterface $promise)
|
|---|
| 142 | {
|
|---|
| 143 | try {
|
|---|
| 144 | return [
|
|---|
| 145 | 'state' => PromiseInterface::FULFILLED,
|
|---|
| 146 | 'value' => $promise->wait()
|
|---|
| 147 | ];
|
|---|
| 148 | } catch (RejectionException $e) {
|
|---|
| 149 | return ['state' => 'rejected', 'reason' => $e->getReason()];
|
|---|
| 150 | } catch (\Exception $e) {
|
|---|
| 151 | return ['state' => 'rejected', 'reason' => $e];
|
|---|
| 152 | }
|
|---|
| 153 | }
|
|---|
| 154 |
|
|---|
| 155 | /**
|
|---|
| 156 | * Waits on all of the provided promises, but does not unwrap rejected promises
|
|---|
| 157 | * as thrown exception.
|
|---|
| 158 | *
|
|---|
| 159 | * Returns an array of inspection state arrays.
|
|---|
| 160 | *
|
|---|
| 161 | * @param PromiseInterface[] $promises Traversable of promises to wait upon.
|
|---|
| 162 | *
|
|---|
| 163 | * @return array
|
|---|
| 164 | * @see GuzzleHttp\Promise\inspect for the inspection state array format.
|
|---|
| 165 | */
|
|---|
| 166 | function inspect_all($promises)
|
|---|
| 167 | {
|
|---|
| 168 | $results = [];
|
|---|
| 169 | foreach ($promises as $key => $promise) {
|
|---|
| 170 | $results[$key] = inspect($promise);
|
|---|
| 171 | }
|
|---|
| 172 |
|
|---|
| 173 | return $results;
|
|---|
| 174 | }
|
|---|
| 175 |
|
|---|
| 176 | /**
|
|---|
| 177 | * Waits on all of the provided promises and returns the fulfilled values.
|
|---|
| 178 | *
|
|---|
| 179 | * Returns an array that contains the value of each promise (in the same order
|
|---|
| 180 | * the promises were provided). An exception is thrown if any of the promises
|
|---|
| 181 | * are rejected.
|
|---|
| 182 | *
|
|---|
| 183 | * @param mixed $promises Iterable of PromiseInterface objects to wait on.
|
|---|
| 184 | *
|
|---|
| 185 | * @return array
|
|---|
| 186 | * @throws \Exception on error
|
|---|
| 187 | */
|
|---|
| 188 | function unwrap($promises)
|
|---|
| 189 | {
|
|---|
| 190 | $results = [];
|
|---|
| 191 | foreach ($promises as $key => $promise) {
|
|---|
| 192 | $results[$key] = $promise->wait();
|
|---|
| 193 | }
|
|---|
| 194 |
|
|---|
| 195 | return $results;
|
|---|
| 196 | }
|
|---|
| 197 |
|
|---|
| 198 | /**
|
|---|
| 199 | * Given an array of promises, return a promise that is fulfilled when all the
|
|---|
| 200 | * items in the array are fulfilled.
|
|---|
| 201 | *
|
|---|
| 202 | * The promise's fulfillment value is an array with fulfillment values at
|
|---|
| 203 | * respective positions to the original array. If any promise in the array
|
|---|
| 204 | * rejects, the returned promise is rejected with the rejection reason.
|
|---|
| 205 | *
|
|---|
| 206 | * @param mixed $promises Promises or values.
|
|---|
| 207 | *
|
|---|
| 208 | * @return Promise
|
|---|
| 209 | */
|
|---|
| 210 | function all($promises)
|
|---|
| 211 | {
|
|---|
| 212 | $results = [];
|
|---|
| 213 | return each(
|
|---|
| 214 | $promises,
|
|---|
| 215 | function ($value, $idx) use (&$results) {
|
|---|
| 216 | $results[$idx] = $value;
|
|---|
| 217 | },
|
|---|
| 218 | function ($reason, $idx, Promise $aggregate) {
|
|---|
| 219 | $aggregate->reject($reason);
|
|---|
| 220 | }
|
|---|
| 221 | )->then(function () use (&$results) {
|
|---|
| 222 | ksort($results);
|
|---|
| 223 | return $results;
|
|---|
| 224 | });
|
|---|
| 225 | }
|
|---|
| 226 |
|
|---|
| 227 | /**
|
|---|
| 228 | * Initiate a competitive race between multiple promises or values (values will
|
|---|
| 229 | * become immediately fulfilled promises).
|
|---|
| 230 | *
|
|---|
| 231 | * When count amount of promises have been fulfilled, the returned promise is
|
|---|
| 232 | * fulfilled with an array that contains the fulfillment values of the winners
|
|---|
| 233 | * in order of resolution.
|
|---|
| 234 | *
|
|---|
| 235 | * This prommise is rejected with a {@see GuzzleHttp\Promise\AggregateException}
|
|---|
| 236 | * if the number of fulfilled promises is less than the desired $count.
|
|---|
| 237 | *
|
|---|
| 238 | * @param int $count Total number of promises.
|
|---|
| 239 | * @param mixed $promises Promises or values.
|
|---|
| 240 | *
|
|---|
| 241 | * @return Promise
|
|---|
| 242 | */
|
|---|
| 243 | function some($count, $promises)
|
|---|
| 244 | {
|
|---|
| 245 | $results = [];
|
|---|
| 246 | $rejections = [];
|
|---|
| 247 |
|
|---|
| 248 | return each(
|
|---|
| 249 | $promises,
|
|---|
| 250 | function ($value, $idx, PromiseInterface $p) use (&$results, $count) {
|
|---|
| 251 | if ($p->getState() !== PromiseInterface::PENDING) {
|
|---|
| 252 | return;
|
|---|
| 253 | }
|
|---|
| 254 | $results[$idx] = $value;
|
|---|
| 255 | if (count($results) >= $count) {
|
|---|
| 256 | $p->resolve(null);
|
|---|
| 257 | }
|
|---|
| 258 | },
|
|---|
| 259 | function ($reason) use (&$rejections) {
|
|---|
| 260 | $rejections[] = $reason;
|
|---|
| 261 | }
|
|---|
| 262 | )->then(
|
|---|
| 263 | function () use (&$results, &$rejections, $count) {
|
|---|
| 264 | if (count($results) !== $count) {
|
|---|
| 265 | throw new AggregateException(
|
|---|
| 266 | 'Not enough promises to fulfill count',
|
|---|
| 267 | $rejections
|
|---|
| 268 | );
|
|---|
| 269 | }
|
|---|
| 270 | ksort($results);
|
|---|
| 271 | return array_values($results);
|
|---|
| 272 | }
|
|---|
| 273 | );
|
|---|
| 274 | }
|
|---|
| 275 |
|
|---|
| 276 | /**
|
|---|
| 277 | * Like some(), with 1 as count. However, if the promise fulfills, the
|
|---|
| 278 | * fulfillment value is not an array of 1 but the value directly.
|
|---|
| 279 | *
|
|---|
| 280 | * @param mixed $promises Promises or values.
|
|---|
| 281 | *
|
|---|
| 282 | * @return PromiseInterface
|
|---|
| 283 | */
|
|---|
| 284 | function any($promises)
|
|---|
| 285 | {
|
|---|
| 286 | return some(1, $promises)->then(function ($values) { return $values[0]; });
|
|---|
| 287 | }
|
|---|
| 288 |
|
|---|
| 289 | /**
|
|---|
| 290 | * Returns a promise that is fulfilled when all of the provided promises have
|
|---|
| 291 | * been fulfilled or rejected.
|
|---|
| 292 | *
|
|---|
| 293 | * The returned promise is fulfilled with an array of inspection state arrays.
|
|---|
| 294 | *
|
|---|
| 295 | * @param mixed $promises Promises or values.
|
|---|
| 296 | *
|
|---|
| 297 | * @return Promise
|
|---|
| 298 | * @see GuzzleHttp\Promise\inspect for the inspection state array format.
|
|---|
| 299 | */
|
|---|
| 300 | function settle($promises)
|
|---|
| 301 | {
|
|---|
| 302 | $results = [];
|
|---|
| 303 |
|
|---|
| 304 | return each(
|
|---|
| 305 | $promises,
|
|---|
| 306 | function ($value, $idx) use (&$results) {
|
|---|
| 307 | $results[$idx] = ['state' => 'fulfilled', 'value' => $value];
|
|---|
| 308 | },
|
|---|
| 309 | function ($reason, $idx) use (&$results) {
|
|---|
| 310 | $results[$idx] = ['state' => 'rejected', 'reason' => $reason];
|
|---|
| 311 | }
|
|---|
| 312 | )->then(function () use (&$results) {
|
|---|
| 313 | ksort($results);
|
|---|
| 314 | return $results;
|
|---|
| 315 | });
|
|---|
| 316 | }
|
|---|
| 317 |
|
|---|
| 318 | /**
|
|---|
| 319 | * Given an iterator that yields promises or values, returns a promise that is
|
|---|
| 320 | * fulfilled with a null value when the iterator has been consumed or the
|
|---|
| 321 | * aggregate promise has been fulfilled or rejected.
|
|---|
| 322 | *
|
|---|
| 323 | * $onFulfilled is a function that accepts the fulfilled value, iterator
|
|---|
| 324 | * index, and the aggregate promise. The callback can invoke any necessary side
|
|---|
| 325 | * effects and choose to resolve or reject the aggregate promise if needed.
|
|---|
| 326 | *
|
|---|
| 327 | * $onRejected is a function that accepts the rejection reason, iterator
|
|---|
| 328 | * index, and the aggregate promise. The callback can invoke any necessary side
|
|---|
| 329 | * effects and choose to resolve or reject the aggregate promise if needed.
|
|---|
| 330 | *
|
|---|
| 331 | * @param mixed $iterable Iterator or array to iterate over.
|
|---|
| 332 | * @param callable $onFulfilled
|
|---|
| 333 | * @param callable $onRejected
|
|---|
| 334 | *
|
|---|
| 335 | * @return Promise
|
|---|
| 336 | */
|
|---|
| 337 | function each(
|
|---|
| 338 | $iterable,
|
|---|
| 339 | callable $onFulfilled = null,
|
|---|
| 340 | callable $onRejected = null
|
|---|
| 341 | ) {
|
|---|
| 342 | return (new EachPromise($iterable, [
|
|---|
| 343 | 'fulfilled' => $onFulfilled,
|
|---|
| 344 | 'rejected' => $onRejected
|
|---|
| 345 | ]))->promise();
|
|---|
| 346 | }
|
|---|
| 347 |
|
|---|
| 348 | /**
|
|---|
| 349 | * Like each, but only allows a certain number of outstanding promises at any
|
|---|
| 350 | * given time.
|
|---|
| 351 | *
|
|---|
| 352 | * $concurrency may be an integer or a function that accepts the number of
|
|---|
| 353 | * pending promises and returns a numeric concurrency limit value to allow for
|
|---|
| 354 | * dynamic a concurrency size.
|
|---|
| 355 | *
|
|---|
| 356 | * @param mixed $iterable
|
|---|
| 357 | * @param int|callable $concurrency
|
|---|
| 358 | * @param callable $onFulfilled
|
|---|
| 359 | * @param callable $onRejected
|
|---|
| 360 | *
|
|---|
| 361 | * @return mixed
|
|---|
| 362 | */
|
|---|
| 363 | function each_limit(
|
|---|
| 364 | $iterable,
|
|---|
| 365 | $concurrency,
|
|---|
| 366 | callable $onFulfilled = null,
|
|---|
| 367 | callable $onRejected = null
|
|---|
| 368 | ) {
|
|---|
| 369 | return (new EachPromise($iterable, [
|
|---|
| 370 | 'fulfilled' => $onFulfilled,
|
|---|
| 371 | 'rejected' => $onRejected,
|
|---|
| 372 | 'concurrency' => $concurrency
|
|---|
| 373 | ]))->promise();
|
|---|
| 374 | }
|
|---|
| 375 |
|
|---|
| 376 | /**
|
|---|
| 377 | * Like each_limit, but ensures that no promise in the given $iterable argument
|
|---|
| 378 | * is rejected. If any promise is rejected, then the aggregate promise is
|
|---|
| 379 | * rejected with the encountered rejection.
|
|---|
| 380 | *
|
|---|
| 381 | * @param mixed $iterable
|
|---|
| 382 | * @param int|callable $concurrency
|
|---|
| 383 | * @param callable $onFulfilled
|
|---|
| 384 | *
|
|---|
| 385 | * @return mixed
|
|---|
| 386 | */
|
|---|
| 387 | function each_limit_all(
|
|---|
| 388 | $iterable,
|
|---|
| 389 | $concurrency,
|
|---|
| 390 | callable $onFulfilled = null
|
|---|
| 391 | ) {
|
|---|
| 392 | return each_limit(
|
|---|
| 393 | $iterable,
|
|---|
| 394 | $concurrency,
|
|---|
| 395 | $onFulfilled,
|
|---|
| 396 | function ($reason, $idx, PromiseInterface $aggregate) {
|
|---|
| 397 | $aggregate->reject($reason);
|
|---|
| 398 | }
|
|---|
| 399 | );
|
|---|
| 400 | }
|
|---|
| 401 |
|
|---|
| 402 | /**
|
|---|
| 403 | * Returns true if a promise is fulfilled.
|
|---|
| 404 | *
|
|---|
| 405 | * @param PromiseInterface $promise
|
|---|
| 406 | *
|
|---|
| 407 | * @return bool
|
|---|
| 408 | */
|
|---|
| 409 | function is_fulfilled(PromiseInterface $promise)
|
|---|
| 410 | {
|
|---|
| 411 | return $promise->getState() === PromiseInterface::FULFILLED;
|
|---|
| 412 | }
|
|---|
| 413 |
|
|---|
| 414 | /**
|
|---|
| 415 | * Returns true if a promise is rejected.
|
|---|
| 416 | *
|
|---|
| 417 | * @param PromiseInterface $promise
|
|---|
| 418 | *
|
|---|
| 419 | * @return bool
|
|---|
| 420 | */
|
|---|
| 421 | function is_rejected(PromiseInterface $promise)
|
|---|
| 422 | {
|
|---|
| 423 | return $promise->getState() === PromiseInterface::REJECTED;
|
|---|
| 424 | }
|
|---|
| 425 |
|
|---|
| 426 | /**
|
|---|
| 427 | * Returns true if a promise is fulfilled or rejected.
|
|---|
| 428 | *
|
|---|
| 429 | * @param PromiseInterface $promise
|
|---|
| 430 | *
|
|---|
| 431 | * @return bool
|
|---|
| 432 | */
|
|---|
| 433 | function is_settled(PromiseInterface $promise)
|
|---|
| 434 | {
|
|---|
| 435 | return $promise->getState() !== PromiseInterface::PENDING;
|
|---|
| 436 | }
|
|---|
| 437 |
|
|---|
| 438 | /**
|
|---|
| 439 | * Creates a promise that is resolved using a generator that yields values or
|
|---|
| 440 | * promises (somewhat similar to C#'s async keyword).
|
|---|
| 441 | *
|
|---|
| 442 | * When called, the coroutine function will start an instance of the generator
|
|---|
| 443 | * and returns a promise that is fulfilled with its final yielded value.
|
|---|
| 444 | *
|
|---|
| 445 | * Control is returned back to the generator when the yielded promise settles.
|
|---|
| 446 | * This can lead to less verbose code when doing lots of sequential async calls
|
|---|
| 447 | * with minimal processing in between.
|
|---|
| 448 | *
|
|---|
| 449 | * use GuzzleHttp\Promise;
|
|---|
| 450 | *
|
|---|
| 451 | * function createPromise($value) {
|
|---|
| 452 | * return new Promise\FulfilledPromise($value);
|
|---|
| 453 | * }
|
|---|
| 454 | *
|
|---|
| 455 | * $promise = Promise\coroutine(function () {
|
|---|
| 456 | * $value = (yield createPromise('a'));
|
|---|
| 457 | * try {
|
|---|
| 458 | * $value = (yield createPromise($value . 'b'));
|
|---|
| 459 | * } catch (\Exception $e) {
|
|---|
| 460 | * // The promise was rejected.
|
|---|
| 461 | * }
|
|---|
| 462 | * yield $value . 'c';
|
|---|
| 463 | * });
|
|---|
| 464 | *
|
|---|
| 465 | * // Outputs "abc"
|
|---|
| 466 | * $promise->then(function ($v) { echo $v; });
|
|---|
| 467 | *
|
|---|
| 468 | * @param callable $generatorFn Generator function to wrap into a promise.
|
|---|
| 469 | *
|
|---|
| 470 | * @return Promise
|
|---|
| 471 | * @link https://github.com/petkaantonov/bluebird/blob/master/API.md#generators inspiration
|
|---|
| 472 | */
|
|---|
| 473 | function coroutine(callable $generatorFn)
|
|---|
| 474 | {
|
|---|
| 475 | $generator = $generatorFn();
|
|---|
| 476 | return __next_coroutine($generator->current(), $generator)->then();
|
|---|
| 477 | }
|
|---|
| 478 |
|
|---|
| 479 | /** @internal */
|
|---|
| 480 | function __next_coroutine($yielded, \Generator $generator)
|
|---|
| 481 | {
|
|---|
| 482 | return promise_for($yielded)->then(
|
|---|
| 483 | function ($value) use ($generator) {
|
|---|
| 484 | $nextYield = $generator->send($value);
|
|---|
| 485 | return $generator->valid()
|
|---|
| 486 | ? __next_coroutine($nextYield, $generator)
|
|---|
| 487 | : $value;
|
|---|
| 488 | },
|
|---|
| 489 | function ($reason) use ($generator) {
|
|---|
| 490 | $nextYield = $generator->throw(exception_for($reason));
|
|---|
| 491 | // The throw was caught, so keep iterating on the coroutine
|
|---|
| 492 | return __next_coroutine($nextYield, $generator);
|
|---|
| 493 | }
|
|---|
| 494 | );
|
|---|
| 495 | }
|
|---|