2016-12-29 21:09:49 +01:00
|
|
|
<?php
|
2016-08-16 06:46:26 +02:00
|
|
|
|
2016-05-24 05:48:28 +02:00
|
|
|
namespace Amp;
|
2016-05-21 16:44:52 +02:00
|
|
|
|
2016-07-12 18:20:06 +02:00
|
|
|
// @codeCoverageIgnoreStart
|
2016-05-21 16:44:52 +02:00
|
|
|
try {
|
2017-01-04 17:24:51 +01:00
|
|
|
if (!@\assert(false)) {
|
|
|
|
development: // PHP 7 development (zend.assertions=1)
|
|
|
|
/**
|
|
|
|
* Deferred is a container for a promise that is resolved using the resolve() and fail() methods of this object.
|
|
|
|
* The contained promise may be accessed using the promise() method. This object should not be part of a public
|
|
|
|
* API, but used internally to create and resolve a promise.
|
|
|
|
*/
|
2016-05-21 16:44:52 +02:00
|
|
|
final class Deferred {
|
|
|
|
/**
|
2017-03-10 21:58:46 +01:00
|
|
|
* @var \Amp\Promise
|
2016-05-21 16:44:52 +02:00
|
|
|
*/
|
2016-11-14 20:59:21 +01:00
|
|
|
private $promise;
|
2016-05-21 16:44:52 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var callable
|
|
|
|
*/
|
|
|
|
private $resolve;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var callable
|
|
|
|
*/
|
|
|
|
private $fail;
|
|
|
|
|
|
|
|
public function __construct() {
|
2016-11-14 20:59:21 +01:00
|
|
|
$this->promise = new Internal\PrivatePromise(function (callable $resolve, callable $fail) {
|
2016-05-21 16:44:52 +02:00
|
|
|
$this->resolve = $resolve;
|
|
|
|
$this->fail = $fail;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-03-10 21:58:46 +01:00
|
|
|
* @return \Amp\Promise
|
2016-05-21 16:44:52 +02:00
|
|
|
*/
|
2016-11-14 20:59:21 +01:00
|
|
|
public function promise(): Promise {
|
|
|
|
return $this->promise;
|
2016-05-21 16:44:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-11-14 20:59:21 +01:00
|
|
|
* Fulfill the promise with the given value.
|
2016-05-21 16:44:52 +02:00
|
|
|
*
|
|
|
|
* @param mixed $value
|
|
|
|
*/
|
|
|
|
public function resolve($value = null) {
|
2016-08-11 21:35:58 +02:00
|
|
|
($this->resolve)($value);
|
2016-05-21 16:44:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-11-14 20:59:21 +01:00
|
|
|
* Fails the promise the the given reason.
|
2016-05-21 16:44:52 +02:00
|
|
|
*
|
2016-08-11 21:35:58 +02:00
|
|
|
* @param \Throwable $reason
|
2016-05-21 16:44:52 +02:00
|
|
|
*/
|
2016-08-11 21:35:58 +02:00
|
|
|
public function fail(\Throwable $reason) {
|
|
|
|
($this->fail)($reason);
|
2016-05-21 16:44:52 +02:00
|
|
|
}
|
|
|
|
}
|
2017-01-04 17:24:51 +01:00
|
|
|
} else {
|
|
|
|
production: // PHP 7 production environment (zend.assertions=0)
|
|
|
|
/**
|
|
|
|
* An optimized version of Deferred for production environments that is itself the promise.
|
|
|
|
*/
|
|
|
|
final class Deferred implements Promise {
|
|
|
|
use Internal\Placeholder {
|
|
|
|
resolve as public;
|
|
|
|
fail as public;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-03-10 21:58:46 +01:00
|
|
|
* @return \Amp\Promise
|
2017-01-04 17:24:51 +01:00
|
|
|
*/
|
|
|
|
public function promise(): Promise {
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
}
|
2016-05-21 16:44:52 +02:00
|
|
|
}
|
|
|
|
} catch (\AssertionError $exception) {
|
|
|
|
goto development; // zend.assertions=1 and assert.exception=1, use development definition.
|
2016-07-12 18:20:06 +02:00
|
|
|
} // @codeCoverageIgnoreEnd
|