京东云

This commit is contained in:
“wanyongkang”
2024-05-28 17:04:50 +08:00
parent f644932afb
commit f3c7432100
672 changed files with 209344 additions and 6593 deletions

View File

@@ -1,43 +1,17 @@
# CHANGELOG
## 2.0.1 - 2023-08-03
### Changed
- PHP 8.3 support
## 2.0.0 - 2023-05-21
### Added
- Added PHP 7 type hints
### Changed
- All previously non-final non-exception classes have been marked as soft-final
### Removed
- Dropped PHP < 7.2 support
- All functions in the `GuzzleHttp\Promise` namespace
## 1.5.3 - 2023-05-21
### Changed
- Removed remaining usage of deprecated functions
## 1.5.2 - 2022-08-07
### Changed
- Officially support PHP 8.2
## 1.5.1 - 2021-10-22
### Fixed
@@ -45,7 +19,6 @@
- Revert "Call handler when waiting on fulfilled/rejected Promise"
- Fix pool memory leak when empty array of promises provided
## 1.5.0 - 2021-10-07
### Changed
@@ -57,14 +30,12 @@
- Fix manually settle promises generated with `Utils::task`
## 1.4.1 - 2021-02-18
### Fixed
- Fixed `each_limit` skipping promises and failing
## 1.4.0 - 2020-09-30
### Added

View File

@@ -29,21 +29,6 @@ for a general introduction to promises.
`GuzzleHttp\Promise\Coroutine::of()`.
## Installation
```shell
composer require guzzlehttp/promises
```
## Version Guidance
| Version | Status | PHP Version |
|---------|------------------------|--------------|
| 1.x | Bug and security fixes | >=5.5,<8.3 |
| 2.x | Latest | >=7.2.5,<8.4 |
## Quick Start
A *promise* represents the eventual result of an asynchronous operation. The
@@ -445,6 +430,8 @@ $loop = React\EventLoop\Factory::create();
$loop->addPeriodicTimer(0, [$queue, 'run']);
```
*TODO*: Perhaps adding a `futureTick()` on each tick would be faster?
## Implementation Notes
@@ -514,8 +501,8 @@ $promise->resolve('foo');
A static API was first introduced in 1.4.0, in order to mitigate problems with
functions conflicting between global and local copies of the package. The
function API was removed in 2.0.0. A migration table has been provided here for
your convenience:
function API will be removed in 2.0.0. A migration table has been provided here
for your convenience:
| Original Function | Replacement Method |
|----------------|----------------|

View File

@@ -26,32 +26,27 @@
}
],
"require": {
"php": "^7.2.5 || ^8.0"
"php": ">=5.5"
},
"require-dev": {
"bamarni/composer-bin-plugin": "^1.8.1",
"phpunit/phpunit": "^8.5.29 || ^9.5.23"
"symfony/phpunit-bridge": "^4.4 || ^5.1"
},
"autoload": {
"psr-4": {
"GuzzleHttp\\Promise\\": "src/"
}
},
"files": ["src/functions_include.php"]
},
"autoload-dev": {
"psr-4": {
"GuzzleHttp\\Promise\\Tests\\": "tests/"
}
},
"extra": {
"bamarni-bin": {
"bin-links": true,
"forward-command": false
}
"scripts": {
"test": "vendor/bin/simple-phpunit",
"test-ci": "vendor/bin/simple-phpunit --coverage-text"
},
"config": {
"allow-plugins": {
"bamarni/composer-bin-plugin": true
},
"preferred-install": "dist",
"sort-packages": true
}

View File

@@ -1,7 +1,5 @@
<?php
declare(strict_types=1);
namespace GuzzleHttp\Promise;
/**
@@ -9,7 +7,7 @@ namespace GuzzleHttp\Promise;
*/
class AggregateException extends RejectionException
{
public function __construct(string $msg, array $reasons)
public function __construct($msg, array $reasons)
{
parent::__construct(
$reasons,

View File

@@ -1,7 +1,5 @@
<?php
declare(strict_types=1);
namespace GuzzleHttp\Promise;
/**

View File

@@ -1,9 +1,8 @@
<?php
declare(strict_types=1);
namespace GuzzleHttp\Promise;
use Exception;
use Generator;
use Throwable;
@@ -28,7 +27,7 @@ use Throwable;
* $value = (yield createPromise('a'));
* try {
* $value = (yield createPromise($value . 'b'));
* } catch (\Throwable $e) {
* } catch (\Exception $e) {
* // The promise was rejected.
* }
* yield $value . 'c';
@@ -41,7 +40,7 @@ use Throwable;
*
* @return Promise
*
* @see https://github.com/petkaantonov/bluebird/blob/master/API.md#generators inspiration
* @link https://github.com/petkaantonov/bluebird/blob/master/API.md#generators inspiration
*/
final class Coroutine implements PromiseInterface
{
@@ -63,13 +62,15 @@ final class Coroutine implements PromiseInterface
public function __construct(callable $generatorFn)
{
$this->generator = $generatorFn();
$this->result = new Promise(function (): void {
$this->result = new Promise(function () {
while (isset($this->currentPromise)) {
$this->currentPromise->wait();
}
});
try {
$this->nextCoroutine($this->generator->current());
} catch (\Exception $exception) {
$this->result->reject($exception);
} catch (Throwable $throwable) {
$this->result->reject($throwable);
}
@@ -77,8 +78,10 @@ final class Coroutine implements PromiseInterface
/**
* Create a new coroutine.
*
* @return self
*/
public static function of(callable $generatorFn): self
public static function of(callable $generatorFn)
{
return new self($generatorFn);
}
@@ -86,42 +89,42 @@ final class Coroutine implements PromiseInterface
public function then(
callable $onFulfilled = null,
callable $onRejected = null
): PromiseInterface {
) {
return $this->result->then($onFulfilled, $onRejected);
}
public function otherwise(callable $onRejected): PromiseInterface
public function otherwise(callable $onRejected)
{
return $this->result->otherwise($onRejected);
}
public function wait(bool $unwrap = true)
public function wait($unwrap = true)
{
return $this->result->wait($unwrap);
}
public function getState(): string
public function getState()
{
return $this->result->getState();
}
public function resolve($value): void
public function resolve($value)
{
$this->result->resolve($value);
}
public function reject($reason): void
public function reject($reason)
{
$this->result->reject($reason);
}
public function cancel(): void
public function cancel()
{
$this->currentPromise->cancel();
$this->result->cancel();
}
private function nextCoroutine($yielded): void
private function nextCoroutine($yielded)
{
$this->currentPromise = Create::promiseFor($yielded)
->then([$this, '_handleSuccess'], [$this, '_handleFailure']);
@@ -130,7 +133,7 @@ final class Coroutine implements PromiseInterface
/**
* @internal
*/
public function _handleSuccess($value): void
public function _handleSuccess($value)
{
unset($this->currentPromise);
try {
@@ -140,6 +143,8 @@ final class Coroutine implements PromiseInterface
} else {
$this->result->resolve($value);
}
} catch (Exception $exception) {
$this->result->reject($exception);
} catch (Throwable $throwable) {
$this->result->reject($throwable);
}
@@ -148,13 +153,15 @@ final class Coroutine implements PromiseInterface
/**
* @internal
*/
public function _handleFailure($reason): void
public function _handleFailure($reason)
{
unset($this->currentPromise);
try {
$nextYield = $this->generator->throw(Create::exceptionFor($reason));
// The throw was caught, so keep iterating on the coroutine
$this->nextCoroutine($nextYield);
} catch (Exception $exception) {
$this->result->reject($exception);
} catch (Throwable $throwable) {
$this->result->reject($throwable);
}

View File

@@ -1,7 +1,5 @@
<?php
declare(strict_types=1);
namespace GuzzleHttp\Promise;
final class Create
@@ -10,8 +8,10 @@ final class Create
* Creates a promise for a value if the value is not a promise.
*
* @param mixed $value Promise or value.
*
* @return PromiseInterface
*/
public static function promiseFor($value): PromiseInterface
public static function promiseFor($value)
{
if ($value instanceof PromiseInterface) {
return $value;
@@ -23,7 +23,6 @@ final class Create
$cfn = method_exists($value, 'cancel') ? [$value, 'cancel'] : null;
$promise = new Promise($wfn, $cfn);
$value->then([$promise, 'resolve'], [$promise, 'reject']);
return $promise;
}
@@ -35,8 +34,10 @@ final class Create
* If the provided reason is a promise, then it is returned as-is.
*
* @param mixed $reason Promise or reason.
*
* @return PromiseInterface
*/
public static function rejectionFor($reason): PromiseInterface
public static function rejectionFor($reason)
{
if ($reason instanceof PromiseInterface) {
return $reason;
@@ -49,10 +50,12 @@ final class Create
* Create an exception for a rejected promise value.
*
* @param mixed $reason
*
* @return \Exception|\Throwable
*/
public static function exceptionFor($reason): \Throwable
public static function exceptionFor($reason)
{
if ($reason instanceof \Throwable) {
if ($reason instanceof \Exception || $reason instanceof \Throwable) {
return $reason;
}
@@ -63,8 +66,10 @@ final class Create
* Returns an iterator for the given value.
*
* @param mixed $value
*
* @return \Iterator
*/
public static function iterFor($value): \Iterator
public static function iterFor($value)
{
if ($value instanceof \Iterator) {
return $value;

View File

@@ -1,7 +1,5 @@
<?php
declare(strict_types=1);
namespace GuzzleHttp\Promise;
final class Each
@@ -22,15 +20,17 @@ final class Each
* @param mixed $iterable Iterator or array to iterate over.
* @param callable $onFulfilled
* @param callable $onRejected
*
* @return PromiseInterface
*/
public static function of(
$iterable,
callable $onFulfilled = null,
callable $onRejected = null
): PromiseInterface {
) {
return (new EachPromise($iterable, [
'fulfilled' => $onFulfilled,
'rejected' => $onRejected,
'rejected' => $onRejected
]))->promise();
}
@@ -46,17 +46,19 @@ final class Each
* @param int|callable $concurrency
* @param callable $onFulfilled
* @param callable $onRejected
*
* @return PromiseInterface
*/
public static function ofLimit(
$iterable,
$concurrency,
callable $onFulfilled = null,
callable $onRejected = null
): PromiseInterface {
) {
return (new EachPromise($iterable, [
'fulfilled' => $onFulfilled,
'rejected' => $onRejected,
'concurrency' => $concurrency,
'fulfilled' => $onFulfilled,
'rejected' => $onRejected,
'concurrency' => $concurrency
]))->promise();
}
@@ -68,17 +70,19 @@ final class Each
* @param mixed $iterable
* @param int|callable $concurrency
* @param callable $onFulfilled
*
* @return PromiseInterface
*/
public static function ofLimitAll(
$iterable,
$concurrency,
callable $onFulfilled = null
): PromiseInterface {
) {
return self::ofLimit(
$iterable,
$concurrency,
$onFulfilled,
function ($reason, $idx, PromiseInterface $aggregate): void {
function ($reason, $idx, PromiseInterface $aggregate) {
$aggregate->reject($reason);
}
);

View File

@@ -1,14 +1,10 @@
<?php
declare(strict_types=1);
namespace GuzzleHttp\Promise;
/**
* Represents a promise that iterates over many promises and invokes
* side-effect functions in the process.
*
* @final
*/
class EachPromise implements PromisorInterface
{
@@ -73,7 +69,7 @@ class EachPromise implements PromisorInterface
}
/** @psalm-suppress InvalidNullableReturnType */
public function promise(): PromiseInterface
public function promise()
{
if ($this->aggregate) {
return $this->aggregate;
@@ -86,18 +82,21 @@ class EachPromise implements PromisorInterface
$this->refillPending();
} catch (\Throwable $e) {
$this->aggregate->reject($e);
} catch (\Exception $e) {
$this->aggregate->reject($e);
}
/**
* @psalm-suppress NullableReturnStatement
* @phpstan-ignore-next-line
*/
return $this->aggregate;
}
private function createPromise(): void
private function createPromise()
{
$this->mutex = false;
$this->aggregate = new Promise(function (): void {
$this->aggregate = new Promise(function () {
if ($this->checkIfFinished()) {
return;
}
@@ -114,7 +113,7 @@ class EachPromise implements PromisorInterface
});
// Clear the references when the promise is resolved.
$clearFn = function (): void {
$clearFn = function () {
$this->iterable = $this->concurrency = $this->pending = null;
$this->onFulfilled = $this->onRejected = null;
$this->nextPendingIndex = 0;
@@ -123,13 +122,11 @@ class EachPromise implements PromisorInterface
$this->aggregate->then($clearFn, $clearFn);
}
private function refillPending(): void
private function refillPending()
{
if (!$this->concurrency) {
// Add all pending promises.
while ($this->addPending() && $this->advanceIterator()) {
}
while ($this->addPending() && $this->advanceIterator());
return;
}
@@ -150,11 +147,10 @@ class EachPromise implements PromisorInterface
// next value to yield until promise callbacks are called.
while (--$concurrency
&& $this->advanceIterator()
&& $this->addPending()) {
}
&& $this->addPending());
}
private function addPending(): bool
private function addPending()
{
if (!$this->iterable || !$this->iterable->valid()) {
return false;
@@ -168,7 +164,7 @@ class EachPromise implements PromisorInterface
$idx = $this->nextPendingIndex++;
$this->pending[$idx] = $promise->then(
function ($value) use ($idx, $key): void {
function ($value) use ($idx, $key) {
if ($this->onFulfilled) {
call_user_func(
$this->onFulfilled,
@@ -179,7 +175,7 @@ class EachPromise implements PromisorInterface
}
$this->step($idx);
},
function ($reason) use ($idx, $key): void {
function ($reason) use ($idx, $key) {
if ($this->onRejected) {
call_user_func(
$this->onRejected,
@@ -195,7 +191,7 @@ class EachPromise implements PromisorInterface
return true;
}
private function advanceIterator(): bool
private function advanceIterator()
{
// Place a lock on the iterator so that we ensure to not recurse,
// preventing fatal generator errors.
@@ -208,17 +204,19 @@ class EachPromise implements PromisorInterface
try {
$this->iterable->next();
$this->mutex = false;
return true;
} catch (\Throwable $e) {
$this->aggregate->reject($e);
$this->mutex = false;
return false;
} catch (\Exception $e) {
$this->aggregate->reject($e);
$this->mutex = false;
return false;
}
}
private function step(int $idx): void
private function step($idx)
{
// If the promise was already resolved, then ignore this step.
if (Is::settled($this->aggregate)) {
@@ -236,12 +234,11 @@ class EachPromise implements PromisorInterface
}
}
private function checkIfFinished(): bool
private function checkIfFinished()
{
if (!$this->pending && !$this->iterable->valid()) {
// Resolve the promise if there's nothing left to do.
$this->aggregate->resolve(null);
return true;
}

View File

@@ -1,7 +1,5 @@
<?php
declare(strict_types=1);
namespace GuzzleHttp\Promise;
/**
@@ -9,16 +7,11 @@ namespace GuzzleHttp\Promise;
*
* Thenning off of this promise will invoke the onFulfilled callback
* immediately and ignore other callbacks.
*
* @final
*/
class FulfilledPromise implements PromiseInterface
{
private $value;
/**
* @param mixed $value
*/
public function __construct($value)
{
if (is_object($value) && method_exists($value, 'then')) {
@@ -33,7 +26,7 @@ class FulfilledPromise implements PromiseInterface
public function then(
callable $onFulfilled = null,
callable $onRejected = null
): PromiseInterface {
) {
// Return itself if there is no onFulfilled function.
if (!$onFulfilled) {
return $this;
@@ -42,12 +35,14 @@ class FulfilledPromise implements PromiseInterface
$queue = Utils::queue();
$p = new Promise([$queue, 'run']);
$value = $this->value;
$queue->add(static function () use ($p, $value, $onFulfilled): void {
$queue->add(static function () use ($p, $value, $onFulfilled) {
if (Is::pending($p)) {
try {
$p->resolve($onFulfilled($value));
} catch (\Throwable $e) {
$p->reject($e);
} catch (\Exception $e) {
$p->reject($e);
}
}
});
@@ -55,34 +50,34 @@ class FulfilledPromise implements PromiseInterface
return $p;
}
public function otherwise(callable $onRejected): PromiseInterface
public function otherwise(callable $onRejected)
{
return $this->then(null, $onRejected);
}
public function wait(bool $unwrap = true)
public function wait($unwrap = true, $defaultDelivery = null)
{
return $unwrap ? $this->value : null;
}
public function getState(): string
public function getState()
{
return self::FULFILLED;
}
public function resolve($value): void
public function resolve($value)
{
if ($value !== $this->value) {
throw new \LogicException('Cannot resolve a fulfilled promise');
throw new \LogicException("Cannot resolve a fulfilled promise");
}
}
public function reject($reason): void
public function reject($reason)
{
throw new \LogicException('Cannot reject a fulfilled promise');
throw new \LogicException("Cannot reject a fulfilled promise");
}
public function cancel(): void
public function cancel()
{
// pass
}

View File

@@ -1,39 +1,45 @@
<?php
declare(strict_types=1);
namespace GuzzleHttp\Promise;
final class Is
{
/**
* Returns true if a promise is pending.
*
* @return bool
*/
public static function pending(PromiseInterface $promise): bool
public static function pending(PromiseInterface $promise)
{
return $promise->getState() === PromiseInterface::PENDING;
}
/**
* Returns true if a promise is fulfilled or rejected.
*
* @return bool
*/
public static function settled(PromiseInterface $promise): bool
public static function settled(PromiseInterface $promise)
{
return $promise->getState() !== PromiseInterface::PENDING;
}
/**
* Returns true if a promise is fulfilled.
*
* @return bool
*/
public static function fulfilled(PromiseInterface $promise): bool
public static function fulfilled(PromiseInterface $promise)
{
return $promise->getState() === PromiseInterface::FULFILLED;
}
/**
* Returns true if a promise is rejected.
*
* @return bool
*/
public static function rejected(PromiseInterface $promise): bool
public static function rejected(PromiseInterface $promise)
{
return $promise->getState() === PromiseInterface::REJECTED;
}

View File

@@ -1,15 +1,11 @@
<?php
declare(strict_types=1);
namespace GuzzleHttp\Promise;
/**
* Promises/A+ implementation that avoids recursion when possible.
*
* @see https://promisesaplus.com/
*
* @final
* @link https://promisesaplus.com/
*/
class Promise implements PromiseInterface
{
@@ -35,36 +31,33 @@ class Promise implements PromiseInterface
public function then(
callable $onFulfilled = null,
callable $onRejected = null
): PromiseInterface {
) {
if ($this->state === self::PENDING) {
$p = new Promise(null, [$this, 'cancel']);
$this->handlers[] = [$p, $onFulfilled, $onRejected];
$p->waitList = $this->waitList;
$p->waitList[] = $this;
return $p;
}
// Return a fulfilled promise and immediately invoke any callbacks.
if ($this->state === self::FULFILLED) {
$promise = Create::promiseFor($this->result);
return $onFulfilled ? $promise->then($onFulfilled) : $promise;
}
// It's either cancelled or rejected, so return a rejected promise
// and immediately invoke any callbacks.
$rejection = Create::rejectionFor($this->result);
return $onRejected ? $rejection->then(null, $onRejected) : $rejection;
}
public function otherwise(callable $onRejected): PromiseInterface
public function otherwise(callable $onRejected)
{
return $this->then(null, $onRejected);
}
public function wait(bool $unwrap = true)
public function wait($unwrap = true)
{
$this->waitIfPending();
@@ -80,12 +73,12 @@ class Promise implements PromiseInterface
}
}
public function getState(): string
public function getState()
{
return $this->state;
}
public function cancel(): void
public function cancel()
{
if ($this->state !== self::PENDING) {
return;
@@ -100,6 +93,8 @@ class Promise implements PromiseInterface
$fn();
} catch (\Throwable $e) {
$this->reject($e);
} catch (\Exception $e) {
$this->reject($e);
}
}
@@ -110,17 +105,17 @@ class Promise implements PromiseInterface
}
}
public function resolve($value): void
public function resolve($value)
{
$this->settle(self::FULFILLED, $value);
}
public function reject($reason): void
public function reject($reason)
{
$this->settle(self::REJECTED, $reason);
}
private function settle(string $state, $value): void
private function settle($state, $value)
{
if ($this->state !== self::PENDING) {
// Ignore calls with the same resolution.
@@ -153,7 +148,7 @@ class Promise implements PromiseInterface
if (!is_object($value) || !method_exists($value, 'then')) {
$id = $state === self::FULFILLED ? 1 : 2;
// It's a success, so resolve the handlers in the queue.
Utils::queue()->add(static function () use ($id, $value, $handlers): void {
Utils::queue()->add(static function () use ($id, $value, $handlers) {
foreach ($handlers as $handler) {
self::callHandler($id, $value, $handler);
}
@@ -164,12 +159,12 @@ class Promise implements PromiseInterface
} else {
// Resolve the handlers when the forwarded promise is resolved.
$value->then(
static function ($value) use ($handlers): void {
static function ($value) use ($handlers) {
foreach ($handlers as $handler) {
self::callHandler(1, $value, $handler);
}
},
static function ($reason) use ($handlers): void {
static function ($reason) use ($handlers) {
foreach ($handlers as $handler) {
self::callHandler(2, $reason, $handler);
}
@@ -185,7 +180,7 @@ class Promise implements PromiseInterface
* @param mixed $value Value to pass to the callback.
* @param array $handler Array of handler data (promise and callbacks).
*/
private static function callHandler(int $index, $value, array $handler): void
private static function callHandler($index, $value, array $handler)
{
/** @var PromiseInterface $promise */
$promise = $handler[0];
@@ -216,10 +211,12 @@ class Promise implements PromiseInterface
}
} catch (\Throwable $reason) {
$promise->reject($reason);
} catch (\Exception $reason) {
$promise->reject($reason);
}
}
private function waitIfPending(): void
private function waitIfPending()
{
if ($this->state !== self::PENDING) {
return;
@@ -230,9 +227,9 @@ class Promise implements PromiseInterface
} else {
// If there's no wait function, then reject the promise.
$this->reject('Cannot wait on a promise that has '
.'no internal wait function. You must provide a wait '
.'function when constructing the promise to be able to '
.'wait on a promise.');
. 'no internal wait function. You must provide a wait '
. 'function when constructing the promise to be able to '
. 'wait on a promise.');
}
Utils::queue()->run();
@@ -243,13 +240,13 @@ class Promise implements PromiseInterface
}
}
private function invokeWaitFn(): void
private function invokeWaitFn()
{
try {
$wfn = $this->waitFn;
$this->waitFn = null;
$wfn(true);
} catch (\Throwable $reason) {
} catch (\Exception $reason) {
if ($this->state === self::PENDING) {
// The promise has not been resolved yet, so reject the promise
// with the exception.
@@ -262,7 +259,7 @@ class Promise implements PromiseInterface
}
}
private function invokeWaitList(): void
private function invokeWaitList()
{
$waitList = $this->waitList;
$this->waitList = null;

View File

@@ -1,7 +1,5 @@
<?php
declare(strict_types=1);
namespace GuzzleHttp\Promise;
/**
@@ -11,13 +9,13 @@ namespace GuzzleHttp\Promise;
* which registers callbacks to receive either a promises eventual value or
* the reason why the promise cannot be fulfilled.
*
* @see https://promisesaplus.com/
* @link https://promisesaplus.com/
*/
interface PromiseInterface
{
public const PENDING = 'pending';
public const FULFILLED = 'fulfilled';
public const REJECTED = 'rejected';
const PENDING = 'pending';
const FULFILLED = 'fulfilled';
const REJECTED = 'rejected';
/**
* Appends fulfillment and rejection handlers to the promise, and returns
@@ -25,11 +23,13 @@ interface PromiseInterface
*
* @param callable $onFulfilled Invoked when the promise fulfills.
* @param callable $onRejected Invoked when the promise is rejected.
*
* @return PromiseInterface
*/
public function then(
callable $onFulfilled = null,
callable $onRejected = null
): PromiseInterface;
);
/**
* Appends a rejection handler callback to the promise, and returns a new
@@ -38,16 +38,20 @@ interface PromiseInterface
* fulfilled.
*
* @param callable $onRejected Invoked when the promise is rejected.
*
* @return PromiseInterface
*/
public function otherwise(callable $onRejected): PromiseInterface;
public function otherwise(callable $onRejected);
/**
* Get the state of the promise ("pending", "rejected", or "fulfilled").
*
* The three states can be checked against the constants defined on
* PromiseInterface: PENDING, FULFILLED, and REJECTED.
*
* @return string
*/
public function getState(): string;
public function getState();
/**
* Resolve the promise with the given value.
@@ -56,7 +60,7 @@ interface PromiseInterface
*
* @throws \RuntimeException if the promise is already resolved.
*/
public function resolve($value): void;
public function resolve($value);
/**
* Reject the promise with the given reason.
@@ -65,14 +69,14 @@ interface PromiseInterface
*
* @throws \RuntimeException if the promise is already resolved.
*/
public function reject($reason): void;
public function reject($reason);
/**
* Cancels the promise if possible.
*
* @see https://github.com/promises-aplus/cancellation-spec/issues/7
* @link https://github.com/promises-aplus/cancellation-spec/issues/7
*/
public function cancel(): void;
public function cancel();
/**
* Waits until the promise completes if possible.
@@ -82,10 +86,12 @@ interface PromiseInterface
*
* If the promise cannot be waited on, then the promise will be rejected.
*
* @param bool $unwrap
*
* @return mixed
*
* @throws \LogicException if the promise has no wait function or if the
* promise does not settle after waiting.
*/
public function wait(bool $unwrap = true);
public function wait($unwrap = true);
}

View File

@@ -1,7 +1,5 @@
<?php
declare(strict_types=1);
namespace GuzzleHttp\Promise;
/**
@@ -11,6 +9,8 @@ interface PromisorInterface
{
/**
* Returns a promise.
*
* @return PromiseInterface
*/
public function promise(): PromiseInterface;
public function promise();
}

View File

@@ -1,7 +1,5 @@
<?php
declare(strict_types=1);
namespace GuzzleHttp\Promise;
/**
@@ -9,16 +7,11 @@ namespace GuzzleHttp\Promise;
*
* Thenning off of this promise will invoke the onRejected callback
* immediately and ignore other callbacks.
*
* @final
*/
class RejectedPromise implements PromiseInterface
{
private $reason;
/**
* @param mixed $reason
*/
public function __construct($reason)
{
if (is_object($reason) && method_exists($reason, 'then')) {
@@ -33,7 +26,7 @@ class RejectedPromise implements PromiseInterface
public function then(
callable $onFulfilled = null,
callable $onRejected = null
): PromiseInterface {
) {
// If there's no onRejected callback then just return self.
if (!$onRejected) {
return $this;
@@ -42,7 +35,7 @@ class RejectedPromise implements PromiseInterface
$queue = Utils::queue();
$reason = $this->reason;
$p = new Promise([$queue, 'run']);
$queue->add(static function () use ($p, $reason, $onRejected): void {
$queue->add(static function () use ($p, $reason, $onRejected) {
if (Is::pending($p)) {
try {
// Return a resolved promise if onRejected does not throw.
@@ -50,6 +43,9 @@ class RejectedPromise implements PromiseInterface
} catch (\Throwable $e) {
// onRejected threw, so return a rejected promise.
$p->reject($e);
} catch (\Exception $e) {
// onRejected threw, so return a rejected promise.
$p->reject($e);
}
}
});
@@ -57,12 +53,12 @@ class RejectedPromise implements PromiseInterface
return $p;
}
public function otherwise(callable $onRejected): PromiseInterface
public function otherwise(callable $onRejected)
{
return $this->then(null, $onRejected);
}
public function wait(bool $unwrap = true)
public function wait($unwrap = true, $defaultDelivery = null)
{
if ($unwrap) {
throw Create::exceptionFor($this->reason);
@@ -71,24 +67,24 @@ class RejectedPromise implements PromiseInterface
return null;
}
public function getState(): string
public function getState()
{
return self::REJECTED;
}
public function resolve($value): void
public function resolve($value)
{
throw new \LogicException('Cannot resolve a rejected promise');
throw new \LogicException("Cannot resolve a rejected promise");
}
public function reject($reason): void
public function reject($reason)
{
if ($reason !== $this->reason) {
throw new \LogicException('Cannot reject a rejected promise');
throw new \LogicException("Cannot reject a rejected promise");
}
}
public function cancel(): void
public function cancel()
{
// pass
}

View File

@@ -1,7 +1,5 @@
<?php
declare(strict_types=1);
namespace GuzzleHttp\Promise;
/**
@@ -15,23 +13,24 @@ class RejectionException extends \RuntimeException
private $reason;
/**
* @param mixed $reason Rejection reason.
* @param string|null $description Optional description.
* @param mixed $reason Rejection reason.
* @param string $description Optional description
*/
public function __construct($reason, ?string $description = null)
public function __construct($reason, $description = null)
{
$this->reason = $reason;
$message = 'The promise was rejected';
if ($description) {
$message .= ' with reason: '.$description;
$message .= ' with reason: ' . $description;
} elseif (is_string($reason)
|| (is_object($reason) && method_exists($reason, '__toString'))
) {
$message .= ' with reason: '.$this->reason;
$message .= ' with reason: ' . $this->reason;
} elseif ($reason instanceof \JsonSerializable) {
$message .= ' with reason: '.json_encode($this->reason, JSON_PRETTY_PRINT);
$message .= ' with reason: '
. json_encode($this->reason, JSON_PRETTY_PRINT);
}
parent::__construct($message);

View File

@@ -1,7 +1,5 @@
<?php
declare(strict_types=1);
namespace GuzzleHttp\Promise;
/**
@@ -12,18 +10,16 @@ namespace GuzzleHttp\Promise;
* by calling the `run()` function of the global task queue in an event loop.
*
* GuzzleHttp\Promise\Utils::queue()->run();
*
* @final
*/
class TaskQueue implements TaskQueueInterface
{
private $enableShutdown = true;
private $queue = [];
public function __construct(bool $withShutdown = true)
public function __construct($withShutdown = true)
{
if ($withShutdown) {
register_shutdown_function(function (): void {
register_shutdown_function(function () {
if ($this->enableShutdown) {
// Only run the tasks if an E_ERROR didn't occur.
$err = error_get_last();
@@ -35,17 +31,17 @@ class TaskQueue implements TaskQueueInterface
}
}
public function isEmpty(): bool
public function isEmpty()
{
return !$this->queue;
}
public function add(callable $task): void
public function add(callable $task)
{
$this->queue[] = $task;
}
public function run(): void
public function run()
{
while ($task = array_shift($this->queue)) {
/** @var callable $task */
@@ -64,7 +60,7 @@ class TaskQueue implements TaskQueueInterface
*
* Note: This shutdown will occur before any destructors are triggered.
*/
public function disableShutdown(): void
public function disableShutdown()
{
$this->enableShutdown = false;
}

View File

@@ -1,24 +1,24 @@
<?php
declare(strict_types=1);
namespace GuzzleHttp\Promise;
interface TaskQueueInterface
{
/**
* Returns true if the queue is empty.
*
* @return bool
*/
public function isEmpty(): bool;
public function isEmpty();
/**
* Adds a task to the queue that will be executed the next time run is
* called.
*/
public function add(callable $task): void;
public function add(callable $task);
/**
* Execute all of the pending task in the queue.
*/
public function run(): void;
public function run();
}

View File

@@ -1,7 +1,5 @@
<?php
declare(strict_types=1);
namespace GuzzleHttp\Promise;
final class Utils
@@ -19,9 +17,11 @@ final class Utils
* }
* </code>
*
* @param TaskQueueInterface|null $assign Optionally specify a new queue instance.
* @param TaskQueueInterface $assign Optionally specify a new queue instance.
*
* @return TaskQueueInterface
*/
public static function queue(TaskQueueInterface $assign = null): TaskQueueInterface
public static function queue(TaskQueueInterface $assign = null)
{
static $queue;
@@ -39,18 +39,22 @@ final class Utils
* returns a promise that is fulfilled or rejected with the result.
*
* @param callable $task Task function to run.
*
* @return PromiseInterface
*/
public static function task(callable $task): PromiseInterface
public static function task(callable $task)
{
$queue = self::queue();
$promise = new Promise([$queue, 'run']);
$queue->add(function () use ($task, $promise): void {
$queue->add(function () use ($task, $promise) {
try {
if (Is::pending($promise)) {
$promise->resolve($task());
}
} catch (\Throwable $e) {
$promise->reject($e);
} catch (\Exception $e) {
$promise->reject($e);
}
});
@@ -68,18 +72,22 @@ final class Utils
* key mapping to the rejection reason of the promise.
*
* @param PromiseInterface $promise Promise or value.
*
* @return array
*/
public static function inspect(PromiseInterface $promise): array
public static function inspect(PromiseInterface $promise)
{
try {
return [
'state' => PromiseInterface::FULFILLED,
'value' => $promise->wait(),
'value' => $promise->wait()
];
} catch (RejectionException $e) {
return ['state' => PromiseInterface::REJECTED, 'reason' => $e->getReason()];
} catch (\Throwable $e) {
return ['state' => PromiseInterface::REJECTED, 'reason' => $e];
} catch (\Exception $e) {
return ['state' => PromiseInterface::REJECTED, 'reason' => $e];
}
}
@@ -92,8 +100,10 @@ final class Utils
* @see inspect for the inspection state array format.
*
* @param PromiseInterface[] $promises Traversable of promises to wait upon.
*
* @return array
*/
public static function inspectAll($promises): array
public static function inspectAll($promises)
{
$results = [];
foreach ($promises as $key => $promise) {
@@ -112,9 +122,12 @@ final class Utils
*
* @param iterable<PromiseInterface> $promises Iterable of PromiseInterface objects to wait on.
*
* @throws \Throwable on error
* @return array
*
* @throws \Exception on error
* @throws \Throwable on error in PHP >=7
*/
public static function unwrap($promises): array
public static function unwrap($promises)
{
$results = [];
foreach ($promises as $key => $promise) {
@@ -134,21 +147,22 @@ final class Utils
*
* @param mixed $promises Promises or values.
* @param bool $recursive If true, resolves new promises that might have been added to the stack during its own resolution.
*
* @return PromiseInterface
*/
public static function all($promises, bool $recursive = false): PromiseInterface
public static function all($promises, $recursive = false)
{
$results = [];
$promise = Each::of(
$promises,
function ($value, $idx) use (&$results): void {
function ($value, $idx) use (&$results) {
$results[$idx] = $value;
},
function ($reason, $idx, Promise $aggregate): void {
function ($reason, $idx, Promise $aggregate) {
$aggregate->reject($reason);
}
)->then(function () use (&$results) {
ksort($results);
return $results;
});
@@ -159,7 +173,6 @@ final class Utils
return self::all($promises, $recursive);
}
}
return $results;
});
}
@@ -180,15 +193,17 @@ final class Utils
*
* @param int $count Total number of promises.
* @param mixed $promises Promises or values.
*
* @return PromiseInterface
*/
public static function some(int $count, $promises): PromiseInterface
public static function some($count, $promises)
{
$results = [];
$rejections = [];
return Each::of(
$promises,
function ($value, $idx, PromiseInterface $p) use (&$results, $count): void {
function ($value, $idx, PromiseInterface $p) use (&$results, $count) {
if (Is::settled($p)) {
return;
}
@@ -197,7 +212,7 @@ final class Utils
$p->resolve(null);
}
},
function ($reason) use (&$rejections): void {
function ($reason) use (&$rejections) {
$rejections[] = $reason;
}
)->then(
@@ -209,7 +224,6 @@ final class Utils
);
}
ksort($results);
return array_values($results);
}
);
@@ -220,8 +234,10 @@ final class Utils
* fulfillment value is not an array of 1 but the value directly.
*
* @param mixed $promises Promises or values.
*
* @return PromiseInterface
*/
public static function any($promises): PromiseInterface
public static function any($promises)
{
return self::some(1, $promises)->then(function ($values) {
return $values[0];
@@ -237,22 +253,23 @@ final class Utils
* @see inspect for the inspection state array format.
*
* @param mixed $promises Promises or values.
*
* @return PromiseInterface
*/
public static function settle($promises): PromiseInterface
public static function settle($promises)
{
$results = [];
return Each::of(
$promises,
function ($value, $idx) use (&$results): void {
function ($value, $idx) use (&$results) {
$results[$idx] = ['state' => PromiseInterface::FULFILLED, 'value' => $value];
},
function ($reason, $idx) use (&$results): void {
function ($reason, $idx) use (&$results) {
$results[$idx] = ['state' => PromiseInterface::REJECTED, 'reason' => $reason];
}
)->then(function () use (&$results) {
ksort($results);
return $results;
});
}

View File

@@ -0,0 +1,363 @@
<?php
namespace GuzzleHttp\Promise;
/**
* Get the global task queue used for promise resolution.
*
* This task queue MUST be run in an event loop in order for promises to be
* settled asynchronously. It will be automatically run when synchronously
* waiting on a promise.
*
* <code>
* while ($eventLoop->isRunning()) {
* GuzzleHttp\Promise\queue()->run();
* }
* </code>
*
* @param TaskQueueInterface $assign Optionally specify a new queue instance.
*
* @return TaskQueueInterface
*
* @deprecated queue will be removed in guzzlehttp/promises:2.0. Use Utils::queue instead.
*/
function queue(TaskQueueInterface $assign = null)
{
return Utils::queue($assign);
}
/**
* Adds a function to run in the task queue when it is next `run()` and returns
* a promise that is fulfilled or rejected with the result.
*
* @param callable $task Task function to run.
*
* @return PromiseInterface
*
* @deprecated task will be removed in guzzlehttp/promises:2.0. Use Utils::task instead.
*/
function task(callable $task)
{
return Utils::task($task);
}
/**
* Creates a promise for a value if the value is not a promise.
*
* @param mixed $value Promise or value.
*
* @return PromiseInterface
*
* @deprecated promise_for will be removed in guzzlehttp/promises:2.0. Use Create::promiseFor instead.
*/
function promise_for($value)
{
return Create::promiseFor($value);
}
/**
* Creates a rejected promise for a reason if the reason is not a promise. If
* the provided reason is a promise, then it is returned as-is.
*
* @param mixed $reason Promise or reason.
*
* @return PromiseInterface
*
* @deprecated rejection_for will be removed in guzzlehttp/promises:2.0. Use Create::rejectionFor instead.
*/
function rejection_for($reason)
{
return Create::rejectionFor($reason);
}
/**
* Create an exception for a rejected promise value.
*
* @param mixed $reason
*
* @return \Exception|\Throwable
*
* @deprecated exception_for will be removed in guzzlehttp/promises:2.0. Use Create::exceptionFor instead.
*/
function exception_for($reason)
{
return Create::exceptionFor($reason);
}
/**
* Returns an iterator for the given value.
*
* @param mixed $value
*
* @return \Iterator
*
* @deprecated iter_for will be removed in guzzlehttp/promises:2.0. Use Create::iterFor instead.
*/
function iter_for($value)
{
return Create::iterFor($value);
}
/**
* Synchronously waits on a promise to resolve and returns an inspection state
* array.
*
* Returns a state associative array containing a "state" key mapping to a
* valid promise state. If the state of the promise is "fulfilled", the array
* will contain a "value" key mapping to the fulfilled value of the promise. If
* the promise is rejected, the array will contain a "reason" key mapping to
* the rejection reason of the promise.
*
* @param PromiseInterface $promise Promise or value.
*
* @return array
*
* @deprecated inspect will be removed in guzzlehttp/promises:2.0. Use Utils::inspect instead.
*/
function inspect(PromiseInterface $promise)
{
return Utils::inspect($promise);
}
/**
* Waits on all of the provided promises, but does not unwrap rejected promises
* as thrown exception.
*
* Returns an array of inspection state arrays.
*
* @see inspect for the inspection state array format.
*
* @param PromiseInterface[] $promises Traversable of promises to wait upon.
*
* @return array
*
* @deprecated inspect will be removed in guzzlehttp/promises:2.0. Use Utils::inspectAll instead.
*/
function inspect_all($promises)
{
return Utils::inspectAll($promises);
}
/**
* Waits on all of the provided promises and returns the fulfilled values.
*
* Returns an array that contains the value of each promise (in the same order
* the promises were provided). An exception is thrown if any of the promises
* are rejected.
*
* @param iterable<PromiseInterface> $promises Iterable of PromiseInterface objects to wait on.
*
* @return array
*
* @throws \Exception on error
* @throws \Throwable on error in PHP >=7
*
* @deprecated unwrap will be removed in guzzlehttp/promises:2.0. Use Utils::unwrap instead.
*/
function unwrap($promises)
{
return Utils::unwrap($promises);
}
/**
* Given an array of promises, return a promise that is fulfilled when all the
* items in the array are fulfilled.
*
* The promise's fulfillment value is an array with fulfillment values at
* respective positions to the original array. If any promise in the array
* rejects, the returned promise is rejected with the rejection reason.
*
* @param mixed $promises Promises or values.
* @param bool $recursive If true, resolves new promises that might have been added to the stack during its own resolution.
*
* @return PromiseInterface
*
* @deprecated all will be removed in guzzlehttp/promises:2.0. Use Utils::all instead.
*/
function all($promises, $recursive = false)
{
return Utils::all($promises, $recursive);
}
/**
* Initiate a competitive race between multiple promises or values (values will
* become immediately fulfilled promises).
*
* When count amount of promises have been fulfilled, the returned promise is
* fulfilled with an array that contains the fulfillment values of the winners
* in order of resolution.
*
* This promise is rejected with a {@see AggregateException} if the number of
* fulfilled promises is less than the desired $count.
*
* @param int $count Total number of promises.
* @param mixed $promises Promises or values.
*
* @return PromiseInterface
*
* @deprecated some will be removed in guzzlehttp/promises:2.0. Use Utils::some instead.
*/
function some($count, $promises)
{
return Utils::some($count, $promises);
}
/**
* Like some(), with 1 as count. However, if the promise fulfills, the
* fulfillment value is not an array of 1 but the value directly.
*
* @param mixed $promises Promises or values.
*
* @return PromiseInterface
*
* @deprecated any will be removed in guzzlehttp/promises:2.0. Use Utils::any instead.
*/
function any($promises)
{
return Utils::any($promises);
}
/**
* Returns a promise that is fulfilled when all of the provided promises have
* been fulfilled or rejected.
*
* The returned promise is fulfilled with an array of inspection state arrays.
*
* @see inspect for the inspection state array format.
*
* @param mixed $promises Promises or values.
*
* @return PromiseInterface
*
* @deprecated settle will be removed in guzzlehttp/promises:2.0. Use Utils::settle instead.
*/
function settle($promises)
{
return Utils::settle($promises);
}
/**
* Given an iterator that yields promises or values, returns a promise that is
* fulfilled with a null value when the iterator has been consumed or the
* aggregate promise has been fulfilled or rejected.
*
* $onFulfilled is a function that accepts the fulfilled value, iterator index,
* and the aggregate promise. The callback can invoke any necessary side
* effects and choose to resolve or reject the aggregate if needed.
*
* $onRejected is a function that accepts the rejection reason, iterator index,
* and the aggregate promise. The callback can invoke any necessary side
* effects and choose to resolve or reject the aggregate if needed.
*
* @param mixed $iterable Iterator or array to iterate over.
* @param callable $onFulfilled
* @param callable $onRejected
*
* @return PromiseInterface
*
* @deprecated each will be removed in guzzlehttp/promises:2.0. Use Each::of instead.
*/
function each(
$iterable,
callable $onFulfilled = null,
callable $onRejected = null
) {
return Each::of($iterable, $onFulfilled, $onRejected);
}
/**
* Like each, but only allows a certain number of outstanding promises at any
* given time.
*
* $concurrency may be an integer or a function that accepts the number of
* pending promises and returns a numeric concurrency limit value to allow for
* dynamic a concurrency size.
*
* @param mixed $iterable
* @param int|callable $concurrency
* @param callable $onFulfilled
* @param callable $onRejected
*
* @return PromiseInterface
*
* @deprecated each_limit will be removed in guzzlehttp/promises:2.0. Use Each::ofLimit instead.
*/
function each_limit(
$iterable,
$concurrency,
callable $onFulfilled = null,
callable $onRejected = null
) {
return Each::ofLimit($iterable, $concurrency, $onFulfilled, $onRejected);
}
/**
* Like each_limit, but ensures that no promise in the given $iterable argument
* is rejected. If any promise is rejected, then the aggregate promise is
* rejected with the encountered rejection.
*
* @param mixed $iterable
* @param int|callable $concurrency
* @param callable $onFulfilled
*
* @return PromiseInterface
*
* @deprecated each_limit_all will be removed in guzzlehttp/promises:2.0. Use Each::ofLimitAll instead.
*/
function each_limit_all(
$iterable,
$concurrency,
callable $onFulfilled = null
) {
return Each::ofLimitAll($iterable, $concurrency, $onFulfilled);
}
/**
* Returns true if a promise is fulfilled.
*
* @return bool
*
* @deprecated is_fulfilled will be removed in guzzlehttp/promises:2.0. Use Is::fulfilled instead.
*/
function is_fulfilled(PromiseInterface $promise)
{
return Is::fulfilled($promise);
}
/**
* Returns true if a promise is rejected.
*
* @return bool
*
* @deprecated is_rejected will be removed in guzzlehttp/promises:2.0. Use Is::rejected instead.
*/
function is_rejected(PromiseInterface $promise)
{
return Is::rejected($promise);
}
/**
* Returns true if a promise is fulfilled or rejected.
*
* @return bool
*
* @deprecated is_settled will be removed in guzzlehttp/promises:2.0. Use Is::settled instead.
*/
function is_settled(PromiseInterface $promise)
{
return Is::settled($promise);
}
/**
* Create a new coroutine.
*
* @see Coroutine
*
* @return PromiseInterface
*
* @deprecated coroutine will be removed in guzzlehttp/promises:2.0. Use Coroutine::of instead.
*/
function coroutine(callable $generatorFn)
{
return Coroutine::of($generatorFn);
}

View File

@@ -0,0 +1,6 @@
<?php
// Don't redefine the functions if included multiple times.
if (!function_exists('GuzzleHttp\Promise\promise_for')) {
require __DIR__ . '/functions.php';
}