Open Monograph Press  3.3.0
guzzlehttp/promises/src/functions.php
1 <?php
2 namespace GuzzleHttp\Promise;
3 
21 function queue(TaskQueueInterface $assign = null)
22 {
23  static $queue;
24 
25  if ($assign) {
26  $queue = $assign;
27  } elseif (!$queue) {
28  $queue = new TaskQueue();
29  }
30 
31  return $queue;
32 }
33 
42 function task(callable $task)
43 {
44  $queue = queue();
45  $promise = new Promise([$queue, 'run']);
46  $queue->add(function () use ($task, $promise) {
47  try {
48  $promise->resolve($task());
49  } catch (\Throwable $e) {
50  $promise->reject($e);
51  } catch (\Exception $e) {
52  $promise->reject($e);
53  }
54  });
55 
56  return $promise;
57 }
58 
66 function promise_for($value)
67 {
68  if ($value instanceof PromiseInterface) {
69  return $value;
70  }
71 
72  // Return a Guzzle promise that shadows the given promise.
73  if (method_exists($value, 'then')) {
74  $wfn = method_exists($value, 'wait') ? [$value, 'wait'] : null;
75  $cfn = method_exists($value, 'cancel') ? [$value, 'cancel'] : null;
76  $promise = new Promise($wfn, $cfn);
77  $value->then([$promise, 'resolve'], [$promise, 'reject']);
78  return $promise;
79  }
80 
81  return new FulfilledPromise($value);
82 }
83 
92 function rejection_for($reason)
93 {
94  if ($reason instanceof PromiseInterface) {
95  return $reason;
96  }
97 
98  return new RejectedPromise($reason);
99 }
100 
108 function exception_for($reason)
109 {
110  return $reason instanceof \Exception || $reason instanceof \Throwable
111  ? $reason
112  : new RejectionException($reason);
113 }
114 
122 function iter_for($value)
123 {
124  if ($value instanceof \Iterator) {
125  return $value;
126  } elseif (is_array($value)) {
127  return new \ArrayIterator($value);
128  } else {
129  return new \ArrayIterator([$value]);
130  }
131 }
132 
147 function inspect(PromiseInterface $promise)
148 {
149  try {
150  return [
151  'state' => PromiseInterface::FULFILLED,
152  'value' => $promise->wait()
153  ];
154  } catch (RejectionException $e) {
155  return ['state' => PromiseInterface::REJECTED, 'reason' => $e->getReason()];
156  } catch (\Throwable $e) {
157  return ['state' => PromiseInterface::REJECTED, 'reason' => $e];
158  } catch (\Exception $e) {
159  return ['state' => PromiseInterface::REJECTED, 'reason' => $e];
160  }
161 }
162 
174 function inspect_all($promises)
175 {
176  $results = [];
177  foreach ($promises as $key => $promise) {
178  $results[$key] = inspect($promise);
179  }
180 
181  return $results;
182 }
183 
197 function unwrap($promises)
198 {
199  $results = [];
200  foreach ($promises as $key => $promise) {
201  $results[$key] = $promise->wait();
202  }
203 
204  return $results;
205 }
206 
219 function all($promises)
220 {
221  $results = [];
222  return each(
223  $promises,
224  function ($value, $idx) use (&$results) {
225  $results[$idx] = $value;
226  },
227  function ($reason, $idx, Promise $aggregate) {
228  $aggregate->reject($reason);
229  }
230  )->then(function () use (&$results) {
231  ksort($results);
232  return $results;
233  });
234 }
235 
252 function some($count, $promises)
253 {
254  $results = [];
255  $rejections = [];
256 
257  return each(
258  $promises,
259  function ($value, $idx, PromiseInterface $p) use (&$results, $count) {
260  if ($p->getState() !== PromiseInterface::PENDING) {
261  return;
262  }
263  $results[$idx] = $value;
264  if (count($results) >= $count) {
265  $p->resolve(null);
266  }
267  },
268  function ($reason) use (&$rejections) {
269  $rejections[] = $reason;
270  }
271  )->then(
272  function () use (&$results, &$rejections, $count) {
273  if (count($results) !== $count) {
274  throw new AggregateException(
275  'Not enough promises to fulfill count',
276  $rejections
277  );
278  }
279  ksort($results);
280  return array_values($results);
281  }
282  );
283 }
284 
293 function any($promises)
294 {
295  return some(1, $promises)->then(function ($values) { return $values[0]; });
296 }
297 
309 function settle($promises)
310 {
311  $results = [];
312 
313  return each(
314  $promises,
315  function ($value, $idx) use (&$results) {
316  $results[$idx] = ['state' => PromiseInterface::FULFILLED, 'value' => $value];
317  },
318  function ($reason, $idx) use (&$results) {
319  $results[$idx] = ['state' => PromiseInterface::REJECTED, 'reason' => $reason];
320  }
321  )->then(function () use (&$results) {
322  ksort($results);
323  return $results;
324  });
325 }
326 
346 function each(
347  $iterable,
348  callable $onFulfilled = null,
349  callable $onRejected = null
350 ) {
351  return (new EachPromise($iterable, [
352  'fulfilled' => $onFulfilled,
353  'rejected' => $onRejected
354  ]))->promise();
355 }
356 
372 function each_limit(
373  $iterable,
374  $concurrency,
375  callable $onFulfilled = null,
376  callable $onRejected = null
377 ) {
378  return (new EachPromise($iterable, [
379  'fulfilled' => $onFulfilled,
380  'rejected' => $onRejected,
381  'concurrency' => $concurrency
382  ]))->promise();
383 }
384 
396 function each_limit_all(
397  $iterable,
398  $concurrency,
399  callable $onFulfilled = null
400 ) {
401  return each_limit(
402  $iterable,
403  $concurrency,
404  $onFulfilled,
405  function ($reason, $idx, PromiseInterface $aggregate) {
406  $aggregate->reject($reason);
407  }
408  );
409 }
410 
418 function is_fulfilled(PromiseInterface $promise)
419 {
420  return $promise->getState() === PromiseInterface::FULFILLED;
421 }
422 
430 function is_rejected(PromiseInterface $promise)
431 {
432  return $promise->getState() === PromiseInterface::REJECTED;
433 }
434 
442 function is_settled(PromiseInterface $promise)
443 {
444  return $promise->getState() !== PromiseInterface::PENDING;
445 }
446 
454 function coroutine(callable $generatorFn)
455 {
456  return new Coroutine($generatorFn);
457 }
GuzzleHttp\Promise\promise_for
promise_for($value)
Definition: guzzlehttp/promises/src/functions.php:66
GuzzleHttp\Promise\PromiseInterface\wait
wait($unwrap=true)
GuzzleHttp\Promise\each_limit_all
each_limit_all( $iterable, $concurrency, callable $onFulfilled=null)
Definition: guzzlehttp/promises/src/functions.php:396
GuzzleHttp\Promise\each_limit
each_limit( $iterable, $concurrency, callable $onFulfilled=null, callable $onRejected=null)
Definition: guzzlehttp/promises/src/functions.php:372
GuzzleHttp\Promise\PromiseInterface
Definition: PromiseInterface.php:13
GuzzleHttp\Promise\RejectionException\getReason
getReason()
Definition: RejectionException.php:46
GuzzleHttp\Promise\rejection_for
rejection_for($reason)
Definition: guzzlehttp/promises/src/functions.php:92
GuzzleHttp\Promise\PromiseInterface\reject
reject($reason)
GuzzleHttp\Promise\some
some($count, $promises)
Definition: guzzlehttp/promises/src/functions.php:252
GuzzleHttp\Promise\PromiseInterface\resolve
resolve($value)
GuzzleHttp\Promise\any
any($promises)
Definition: guzzlehttp/promises/src/functions.php:293
GuzzleHttp\Promise\inspect
inspect(PromiseInterface $promise)
Definition: guzzlehttp/promises/src/functions.php:147
GuzzleHttp\Promise\is_settled
is_settled(PromiseInterface $promise)
Definition: guzzlehttp/promises/src/functions.php:442
GuzzleHttp\Promise\AggregateException
Definition: AggregateException.php:7
GuzzleHttp\Promise\all
all($promises)
Definition: guzzlehttp/promises/src/functions.php:219
GuzzleHttp\Promise\Promise\then
then(callable $onFulfilled=null, callable $onRejected=null)
Definition: guzzlehttp/promises/src/Promise.php:30
GuzzleHttp\Promise\is_rejected
is_rejected(PromiseInterface $promise)
Definition: guzzlehttp/promises/src/functions.php:430
GuzzleHttp\Promise\RejectionException
Definition: RejectionException.php:9
GuzzleHttp\Promise\queue
queue(TaskQueueInterface $assign=null)
Definition: guzzlehttp/promises/src/functions.php:21
GuzzleHttp\Promise\task
task(callable $task)
Definition: guzzlehttp/promises/src/functions.php:42
GuzzleHttp\Promise\each
each( $iterable, callable $onFulfilled=null, callable $onRejected=null)
Definition: guzzlehttp/promises/src/functions.php:346
GuzzleHttp\Promise\iter_for
iter_for($value)
Definition: guzzlehttp/promises/src/functions.php:122
GuzzleHttp\Promise\PromiseInterface\FULFILLED
const FULFILLED
Definition: PromiseInterface.php:16
GuzzleHttp\Promise\PromiseInterface\PENDING
const PENDING
Definition: PromiseInterface.php:15
GuzzleHttp\Promise\Coroutine
Definition: Coroutine.php:43
GuzzleHttp\Promise\settle
settle($promises)
Definition: guzzlehttp/promises/src/functions.php:309
GuzzleHttp\Promise\PromiseInterface\getState
getState()
GuzzleHttp\Promise\is_fulfilled
is_fulfilled(PromiseInterface $promise)
Definition: guzzlehttp/promises/src/functions.php:418
GuzzleHttp\Promise\EachPromise
Definition: EachPromise.php:8
GuzzleHttp\Promise\PromiseInterface\REJECTED
const REJECTED
Definition: PromiseInterface.php:17
GuzzleHttp\Promise\FulfilledPromise
Definition: guzzlehttp/promises/src/FulfilledPromise.php:10
GuzzleHttp\Promise\TaskQueue
Definition: TaskQueue.php:13
GuzzleHttp\Promise\inspect_all
inspect_all($promises)
Definition: guzzlehttp/promises/src/functions.php:174
GuzzleHttp\Promise\TaskQueueInterface
Definition: TaskQueueInterface.php:4
GuzzleHttp\Promise\Promise
Definition: guzzlehttp/promises/src/Promise.php:9
GuzzleHttp\Promise\RejectedPromise
Definition: guzzlehttp/promises/src/RejectedPromise.php:10
GuzzleHttp\Promise
Definition: AggregateException.php:2
GuzzleHttp\Promise\coroutine
coroutine(callable $generatorFn)
Definition: guzzlehttp/promises/src/functions.php:454
GuzzleHttp\Promise\unwrap
unwrap($promises)
Definition: guzzlehttp/promises/src/functions.php:197
GuzzleHttp\Promise\exception_for
exception_for($reason)
Definition: guzzlehttp/promises/src/functions.php:108