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
|
|
|
|
2017-12-03 02:07:46 +01:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
final class Deferred {
|
2017-12-20 17:30:43 +01:00
|
|
|
/** @var object Has public resolve and fail methods. */
|
|
|
|
private $resolver;
|
|
|
|
|
|
|
|
/** @var \Amp\Promise Hides placeholder methods */
|
2017-12-03 02:07:46 +01:00
|
|
|
private $promise;
|
|
|
|
|
|
|
|
public function __construct() {
|
2018-01-13 17:36:01 +01:00
|
|
|
$this->resolver = new class implements Promise {
|
2017-12-03 02:07:46 +01:00
|
|
|
use Internal\Placeholder {
|
|
|
|
resolve as public;
|
|
|
|
fail as public;
|
2016-05-21 16:44:52 +02:00
|
|
|
}
|
2017-12-03 02:07:46 +01:00
|
|
|
};
|
2017-12-20 17:30:43 +01:00
|
|
|
|
2018-01-13 17:36:01 +01:00
|
|
|
$this->promise = new Internal\PrivatePromise($this->resolver);
|
2017-12-03 02:07:46 +01:00
|
|
|
}
|
2016-05-21 16:44:52 +02:00
|
|
|
|
2017-12-03 02:07:46 +01:00
|
|
|
/**
|
|
|
|
* @return \Amp\Promise
|
|
|
|
*/
|
|
|
|
public function promise(): Promise {
|
|
|
|
return $this->promise;
|
|
|
|
}
|
2016-05-21 16:44:52 +02:00
|
|
|
|
2017-12-03 02:07:46 +01:00
|
|
|
/**
|
|
|
|
* Fulfill the promise with the given value.
|
|
|
|
*
|
|
|
|
* @param mixed $value
|
|
|
|
*/
|
|
|
|
public function resolve($value = null) {
|
2017-12-20 17:30:43 +01:00
|
|
|
$this->resolver->resolve($value);
|
2017-12-03 02:07:46 +01:00
|
|
|
}
|
2016-05-21 16:44:52 +02:00
|
|
|
|
2017-12-03 02:07:46 +01:00
|
|
|
/**
|
|
|
|
* Fails the promise the the given reason.
|
|
|
|
*
|
|
|
|
* @param \Throwable $reason
|
|
|
|
*/
|
|
|
|
public function fail(\Throwable $reason) {
|
2017-12-20 17:30:43 +01:00
|
|
|
$this->resolver->fail($reason);
|
2016-05-21 16:44:52 +02:00
|
|
|
}
|
2017-12-03 02:07:46 +01:00
|
|
|
}
|