This commit is contained in:
2026-02-02 15:18:51 +01:00
parent 7a26dd69a5
commit ae0ee002ec
170 changed files with 7446 additions and 1519 deletions

View File

@@ -6,30 +6,23 @@ namespace Http\Promise;
* A promise already fulfilled.
*
* @author Joel Wurtz <joel.wurtz@gmail.com>
*
* @template-covariant T
*
* @implements Promise<T>
*/
final class FulfilledPromise implements Promise
{
/**
* @var T
* @var mixed
*/
private $result;
/**
* @param T $result
* @param mixed $result
*/
public function __construct($result)
{
$this->result = $result;
}
/**
* {@inheritdoc}
*/
public function then(callable $onFulfilled = null, callable $onRejected = null)
public function then(?callable $onFulfilled = null, ?callable $onRejected = null)
{
if (null === $onFulfilled) {
return $this;
@@ -42,21 +35,17 @@ final class FulfilledPromise implements Promise
}
}
/**
* {@inheritdoc}
*/
public function getState()
{
return Promise::FULFILLED;
}
/**
* {@inheritdoc}
*/
public function wait($unwrap = true)
{
if ($unwrap) {
return $this->result;
}
return null;
}
}

View File

@@ -12,8 +12,6 @@ namespace Http\Promise;
*
* @author Joel Wurtz <joel.wurtz@gmail.com>
* @author Márk Sági-Kazár <mark.sagikazar@gmail.com>
*
* @template-covariant T
*/
interface Promise
{
@@ -38,14 +36,12 @@ interface Promise
* If you do not care about one of the cases, you can set the corresponding callable to null
* The callback will be called when the value arrived and never more than once.
*
* @param callable(T): V|null $onFulfilled called when a response will be available
* @param callable(\Exception): V|null $onRejected called when an exception occurs
* @param callable|null $onFulfilled called when a response will be available
* @param callable|null $onRejected called when an exception occurs
*
* @return Promise<V> a new resolved promise with value of the executed callback (onFulfilled / onRejected)
*
* @template V
* @return Promise a new resolved promise with value of the executed callback (onFulfilled / onRejected)
*/
public function then(callable $onFulfilled = null, callable $onRejected = null);
public function then(?callable $onFulfilled = null, ?callable $onRejected = null);
/**
* Returns the state of the promise, one of PENDING, FULFILLED or REJECTED.
@@ -65,9 +61,9 @@ interface Promise
*
* @param bool $unwrap Whether to return resolved value / throw reason or not
*
* @return T Resolved value, null if $unwrap is set to false
* @return ($unwrap is true ? mixed : null) Resolved value, null if $unwrap is set to false
*
* @throws \Exception the rejection reason if $unwrap is set to true and the request failed
* @throws \Throwable the rejection reason if $unwrap is set to true and the request failed
*/
public function wait($unwrap = true);
}

View File

@@ -6,27 +6,20 @@ namespace Http\Promise;
* A rejected promise.
*
* @author Joel Wurtz <joel.wurtz@gmail.com>
*
* @template-covariant T
*
* @implements Promise<T>
*/
final class RejectedPromise implements Promise
{
/**
* @var \Exception
* @var \Throwable
*/
private $exception;
public function __construct(\Exception $exception)
public function __construct(\Throwable $exception)
{
$this->exception = $exception;
}
/**
* {@inheritdoc}
*/
public function then(callable $onFulfilled = null, callable $onRejected = null)
public function then(?callable $onFulfilled = null, ?callable $onRejected = null)
{
if (null === $onRejected) {
return $this;
@@ -39,21 +32,17 @@ final class RejectedPromise implements Promise
}
}
/**
* {@inheritdoc}
*/
public function getState()
{
return Promise::REJECTED;
}
/**
* {@inheritdoc}
*/
public function wait($unwrap = true)
{
if ($unwrap) {
throw $this->exception;
}
return null;
}
}