PK tOSH+ phpstan-baseline.neonnu ٘ parameters:
ignoreErrors:
-
message: "#^Parameter \\#1 \\$function of function register_shutdown_function expects callable\\(\\)\\: void, Closure\\(\\)\\: mixed given\\.$#"
count: 1
path: src/TaskQueue.php
PK tOS=- .travis.ymlnu ٘ language: php
dist: trusty
matrix:
include:
- php: 5.5.9
- php: hhvm-3.24
fast_finish: true
before_install:
- if [[ "$TRAVIS_PHP_VERSION" != "hhvm-3.24" ]]; then echo "xdebug.overload_var_dump = 1" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini; fi
install:
- travis_retry composer update
- travis_retry ./vendor/bin/simple-phpunit install
script:
- ./vendor/bin/simple-phpunit
PK tOSBi Makefilenu ٘ all: clean test
test:
vendor/bin/phpunit
coverage:
vendor/bin/phpunit --coverage-html=artifacts/coverage
view-coverage:
open artifacts/coverage/index.html
clean:
rm -rf artifacts/*
PK tOScC C README.mdnu ٘ # Guzzle Promises
[Promises/A+](https://promisesaplus.com/) implementation that handles promise
chaining and resolution iteratively, allowing for "infinite" promise chaining
while keeping the stack size constant. Read [this blog post](https://blog.domenic.me/youre-missing-the-point-of-promises/)
for a general introduction to promises.
- [Features](#features)
- [Quick start](#quick-start)
- [Synchronous wait](#synchronous-wait)
- [Cancellation](#cancellation)
- [API](#api)
- [Promise](#promise)
- [FulfilledPromise](#fulfilledpromise)
- [RejectedPromise](#rejectedpromise)
- [Promise interop](#promise-interop)
- [Implementation notes](#implementation-notes)
# Features
- [Promises/A+](https://promisesaplus.com/) implementation.
- Promise resolution and chaining is handled iteratively, allowing for
"infinite" promise chaining.
- Promises have a synchronous `wait` method.
- Promises can be cancelled.
- Works with any object that has a `then` function.
- C# style async/await coroutine promises using
`GuzzleHttp\Promise\Coroutine::of()`.
# Quick start
A *promise* represents the eventual result of an asynchronous operation. The
primary way of interacting with a promise is through its `then` method, which
registers callbacks to receive either a promise's eventual value or the reason
why the promise cannot be fulfilled.
## Callbacks
Callbacks are registered with the `then` method by providing an optional
`$onFulfilled` followed by an optional `$onRejected` function.
```php
use GuzzleHttp\Promise\Promise;
$promise = new Promise();
$promise->then(
// $onFulfilled
function ($value) {
echo 'The promise was fulfilled.';
},
// $onRejected
function ($reason) {
echo 'The promise was rejected.';
}
);
```
*Resolving* a promise means that you either fulfill a promise with a *value* or
reject a promise with a *reason*. Resolving a promises triggers callbacks
registered with the promises's `then` method. These callbacks are triggered
only once and in the order in which they were added.
## Resolving a promise
Promises are fulfilled using the `resolve($value)` method. Resolving a promise
with any value other than a `GuzzleHttp\Promise\RejectedPromise` will trigger
all of the onFulfilled callbacks (resolving a promise with a rejected promise
will reject the promise and trigger the `$onRejected` callbacks).
```php
use GuzzleHttp\Promise\Promise;
$promise = new Promise();
$promise
->then(function ($value) {
// Return a value and don't break the chain
return "Hello, " . $value;
})
// This then is executed after the first then and receives the value
// returned from the first then.
->then(function ($value) {
echo $value;
});
// Resolving the promise triggers the $onFulfilled callbacks and outputs
// "Hello, reader."
$promise->resolve('reader.');
```
## Promise forwarding
Promises can be chained one after the other. Each then in the chain is a new
promise. The return value of a promise is what's forwarded to the next
promise in the chain. Returning a promise in a `then` callback will cause the
subsequent promises in the chain to only be fulfilled when the returned promise
has been fulfilled. The next promise in the chain will be invoked with the
resolved value of the promise.
```php
use GuzzleHttp\Promise\Promise;
$promise = new Promise();
$nextPromise = new Promise();
$promise
->then(function ($value) use ($nextPromise) {
echo $value;
return $nextPromise;
})
->then(function ($value) {
echo $value;
});
// Triggers the first callback and outputs "A"
$promise->resolve('A');
// Triggers the second callback and outputs "B"
$nextPromise->resolve('B');
```
## Promise rejection
When a promise is rejected, the `$onRejected` callbacks are invoked with the
rejection reason.
```php
use GuzzleHttp\Promise\Promise;
$promise = new Promise();
$promise->then(null, function ($reason) {
echo $reason;
});
$promise->reject('Error!');
// Outputs "Error!"
```
## Rejection forwarding
If an exception is thrown in an `$onRejected` callback, subsequent
`$onRejected` callbacks are invoked with the thrown exception as the reason.
```php
use GuzzleHttp\Promise\Promise;
$promise = new Promise();
$promise->then(null, function ($reason) {
throw new Exception($reason);
})->then(null, function ($reason) {
assert($reason->getMessage() === 'Error!');
});
$promise->reject('Error!');
```
You can also forward a rejection down the promise chain by returning a
`GuzzleHttp\Promise\RejectedPromise` in either an `$onFulfilled` or
`$onRejected` callback.
```php
use GuzzleHttp\Promise\Promise;
use GuzzleHttp\Promise\RejectedPromise;
$promise = new Promise();
$promise->then(null, function ($reason) {
return new RejectedPromise($reason);
})->then(null, function ($reason) {
assert($reason === 'Error!');
});
$promise->reject('Error!');
```
If an exception is not thrown in a `$onRejected` callback and the callback
does not return a rejected promise, downstream `$onFulfilled` callbacks are
invoked using the value returned from the `$onRejected` callback.
```php
use GuzzleHttp\Promise\Promise;
$promise = new Promise();
$promise
->then(null, function ($reason) {
return "It's ok";
})
->then(function ($value) {
assert($value === "It's ok");
});
$promise->reject('Error!');
```
# Synchronous wait
You can synchronously force promises to complete using a promise's `wait`
method. When creating a promise, you can provide a wait function that is used
to synchronously force a promise to complete. When a wait function is invoked
it is expected to deliver a value to the promise or reject the promise. If the
wait function does not deliver a value, then an exception is thrown. The wait
function provided to a promise constructor is invoked when the `wait` function
of the promise is called.
```php
$promise = new Promise(function () use (&$promise) {
$promise->resolve('foo');
});
// Calling wait will return the value of the promise.
echo $promise->wait(); // outputs "foo"
```
If an exception is encountered while invoking the wait function of a promise,
the promise is rejected with the exception and the exception is thrown.
```php
$promise = new Promise(function () use (&$promise) {
throw new Exception('foo');
});
$promise->wait(); // throws the exception.
```
Calling `wait` on a promise that has been fulfilled will not trigger the wait
function. It will simply return the previously resolved value.
```php
$promise = new Promise(function () { die('this is not called!'); });
$promise->resolve('foo');
echo $promise->wait(); // outputs "foo"
```
Calling `wait` on a promise that has been rejected will throw an exception. If
the rejection reason is an instance of `\Exception` the reason is thrown.
Otherwise, a `GuzzleHttp\Promise\RejectionException` is thrown and the reason
can be obtained by calling the `getReason` method of the exception.
```php
$promise = new Promise();
$promise->reject('foo');
$promise->wait();
```
> PHP Fatal error: Uncaught exception 'GuzzleHttp\Promise\RejectionException' with message 'The promise was rejected with value: foo'
## Unwrapping a promise
When synchronously waiting on a promise, you are joining the state of the
promise into the current state of execution (i.e., return the value of the
promise if it was fulfilled or throw an exception if it was rejected). This is
called "unwrapping" the promise. Waiting on a promise will by default unwrap
the promise state.
You can force a promise to resolve and *not* unwrap the state of the promise
by passing `false` to the first argument of the `wait` function:
```php
$promise = new Promise();
$promise->reject('foo');
// This will not throw an exception. It simply ensures the promise has
// been resolved.
$promise->wait(false);
```
When unwrapping a promise, the resolved value of the promise will be waited
upon until the unwrapped value is not a promise. This means that if you resolve
promise A with a promise B and unwrap promise A, the value returned by the
wait function will be the value delivered to promise B.
**Note**: when you do not unwrap the promise, no value is returned.
# Cancellation
You can cancel a promise that has not yet been fulfilled using the `cancel()`
method of a promise. When creating a promise you can provide an optional
cancel function that when invoked cancels the action of computing a resolution
of the promise.
# API
## Promise
When creating a promise object, you can provide an optional `$waitFn` and
`$cancelFn`. `$waitFn` is a function that is invoked with no arguments and is
expected to resolve the promise. `$cancelFn` is a function with no arguments
that is expected to cancel the computation of a promise. It is invoked when the
`cancel()` method of a promise is called.
```php
use GuzzleHttp\Promise\Promise;
$promise = new Promise(
function () use (&$promise) {
$promise->resolve('waited');
},
function () {
// do something that will cancel the promise computation (e.g., close
// a socket, cancel a database query, etc...)
}
);
assert('waited' === $promise->wait());
```
A promise has the following methods:
- `then(callable $onFulfilled, callable $onRejected) : PromiseInterface`
Appends fulfillment and rejection handlers to the promise, and returns a new promise resolving to the return value of the called handler.
- `otherwise(callable $onRejected) : PromiseInterface`
Appends a rejection handler callback to the promise, and returns a new promise resolving to the return value of the callback if it is called, or to its original fulfillment value if the promise is instead fulfilled.
- `wait($unwrap = true) : mixed`
Synchronously waits on the promise to complete.
`$unwrap` controls whether or not the value of the promise is returned for a
fulfilled promise or if an exception is thrown if the promise is rejected.
This is set to `true` by default.
- `cancel()`
Attempts to cancel the promise if possible. The promise being cancelled and
the parent most ancestor that has not yet been resolved will also be
cancelled. Any promises waiting on the cancelled promise to resolve will also
be cancelled.
- `getState() : string`
Returns the state of the promise. One of `pending`, `fulfilled`, or
`rejected`.
- `resolve($value)`
Fulfills the promise with the given `$value`.
- `reject($reason)`
Rejects the promise with the given `$reason`.
## FulfilledPromise
A fulfilled promise can be created to represent a promise that has been
fulfilled.
```php
use GuzzleHttp\Promise\FulfilledPromise;
$promise = new FulfilledPromise('value');
// Fulfilled callbacks are immediately invoked.
$promise->then(function ($value) {
echo $value;
});
```
## RejectedPromise
A rejected promise can be created to represent a promise that has been
rejected.
```php
use GuzzleHttp\Promise\RejectedPromise;
$promise = new RejectedPromise('Error');
// Rejected callbacks are immediately invoked.
$promise->then(null, function ($reason) {
echo $reason;
});
```
# Promise interop
This library works with foreign promises that have a `then` method. This means
you can use Guzzle promises with [React promises](https://github.com/reactphp/promise)
for example. When a foreign promise is returned inside of a then method
callback, promise resolution will occur recursively.
```php
// Create a React promise
$deferred = new React\Promise\Deferred();
$reactPromise = $deferred->promise();
// Create a Guzzle promise that is fulfilled with a React promise.
$guzzlePromise = new GuzzleHttp\Promise\Promise();
$guzzlePromise->then(function ($value) use ($reactPromise) {
// Do something something with the value...
// Return the React promise
return $reactPromise;
});
```
Please note that wait and cancel chaining is no longer possible when forwarding
a foreign promise. You will need to wrap a third-party promise with a Guzzle
promise in order to utilize wait and cancel functions with foreign promises.
## Event Loop Integration
In order to keep the stack size constant, Guzzle promises are resolved
asynchronously using a task queue. When waiting on promises synchronously, the
task queue will be automatically run to ensure that the blocking promise and
any forwarded promises are resolved. When using promises asynchronously in an
event loop, you will need to run the task queue on each tick of the loop. If
you do not run the task queue, then promises will not be resolved.
You can run the task queue using the `run()` method of the global task queue
instance.
```php
// Get the global task queue
$queue = GuzzleHttp\Promise\Utils::queue();
$queue->run();
```
For example, you could use Guzzle promises with React using a periodic timer:
```php
$loop = React\EventLoop\Factory::create();
$loop->addPeriodicTimer(0, [$queue, 'run']);
```
*TODO*: Perhaps adding a `futureTick()` on each tick would be faster?
# Implementation notes
## Promise resolution and chaining is handled iteratively
By shuffling pending handlers from one owner to another, promises are
resolved iteratively, allowing for "infinite" then chaining.
```php
then(function ($v) {
// The stack size remains constant (a good thing)
echo xdebug_get_stack_depth() . ', ';
return $v + 1;
});
}
$parent->resolve(0);
var_dump($p->wait()); // int(1000)
```
When a promise is fulfilled or rejected with a non-promise value, the promise
then takes ownership of the handlers of each child promise and delivers values
down the chain without using recursion.
When a promise is resolved with another promise, the original promise transfers
all of its pending handlers to the new promise. When the new promise is
eventually resolved, all of the pending handlers are delivered the forwarded
value.
## A promise is the deferred.
Some promise libraries implement promises using a deferred object to represent
a computation and a promise object to represent the delivery of the result of
the computation. This is a nice separation of computation and delivery because
consumers of the promise cannot modify the value that will be eventually
delivered.
One side effect of being able to implement promise resolution and chaining
iteratively is that you need to be able for one promise to reach into the state
of another promise to shuffle around ownership of handlers. In order to achieve
this without making the handlers of a promise publicly mutable, a promise is
also the deferred value, allowing promises of the same parent class to reach
into and modify the private properties of promises of the same type. While this
does allow consumers of the value to modify the resolution or rejection of the
deferred, it is a small price to pay for keeping the stack size constant.
```php
$promise = new Promise();
$promise->then(function ($value) { echo $value; });
// The promise is the deferred value, so you can deliver a value to it.
$promise->resolve('foo');
// prints "foo"
```
## Upgrading from Function API
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 will be removed in 2.0.0. A migration table has been provided here for your convenience:
| Original Function | Replacement Method |
|----------------|----------------|
| `queue` | `Utils::queue` |
| `task` | `Utils::task` |
| `promise_for` | `Create::promiseFor` |
| `rejection_for` | `Create::rejectionFor` |
| `exception_for` | `Create::exceptionFor` |
| `iter_for` | `Create::iterFor` |
| `inspect` | `Utils::inspect` |
| `inspect_all` | `Utils::inspectAll` |
| `unwrap` | `Utils::unwrap` |
| `all` | `Utils::all` |
| `some` | `Utils::some` |
| `any` | `Utils::any` |
| `settle` | `Utils::settle` |
| `each` | `Each::of` |
| `each_limit` | `Each::ofLimit` |
| `each_limit_all` | `Each::ofLimitAll` |
| `!is_fulfilled` | `Is::pending` |
| `is_fulfilled` | `Is::fulfilled` |
| `is_rejected` | `Is::rejected` |
| `is_settled` | `Is::settled` |
| `coroutine` | `Coroutine::of` |
## Security
If you discover a security vulnerability within this package, please send an email to security@tidelift.com. All security vulnerabilities will be promptly addressed. Please do not disclose security-related issues publicly until a fix has been announced. Please see [Security Policy](https://github.com/guzzle/promises/security/policy) for more information.
## License
Guzzle is made available under the MIT License (MIT). Please see [License File](LICENSE) for more information.
## For Enterprise
Available as part of the Tidelift Subscription
The maintainers of Guzzle and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. [Learn more.](https://tidelift.com/subscription/pkg/packagist-guzzlehttp-promises?utm_source=packagist-guzzlehttp-promises&utm_medium=referral&utm_campaign=enterprise&utm_term=repo)
PK tOSnS psalm.xmlnu ٘
PK tOS.8i i
.gitignorenu ٘ artifacts/
vendor/
composer.lock
phpunit.xml
.php-cs-fixer.php
.php-cs-fixer.cache
.phpunit.result.cache
PK tOSrW
src/Is.phpnu ٘ getState() === PromiseInterface::PENDING;
}
/**
* Returns true if a promise is fulfilled or rejected.
*
* @return 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)
{
return $promise->getState() === PromiseInterface::FULFILLED;
}
/**
* Returns true if a promise is rejected.
*
* @return bool
*/
public static function rejected(PromiseInterface $promise)
{
return $promise->getState() === PromiseInterface::REJECTED;
}
}
PK tOSc]@ @ src/Create.phpnu ٘ then([$promise, 'resolve'], [$promise, 'reject']);
return $promise;
}
return new FulfilledPromise($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
*/
public static function rejectionFor($reason)
{
if ($reason instanceof PromiseInterface) {
return $reason;
}
return new RejectedPromise($reason);
}
/**
* Create an exception for a rejected promise value.
*
* @param mixed $reason
*
* @return \Exception|\Throwable
*/
public static function exceptionFor($reason)
{
if ($reason instanceof \Exception || $reason instanceof \Throwable) {
return $reason;
}
return new RejectionException($reason);
}
/**
* Returns an iterator for the given value.
*
* @param mixed $value
*
* @return \Iterator
*/
public static function iterFor($value)
{
if ($value instanceof \Iterator) {
return $value;
}
if (is_array($value)) {
return new \ArrayIterator($value);
}
return new \ArrayIterator([$value]);
}
}
PK tOSCM src/Coroutine.phpnu ٘ then(function ($v) { echo $v; });
*
* @param callable $generatorFn Generator function to wrap into a promise.
*
* @return Promise
*
* @link https://github.com/petkaantonov/bluebird/blob/master/API.md#generators inspiration
*/
final class Coroutine implements PromiseInterface
{
/**
* @var PromiseInterface|null
*/
private $currentPromise;
/**
* @var Generator
*/
private $generator;
/**
* @var Promise
*/
private $result;
public function __construct(callable $generatorFn)
{
$this->generator = $generatorFn();
$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);
}
}
/**
* Create a new coroutine.
*
* @return self
*/
public static function of(callable $generatorFn)
{
return new self($generatorFn);
}
public function then(
callable $onFulfilled = null,
callable $onRejected = null
) {
return $this->result->then($onFulfilled, $onRejected);
}
public function otherwise(callable $onRejected)
{
return $this->result->otherwise($onRejected);
}
public function wait($unwrap = true)
{
return $this->result->wait($unwrap);
}
public function getState()
{
return $this->result->getState();
}
public function resolve($value)
{
$this->result->resolve($value);
}
public function reject($reason)
{
$this->result->reject($reason);
}
public function cancel()
{
$this->currentPromise->cancel();
$this->result->cancel();
}
private function nextCoroutine($yielded)
{
$this->currentPromise = Create::promiseFor($yielded)
->then([$this, '_handleSuccess'], [$this, '_handleFailure']);
}
/**
* @internal
*/
public function _handleSuccess($value)
{
unset($this->currentPromise);
try {
$next = $this->generator->send($value);
if ($this->generator->valid()) {
$this->nextCoroutine($next);
} else {
$this->result->resolve($value);
}
} catch (Exception $exception) {
$this->result->reject($exception);
} catch (Throwable $throwable) {
$this->result->reject($throwable);
}
}
/**
* @internal
*/
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);
}
}
}
PK tOS' ' src/functions.phpnu ٘
* while ($eventLoop->isRunning()) {
* GuzzleHttp\Promise\queue()->run();
* }
*
*
* @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 $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);
}
PK tOSRE " "
src/Utils.phpnu ٘
* while ($eventLoop->isRunning()) {
* GuzzleHttp\Promise\Utils::queue()->run();
* }
*
*
* @param TaskQueueInterface $assign Optionally specify a new queue instance.
*
* @return TaskQueueInterface
*/
public static function queue(TaskQueueInterface $assign = null)
{
static $queue;
if ($assign) {
$queue = $assign;
} elseif (!$queue) {
$queue = new TaskQueue();
}
return $queue;
}
/**
* 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
*/
public static function task(callable $task)
{
$queue = self::queue();
$promise = new Promise([$queue, 'run']);
$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);
}
});
return $promise;
}
/**
* 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
*/
public static function inspect(PromiseInterface $promise)
{
try {
return [
'state' => PromiseInterface::FULFILLED,
'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];
}
}
/**
* 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
*/
public static function inspectAll($promises)
{
$results = [];
foreach ($promises as $key => $promise) {
$results[$key] = inspect($promise);
}
return $results;
}
/**
* 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 $promises Iterable of PromiseInterface objects to wait on.
*
* @return array
*
* @throws \Exception on error
* @throws \Throwable on error in PHP >=7
*/
public static function unwrap($promises)
{
$results = [];
foreach ($promises as $key => $promise) {
$results[$key] = $promise->wait();
}
return $results;
}
/**
* 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
*/
public static function all($promises, $recursive = false)
{
$results = [];
$promise = Each::of(
$promises,
function ($value, $idx) use (&$results) {
$results[$idx] = $value;
},
function ($reason, $idx, Promise $aggregate) {
$aggregate->reject($reason);
}
)->then(function () use (&$results) {
ksort($results);
return $results;
});
if (true === $recursive) {
$promise = $promise->then(function ($results) use ($recursive, &$promises) {
foreach ($promises as $promise) {
if (Is::pending($promise)) {
return self::all($promises, $recursive);
}
}
return $results;
});
}
return $promise;
}
/**
* 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
*/
public static function some($count, $promises)
{
$results = [];
$rejections = [];
return Each::of(
$promises,
function ($value, $idx, PromiseInterface $p) use (&$results, $count) {
if (Is::settled($p)) {
return;
}
$results[$idx] = $value;
if (count($results) >= $count) {
$p->resolve(null);
}
},
function ($reason) use (&$rejections) {
$rejections[] = $reason;
}
)->then(
function () use (&$results, &$rejections, $count) {
if (count($results) !== $count) {
throw new AggregateException(
'Not enough promises to fulfill count',
$rejections
);
}
ksort($results);
return array_values($results);
}
);
}
/**
* 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
*/
public static function any($promises)
{
return self::some(1, $promises)->then(function ($values) {
return $values[0];
});
}
/**
* 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
*/
public static function settle($promises)
{
$results = [];
return Each::of(
$promises,
function ($value, $idx) use (&$results) {
$results[$idx] = ['state' => PromiseInterface::FULFILLED, 'value' => $value];
},
function ($reason, $idx) use (&$results) {
$results[$idx] = ['state' => PromiseInterface::REJECTED, 'reason' => $reason];
}
)->then(function () use (&$results) {
ksort($results);
return $results;
});
}
}
PK tOS(K| | src/AggregateException.phpnu ٘ run();
*/
class TaskQueue implements TaskQueueInterface
{
private $enableShutdown = true;
private $queue = [];
public function __construct($withShutdown = true)
{
if ($withShutdown) {
register_shutdown_function(function () {
if ($this->enableShutdown) {
// Only run the tasks if an E_ERROR didn't occur.
$err = error_get_last();
if (!$err || ($err['type'] ^ E_ERROR)) {
$this->run();
}
}
});
}
}
public function isEmpty()
{
return !$this->queue;
}
public function add(callable $task)
{
$this->queue[] = $task;
}
public function run()
{
while ($task = array_shift($this->queue)) {
/** @var callable $task */
$task();
}
}
/**
* The task queue will be run and exhausted by default when the process
* exits IFF the exit is not the result of a PHP E_ERROR error.
*
* You can disable running the automatic shutdown of the queue by calling
* this function. If you disable the task queue shutdown process, then you
* MUST either run the task queue (as a result of running your event loop
* or manually using the run() method) or wait on each outstanding promise.
*
* Note: This shutdown will occur before any destructors are triggered.
*/
public function disableShutdown()
{
$this->enableShutdown = false;
}
}
PK tOSl src/TaskQueueInterface.phpnu ٘ waitFn = $waitFn;
$this->cancelFn = $cancelFn;
}
public function then(
callable $onFulfilled = null,
callable $onRejected = null
) {
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)
{
return $this->then(null, $onRejected);
}
public function wait($unwrap = true)
{
$this->waitIfPending();
if ($this->result instanceof PromiseInterface) {
return $this->result->wait($unwrap);
}
if ($unwrap) {
if ($this->state === self::FULFILLED) {
return $this->result;
}
// It's rejected so "unwrap" and throw an exception.
throw Create::exceptionFor($this->result);
}
}
public function getState()
{
return $this->state;
}
public function cancel()
{
if ($this->state !== self::PENDING) {
return;
}
$this->waitFn = $this->waitList = null;
if ($this->cancelFn) {
$fn = $this->cancelFn;
$this->cancelFn = null;
try {
$fn();
} catch (\Throwable $e) {
$this->reject($e);
} catch (\Exception $e) {
$this->reject($e);
}
}
// Reject the promise only if it wasn't rejected in a then callback.
/** @psalm-suppress RedundantCondition */
if ($this->state === self::PENDING) {
$this->reject(new CancellationException('Promise has been cancelled'));
}
}
public function resolve($value)
{
$this->settle(self::FULFILLED, $value);
}
public function reject($reason)
{
$this->settle(self::REJECTED, $reason);
}
private function settle($state, $value)
{
if ($this->state !== self::PENDING) {
// Ignore calls with the same resolution.
if ($state === $this->state && $value === $this->result) {
return;
}
throw $this->state === $state
? new \LogicException("The promise is already {$state}.")
: new \LogicException("Cannot change a {$this->state} promise to {$state}");
}
if ($value === $this) {
throw new \LogicException('Cannot fulfill or reject a promise with itself');
}
// Clear out the state of the promise but stash the handlers.
$this->state = $state;
$this->result = $value;
$handlers = $this->handlers;
$this->handlers = null;
$this->waitList = $this->waitFn = null;
$this->cancelFn = null;
if (!$handlers) {
return;
}
// If the value was not a settled promise or a thenable, then resolve
// it in the task queue using the correct ID.
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) {
foreach ($handlers as $handler) {
self::callHandler($id, $value, $handler);
}
});
} elseif ($value instanceof Promise && Is::pending($value)) {
// We can just merge our handlers onto the next promise.
$value->handlers = array_merge($value->handlers, $handlers);
} else {
// Resolve the handlers when the forwarded promise is resolved.
$value->then(
static function ($value) use ($handlers) {
foreach ($handlers as $handler) {
self::callHandler(1, $value, $handler);
}
},
static function ($reason) use ($handlers) {
foreach ($handlers as $handler) {
self::callHandler(2, $reason, $handler);
}
}
);
}
}
/**
* Call a stack of handlers using a specific callback index and value.
*
* @param int $index 1 (resolve) or 2 (reject).
* @param mixed $value Value to pass to the callback.
* @param array $handler Array of handler data (promise and callbacks).
*/
private static function callHandler($index, $value, array $handler)
{
/** @var PromiseInterface $promise */
$promise = $handler[0];
// The promise may have been cancelled or resolved before placing
// this thunk in the queue.
if (Is::settled($promise)) {
return;
}
try {
if (isset($handler[$index])) {
/*
* If $f throws an exception, then $handler will be in the exception
* stack trace. Since $handler contains a reference to the callable
* itself we get a circular reference. We clear the $handler
* here to avoid that memory leak.
*/
$f = $handler[$index];
unset($handler);
$promise->resolve($f($value));
} elseif ($index === 1) {
// Forward resolution values as-is.
$promise->resolve($value);
} else {
// Forward rejections down the chain.
$promise->reject($value);
}
} catch (\Throwable $reason) {
$promise->reject($reason);
} catch (\Exception $reason) {
$promise->reject($reason);
}
}
private function waitIfPending()
{
if ($this->state !== self::PENDING) {
return;
} elseif ($this->waitFn) {
$this->invokeWaitFn();
} elseif ($this->waitList) {
$this->invokeWaitList();
} 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.');
}
Utils::queue()->run();
/** @psalm-suppress RedundantCondition */
if ($this->state === self::PENDING) {
$this->reject('Invoking the wait callback did not resolve the promise');
}
}
private function invokeWaitFn()
{
try {
$wfn = $this->waitFn;
$this->waitFn = null;
$wfn(true);
} catch (\Exception $reason) {
if ($this->state === self::PENDING) {
// The promise has not been resolved yet, so reject the promise
// with the exception.
$this->reject($reason);
} else {
// The promise was already resolved, so there's a problem in
// the application.
throw $reason;
}
}
}
private function invokeWaitList()
{
$waitList = $this->waitList;
$this->waitList = null;
foreach ($waitList as $result) {
do {
$result->waitIfPending();
$result = $result->result;
} while ($result instanceof Promise);
if ($result instanceof PromiseInterface) {
$result->wait(false);
}
}
}
}
PK tOS9 src/RejectionException.phpnu ٘ reason = $reason;
$message = 'The promise was rejected';
if ($description) {
$message .= ' with reason: ' . $description;
} elseif (is_string($reason)
|| (is_object($reason) && method_exists($reason, '__toString'))
) {
$message .= ' with reason: ' . $this->reason;
} elseif ($reason instanceof \JsonSerializable) {
$message .= ' with reason: '
. json_encode($this->reason, JSON_PRETTY_PRINT);
}
parent::__construct($message);
}
/**
* Returns the rejection reason.
*
* @return mixed
*/
public function getReason()
{
return $this->reason;
}
}
PK tOS߇`G G src/Each.phpnu ٘ $onFulfilled,
'rejected' => $onRejected
]))->promise();
}
/**
* Like of, 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
*/
public static function ofLimit(
$iterable,
$concurrency,
callable $onFulfilled = null,
callable $onRejected = null
) {
return (new EachPromise($iterable, [
'fulfilled' => $onFulfilled,
'rejected' => $onRejected,
'concurrency' => $concurrency
]))->promise();
}
/**
* Like 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
*/
public static function ofLimitAll(
$iterable,
$concurrency,
callable $onFulfilled = null
) {
return each_limit(
$iterable,
$concurrency,
$onFulfilled,
function ($reason, $idx, PromiseInterface $aggregate) {
$aggregate->reject($reason);
}
);
}
}
PK tOSu src/FulfilledPromise.phpnu ٘ value = $value;
}
public function then(
callable $onFulfilled = null,
callable $onRejected = null
) {
// Return itself if there is no onFulfilled function.
if (!$onFulfilled) {
return $this;
}
$this->onFulfilled = $onFulfilled;
$queue = Utils::queue();
$p = $this->promise = new Promise([$queue, 'run']);
$value = $this->value;
$queue->add(static function () use ($p, $value, $onFulfilled) {
if (Is::pending($p)) {
self::callHandler($p, $value, $onFulfilled);
}
});
return $p;
}
public function otherwise(callable $onRejected)
{
return $this->then(null, $onRejected);
}
public function wait($unwrap = true, $defaultDelivery = null)
{
// Don't run the queue to avoid deadlocks, instead directly resolve the promise.
if ($this->promise && Is::pending($this->promise)) {
self::callHandler($this->promise, $this->value, $this->onFulfilled);
}
return $unwrap ? $this->value : null;
}
public function getState()
{
return self::FULFILLED;
}
public function resolve($value)
{
if ($value !== $this->value) {
throw new \LogicException("Cannot resolve a fulfilled promise");
}
}
public function reject($reason)
{
throw new \LogicException("Cannot reject a fulfilled promise");
}
public function cancel()
{
// pass
}
private static function callHandler(Promise $promise, $value, callable $handler)
{
try {
$promise->resolve($handler($value));
} catch (\Throwable $e) {
$promise->reject($e);
} catch (\Exception $e) {
$promise->reject($e);
}
}
}
PK tOS69 src/CancellationException.phpnu ٘ iterable = Create::iterFor($iterable);
if (isset($config['concurrency'])) {
$this->concurrency = $config['concurrency'];
}
if (isset($config['fulfilled'])) {
$this->onFulfilled = $config['fulfilled'];
}
if (isset($config['rejected'])) {
$this->onRejected = $config['rejected'];
}
}
/** @psalm-suppress InvalidNullableReturnType */
public function promise()
{
if ($this->aggregate) {
return $this->aggregate;
}
try {
$this->createPromise();
/** @psalm-assert Promise $this->aggregate */
$this->iterable->rewind();
$this->refillPending();
} catch (\Throwable $e) {
/**
* @psalm-suppress NullReference
* @phpstan-ignore-next-line
*/
$this->aggregate->reject($e);
} catch (\Exception $e) {
/**
* @psalm-suppress NullReference
* @phpstan-ignore-next-line
*/
$this->aggregate->reject($e);
}
/**
* @psalm-suppress NullableReturnStatement
* @phpstan-ignore-next-line
*/
return $this->aggregate;
}
private function createPromise()
{
$this->mutex = false;
$this->aggregate = new Promise(function () {
if ($this->checkIfFinished()) {
return;
}
reset($this->pending);
// Consume a potentially fluctuating list of promises while
// ensuring that indexes are maintained (precluding array_shift).
while ($promise = current($this->pending)) {
next($this->pending);
$promise->wait();
if (Is::settled($this->aggregate)) {
return;
}
}
});
// Clear the references when the promise is resolved.
$clearFn = function () {
$this->iterable = $this->concurrency = $this->pending = null;
$this->onFulfilled = $this->onRejected = null;
$this->nextPendingIndex = 0;
};
$this->aggregate->then($clearFn, $clearFn);
}
private function refillPending()
{
if (!$this->concurrency) {
// Add all pending promises.
while ($this->addPending() && $this->advanceIterator());
return;
}
// Add only up to N pending promises.
$concurrency = is_callable($this->concurrency)
? call_user_func($this->concurrency, count($this->pending))
: $this->concurrency;
$concurrency = max($concurrency - count($this->pending), 0);
// Concurrency may be set to 0 to disallow new promises.
if (!$concurrency) {
return;
}
// Add the first pending promise.
$this->addPending();
// Note this is special handling for concurrency=1 so that we do
// not advance the iterator after adding the first promise. This
// helps work around issues with generators that might not have the
// next value to yield until promise callbacks are called.
while (--$concurrency
&& $this->advanceIterator()
&& $this->addPending());
}
private function addPending()
{
if (!$this->iterable || !$this->iterable->valid()) {
return false;
}
$promise = Create::promiseFor($this->iterable->current());
$key = $this->iterable->key();
// Iterable keys may not be unique, so we use a counter to
// guarantee uniqueness
$idx = $this->nextPendingIndex++;
$this->pending[$idx] = $promise->then(
function ($value) use ($idx, $key) {
if ($this->onFulfilled) {
call_user_func(
$this->onFulfilled,
$value,
$key,
$this->aggregate
);
}
$this->step($idx);
},
function ($reason) use ($idx, $key) {
if ($this->onRejected) {
call_user_func(
$this->onRejected,
$reason,
$key,
$this->aggregate
);
}
$this->step($idx);
}
);
return true;
}
private function advanceIterator()
{
// Place a lock on the iterator so that we ensure to not recurse,
// preventing fatal generator errors.
if ($this->mutex) {
return false;
}
$this->mutex = true;
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($idx)
{
// If the promise was already resolved, then ignore this step.
if (Is::settled($this->aggregate)) {
return;
}
unset($this->pending[$idx]);
// Only refill pending promises if we are not locked, preventing the
// EachPromise to recursively invoke the provided iterator, which
// cause a fatal error: "Cannot resume an already running generator"
if ($this->advanceIterator() && !$this->checkIfFinished()) {
// Add more pending promises if possible.
$this->refillPending();
}
}
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;
}
return false;
}
}
PK tOSǬp
src/RejectedPromise.phpnu ٘ reason = $reason;
}
public function then(
callable $onFulfilled = null,
callable $onRejected = null
) {
// If there's no onRejected callback then just return self.
if (!$onRejected) {
return $this;
}
$this->onRejected = $onRejected;
$queue = Utils::queue();
$reason = $this->reason;
$p = $this->promise = new Promise([$queue, 'run']);
$queue->add(static function () use ($p, $reason, $onRejected) {
if (Is::pending($p)) {
self::callHandler($p, $reason, $onRejected);
}
});
return $p;
}
public function otherwise(callable $onRejected)
{
return $this->then(null, $onRejected);
}
public function wait($unwrap = true, $defaultDelivery = null)
{
if ($unwrap) {
throw Create::exceptionFor($this->reason);
}
// Don't run the queue to avoid deadlocks, instead directly reject the promise.
if ($this->promise && Is::pending($this->promise)) {
self::callHandler($this->promise, $this->reason, $this->onRejected);
}
return null;
}
public function getState()
{
return self::REJECTED;
}
public function resolve($value)
{
throw new \LogicException("Cannot resolve a rejected promise");
}
public function reject($reason)
{
if ($reason !== $this->reason) {
throw new \LogicException("Cannot reject a rejected promise");
}
}
public function cancel()
{
// pass
}
private static function callHandler(Promise $promise, $reason, callable $handler)
{
try {
$promise->resolve($handler($reason));
} catch (\Throwable $e) {
$promise->reject($e);
} catch (\Exception $e) {
$promise->reject($e);
}
}
}
PK tOS߇' src/functions_include.phpnu ٘ 8
- name: Download dependencies
run: composer update --no-interaction --optimize-autoloader --prefer-stable
- name: Run tests
run: ./vendor/bin/simple-phpunit
env:
SYMFONY_PHPUNIT_VERSION: 9.5
lowest:
name: Lowest deps
runs-on: ubuntu-latest
steps:
- name: Set up PHP
uses: shivammathur/setup-php@v2
with:
php-version: 7.3
coverage: pcov
- name: Checkout code
uses: actions/checkout@v2
- name: Download dependencies
run: composer update --no-interaction --optimize-autoloader --prefer-stable --prefer-lowest
- name: Run tests
env:
SYMFONY_DEPRECATIONS_HELPER: "max[self]=0"
run: ./vendor/bin/simple-phpunit --coverage-text
PK tOSI5 .github/workflows/static.ymlnu ٘ on: [push, pull_request]
name: Static analysis
jobs:
phpstan:
name: PHPStan
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Download dependencies
run: |
composer update --no-interaction --optimize-autoloader
- name: PHPStan
uses: docker://oskarstark/phpstan-ga:0.12.25
with:
entrypoint: /composer/vendor/bin/phpstan
args: analyze --no-progress
php-cs-fixer:
name: PHP-CS-Fixer
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '7.4'
coverage: none
extensions: mbstring
- name: Download dependencies
run: composer update --no-interaction --no-progress
- name: Download PHP CS Fixer
run: composer require "friendsofphp/php-cs-fixer:3.2.1"
- name: Execute PHP CS Fixer
run: vendor/bin/php-cs-fixer fix --diff --dry-run
psalm:
name: Psalm
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Psalm
uses: docker://vimeo/psalm-github-actions
PK tOSM@ .github/workflows/.editorconfignu ٘ [*.yml]
indent_size = 2
PK tOS)K K .github/FUNDING.ymlnu ٘ github: [Nyholm, GrahamCampbell]
tidelift: "packagist/guzzlehttp/promises"
PK tOSp- - .github/stale.ymlnu ٘ daysUntilStale: 120
daysUntilClose: 14
exemptLabels:
- lifecycle/keep-open
- lifecycle/ready-for-merge
# Label to use when marking an issue as stale
staleLabel: lifecycle/stale
# Comment to post when marking an issue as stale. Set to `false` to disable
markComment: >
This issue has been automatically marked as stale because it has not had
recent activity. It will be closed after 2 weeks if no further activity occurs. Thank you
for your contributions.
# Comment to post when closing a stale issue. Set to `false` to disable
closeComment: false
PK tOS%O .gitattributesnu ٘ .editorconfig export-ignore
.gitattributes export-ignore
/.github/ export-ignore
.gitignore export-ignore
/.travis.yml export-ignore
/.php-cs-fixer.dist.php export-ignore
/phpstan-baseline.neon export-ignore
/phpstan.neon.dist export-ignore
/phpunit.xml.dist export-ignore
/psalm.xml export-ignore
/tests/ export-ignore
PK tOSm m .php-cs-fixer.dist.phpnu ٘ setRiskyAllowed(true)
->setRules([
'@PSR2' => true,
'array_syntax' => ['syntax' => 'short'],
'binary_operator_spaces' => ['operators' => ['=>' => null]],
'blank_line_after_opening_tag' => true,
'class_attributes_separation' => ['elements' => ['method' => 'one']],
'compact_nullable_typehint' => true,
'concat_space' => ['spacing' => 'one'],
'declare_equal_normalize' => ['space' => 'none'],
'declare_strict_types' => false,
'dir_constant' => true,
'fully_qualified_strict_types' => true,
'function_to_constant' => true,
'function_typehint_space' => true,
'header_comment' => false,
'list_syntax' => ['syntax' => 'short'],
'lowercase_cast' => true,
'magic_method_casing' => true,
'modernize_types_casting' => true,
'multiline_comment_opening_closing' => true,
//'native_constant_invocation' => true,
'no_alias_functions' => true,
'no_alternative_syntax' => true,
'no_blank_lines_after_phpdoc' => true,
'no_empty_comment' => true,
'no_empty_phpdoc' => true,
'no_extra_blank_lines' => true,
'no_leading_import_slash' => true,
'no_leading_namespace_whitespace' => true,
'no_spaces_around_offset' => true,
'no_superfluous_phpdoc_tags' => ['allow_mixed' => true],
'no_trailing_comma_in_singleline_array' => true,
'no_unneeded_control_parentheses' => true,
'no_unset_cast' => true,
'no_unused_imports' => true,
'no_useless_else' => true,
'no_useless_return' => true,
'no_whitespace_in_blank_line' => true,
'normalize_index_brace' => true,
'ordered_imports' => true,
'php_unit_construct' => true,
'php_unit_dedicate_assert' => ['target' => 'newest'],
'php_unit_dedicate_assert_internal_type' => ['target' => 'newest'],
'php_unit_expectation' => ['target' => 'newest'],
'php_unit_mock' => ['target' => 'newest'],
'php_unit_mock_short_will_return' => true,
'php_unit_no_expectation_annotation' => ['target' => 'newest'],
'php_unit_test_annotation' => ['style' => 'prefix'],
//'php_unit_test_case_static_method_calls' => ['call_type' => 'self'],
'phpdoc_align' => ['align' => 'vertical'],
//'phpdoc_line_span' => ['method' => 'multi', 'property' => 'multi'],
'phpdoc_no_package' => true,
'phpdoc_no_useless_inheritdoc' => true,
'phpdoc_scalar' => true,
'phpdoc_separation' => true,
'phpdoc_single_line_var_spacing' => true,
'phpdoc_trim' => true,
'phpdoc_trim_consecutive_blank_line_separation' => true,
'phpdoc_types' => true,
'phpdoc_types_order' => ['null_adjustment' => 'always_last', 'sort_algorithm' => 'none'],
'phpdoc_var_without_name' => true,
'return_assignment' => true,
'self_static_accessor' => true,
'short_scalar_cast' => true,
'single_trait_insert_per_statement' => true,
'standardize_not_equals' => true,
//'static_lambda' => true,
'ternary_to_null_coalescing' => true,
'trim_array_spaces' => true,
'visibility_required' => ['elements' => ['property', 'method']],
'yoda_style' => false,
// 'native_function_invocation' => true,
'braces' => ['allow_single_line_closure'=>true],
])
->setFinder(
PhpCsFixer\Finder::create()
->in(__DIR__.'/src')
->in(__DIR__.'/tests')
->name('*.php')
)
;
return $config;
PK tOS]QG phpstan.neon.distnu ٘ includes:
- phpstan-baseline.neon
parameters:
level: 5
paths:
- src
ignoreErrors:
- "#^Dead catch - Exception is already caught by Throwable above\\.$#"
PK tOSRG G phpunit.xml.distnu ٘
tests/
src/
src/
PK tOSv
.editorconfignu ٘ root = true
[*]
charset = utf-8
end_of_line = lf
indent_size = 4
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true
PK tOSg CHANGELOG.mdnu ٘ # CHANGELOG
## 1.5.0 - 2021-10-07
### Changed
- Call handler when waiting on fulfilled/rejected Promise
### Fixed
- 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
- Support for PHP 8
- Optional `$recursive` flag to `all`
- Replaced functions by static methods
### Fixed
- Fix empty `each` processing
- Fix promise handling for Iterators of non-unique keys
- Fixed `method_exists` crashes on PHP 8
- Memory leak on exceptions
## 1.3.1 - 2016-12-20
### Fixed
- `wait()` foreign promise compatibility
## 1.3.0 - 2016-11-18
### Added
- Adds support for custom task queues.
### Fixed
- Fixed coroutine promise memory leak.
## 1.2.0 - 2016-05-18
### Changed
- Update to now catch `\Throwable` on PHP 7+
## 1.1.0 - 2016-03-07
### Changed
- Update EachPromise to prevent recurring on a iterator when advancing, as this
could trigger fatal generator errors.
- Update Promise to allow recursive waiting without unwrapping exceptions.
## 1.0.3 - 2015-10-15
### Changed
- Update EachPromise to immediately resolve when the underlying promise iterator
is empty. Previously, such a promise would throw an exception when its `wait`
function was called.
## 1.0.2 - 2015-05-15
### Changed
- Conditionally require functions.php.
## 1.0.1 - 2015-06-24
### Changed
- Updating EachPromise to call next on the underlying promise iterator as late
as possible to ensure that generators that generate new requests based on
callbacks are not iterated until after callbacks are invoked.
## 1.0.0 - 2015-05-12
- Initial release
PK tOSb:, tests/IsTest.phpnu ٘ assertTrue(P\Is::fulfilled($p));
$this->assertFalse(P\Is::rejected($p));
}
public function testKnowsIfRejected()
{
$p = new RejectedPromise(null);
$this->assertTrue(P\Is::rejected($p));
$this->assertFalse(P\Is::fulfilled($p));
}
public function testKnowsIfSettled()
{
$p = new RejectedPromise(null);
$this->assertTrue(P\Is::settled($p));
$this->assertFalse(P\Is::pending($p));
}
public function testKnowsIfPending()
{
$p = new Promise();
$this->assertFalse(P\Is::settled($p));
$this->assertTrue(P\Is::pending($p));
}
}
PK tOS tests/FulfilledPromiseTest.phpnu ٘ assertTrue(P\Is::fulfilled($p));
$this->assertSame('foo', $p->wait(true));
}
public function testCannotCancel()
{
$p = new FulfilledPromise('foo');
$this->assertTrue(P\Is::fulfilled($p));
$p->cancel();
$this->assertSame('foo', $p->wait());
}
/**
* @exepctedExceptionMessage Cannot resolve a fulfilled promise
*/
public function testCannotResolve()
{
$this->expectException(\LogicException::class);
$p = new FulfilledPromise('foo');
$p->resolve('bar');
}
/**
* @exepctedExceptionMessage Cannot reject a fulfilled promise
*/
public function testCannotReject()
{
$this->expectException(\LogicException::class);
$p = new FulfilledPromise('foo');
$p->reject('bar');
}
public function testCanResolveWithSameValue()
{
$p = new FulfilledPromise('foo');
$p->resolve('foo');
$this->assertSame('foo', $p->wait());
}
public function testCannotResolveWithPromise()
{
$this->expectException(\InvalidArgumentException::class);
new FulfilledPromise(new Promise());
}
public function testReturnsSelfWhenNoOnFulfilled()
{
$p = new FulfilledPromise('a');
$this->assertSame($p, $p->then());
}
public function testAsynchronouslyInvokesOnFulfilled()
{
$p = new FulfilledPromise('a');
$r = null;
$f = function ($d) use (&$r) { $r = $d; };
$p2 = $p->then($f);
$this->assertNotSame($p, $p2);
$this->assertNull($r);
P\Utils::queue()->run();
$this->assertSame('a', $r);
}
public function testInvokesOnFulfilledOnWait()
{
$p = new FulfilledPromise('a');
$r = null;
$f = function ($d) use (&$r) { $r = $d; };
$p->then($f);
$this->assertNull($r);
$p->wait();
$this->assertSame('a', $r);
}
public function testReturnsNewRejectedWhenOnFulfilledFails()
{
$p = new FulfilledPromise('a');
$f = function () { throw new \Exception('b'); };
$p2 = $p->then($f);
$this->assertNotSame($p, $p2);
try {
$p2->wait();
$this->fail();
} catch (\Exception $e) {
$this->assertSame('b', $e->getMessage());
}
}
public function testOtherwiseIsSugarForRejections()
{
$c = null;
$p = new FulfilledPromise('foo');
$p->otherwise(function ($v) use (&$c) { $c = $v; });
$this->assertNull($c);
}
public function testDoesNotTryToFulfillTwiceDuringTrampoline()
{
$fp = new FulfilledPromise('a');
$t1 = $fp->then(function ($v) { return $v . ' b'; });
$t1->resolve('why!');
$this->assertSame('why!', $t1->wait());
P\Utils::queue()->run();
}
}
PK tOSwo[ [ tests/UtilsTest.phpnu ٘ resolve('a'); });
$b = new Promise(function () use (&$b) { $b->reject('b'); });
$c = new Promise(function () use (&$c, $e) { $c->reject($e); });
$results = P\Utils::inspectAll([$a, $b, $c]);
$this->assertSame([
['state' => 'fulfilled', 'value' => 'a'],
['state' => 'rejected', 'reason' => 'b'],
['state' => 'rejected', 'reason' => $e]
], $results);
}
public function testUnwrapsPromisesWithNoDefaultAndFailure()
{
$this->expectException(\GuzzleHttp\Promise\RejectionException::class);
$promises = [new FulfilledPromise('a'), new Promise()];
P\Utils::unwrap($promises);
}
public function testUnwrapsPromisesWithNoDefault()
{
$promises = [new FulfilledPromise('a')];
$this->assertSame(['a'], P\Utils::unwrap($promises));
}
public function testUnwrapsPromisesWithKeys()
{
$promises = [
'foo' => new FulfilledPromise('a'),
'bar' => new FulfilledPromise('b'),
];
$this->assertSame([
'foo' => 'a',
'bar' => 'b'
], P\Utils::unwrap($promises));
}
public function testAllAggregatesSortedArray()
{
$a = new Promise();
$b = new Promise();
$c = new Promise();
$d = P\Utils::all([$a, $b, $c]);
$b->resolve('b');
$a->resolve('a');
$c->resolve('c');
$d->then(
function ($value) use (&$result) { $result = $value; },
function ($reason) use (&$result) { $result = $reason; }
);
P\Utils::queue()->run();
$this->assertSame(['a', 'b', 'c'], $result);
}
public function testPromisesDynamicallyAddedToStack()
{
$promises = new \ArrayIterator();
$counter = 0;
$promises['a'] = new FulfilledPromise('a');
$promises['b'] = $promise = new Promise(function () use (&$promise, &$promises, &$counter) {
$counter++; // Make sure the wait function is called only once
$promise->resolve('b');
$promises['c'] = $subPromise = new Promise(function () use (&$subPromise) {
$subPromise->resolve('c');
});
});
$result = P\Utils::all($promises, true)->wait();
$this->assertCount(3, $promises);
$this->assertCount(3, $result);
$this->assertSame($result['c'], 'c');
$this->assertSame(1, $counter);
}
public function testAllThrowsWhenAnyRejected()
{
$a = new Promise();
$b = new Promise();
$c = new Promise();
$d = P\Utils::all([$a, $b, $c]);
$b->resolve('b');
$a->reject('fail');
$c->resolve('c');
$d->then(
function ($value) use (&$result) { $result = $value; },
function ($reason) use (&$result) { $result = $reason; }
);
P\Utils::queue()->run();
$this->assertSame('fail', $result);
}
public function testSomeAggregatesSortedArrayWithMax()
{
$a = new Promise();
$b = new Promise();
$c = new Promise();
$d = P\Utils::some(2, [$a, $b, $c]);
$b->resolve('b');
$c->resolve('c');
$a->resolve('a');
$d->then(function ($value) use (&$result) { $result = $value; });
P\Utils::queue()->run();
$this->assertSame(['b', 'c'], $result);
}
public function testSomeRejectsWhenTooManyRejections()
{
$a = new Promise();
$b = new Promise();
$d = P\Utils::some(2, [$a, $b]);
$a->reject('bad');
$b->resolve('good');
P\Utils::queue()->run();
$this->assertTrue(P\Is::rejected($d));
$d->then(null, function ($reason) use (&$called) {
$called = $reason;
});
P\Utils::queue()->run();
$this->assertInstanceOf(AggregateException::class, $called);
$this->assertContains('bad', $called->getReason());
}
public function testCanWaitUntilSomeCountIsSatisfied()
{
$a = new Promise(function () use (&$a) { $a->resolve('a'); });
$b = new Promise(function () use (&$b) { $b->resolve('b'); });
$c = new Promise(function () use (&$c) { $c->resolve('c'); });
$d = P\Utils::some(2, [$a, $b, $c]);
$this->assertSame(['a', 'b'], $d->wait());
}
public function testThrowsIfImpossibleToWaitForSomeCount()
{
$this->expectException(\GuzzleHttp\Promise\AggregateException::class);
$this->expectExceptionMessage('Not enough promises to fulfill count');
$a = new Promise(function () use (&$a) { $a->resolve('a'); });
$d = P\Utils::some(2, [$a]);
$d->wait();
}
public function testThrowsIfResolvedWithoutCountTotalResults()
{
$this->expectException(\GuzzleHttp\Promise\AggregateException::class);
$this->expectExceptionMessage('Not enough promises to fulfill count');
$a = new Promise();
$b = new Promise();
$d = P\Utils::some(3, [$a, $b]);
$a->resolve('a');
$b->resolve('b');
$d->wait();
}
public function testAnyReturnsFirstMatch()
{
$a = new Promise();
$b = new Promise();
$c = P\Utils::any([$a, $b]);
$b->resolve('b');
$a->resolve('a');
$c->then(function ($value) use (&$result) { $result = $value; });
P\Utils::queue()->run();
$this->assertSame('b', $result);
}
public function testSettleFulfillsWithFulfilledAndRejected()
{
$a = new Promise();
$b = new Promise();
$c = new Promise();
$d = P\Utils::settle([$a, $b, $c]);
$b->resolve('b');
$c->resolve('c');
$a->reject('a');
P\Utils::queue()->run();
$this->assertTrue(P\Is::fulfilled($d));
$d->then(function ($value) use (&$result) { $result = $value; });
P\Utils::queue()->run();
$this->assertSame([
['state' => 'rejected', 'reason' => 'a'],
['state' => 'fulfilled', 'value' => 'b'],
['state' => 'fulfilled', 'value' => 'c']
], $result);
}
public function testCanInspectFulfilledPromise()
{
$p = new FulfilledPromise('foo');
$this->assertSame([
'state' => 'fulfilled',
'value' => 'foo'
], P\Utils::inspect($p));
}
public function testCanInspectRejectedPromise()
{
$p = new RejectedPromise('foo');
$this->assertSame([
'state' => 'rejected',
'reason' => 'foo'
], P\Utils::inspect($p));
}
public function testCanInspectRejectedPromiseWithNormalException()
{
$e = new \Exception('foo');
$p = new RejectedPromise($e);
$this->assertSame([
'state' => 'rejected',
'reason' => $e
], P\Utils::inspect($p));
}
public function testReturnsTrampoline()
{
$this->assertInstanceOf(TaskQueue::class, P\Utils::queue());
$this->assertSame(P\Utils::queue(), P\Utils::queue());
}
public function testCanScheduleThunk()
{
$tramp = P\Utils::queue();
$promise = P\task(function () { return 'Hi!'; });
$c = null;
$promise->then(function ($v) use (&$c) { $c = $v; });
$this->assertNull($c);
$tramp->run();
$this->assertSame('Hi!', $c);
}
public function testCanScheduleThunkWithRejection()
{
$tramp = P\Utils::queue();
$promise = P\task(function () { throw new \Exception('Hi!'); });
$c = null;
$promise->otherwise(function ($v) use (&$c) { $c = $v; });
$this->assertNull($c);
$tramp->run();
$this->assertSame('Hi!', $c->getMessage());
}
public function testCanScheduleThunkWithWait()
{
$tramp = P\Utils::queue();
$promise = P\task(function () { return 'a'; });
$this->assertSame('a', $promise->wait());
$tramp->run();
}
public function testYieldsFromCoroutine()
{
if (defined('HHVM_VERSION')) {
$this->markTestIncomplete('Broken on HHVM.');
}
$promise = P\Coroutine::of(function () {
$value = (yield new FulfilledPromise('a'));
yield $value . 'b';
});
$promise->then(function ($value) use (&$result) { $result = $value; });
P\Utils::queue()->run();
$this->assertSame('ab', $result);
}
public function testCanCatchExceptionsInCoroutine()
{
if (defined('HHVM_VERSION')) {
$this->markTestIncomplete('Broken on HHVM.');
}
$promise = P\Coroutine::of(function () {
try {
yield new RejectedPromise('a');
$this->fail('Should have thrown into the coroutine!');
} catch (RejectionException $e) {
$value = (yield new FulfilledPromise($e->getReason()));
yield $value . 'b';
}
});
$promise->then(function ($value) use (&$result) { $result = $value; });
P\Utils::queue()->run();
$this->assertTrue(P\Is::fulfilled($promise));
$this->assertSame('ab', $result);
}
/**
* @dataProvider rejectsParentExceptionProvider
*/
public function testRejectsParentExceptionWhenException(PromiseInterface $promise)
{
$promise->then(
function () { $this->fail(); },
function ($reason) use (&$result) { $result = $reason; }
);
P\Utils::queue()->run();
$this->assertInstanceOf(\Exception::class, $result);
$this->assertSame('a', $result->getMessage());
}
public function rejectsParentExceptionProvider()
{
return [
[P\Coroutine::of(function () {
yield new FulfilledPromise(0);
throw new \Exception('a');
})],
[P\Coroutine::of(function () {
throw new \Exception('a');
yield new FulfilledPromise(0);
})],
];
}
public function testCanRejectFromRejectionCallback()
{
if (defined('HHVM_VERSION')) {
$this->markTestIncomplete('Broken on HHVM.');
}
$promise = P\Coroutine::of(function () {
yield new FulfilledPromise(0);
yield new RejectedPromise('no!');
});
$promise->then(
function () { $this->fail(); },
function ($reason) use (&$result) { $result = $reason; }
);
P\Utils::queue()->run();
$this->assertInstanceOf(RejectionException::class, $result);
$this->assertSame('no!', $result->getReason());
}
public function testCanAsyncReject()
{
if (defined('HHVM_VERSION')) {
$this->markTestIncomplete('Broken on HHVM.');
}
$rej = new Promise();
$promise = P\Coroutine::of(function () use ($rej) {
yield new FulfilledPromise(0);
yield $rej;
});
$promise->then(
function () { $this->fail(); },
function ($reason) use (&$result) { $result = $reason; }
);
$rej->reject('no!');
P\Utils::queue()->run();
$this->assertInstanceOf(RejectionException::class, $result);
$this->assertSame('no!', $result->getReason());
}
public function testCanCatchAndThrowOtherException()
{
$promise = P\Coroutine::of(function () {
try {
yield new RejectedPromise('a');
$this->fail('Should have thrown into the coroutine!');
} catch (RejectionException $e) {
throw new \Exception('foo');
}
});
$promise->otherwise(function ($value) use (&$result) { $result = $value; });
P\Utils::queue()->run();
$this->assertTrue(P\Is::rejected($promise));
$this->assertStringContainsString('foo', $result->getMessage());
}
public function testCanCatchAndYieldOtherException()
{
if (defined('HHVM_VERSION')) {
$this->markTestIncomplete('Broken on HHVM.');
}
$promise = P\Coroutine::of(function () {
try {
yield new RejectedPromise('a');
$this->fail('Should have thrown into the coroutine!');
} catch (RejectionException $e) {
yield new RejectedPromise('foo');
}
});
$promise->otherwise(function ($value) use (&$result) { $result = $value; });
P\Utils::queue()->run();
$this->assertTrue(P\Is::rejected($promise));
$this->assertStringContainsString('foo', $result->getMessage());
}
public function createLotsOfSynchronousPromise()
{
return P\Coroutine::of(function () {
$value = 0;
for ($i = 0; $i < 1000; $i++) {
$value = (yield new FulfilledPromise($i));
}
yield $value;
});
}
public function testLotsOfSynchronousDoesNotBlowStack()
{
if (defined('HHVM_VERSION')) {
$this->markTestIncomplete('Broken on HHVM.');
}
$promise = $this->createLotsOfSynchronousPromise();
$promise->then(function ($v) use (&$r) { $r = $v; });
P\Utils::queue()->run();
$this->assertSame(999, $r);
}
public function testLotsOfSynchronousWaitDoesNotBlowStack()
{
if (defined('HHVM_VERSION')) {
$this->markTestIncomplete('Broken on HHVM.');
}
$promise = $this->createLotsOfSynchronousPromise();
$promise->then(function ($v) use (&$r) { $r = $v; });
$this->assertSame(999, $promise->wait());
$this->assertSame(999, $r);
}
private function createLotsOfFlappingPromise()
{
return P\Coroutine::of(function () {
$value = 0;
for ($i = 0; $i < 1000; $i++) {
try {
if ($i % 2) {
$value = (yield new FulfilledPromise($i));
} else {
$value = (yield new RejectedPromise($i));
}
} catch (\Exception $e) {
$value = (yield new FulfilledPromise($i));
}
}
yield $value;
});
}
public function testLotsOfTryCatchingDoesNotBlowStack()
{
if (defined('HHVM_VERSION')) {
$this->markTestIncomplete('Broken on HHVM.');
}
$promise = $this->createLotsOfFlappingPromise();
$promise->then(function ($v) use (&$r) { $r = $v; });
P\Utils::queue()->run();
$this->assertSame(999, $r);
}
public function testLotsOfTryCatchingWaitingDoesNotBlowStack()
{
if (defined('HHVM_VERSION')) {
$this->markTestIncomplete('Broken on HHVM.');
}
$promise = $this->createLotsOfFlappingPromise();
$promise->then(function ($v) use (&$r) { $r = $v; });
$this->assertSame(999, $promise->wait());
$this->assertSame(999, $r);
}
public function testAsyncPromisesWithCorrectlyYieldedValues()
{
if (defined('HHVM_VERSION')) {
$this->markTestIncomplete('Broken on HHVM.');
}
$promises = [
new Promise(),
new Promise(),
new Promise(),
];
eval('
$promise = \GuzzleHttp\Promise\Coroutine::of(function () use ($promises) {
$value = null;
$this->assertSame(\'skip\', (yield new \GuzzleHttp\Promise\FulfilledPromise(\'skip\')));
foreach ($promises as $idx => $p) {
$value = (yield $p);
$this->assertSame($idx, $value);
$this->assertSame(\'skip\', (yield new \GuzzleHttp\Promise\FulfilledPromise(\'skip\')));
}
$this->assertSame(\'skip\', (yield new \GuzzleHttp\Promise\FulfilledPromise(\'skip\')));
yield $value;
});
');
$promises[0]->resolve(0);
$promises[1]->resolve(1);
$promises[2]->resolve(2);
$promise->then(function ($v) use (&$r) { $r = $v; });
P\Utils::queue()->run();
$this->assertSame(2, $r);
}
public function testYieldFinalWaitablePromise()
{
if (defined('HHVM_VERSION')) {
$this->markTestIncomplete('Broken on HHVM.');
}
$p1 = new Promise(function () use (&$p1) {
$p1->resolve('skip me');
});
$p2 = new Promise(function () use (&$p2) {
$p2->resolve('hello!');
});
$co = P\Coroutine::of(function () use ($p1, $p2) {
yield $p1;
yield $p2;
});
P\Utils::queue()->run();
$this->assertSame('hello!', $co->wait());
}
public function testCanYieldFinalPendingPromise()
{
if (defined('HHVM_VERSION')) {
$this->markTestIncomplete('Broken on HHVM.');
}
$p1 = new Promise();
$p2 = new Promise();
$co = P\Coroutine::of(function () use ($p1, $p2) {
yield $p1;
yield $p2;
});
$p1->resolve('a');
$p2->resolve('b');
$co->then(function ($value) use (&$result) { $result = $value; });
P\Utils::queue()->run();
$this->assertSame('b', $result);
}
public function testCanNestYieldsAndFailures()
{
if (defined('HHVM_VERSION')) {
$this->markTestIncomplete('Broken on HHVM.');
}
$p1 = new Promise();
$p2 = new Promise();
$p3 = new Promise();
$p4 = new Promise();
$p5 = new Promise();
$co = P\Coroutine::of(function () use ($p1, $p2, $p3, $p4, $p5) {
try {
yield $p1;
} catch (\Exception $e) {
yield $p2;
try {
yield $p3;
yield $p4;
} catch (\Exception $e) {
yield $p5;
}
}
});
$p1->reject('a');
$p2->resolve('b');
$p3->resolve('c');
$p4->reject('d');
$p5->resolve('e');
$co->then(function ($value) use (&$result) { $result = $value; });
P\Utils::queue()->run();
$this->assertSame('e', $result);
}
public function testCanYieldErrorsAndSuccessesWithoutRecursion()
{
if (defined('HHVM_VERSION')) {
$this->markTestIncomplete('Broken on HHVM.');
}
$promises = [];
for ($i = 0; $i < 20; $i++) {
$promises[] = new Promise();
}
$co = P\Coroutine::of(function () use ($promises) {
for ($i = 0; $i < 20; $i += 4) {
try {
yield $promises[$i];
yield $promises[$i + 1];
} catch (\Exception $e) {
yield $promises[$i + 2];
yield $promises[$i + 3];
}
}
});
for ($i = 0; $i < 20; $i += 4) {
$promises[$i]->resolve($i);
$promises[$i + 1]->reject($i + 1);
$promises[$i + 2]->resolve($i + 2);
$promises[$i + 3]->resolve($i + 3);
}
$co->then(function ($value) use (&$result) { $result = $value; });
P\Utils::queue()->run();
$this->assertSame(19, $result);
}
public function testCanWaitOnPromiseAfterFulfilled()
{
if (defined('HHVM_VERSION')) {
$this->markTestIncomplete('Broken on HHVM.');
}
$f = function () {
static $i = 0;
$i++;
return $p = new Promise(function () use (&$p, $i) {
$p->resolve($i . '-bar');
});
};
$promises = [];
for ($i = 0; $i < 20; $i++) {
$promises[] = $f();
}
$p = P\Coroutine::of(function () use ($promises) {
yield new FulfilledPromise('foo!');
foreach ($promises as $promise) {
yield $promise;
}
});
$this->assertSame('20-bar', $p->wait());
}
public function testCanWaitOnErroredPromises()
{
if (defined('HHVM_VERSION')) {
$this->markTestIncomplete('Broken on HHVM.');
}
$p1 = new Promise(function () use (&$p1) { $p1->reject('a'); });
$p2 = new Promise(function () use (&$p2) { $p2->resolve('b'); });
$p3 = new Promise(function () use (&$p3) { $p3->resolve('c'); });
$p4 = new Promise(function () use (&$p4) { $p4->reject('d'); });
$p5 = new Promise(function () use (&$p5) { $p5->resolve('e'); });
$p6 = new Promise(function () use (&$p6) { $p6->reject('f'); });
$co = P\Coroutine::of(function () use ($p1, $p2, $p3, $p4, $p5, $p6) {
try {
yield $p1;
} catch (\Exception $e) {
yield $p2;
try {
yield $p3;
yield $p4;
} catch (\Exception $e) {
yield $p5;
yield $p6;
}
}
});
$res = P\Utils::inspect($co);
$this->assertSame('f', $res['reason']);
}
public function testCoroutineOtherwiseIntegrationTest()
{
if (defined('HHVM_VERSION')) {
$this->markTestIncomplete('Broken on HHVM.');
}
$a = new Promise();
$b = new Promise();
$promise = P\Coroutine::of(function () use ($a, $b) {
// Execute the pool of commands concurrently, and process errors.
yield $a;
yield $b;
})->otherwise(function (\Exception $e) {
// Throw errors from the operations as a specific Multipart error.
throw new \OutOfBoundsException('a', 0, $e);
});
$a->resolve('a');
$b->reject('b');
$reason = P\Utils::inspect($promise)['reason'];
$this->assertInstanceOf(\OutOfBoundsException::class, $reason);
$this->assertInstanceOf(RejectionException::class, $reason->getPrevious());
}
public function testCanManuallySettleTaskQueueGeneratedPromises()
{
$p1 = P\Utils::task(function () { return 'a'; });
$p2 = P\Utils::task(function () { return 'b'; });
$p3 = P\Utils::task(function () { return 'c'; });
$p1->cancel();
$p2->resolve('b2');
$results = P\Utils::inspectAll([$p1, $p2, $p3]);
$this->assertSame([
['state' => 'rejected', 'reason' => 'Promise has been cancelled'],
['state' => 'fulfilled', 'value' => 'b2'],
['state' => 'fulfilled', 'value' => 'c']
], $results);
}
}
PK tOS| tests/CreateTest.phpnu ٘ assertInstanceOf(FulfilledPromise::class, $p);
}
public function testReturnsPromiseForPromise()
{
$p = new Promise();
$this->assertSame($p, P\Create::promiseFor($p));
}
public function testReturnsPromiseForThennable()
{
$p = new Thennable();
$wrapped = P\Create::promiseFor($p);
$this->assertNotSame($p, $wrapped);
$this->assertInstanceOf(PromiseInterface::class, $wrapped);
$p->resolve('foo');
P\Utils::queue()->run();
$this->assertSame('foo', $wrapped->wait());
}
public function testReturnsRejection()
{
$p = P\Create::rejectionFor('fail');
$this->assertInstanceOf(RejectedPromise::class, $p);
$this->assertSame('fail', PropertyHelper::get($p, 'reason'));
}
public function testReturnsPromisesAsIsInRejectionFor()
{
$a = new Promise();
$b = P\Create::rejectionFor($a);
$this->assertSame($a, $b);
}
public function testIterForReturnsIterator()
{
$iter = new \ArrayIterator();
$this->assertSame($iter, P\Create::iterFor($iter));
}
}
PK tOSid tests/Thennable.phpnu ٘ nextPromise = new Promise();
}
public function then(callable $res = null, callable $rej = null)
{
return $this->nextPromise->then($res, $rej);
}
public function resolve($value)
{
$this->nextPromise->resolve($value);
}
}
PK tOS?)d8 8 tests/EachPromiseTest.phpnu ٘ 100]);
$this->assertSame($each->promise(), $each->promise());
}
public function testResolvesInCaseOfAnEmptyList()
{
$promises = [];
$each = new EachPromise($promises);
$p = $each->promise();
$this->assertNull($p->wait());
$this->assertTrue(P\Is::fulfilled($p));
}
public function testResolvesInCaseOfAnEmptyListAndInvokesFulfilled()
{
$promises = [];
$each = new EachPromise($promises);
$p = $each->promise();
$onFulfilledCalled = false;
$onRejectedCalled = false;
$p->then(
function () use (&$onFulfilledCalled) {
$onFulfilledCalled = true;
},
function () use (&$onRejectedCalled) {
$onRejectedCalled = true;
}
);
$this->assertNull($p->wait());
$this->assertTrue(P\Is::fulfilled($p));
$this->assertTrue($onFulfilledCalled);
$this->assertFalse($onRejectedCalled);
}
public function testInvokesAllPromises()
{
$promises = [new Promise(), new Promise(), new Promise()];
$called = [];
$each = new EachPromise($promises, [
'fulfilled' => function ($value) use (&$called) {
$called[] = $value;
}
]);
$p = $each->promise();
$promises[0]->resolve('a');
$promises[1]->resolve('c');
$promises[2]->resolve('b');
P\Utils::queue()->run();
$this->assertSame(['a', 'c', 'b'], $called);
$this->assertTrue(P\Is::fulfilled($p));
}
public function testIsWaitable()
{
$a = $this->createSelfResolvingPromise('a');
$b = $this->createSelfResolvingPromise('b');
$called = [];
$each = new EachPromise([$a, $b], [
'fulfilled' => function ($value) use (&$called) { $called[] = $value; }
]);
$p = $each->promise();
$this->assertNull($p->wait());
$this->assertTrue(P\Is::fulfilled($p));
$this->assertSame(['a', 'b'], $called);
}
public function testCanResolveBeforeConsumingAll()
{
$called = 0;
$a = $this->createSelfResolvingPromise('a');
$b = new Promise(function () { $this->fail(); });
$each = new EachPromise([$a, $b], [
'fulfilled' => function ($value, $idx, Promise $aggregate) use (&$called) {
$this->assertSame($idx, 0);
$this->assertSame('a', $value);
$aggregate->resolve(null);
$called++;
},
'rejected' => function (\Exception $reason) {
$this->fail($reason->getMessage());
}
]);
$p = $each->promise();
$p->wait();
$this->assertNull($p->wait());
$this->assertSame(1, $called);
$this->assertTrue(P\Is::fulfilled($a));
$this->assertTrue(P\Is::pending($b));
// Resolving $b has no effect on the aggregate promise.
$b->resolve('foo');
$this->assertSame(1, $called);
}
public function testLimitsPendingPromises()
{
$pending = [new Promise(), new Promise(), new Promise(), new Promise()];
$promises = new \ArrayIterator($pending);
$each = new EachPromise($promises, ['concurrency' => 2]);
$p = $each->promise();
$this->assertCount(2, PropertyHelper::get($each, 'pending'));
$pending[0]->resolve('a');
$this->assertCount(2, PropertyHelper::get($each, 'pending'));
$this->assertTrue($promises->valid());
$pending[1]->resolve('b');
P\Utils::queue()->run();
$this->assertCount(2, PropertyHelper::get($each, 'pending'));
$this->assertTrue($promises->valid());
$promises[2]->resolve('c');
P\Utils::queue()->run();
$this->assertCount(1, PropertyHelper::get($each, 'pending'));
$this->assertTrue(P\Is::pending($p));
$promises[3]->resolve('d');
P\Utils::queue()->run();
$this->assertNull(PropertyHelper::get($each, 'pending'));
$this->assertTrue(P\Is::fulfilled($p));
$this->assertFalse($promises->valid());
}
public function testDynamicallyLimitsPendingPromises()
{
$calls = [];
$pendingFn = function ($count) use (&$calls) {
$calls[] = $count;
return 2;
};
$pending = [new Promise(), new Promise(), new Promise(), new Promise()];
$promises = new \ArrayIterator($pending);
$each = new EachPromise($promises, ['concurrency' => $pendingFn]);
$p = $each->promise();
$this->assertCount(2, PropertyHelper::get($each, 'pending'));
$pending[0]->resolve('a');
$this->assertCount(2, PropertyHelper::get($each, 'pending'));
$this->assertTrue($promises->valid());
$pending[1]->resolve('b');
$this->assertCount(2, PropertyHelper::get($each, 'pending'));
P\Utils::queue()->run();
$this->assertTrue($promises->valid());
$promises[2]->resolve('c');
P\Utils::queue()->run();
$this->assertCount(1, PropertyHelper::get($each, 'pending'));
$this->assertTrue(P\Is::pending($p));
$promises[3]->resolve('d');
P\Utils::queue()->run();
$this->assertNull(PropertyHelper::get($each, 'pending'));
$this->assertTrue(P\Is::fulfilled($p));
$this->assertSame([0, 1, 1, 1], $calls);
$this->assertFalse($promises->valid());
}
public function testClearsReferencesWhenResolved()
{
$called = false;
$a = new Promise(function () use (&$a, &$called) {
$a->resolve('a');
$called = true;
});
$each = new EachPromise([$a], [
'concurrency' => function () { return 1; },
'fulfilled' => function () {},
'rejected' => function () {}
]);
$each->promise()->wait();
$this->assertNull(PropertyHelper::get($each, 'onFulfilled'));
$this->assertNull(PropertyHelper::get($each, 'onRejected'));
$this->assertNull(PropertyHelper::get($each, 'iterable'));
$this->assertNull(PropertyHelper::get($each, 'pending'));
$this->assertNull(PropertyHelper::get($each, 'concurrency'));
$this->assertTrue($called);
}
public function testCanBeCancelled()
{
$called = false;
$a = new FulfilledPromise('a');
$b = new Promise(function () use (&$called) { $called = true; });
$each = new EachPromise([$a, $b], [
'fulfilled' => function ($value, $idx, Promise $aggregate) {
$aggregate->cancel();
},
'rejected' => function ($reason) use (&$called) {
$called = true;
},
]);
$p = $each->promise();
$p->wait(false);
$this->assertTrue(P\Is::fulfilled($a));
$this->assertTrue(P\Is::pending($b));
$this->assertTrue(P\Is::rejected($p));
$this->assertFalse($called);
}
public function testDoesNotBlowStackWithFulfilledPromises()
{
$pending = [];
for ($i = 0; $i < 100; $i++) {
$pending[] = new FulfilledPromise($i);
}
$values = [];
$each = new EachPromise($pending, [
'fulfilled' => function ($value) use (&$values) {
$values[] = $value;
}
]);
$called = false;
$each->promise()->then(function () use (&$called) {
$called = true;
});
$this->assertFalse($called);
P\Utils::queue()->run();
$this->assertTrue($called);
$this->assertSame(range(0, 99), $values);
}
public function testDoesNotBlowStackWithRejectedPromises()
{
$pending = [];
for ($i = 0; $i < 100; $i++) {
$pending[] = new RejectedPromise($i);
}
$values = [];
$each = new EachPromise($pending, [
'rejected' => function ($value) use (&$values) {
$values[] = $value;
}
]);
$called = false;
$each->promise()->then(
function () use (&$called) { $called = true; },
function () { $this->fail('Should not have rejected.'); }
);
$this->assertFalse($called);
P\Utils::queue()->run();
$this->assertTrue($called);
$this->assertSame(range(0, 99), $values);
}
public function testReturnsPromiseForWhatever()
{
$called = [];
$arr = ['a', 'b'];
$each = new EachPromise($arr, [
'fulfilled' => function ($v) use (&$called) { $called[] = $v; }
]);
$p = $each->promise();
$this->assertNull($p->wait());
$this->assertSame(['a', 'b'], $called);
}
public function testRejectsAggregateWhenNextThrows()
{
$iter = function () {
yield 'a';
throw new \Exception('Failure');
};
$each = new EachPromise($iter());
$p = $each->promise();
$e = null;
$received = null;
$p->then(null, function ($reason) use (&$e) { $e = $reason; });
P\Utils::queue()->run();
$this->assertInstanceOf(\Exception::class, $e);
$this->assertSame('Failure', $e->getMessage());
}
public function testDoesNotCallNextOnIteratorUntilNeededWhenWaiting()
{
$results = [];
$values = [10];
$remaining = 9;
$iter = function () use (&$values) {
while ($value = array_pop($values)) {
yield $value;
}
};
$each = new EachPromise($iter(), [
'concurrency' => 1,
'fulfilled' => function ($r) use (&$results, &$values, &$remaining) {
$results[] = $r;
if ($remaining > 0) {
$values[] = $remaining--;
}
}
]);
$each->promise()->wait();
$this->assertSame(range(10, 1), $results);
}
public function testDoesNotCallNextOnIteratorUntilNeededWhenAsync()
{
$firstPromise = new Promise();
$pending = [$firstPromise];
$values = [$firstPromise];
$results = [];
$remaining = 9;
$iter = function () use (&$values) {
while ($value = array_pop($values)) {
yield $value;
}
};
$each = new EachPromise($iter(), [
'concurrency' => 1,
'fulfilled' => function ($r) use (&$results, &$values, &$remaining, &$pending) {
$results[] = $r;
if ($remaining-- > 0) {
$pending[] = $values[] = new Promise();
}
}
]);
$i = 0;
$each->promise();
while ($promise = array_pop($pending)) {
$promise->resolve($i++);
P\Utils::queue()->run();
}
$this->assertSame(range(0, 9), $results);
}
private function createSelfResolvingPromise($value)
{
$p = new Promise(function () use (&$p, $value) {
$p->resolve($value);
});
$trickCsFixer = true;
return $p;
}
public function testMutexPreventsGeneratorRecursion()
{
if (defined('HHVM_VERSION')) {
$this->markTestIncomplete('Broken on HHVM.');
}
$results = $promises = [];
for ($i = 0; $i < 20; $i++) {
$p = $this->createSelfResolvingPromise($i);
$pending[] = $p;
$promises[] = $p;
}
$iter = function () use (&$promises, &$pending) {
foreach ($promises as $promise) {
// Resolve a promises, which will trigger the then() function,
// which would cause the EachPromise to try to add more
// promises to the queue. Without a lock, this would trigger
// a "Cannot resume an already running generator" fatal error.
if ($p = array_pop($pending)) {
$p->wait();
}
yield $promise;
}
};
$each = new EachPromise($iter(), [
'concurrency' => 5,
'fulfilled' => function ($r) use (&$results, &$pending) {
$results[] = $r;
}
]);
$each->promise()->wait();
$this->assertCount(20, $results);
}
public function testIteratorWithSameKey()
{
if (defined('HHVM_VERSION')) {
$this->markTestIncomplete('Broken on HHVM.');
}
$iter = function () {
yield 'foo' => $this->createSelfResolvingPromise(1);
yield 'foo' => $this->createSelfResolvingPromise(2);
yield 1 => $this->createSelfResolvingPromise(3);
yield 1 => $this->createSelfResolvingPromise(4);
};
$called = 0;
$each = new EachPromise($iter(), [
'fulfilled' => function ($value, $idx, Promise $aggregate) use (&$called) {
$called++;
if ($value < 3) {
$this->assertSame('foo', $idx);
} else {
$this->assertSame(1, $idx);
}
},
]);
$each->promise()->wait();
$this->assertSame(4, $called);
}
public function testIsWaitableWhenLimited()
{
$promises = [
$this->createSelfResolvingPromise('a'),
$this->createSelfResolvingPromise('c'),
$this->createSelfResolvingPromise('b'),
$this->createSelfResolvingPromise('d')
];
$called = [];
$each = new EachPromise($promises, [
'concurrency' => 2,
'fulfilled' => function ($value) use (&$called) {
$called[] = $value;
}
]);
$p = $each->promise();
$this->assertNull($p->wait());
$this->assertSame(['a', 'c', 'b', 'd'], $called);
$this->assertTrue(P\Is::fulfilled($p));
}
}
PK tOSrQ tests/NotPromiseInstance.phpnu ٘ nextPromise = new Promise();
}
public function then(callable $res = null, callable $rej = null)
{
return $this->nextPromise->then($res, $rej);
}
public function otherwise(callable $onRejected)
{
return $this->then($onRejected);
}
public function resolve($value)
{
$this->nextPromise->resolve($value);
}
public function reject($reason)
{
$this->nextPromise->reject($reason);
}
public function wait($unwrap = true, $defaultResolution = null)
{
}
public function cancel()
{
}
public function getState()
{
return $this->nextPromise->getState();
}
}
PK tOSkM tests/Thing1.phpnu ٘ message = $message;
}
public function __toString()
{
return $this->message;
}
}
PK tOS?< tests/PropertyHelper.phpnu ٘
*/
class PropertyHelper
{
/**
* @param object $object
* @param string $property
*
* @throws \ReflectionException
*/
public static function get($object, $property)
{
$property = (new \ReflectionObject($object))->getProperty($property);
$property->setAccessible(true);
return $property->getValue($object);
}
}
PK tOS5G G tests/TaskQueueTest.phpnu ٘ assertTrue($tq->isEmpty());
}
public function testKnowsIfFull()
{
$tq = new TaskQueue(false);
$tq->add(function () {});
$this->assertFalse($tq->isEmpty());
}
public function testExecutesTasksInOrder()
{
$tq = new TaskQueue(false);
$called = [];
$tq->add(function () use (&$called) { $called[] = 'a'; });
$tq->add(function () use (&$called) { $called[] = 'b'; });
$tq->add(function () use (&$called) { $called[] = 'c'; });
$tq->run();
$this->assertSame(['a', 'b', 'c'], $called);
}
}
PK tOS]\ tests/RejectionExceptionTest.phpnu ٘ assertSame($thing, $e->getReason());
$this->assertSame('The promise was rejected with reason: foo', $e->getMessage());
}
public function testCanGetReasonMessageFromJson()
{
$reason = new Thing2();
$e = new RejectionException($reason);
$this->assertStringContainsString("{}", $e->getMessage());
}
}
PK tOSG\f f tests/EachTest.phpnu ٘ resolve('a');
P\Utils::queue()->run();
$this->assertTrue(P\Is::fulfilled($aggregate));
}
public function testEachLimitAllRejectsOnFailure()
{
$p = [new FulfilledPromise('a'), new RejectedPromise('b')];
$aggregate = P\Each::ofLimitAll($p, 2);
P\Utils::queue()->run();
$this->assertTrue(P\Is::rejected($aggregate));
$result = P\Utils::inspect($aggregate);
$this->assertSame('b', $result['reason']);
}
}
PK tOS^.* tests/RejectedPromiseTest.phpnu ٘ assertTrue(P\Is::rejected($p));
try {
$p->wait(true);
$this->fail();
} catch (\Exception $e) {
$this->assertTrue(P\Is::rejected($p));
$this->assertStringContainsString('foo', $e->getMessage());
}
}
public function testCannotCancel()
{
$p = new RejectedPromise('foo');
$p->cancel();
$this->assertTrue(P\Is::rejected($p));
}
/**
* @exepctedExceptionMessage Cannot resolve a rejected promise
*/
public function testCannotResolve()
{
$this->expectException(\LogicException::class);
$p = new RejectedPromise('foo');
$p->resolve('bar');
}
/**
* @exepctedExceptionMessage Cannot reject a rejected promise
*/
public function testCannotReject()
{
$this->expectException(\LogicException::class);
$p = new RejectedPromise('foo');
$p->reject('bar');
}
public function testCanRejectWithSameValue()
{
$p = new RejectedPromise('foo');
$p->reject('foo');
$this->assertTrue(P\Is::rejected($p));
}
public function testThrowsSpecificException()
{
$e = new \Exception();
$p = new RejectedPromise($e);
try {
$p->wait(true);
$this->fail();
} catch (\Exception $e2) {
$this->assertSame($e, $e2);
}
}
public function testCannotResolveWithPromise()
{
$this->expectException(\InvalidArgumentException::class);
new RejectedPromise(new Promise());
}
public function testReturnsSelfWhenNoOnReject()
{
$p = new RejectedPromise('a');
$this->assertSame($p, $p->then());
}
public function testInvokesOnRejectedAsynchronously()
{
$p = new RejectedPromise('a');
$r = null;
$f = function ($reason) use (&$r) { $r = $reason; };
$p->then(null, $f);
$this->assertNull($r);
P\Utils::queue()->run();
$this->assertSame('a', $r);
}
public function testInvokesOnRejectedOnWait()
{
$p = new RejectedPromise('a');
$r = null;
$f = function ($reason) use (&$r) { $r = $reason; };
$p->then(null, $f);
$this->assertNull($r);
$p->wait(false);
$this->assertSame('a', $r);
}
public function testReturnsNewRejectedWhenOnRejectedFails()
{
$p = new RejectedPromise('a');
$f = function () { throw new \Exception('b'); };
$p2 = $p->then(null, $f);
$this->assertNotSame($p, $p2);
try {
$p2->wait();
$this->fail();
} catch (\Exception $e) {
$this->assertSame('b', $e->getMessage());
}
}
public function testWaitingIsNoOp()
{
$p = new RejectedPromise('a');
$p->wait(false);
$this->assertTrue(P\Is::rejected($p));
}
public function testOtherwiseIsSugarForRejections()
{
$p = new RejectedPromise('foo');
$p->otherwise(function ($v) use (&$c) { $c = $v; });
P\Utils::queue()->run();
$this->assertSame('foo', $c);
}
public function testCanResolveThenWithSuccess()
{
$actual = null;
$p = new RejectedPromise('foo');
$p->otherwise(function ($v) {
return $v . ' bar';
})->then(function ($v) use (&$actual) {
$actual = $v;
});
P\Utils::queue()->run();
$this->assertSame('foo bar', $actual);
}
public function testDoesNotTryToRejectTwiceDuringTrampoline()
{
$fp = new RejectedPromise('a');
$t1 = $fp->then(null, function ($v) { return $v . ' b'; });
$t1->resolve('why!');
$this->assertSame('why!', $t1->wait());
P\Utils::queue()->run();
}
}
PK tOSaH
tests/CoroutineTest.phpnu ٘ assertInstanceOf(P\Coroutine::class, P\Coroutine::of($fn));
}
/**
* @dataProvider promiseInterfaceMethodProvider
*
* @param string $method
* @param array $args
*/
public function testShouldProxyPromiseMethodsToResultPromise($method, $args = [])
{
$coroutine = new Coroutine(function () { yield 0; });
$mockPromise = $this->getMockForAbstractClass(PromiseInterface::class);
call_user_func_array([$mockPromise->expects($this->once())->method($method), 'with'], $args);
$resultPromiseProp = (new ReflectionClass(Coroutine::class))->getProperty('result');
$resultPromiseProp->setAccessible(true);
$resultPromiseProp->setValue($coroutine, $mockPromise);
call_user_func_array([$coroutine, $method], $args);
}
public function promiseInterfaceMethodProvider()
{
return [
['then', [null, null]],
['otherwise', [function () {}]],
['wait', [true]],
['getState', []],
['resolve', [null]],
['reject', [null]],
];
}
public function testShouldCancelResultPromiseAndOutsideCurrentPromise()
{
$coroutine = new Coroutine(function () { yield 0; });
$mockPromises = [
'result' => $this->getMockForAbstractClass(PromiseInterface::class),
'currentPromise' => $this->getMockForAbstractClass(PromiseInterface::class),
];
foreach ($mockPromises as $propName => $mockPromise) {
/**
* @var $mockPromise \PHPUnit_Framework_MockObject_MockObject
*/
$mockPromise->expects($this->once())
->method('cancel')
->with();
$promiseProp = (new ReflectionClass(Coroutine::class))->getProperty($propName);
$promiseProp->setAccessible(true);
$promiseProp->setValue($coroutine, $mockPromise);
}
$coroutine->cancel();
}
public function testWaitShouldResolveChainedCoroutines()
{
$promisor = function () {
return P\Coroutine::of(function () {
yield $promise = new Promise(function () use (&$promise) {
$promise->resolve(1);
});
});
};
$promise = $promisor()->then($promisor)->then($promisor);
$this->assertSame(1, $promise->wait());
}
public function testWaitShouldHandleIntermediateErrors()
{
$promise = P\Coroutine::of(function () {
yield $promise = new Promise(function () use (&$promise) {
$promise->resolve(1);
});
})
->then(function () {
return P\Coroutine::of(function () {
yield $promise = new Promise(function () use (&$promise) {
$promise->reject(new \Exception);
});
});
})
->otherwise(function (\Exception $error = null) {
if (!$error) {
self::fail('Error did not propagate.');
}
return 3;
});
$this->assertSame(3, $promise->wait());
}
}
PK tOS{p tests/Thing2.phpnu ٘ expectException(\LogicException::class);
$this->expectExceptionMessage('The promise is already fulfilled');
$p = new Promise();
$p->resolve('foo');
$p->resolve('bar');
$this->assertSame('foo', $p->wait());
}
public function testCanResolveWithSameValue()
{
$p = new Promise();
$p->resolve('foo');
$p->resolve('foo');
$this->assertSame('foo', $p->wait());
}
public function testCannotRejectNonPendingPromise()
{
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Cannot change a fulfilled promise to rejected');
$p = new Promise();
$p->resolve('foo');
$p->reject('bar');
$this->assertSame('foo', $p->wait());
}
public function testCanRejectWithSameValue()
{
$p = new Promise();
$p->reject('foo');
$p->reject('foo');
$this->assertTrue(P\Is::rejected($p));
}
public function testCannotRejectResolveWithSameValue()
{
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Cannot change a fulfilled promise to rejected');
$p = new Promise();
$p->resolve('foo');
$p->reject('foo');
}
public function testInvokesWaitFunction()
{
$p = new Promise(function () use (&$p) { $p->resolve('10'); });
$this->assertSame('10', $p->wait());
}
public function testRejectsAndThrowsWhenWaitFailsToResolve()
{
$this->expectException(\GuzzleHttp\Promise\RejectionException::class);
$this->expectExceptionMessage('The promise was rejected with reason: Invoking the wait callback did not resolve the promise');
$p = new Promise(function () {});
$p->wait();
}
public function testThrowsWhenUnwrapIsRejectedWithNonException()
{
$this->expectException(\GuzzleHttp\Promise\RejectionException::class);
$this->expectExceptionMessage('The promise was rejected with reason: foo');
$p = new Promise(function () use (&$p) { $p->reject('foo'); });
$p->wait();
}
public function testThrowsWhenUnwrapIsRejectedWithException()
{
$this->expectException(\UnexpectedValueException::class);
$this->expectExceptionMessage('foo');
$e = new \UnexpectedValueException('foo');
$p = new Promise(function () use (&$p, $e) { $p->reject($e); });
$p->wait();
}
public function testDoesNotUnwrapExceptionsWhenDisabled()
{
$p = new Promise(function () use (&$p) { $p->reject('foo'); });
$this->assertTrue(P\Is::pending($p));
$p->wait(false);
$this->assertTrue(P\Is::rejected($p));
}
public function testRejectsSelfWhenWaitThrows()
{
$e = new \UnexpectedValueException('foo');
$p = new Promise(function () use ($e) { throw $e; });
try {
$p->wait();
$this->fail();
} catch (\UnexpectedValueException $e) {
$this->assertTrue(P\Is::rejected($p));
}
}
public function testWaitsOnNestedPromises()
{
$p = new Promise(function () use (&$p) { $p->resolve('_'); });
$p2 = new Promise(function () use (&$p2) { $p2->resolve('foo'); });
$p3 = $p->then(function () use ($p2) { return $p2; });
$this->assertSame('foo', $p3->wait());
}
public function testThrowsWhenWaitingOnPromiseWithNoWaitFunction()
{
$this->expectException(\GuzzleHttp\Promise\RejectionException::class);
$p = new Promise();
$p->wait();
}
public function testThrowsWaitExceptionAfterPromiseIsResolved()
{
$p = new Promise(function () use (&$p) {
$p->reject('Foo!');
throw new \Exception('Bar?');
});
try {
$p->wait();
$this->fail();
} catch (\Exception $e) {
$this->assertSame('Bar?', $e->getMessage());
}
}
public function testGetsActualWaitValueFromThen()
{
$p = new Promise(function () use (&$p) { $p->reject('Foo!'); });
$p2 = $p->then(null, function ($reason) {
return new RejectedPromise([$reason]);
});
try {
$p2->wait();
$this->fail('Should have thrown');
} catch (RejectionException $e) {
$this->assertSame(['Foo!'], $e->getReason());
}
}
public function testWaitBehaviorIsBasedOnLastPromiseInChain()
{
$p3 = new Promise(function () use (&$p3) { $p3->resolve('Whoop'); });
$p2 = new Promise(function () use (&$p2, $p3) { $p2->reject($p3); });
$p = new Promise(function () use (&$p, $p2) { $p->reject($p2); });
$this->assertSame('Whoop', $p->wait());
}
public function testWaitsOnAPromiseChainEvenWhenNotUnwrapped()
{
$p2 = new Promise(function () use (&$p2) {
$p2->reject('Fail');
});
$p = new Promise(function () use ($p2, &$p) {
$p->resolve($p2);
});
$p->wait(false);
$this->assertTrue(P\Is::rejected($p2));
}
public function testCannotCancelNonPending()
{
$p = new Promise();
$p->resolve('foo');
$p->cancel();
$this->assertTrue(P\Is::fulfilled($p));
}
public function testCancelsPromiseWhenNoCancelFunction()
{
$this->expectException(\GuzzleHttp\Promise\CancellationException::class);
$p = new Promise();
$p->cancel();
$this->assertTrue(P\Is::rejected($p));
$p->wait();
}
public function testCancelsPromiseWithCancelFunction()
{
$called = false;
$p = new Promise(null, function () use (&$called) { $called = true; });
$p->cancel();
$this->assertTrue(P\Is::rejected($p));
$this->assertTrue($called);
}
public function testCancelsUppermostPendingPromise()
{
$called = false;
$p1 = new Promise(null, function () use (&$called) { $called = true; });
$p2 = $p1->then(function () {});
$p3 = $p2->then(function () {});
$p4 = $p3->then(function () {});
$p3->cancel();
$this->assertTrue(P\Is::rejected($p1));
$this->assertTrue(P\Is::rejected($p2));
$this->assertTrue(P\Is::rejected($p3));
$this->assertTrue(P\Is::pending($p4));
$this->assertTrue($called);
try {
$p3->wait();
$this->fail();
} catch (CancellationException $e) {
$this->assertStringContainsString('cancelled', $e->getMessage());
}
try {
$p4->wait();
$this->fail();
} catch (CancellationException $e) {
$this->assertStringContainsString('cancelled', $e->getMessage());
}
$this->assertTrue(P\Is::rejected($p4));
}
public function testCancelsChildPromises()
{
$called1 = $called2 = $called3 = false;
$p1 = new Promise(null, function () use (&$called1) { $called1 = true; });
$p2 = new Promise(null, function () use (&$called2) { $called2 = true; });
$p3 = new Promise(null, function () use (&$called3) { $called3 = true; });
$p4 = $p2->then(function () use ($p3) { return $p3; });
$p5 = $p4->then(function () { $this->fail(); });
$p4->cancel();
$this->assertTrue(P\Is::pending($p1));
$this->assertTrue(P\Is::rejected($p2));
$this->assertTrue(P\Is::pending($p3));
$this->assertTrue(P\Is::rejected($p4));
$this->assertTrue(P\Is::pending($p5));
$this->assertFalse($called1);
$this->assertTrue($called2);
$this->assertFalse($called3);
}
public function testRejectsPromiseWhenCancelFails()
{
$called = false;
$p = new Promise(null, function () use (&$called) {
$called = true;
throw new \Exception('e');
});
$p->cancel();
$this->assertTrue(P\Is::rejected($p));
$this->assertTrue($called);
try {
$p->wait();
$this->fail();
} catch (\Exception $e) {
$this->assertSame('e', $e->getMessage());
}
}
public function testCreatesPromiseWhenFulfilledAfterThen()
{
$p = new Promise();
$carry = null;
$p2 = $p->then(function ($v) use (&$carry) { $carry = $v; });
$this->assertNotSame($p, $p2);
$p->resolve('foo');
P\Utils::queue()->run();
$this->assertSame('foo', $carry);
}
public function testCreatesPromiseWhenFulfilledBeforeThen()
{
$p = new Promise();
$p->resolve('foo');
$carry = null;
$p2 = $p->then(function ($v) use (&$carry) { $carry = $v; });
$this->assertNotSame($p, $p2);
$this->assertNull($carry);
P\Utils::queue()->run();
$this->assertSame('foo', $carry);
}
public function testCreatesPromiseWhenFulfilledWithNoCallback()
{
$p = new Promise();
$p->resolve('foo');
$p2 = $p->then();
$this->assertNotSame($p, $p2);
$this->assertInstanceOf(FulfilledPromise::class, $p2);
}
public function testCreatesPromiseWhenRejectedAfterThen()
{
$p = new Promise();
$carry = null;
$p2 = $p->then(null, function ($v) use (&$carry) { $carry = $v; });
$this->assertNotSame($p, $p2);
$p->reject('foo');
P\Utils::queue()->run();
$this->assertSame('foo', $carry);
}
public function testCreatesPromiseWhenRejectedBeforeThen()
{
$p = new Promise();
$p->reject('foo');
$carry = null;
$p2 = $p->then(null, function ($v) use (&$carry) { $carry = $v; });
$this->assertNotSame($p, $p2);
$this->assertNull($carry);
P\Utils::queue()->run();
$this->assertSame('foo', $carry);
}
public function testCreatesPromiseWhenRejectedWithNoCallback()
{
$p = new Promise();
$p->reject('foo');
$p2 = $p->then();
$this->assertNotSame($p, $p2);
$this->assertInstanceOf(RejectedPromise::class, $p2);
}
public function testInvokesWaitFnsForThens()
{
$p = new Promise(function () use (&$p) { $p->resolve('a'); });
$p2 = $p
->then(function ($v) { return $v . '-1-'; })
->then(function ($v) { return $v . '2'; });
$this->assertSame('a-1-2', $p2->wait());
}
public function testStacksThenWaitFunctions()
{
$p1 = new Promise(function () use (&$p1) { $p1->resolve('a'); });
$p2 = new Promise(function () use (&$p2) { $p2->resolve('b'); });
$p3 = new Promise(function () use (&$p3) { $p3->resolve('c'); });
$p4 = $p1
->then(function () use ($p2) { return $p2; })
->then(function () use ($p3) { return $p3; });
$this->assertSame('c', $p4->wait());
}
public function testForwardsFulfilledDownChainBetweenGaps()
{
$p = new Promise();
$r = $r2 = null;
$p->then(null, null)
->then(function ($v) use (&$r) { $r = $v; return $v . '2'; })
->then(function ($v) use (&$r2) { $r2 = $v; });
$p->resolve('foo');
P\Utils::queue()->run();
$this->assertSame('foo', $r);
$this->assertSame('foo2', $r2);
}
public function testForwardsRejectedPromisesDownChainBetweenGaps()
{
$p = new Promise();
$r = $r2 = null;
$p->then(null, null)
->then(null, function ($v) use (&$r) { $r = $v; return $v . '2'; })
->then(function ($v) use (&$r2) { $r2 = $v; });
$p->reject('foo');
P\Utils::queue()->run();
$this->assertSame('foo', $r);
$this->assertSame('foo2', $r2);
}
public function testForwardsThrownPromisesDownChainBetweenGaps()
{
$e = new \Exception();
$p = new Promise();
$r = $r2 = null;
$p->then(null, null)
->then(null, function ($v) use (&$r, $e) {
$r = $v;
throw $e;
})
->then(
null,
function ($v) use (&$r2) { $r2 = $v; }
);
$p->reject('foo');
P\Utils::queue()->run();
$this->assertSame('foo', $r);
$this->assertSame($e, $r2);
}
public function testForwardsReturnedRejectedPromisesDownChainBetweenGaps()
{
$p = new Promise();
$rejected = new RejectedPromise('bar');
$r = $r2 = null;
$p->then(null, null)
->then(null, function ($v) use (&$r, $rejected) {
$r = $v;
return $rejected;
})
->then(
null,
function ($v) use (&$r2) { $r2 = $v; }
);
$p->reject('foo');
P\Utils::queue()->run();
$this->assertSame('foo', $r);
$this->assertSame('bar', $r2);
try {
$p->wait();
} catch (RejectionException $e) {
$this->assertSame('foo', $e->getReason());
}
}
public function testForwardsHandlersToNextPromise()
{
$p = new Promise();
$p2 = new Promise();
$resolved = null;
$p
->then(function ($v) use ($p2) { return $p2; })
->then(function ($value) use (&$resolved) { $resolved = $value; });
$p->resolve('a');
$p2->resolve('b');
P\Utils::queue()->run();
$this->assertSame('b', $resolved);
}
public function testRemovesReferenceFromChildWhenParentWaitedUpon()
{
$r = null;
$p = new Promise(function () use (&$p) { $p->resolve('a'); });
$p2 = new Promise(function () use (&$p2) { $p2->resolve('b'); });
$pb = $p->then(
function ($v) use ($p2, &$r) {
$r = $v;
return $p2;
}
)
->then(function ($v) { return $v . '.'; });
$this->assertSame('a', $p->wait());
$this->assertSame('b', $p2->wait());
$this->assertSame('b.', $pb->wait());
$this->assertSame('a', $r);
}
public function testForwardsHandlersWhenFulfilledPromiseIsReturned()
{
$res = [];
$p = new Promise();
$p2 = new Promise();
$p2->resolve('foo');
$p2->then(function ($v) use (&$res) { $res[] = 'A:' . $v; });
// $res is A:foo
$p
->then(function () use ($p2, &$res) { $res[] = 'B'; return $p2; })
->then(function ($v) use (&$res) { $res[] = 'C:' . $v; });
$p->resolve('a');
$p->then(function ($v) use (&$res) { $res[] = 'D:' . $v; });
P\Utils::queue()->run();
$this->assertSame(['A:foo', 'B', 'D:a', 'C:foo'], $res);
}
public function testForwardsHandlersWhenRejectedPromiseIsReturned()
{
$res = [];
$p = new Promise();
$p2 = new Promise();
$p2->reject('foo');
$p2->then(null, function ($v) use (&$res) { $res[] = 'A:' . $v; });
$p->then(null, function () use ($p2, &$res) { $res[] = 'B'; return $p2; })
->then(null, function ($v) use (&$res) { $res[] = 'C:' . $v; });
$p->reject('a');
$p->then(null, function ($v) use (&$res) { $res[] = 'D:' . $v; });
P\Utils::queue()->run();
$this->assertSame(['A:foo', 'B', 'D:a', 'C:foo'], $res);
}
public function testDoesNotForwardRejectedPromise()
{
$res = [];
$p = new Promise();
$p2 = new Promise();
$p2->cancel();
$p2->then(function ($v) use (&$res) { $res[] = "B:$v"; return $v; });
$p->then(function ($v) use ($p2, &$res) { $res[] = "B:$v"; return $p2; })
->then(function ($v) use (&$res) { $res[] = 'C:' . $v; });
$p->resolve('a');
$p->then(function ($v) use (&$res) { $res[] = 'D:' . $v; });
P\Utils::queue()->run();
$this->assertSame(['B:a', 'D:a'], $res);
}
public function testRecursivelyForwardsWhenOnlyThennable()
{
$res = [];
$p = new Promise();
$p2 = new Thennable();
$p2->resolve('foo');
$p2->then(function ($v) use (&$res) { $res[] = 'A:' . $v; });
$p->then(function () use ($p2, &$res) { $res[] = 'B'; return $p2; })
->then(function ($v) use (&$res) { $res[] = 'C:' . $v; });
$p->resolve('a');
$p->then(function ($v) use (&$res) { $res[] = 'D:' . $v; });
P\Utils::queue()->run();
$this->assertSame(['A:foo', 'B', 'D:a', 'C:foo'], $res);
}
public function testRecursivelyForwardsWhenNotInstanceOfPromise()
{
$res = [];
$p = new Promise();
$p2 = new NotPromiseInstance();
$p2->then(function ($v) use (&$res) { $res[] = 'A:' . $v; });
$p->then(function () use ($p2, &$res) { $res[] = 'B'; return $p2; })
->then(function ($v) use (&$res) { $res[] = 'C:' . $v; });
$p->resolve('a');
$p->then(function ($v) use (&$res) { $res[] = 'D:' . $v; });
P\Utils::queue()->run();
$this->assertSame(['B', 'D:a'], $res);
$p2->resolve('foo');
P\Utils::queue()->run();
$this->assertSame(['B', 'D:a', 'A:foo', 'C:foo'], $res);
}
public function testCannotResolveWithSelf()
{
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Cannot fulfill or reject a promise with itself');
$p = new Promise();
$p->resolve($p);
}
public function testCannotRejectWithSelf()
{
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Cannot fulfill or reject a promise with itself');
$p = new Promise();
$p->reject($p);
}
public function testDoesNotBlowStackWhenWaitingOnNestedThens()
{
$inner = new Promise(function () use (&$inner) { $inner->resolve(0); });
$prev = $inner;
for ($i = 1; $i < 100; $i++) {
$prev = $prev->then(function ($i) { return $i + 1; });
}
$parent = new Promise(function () use (&$parent, $prev) {
$parent->resolve($prev);
});
$this->assertSame(99, $parent->wait());
}
public function testOtherwiseIsSugarForRejections()
{
$p = new Promise();
$p->reject('foo');
$p->otherwise(function ($v) use (&$c) { $c = $v; });
P\Utils::queue()->run();
$this->assertSame($c, 'foo');
}
public function testRepeatedWaitFulfilled()
{
$promise = new Promise(function () use (&$promise) {
$promise->resolve('foo');
});
$this->assertSame('foo', $promise->wait());
$this->assertSame('foo', $promise->wait());
}
public function testRepeatedWaitRejected()
{
$promise = new Promise(function () use (&$promise) {
$promise->reject(new \RuntimeException('foo'));
});
$exceptionCount = 0;
try {
$promise->wait();
} catch (\Exception $e) {
$this->assertSame('foo', $e->getMessage());
$exceptionCount++;
}
try {
$promise->wait();
} catch (\Exception $e) {
$this->assertSame('foo', $e->getMessage());
$exceptionCount++;
}
$this->assertSame(2, $exceptionCount);
}
}
PK tOS)P tests/AggregateExceptionTest.phpnu ٘ assertStringContainsString('foo', $e->getMessage());
$this->assertSame(['baz', 'bar'], $e->getReason());
}
}
PK tOSz*/ LICENSEnu ٘ The MIT License (MIT)
Copyright (c) 2015 Michael Dowling
Copyright (c) 2015 Graham Campbell
Copyright (c) 2017 Tobias Schultze
Copyright (c) 2020 Tobias Nyholm
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
PK tOS_\
composer.jsonnu ٘ {
"name": "guzzlehttp/promises",
"description": "Guzzle promises library",
"keywords": ["promise"],
"license": "MIT",
"authors": [
{
"name": "Graham Campbell",
"email": "hello@gjcampbell.co.uk",
"homepage": "https://github.com/GrahamCampbell"
},
{
"name": "Michael Dowling",
"email": "mtdowling@gmail.com",
"homepage": "https://github.com/mtdowling"
},
{
"name": "Tobias Nyholm",
"email": "tobias.nyholm@gmail.com",
"homepage": "https://github.com/Nyholm"
},
{
"name": "Tobias Schultze",
"email": "webmaster@tubo-world.de",
"homepage": "https://github.com/Tobion"
}
],
"require": {
"php": ">=5.5"
},
"require-dev": {
"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/"
}
},
"scripts": {
"test": "vendor/bin/simple-phpunit",
"test-ci": "vendor/bin/simple-phpunit --coverage-text"
},
"extra": {
"branch-alias": {
"dev-master": "1.5-dev"
}
},
"config": {
"preferred-install": "dist",
"sort-packages": true
}
}
PK tOSH+ phpstan-baseline.neonnu ٘ PK tOS=- .travis.ymlnu ٘ PK tOSBi Makefilenu ٘ PK tOScC C README.mdnu ٘ PK tOSnS 6H psalm.xmlnu ٘ PK tOS.8i i
+J .gitignorenu ٘ PK tOSrW
J src/Is.phpnu ٘ PK tOSc]@ @ N src/Create.phpnu ٘ PK tOSCM mW src/Coroutine.phpnu ٘ PK tOS' ' h src/functions.phpnu ٘ PK tOSRE " "
y src/Utils.phpnu ٘ PK tOS(K| | src/AggregateException.phpnu ٘ PK tOSzHy | src/PromisorInterface.phpnu ٘ PK tOS͔ src/TaskQueue.phpnu ٘ PK tOSl src/TaskQueueInterface.phpnu ٘ PK tOSA" " src/Promise.phpnu ٘ PK tOS9 src/RejectionException.phpnu ٘ PK tOS߇`G G src/Each.phpnu ٘ PK tOSu G src/FulfilledPromise.phpnu ٘ PK tOS69 g src/CancellationException.phpnu ٘ PK tOSR$Q Q k src/EachPromise.phpnu ٘ PK tOSǬp
src/RejectedPromise.phpnu ٘ PK tOS߇' d( src/functions_include.phpnu ٘ PK tOS2~$ $ T) src/PromiseInterface.phpnu ٘ PK tOSdu%{ { 4 .github/workflows/ci.ymlnu ٘ PK tOSI5 < .github/workflows/static.ymlnu ٘ PK tOSM@ A .github/workflows/.editorconfignu ٘ PK tOS)K K 0B .github/FUNDING.ymlnu ٘ PK tOSp- - B .github/stale.ymlnu ٘ PK tOS%O ,E .gitattributesnu ٘ PK tOSm m G .php-cs-fixer.dist.phpnu ٘ PK tOS]QG U phpstan.neon.distnu ٘ PK tOSRG G V phpunit.xml.distnu ٘ PK tOSv
BY .editorconfignu ٘ PK tOSg Z CHANGELOG.mdnu ٘ PK tOSb:, ` tests/IsTest.phpnu ٘ PK tOS e tests/FulfilledPromiseTest.phpnu ٘ PK tOSwo[ [ $r tests/UtilsTest.phpnu ٘ PK tOS| tests/CreateTest.phpnu ٘ PK tOSid J tests/Thennable.phpnu ٘ PK tOS?)d8 8 O tests/EachPromiseTest.phpnu ٘ PK tOSrQ h tests/NotPromiseInstance.phpnu ٘ PK tOSkM t tests/Thing1.phpnu ٘ PK tOS?< tests/PropertyHelper.phpnu ٘ PK tOS5G G tests/TaskQueueTest.phpnu ٘ PK tOS]\ tests/RejectionExceptionTest.phpnu ٘ PK tOSG\f f tests/EachTest.phpnu ٘ PK tOS^.* Y! tests/RejectedPromiseTest.phpnu ٘ PK tOSaH
22 tests/CoroutineTest.phpnu ٘ PK tOS{p a@ tests/Thing2.phpnu ٘ PK tOS!mN N ]A tests/PromiseTest.phpnu ٘ PK tOS)P tests/AggregateExceptionTest.phpnu ٘ PK tOSz*/ LICENSEnu ٘ PK tOS_\
Ζ composer.jsonnu ٘ PK 6 6