laravel-websockets/tests/Mocks/PromiseResolver.php

71 lines
1.6 KiB
PHP
Raw Normal View History

<?php
2020-09-10 19:59:26 +00:00
namespace BeyondCode\LaravelWebSockets\Test\Mocks;
use Clue\React\Block;
2020-09-10 19:59:26 +00:00
use React\Promise\FulfilledPromise;
2020-09-10 19:59:49 +00:00
use React\Promise\PromiseInterface;
class PromiseResolver implements PromiseInterface
{
/**
* The promise to resolve.
*
* @var \React\Promise\PromiseInterface
*/
protected $promise;
/**
* The loop.
*
* @var \React\EventLoop\LoopInterface
*/
protected $loop;
/**
* Initialize the promise resolver.
*
* @param PromiseInterface $promise
* @param LoopInterface $loop
* @return void
*/
public function __construct($promise, $loop)
{
$this->promise = $promise;
$this->loop = $loop;
}
/**
* Intercept the promise then() and run it in sync.
*
* @param callable|null $onFulfilled
* @param callable|null $onRejected
* @param callable|null $onProgress
* @return PromiseInterface
*/
public function then(callable $onFulfilled = null, callable $onRejected = null, callable $onProgress = null)
{
$result = Block\await(
$this->promise, $this->loop
);
2020-09-10 19:59:26 +00:00
$result = call_user_func($onFulfilled, $result);
2020-09-10 19:59:26 +00:00
return $result instanceof PromiseInterface
? $result
: new FulfilledPromise($result);
}
/**
* Pass the calls to the promise.
*
* @param string $method
* @param array $args
* @return mixed
*/
public function __call($method, $args)
{
return call_user_func([$this->promise, $method], $args);
}
}