diff --git a/src/Adapter/ReactPhp/EventLoop.php b/src/Adapter/ReactPhp/EventLoop.php index 934fac5..bd3e785 100644 --- a/src/Adapter/ReactPhp/EventLoop.php +++ b/src/Adapter/ReactPhp/EventLoop.php @@ -69,6 +69,7 @@ public function async(\Generator $generator): Promise try { if (!$generator->valid()) { $deferred->resolve($generator->getReturn()); + return; } $promise = $generator->current(); diff --git a/src/Adapter/Swoole/EventLoop.php b/src/Adapter/Swoole/EventLoop.php index d195e69..8fe33a2 100644 --- a/src/Adapter/Swoole/EventLoop.php +++ b/src/Adapter/Swoole/EventLoop.php @@ -2,17 +2,17 @@ namespace M6Web\Tornado\Adapter\Swoole; +use function extension_loaded; use Generator; use M6Web\Tornado\Adapter\Swoole\Internal\DummyPromise; use M6Web\Tornado\Deferred; use M6Web\Tornado\Promise; -use Swoole\Coroutine; use RuntimeException; +use Swoole\Coroutine; use Swoole\Event; use Swoole\IDEHelper\StubGenerators\Swoole; use Swoole\Process; use Throwable; -use function extension_loaded; class EventLoop implements \M6Web\Tornado\EventLoop { @@ -23,9 +23,7 @@ class EventLoop implements \M6Web\Tornado\EventLoop public function __construct() { if (!extension_loaded('swoole')) { - throw new RuntimeException( - 'EventLoop must running only with swoole extension.' - ); + throw new RuntimeException('EventLoop must running only with swoole extension.'); } $this->cids = []; @@ -61,12 +59,12 @@ public function __destruct() private function shiftCoroutine(): mixed { - if(count($this->cids) === 0) { + if (count($this->cids) === 0) { return null; } $cid = array_shift($this->cids); - if(Coroutine::exists($cid)) { + if (Coroutine::exists($cid)) { $this->oldCids[] = $cid; Coroutine::resume($cid); } else { @@ -81,6 +79,7 @@ private function pushCoroutine(): mixed $cid = Coroutine::getCid(); $this->cids[] = $cid; Coroutine::yield(); + return $cid; } @@ -93,10 +92,10 @@ private function createPromise(): DummyPromise private function getValue($value) { - if($value instanceof DummyPromise && !$this->isPending($value)) { - if($value->getException() !== null) { + if ($value instanceof DummyPromise && !$this->isPending($value)) { + if ($value->getException() !== null) { foreach ($this->pendingThrowPromises as $index => $promise) { - if($promise === $value) { + if ($promise === $value) { unset($this->pendingThrowPromises[$index]); } } @@ -106,7 +105,7 @@ private function getValue($value) $value = $this->getValue($value->getValue()); } - if(is_array($value)) { + if (is_array($value)) { foreach ($value as $k => $v) { $value[$k] = $this->getValue($v); } @@ -121,18 +120,18 @@ private function getValue($value) private function isPending(DummyPromise $promise): bool { - if($promise->isPending()) { + if ($promise->isPending()) { return $promise->isPending(); } - if($promise->getException() === null) { + if ($promise->getException() === null) { if ($promise->getValue() instanceof DummyPromise) { return $promise->getValue()->isPending(); } - if(is_array($promise->getValue())) { + if (is_array($promise->getValue())) { foreach ($promise->getValue() as $value) { - if($value instanceof DummyPromise && $value->isPending()) { + if ($value instanceof DummyPromise && $value->isPending()) { return $value->isPending(); } } @@ -149,7 +148,7 @@ public function wait(Promise $promise): mixed { $promise = DummyPromise::wrap($promise); - if(count($this->cids) === 0 && $this->isPending($promise)) { + if (count($this->cids) === 0 && $this->isPending($promise)) { throw new \Error('Impossible to resolve the promise, no more task to execute..'); } @@ -170,7 +169,7 @@ public function async(Generator $generator): Promise try { while ($generator->valid()) { $promise = $generator->current(); - if(!$promise instanceof DummyPromise) { + if (!$promise instanceof DummyPromise) { throw new \Error('Asynchronous function is yielding a ['.gettype($promise).'] instead of a Promise.'); } $this->pushCoroutine(); @@ -261,9 +260,9 @@ public function promiseRace(Promise ...$promises): Promise $deferred = $this->createPromise(); foreach ($promises as $promise) { - DummyPromise::wrap($promise)->addCallback(function(DummyPromise $promise) use ($deferred) { + DummyPromise::wrap($promise)->addCallback(function (DummyPromise $promise) use ($deferred) { if ($deferred->isPending()) { - if($promise->getException() !== null) { + if ($promise->getException() !== null) { $deferred->reject($promise->getException()); } else { $deferred->resolve($promise->getValue()); @@ -303,7 +302,7 @@ public function promiseRejected(Throwable $throwable): Promise public function idle(): Promise { $promise = $this->createPromise(); - Coroutine::create(function() use ($promise) { + Coroutine::create(function () use ($promise) { $this->pushCoroutine(); // Coroutine::defer(function () use ($promise) { $promise->resolve(null); @@ -319,7 +318,7 @@ public function idle(): Promise public function delay(int $milliseconds): Promise { $promise = $this->createPromise(); - Coroutine::create(function() use($milliseconds, $promise) { + Coroutine::create(function () use ($milliseconds, $promise) { $this->pushCoroutine(); // Coroutine::sleep($milliseconds / 1000); usleep($milliseconds * 1000); diff --git a/src/Adapter/Swoole/Internal/DummyPromise.php b/src/Adapter/Swoole/Internal/DummyPromise.php index 88b042a..f0be824 100644 --- a/src/Adapter/Swoole/Internal/DummyPromise.php +++ b/src/Adapter/Swoole/Internal/DummyPromise.php @@ -2,8 +2,8 @@ namespace M6Web\Tornado\Adapter\Swoole\Internal; -use M6Web\Tornado\Promise; use M6Web\Tornado\Deferred; +use M6Web\Tornado\Promise; use Throwable; final class DummyPromise implements Promise, Deferred @@ -26,13 +26,14 @@ public static function wrap(Promise $promise): self return $promise; } - + public function getPromise(): Promise { return $this; } - public function addCallback(callable $callback) { + public function addCallback(callable $callback) + { $this->callbacks[] = $callback; } diff --git a/src/Adapter/Swoole/Internal/PromiseWrapper.php b/src/Adapter/Swoole/Internal/PromiseWrapper.php index d74cf42..f173a5b 100644 --- a/src/Adapter/Swoole/Internal/PromiseWrapper.php +++ b/src/Adapter/Swoole/Internal/PromiseWrapper.php @@ -55,7 +55,7 @@ public static function createHandled(SwoolePromise $swoolePromise, ?callable $de public function getSwoolePromise(): SwoolePromise { - if($this->deferredExecutor) { + if ($this->deferredExecutor) { return $this->swoolePromise->then($this->deferredExecutor); } diff --git a/src/Adapter/Swoole/Internal/SwooleDeferred.php b/src/Adapter/Swoole/Internal/SwooleDeferred.php index 924f2f2..6b65f22 100644 --- a/src/Adapter/Swoole/Internal/SwooleDeferred.php +++ b/src/Adapter/Swoole/Internal/SwooleDeferred.php @@ -2,8 +2,8 @@ namespace M6Web\Tornado\Adapter\Swoole\Internal; -use M6Web\Tornado\Promise; use M6Web\Tornado\Deferred; +use M6Web\Tornado\Promise; /** * @internal @@ -19,7 +19,6 @@ public function __construct() { } - public function getPromise(): Promise { return $this->getSwoolePromise(); @@ -30,7 +29,7 @@ public function getSwoolePromise(): SwoolePromise if (null === $this->promise) { $this->promise = new SwoolePromise(function ($resolve, $reject) { $this->resolveCallback = $resolve; - $this->rejectCallback = $reject; + $this->rejectCallback = $reject; }); } diff --git a/src/Adapter/Swoole/Internal/SwoolePromise.php b/src/Adapter/Swoole/Internal/SwoolePromise.php index 1a44749..5bc1084 100644 --- a/src/Adapter/Swoole/Internal/SwoolePromise.php +++ b/src/Adapter/Swoole/Internal/SwoolePromise.php @@ -2,10 +2,10 @@ namespace M6Web\Tornado\Adapter\Swoole\Internal; +use function count; use M6Web\Tornado\Promise; -use Swoole\Coroutine; use RuntimeException; -use function count; +use Swoole\Coroutine; /** * @internal @@ -21,12 +21,10 @@ final class SwoolePromise implements Promise /** * Promise constructor. - * - * @param callable $executor */ public function __construct(callable $executor) { - $this->onResolve = function($status, $value) { + $this->onResolve = function ($status, $value) { $this->result = [$status, $value]; }; @@ -47,9 +45,6 @@ public function __construct(callable $executor) /** * {@inheritDoc} - * - * @param callable $promise - * @return SwoolePromise */ final public static function create(callable $promise): SwoolePromise { @@ -60,7 +55,6 @@ final public static function create(callable $promise): SwoolePromise * {@inheritDoc} * * @param mixed $value - * @return SwoolePromise */ final public static function resolve($value): SwoolePromise { @@ -73,7 +67,6 @@ final public static function resolve($value): SwoolePromise * {@inheritDoc} * * @param mixed $value - * @return SwoolePromise */ final public static function reject($value): SwoolePromise { @@ -84,9 +77,6 @@ final public static function reject($value): SwoolePromise /** * {@inheritDoc} - * - * @param callable $onRejected - * @return SwoolePromise */ final public function catch(callable $onRejected): SwoolePromise { @@ -95,23 +85,19 @@ final public function catch(callable $onRejected): SwoolePromise /** * {@inheritDoc} - * - * @param callable|null $onFulfilled - * @param callable|null $onRejected - * @return SwoolePromise */ public function then(?callable $onFulfilled = null, ?callable $onRejected = null): SwoolePromise { - if($this->result === null) { + if ($this->result === null) { return self::create(function (callable $resolve, callable $reject) use ($onFulfilled, $onRejected) { - $this->onResolve = function($status, $value) use ($resolve, $reject, $onFulfilled, $onRejected) { - if($status === self::STATUS_RESOLVE) { - if($onFulfilled !== null) { + $this->onResolve = function ($status, $value) use ($resolve, $reject, $onFulfilled, $onRejected) { + if ($status === self::STATUS_RESOLVE) { + if ($onFulfilled !== null) { $onFulfilled($value); } $resolve($value); - } else if($status === self::STATUS_ERROR) { - if($onRejected !== null) { + } elseif ($status === self::STATUS_ERROR) { + if ($onRejected !== null) { $onRejected($value); } $reject($value); @@ -121,15 +107,15 @@ public function then(?callable $onFulfilled = null, ?callable $onRejected = null } return self::create(function (callable $resolve, callable $reject) use ($onFulfilled, $onRejected) { - if($this->result[0] === self::STATUS_RESOLVE) { + if ($this->result[0] === self::STATUS_RESOLVE) { $value = $this->result[1]; - if($onFulfilled !== null) { + if ($onFulfilled !== null) { $onFulfilled($value); } $resolve($value); - } else if($this->result[0] === self::STATUS_ERROR) { + } elseif ($this->result[0] === self::STATUS_ERROR) { $error = $this->result[1]; - if($onRejected !== null) { + if ($onRejected !== null) { $onRejected($error); } $reject($error); @@ -141,7 +127,6 @@ public function then(?callable $onFulfilled = null, ?callable $onRejected = null * {@inheritDoc} * * @param iterable|SwoolePromise[] $promises - * @return SwoolePromise */ public static function all(iterable $promises): SwoolePromise { @@ -149,21 +134,20 @@ public static function all(iterable $promises): SwoolePromise $ticks = count($promises); $firstError = null; - $result = []; - $key = 0; + $result = []; + $key = 0; foreach ($promises as $promise) { if (!$promise instanceof SwoolePromise) { - throw new RuntimeException( - 'Supported only SwoolePromise instance' - ); + throw new RuntimeException('Supported only SwoolePromise instance'); } $promise->then(function ($value) use ($key, &$result, &$ticks, $resolve) { $result[$key] = $value; $ticks--; - if($ticks === 0) { + if ($ticks === 0) { ksort($result); $resolve($result); } + return $value; }, function ($error) use (&$firstError, &$ticks, $reject) { $ticks--; diff --git a/src/Adapter/Swoole/Internal/YieldPromise.php b/src/Adapter/Swoole/Internal/YieldPromise.php index 60478eb..5b07c35 100644 --- a/src/Adapter/Swoole/Internal/YieldPromise.php +++ b/src/Adapter/Swoole/Internal/YieldPromise.php @@ -2,8 +2,8 @@ namespace M6Web\Tornado\Adapter\Swoole\Internal; -use M6Web\Tornado\Promise; use M6Web\Tornado\Deferred; +use M6Web\Tornado\Promise; use Swoole\Coroutine; final class YieldPromise implements Promise, Deferred @@ -21,7 +21,7 @@ public function __construct() public function yield(): void { - if($this->isSettled) { + if ($this->isSettled) { return; } @@ -33,7 +33,7 @@ public function value() { assert($this->isSettled, new \Error('Promise is not resolved.')); - if($this->exception) { + if ($this->exception) { return $this->exception; } @@ -67,7 +67,7 @@ public function resolve($value): void public function reject(\Throwable $throwable): void { assert(false === $this->isSettled, new \Error('Promise is already resolved.')); - + $this->isSettled = true; $this->exception = $throwable; foreach ($this->cids as $cid => $dummy) { diff --git a/src/Adapter/Swoole/PromiseEventLoop.php b/src/Adapter/Swoole/PromiseEventLoop.php index fd77d57..0351531 100644 --- a/src/Adapter/Swoole/PromiseEventLoop.php +++ b/src/Adapter/Swoole/PromiseEventLoop.php @@ -2,15 +2,15 @@ namespace M6Web\Tornado\Adapter\Swoole; +use function extension_loaded; use M6Web\Tornado\Adapter\Common\Internal\FailingPromiseCollection; use M6Web\Tornado\Adapter\Swoole\Internal\SwooleDeferred; use M6Web\Tornado\Adapter\Swoole\Internal\SwoolePromise; use M6Web\Tornado\Deferred; use M6Web\Tornado\Promise; +use RuntimeException; use Swoole\Coroutine; use Swoole\Event; -use RuntimeException; -use function extension_loaded; class PromiseEventLoop implements \M6Web\Tornado\EventLoop { @@ -23,9 +23,7 @@ class PromiseEventLoop implements \M6Web\Tornado\EventLoop public function __construct() { if (!extension_loaded('swoole')) { - throw new RuntimeException( - 'SwoolePromise MUST running only in CLI mode with swoole extension.' - ); + throw new RuntimeException('SwoolePromise MUST running only in CLI mode with swoole extension.'); } $this->streamLoop = new Internal\StreamEventLoop(); @@ -81,6 +79,7 @@ public function async(\Generator $generator): Promise try { if (!$generator->valid()) { $deferred->resolve($generator->getReturn()); + return; } $promise = $generator->current(); @@ -122,8 +121,8 @@ function ($reason) use ($generator, $deferred, $fnWrapGenerator) { */ public function promiseAll(Promise ...$promises): Promise { - $promises = array_map(function($promise) { - if($promise instanceof Internal\PromiseWrapper) { + $promises = array_map(function ($promise) { + if ($promise instanceof Internal\PromiseWrapper) { return $promise->getSwoolePromise(); } @@ -211,7 +210,7 @@ public function promiseRejected(\Throwable $throwable): Promise */ public function idle(): Promise { - return Internal\PromiseWrapper::createUnhandled(new SwoolePromise(function($resolve) { + return Internal\PromiseWrapper::createUnhandled(new SwoolePromise(function ($resolve) { Coroutine::defer(function () use ($resolve) { //Coroutine::sleep(0.001); $resolve(null); @@ -224,8 +223,8 @@ public function idle(): Promise */ public function delay(int $milliseconds): Promise { - return Internal\PromiseWrapper::createUnhandled(SwoolePromise::resolve(null), $this->unhandledFailingPromises, function() use ($milliseconds) { - return new SwoolePromise(function($resolve) use ($milliseconds) { + return Internal\PromiseWrapper::createUnhandled(SwoolePromise::resolve(null), $this->unhandledFailingPromises, function () use ($milliseconds) { + return new SwoolePromise(function ($resolve) use ($milliseconds) { //Coroutine::sleep($milliseconds / 1000000); usleep($milliseconds * 1000); $resolve(null); diff --git a/src/Adapter/Swoole/YieldEventLoop.php b/src/Adapter/Swoole/YieldEventLoop.php index ffbe9fe..b6632ad 100644 --- a/src/Adapter/Swoole/YieldEventLoop.php +++ b/src/Adapter/Swoole/YieldEventLoop.php @@ -2,23 +2,21 @@ namespace M6Web\Tornado\Adapter\Swoole; +use function extension_loaded; use JetBrains\PhpStorm\Pure; use M6Web\Tornado\Adapter\Swoole\Internal\YieldPromise; use M6Web\Tornado\Deferred; use M6Web\Tornado\Promise; +use RuntimeException; use Swoole\Coroutine; use Swoole\Event; -use RuntimeException; -use function extension_loaded; class YieldEventLoop implements \M6Web\Tornado\EventLoop { public function __construct() { if (!extension_loaded('swoole')) { - throw new RuntimeException( - 'EventLoop must running only with swoole extension.' - ); + throw new RuntimeException('EventLoop must running only with swoole extension.'); } } @@ -28,7 +26,7 @@ public function __construct() public function wait(Promise $promise): mixed { $value = null; - $this->async((static function() use($promise, &$value): \Generator { + $this->async((static function () use ($promise, &$value): \Generator { $value = yield $promise; Event::exit(); })()); @@ -43,8 +41,8 @@ public function wait(Promise $promise): mixed public function async(\Generator $generator): Promise { $generatorPromise = new YieldPromise(); - Coroutine::create(function() use($generator, $generatorPromise) { - while($generator->valid()) { + Coroutine::create(function () use ($generator, $generatorPromise) { + while ($generator->valid()) { $promise = YieldPromise::wrap($generator->current()); $promise->yield(); $generator->send($promise->value()); @@ -101,7 +99,6 @@ public function promiseAll(Promise ...$promises): Promise */ public function promiseForeach($traversable, callable $function): Promise { - } /** @@ -109,7 +106,6 @@ public function promiseForeach($traversable, callable $function): Promise */ public function promiseRace(Promise ...$promises): Promise { - } /** @@ -140,7 +136,7 @@ public function promiseRejected(\Throwable $throwable): Promise public function idle(): Promise { $promise = new YieldPromise(); - Coroutine::create(function() use ($promise) { + Coroutine::create(function () use ($promise) { Coroutine::defer(function () use ($promise) { $promise->resolve(null); }); @@ -155,7 +151,7 @@ public function idle(): Promise public function delay(int $milliseconds): Promise { $promise = new YieldPromise(); - Coroutine::create(function() use($milliseconds, $promise) { + Coroutine::create(function () use ($milliseconds, $promise) { Coroutine::sleep($milliseconds / 1000); $promise->resolve(null); }); @@ -166,17 +162,17 @@ public function delay(int $milliseconds): Promise /** * {@inheritdoc} */ - #[Pure] public function deferred(): Deferred - { - return new YieldPromise(); - } + #[Pure] + public function deferred(): Deferred + { + return new YieldPromise(); + } /** * {@inheritdoc} */ public function readable($stream): Promise { - } /** @@ -184,6 +180,5 @@ public function readable($stream): Promise */ public function writable($stream): Promise { - } } diff --git a/tests/Adapter/Swoole/YieldEventLoopTest.php b/tests/Adapter/Swoole/YieldEventLoopTest.php deleted file mode 100644 index 29842c1..0000000 --- a/tests/Adapter/Swoole/YieldEventLoopTest.php +++ /dev/null @@ -1,14 +0,0 @@ -