diff --git a/src/Utils/Coroutine/Coroutine.php b/src/Utils/Coroutine/Coroutine.php new file mode 100644 index 0000000..e208ea8 --- /dev/null +++ b/src/Utils/Coroutine/Coroutine.php @@ -0,0 +1,72 @@ + SwowWaitGroup::class, + Factory::WORKBUNNY_SWOW => SwowWaitGroup::class, + Factory::WORKERMAN_SWOOLE => SwooleWaitGroup::class, + Factory::WORKBUNNY_SWOOLE => SwooleWaitGroup::class, + Factory::RIPPLE_FIBER => RippleWaitGroup::class, + ]; + + /** + * 构造方法 + */ + public function __construct() + { + $this->_interface = new (self::$_handlers[Factory::getCurrentEventLoop()] ?? DefaultWaitGroup::class)(); + } + + /** @inheritdoc */ + public static function registerVerify(mixed $value): false|string + { + return is_a($value, CoroutineInterface::class) ? CoroutineInterface::class : false; + } + + /** @inheritdoc */ + public static function unregisterExecute(string $key): bool + { + return true; + } + + /** + * 代理调用WaitGroupInterface方法 + * + * @param string $name + * @param array $arguments + * @return mixed + */ + public function __call(string $name, array $arguments): mixed + { + if (!method_exists($this->_interface, $name)) { + throw new \BadMethodCallException("Method $name not exists. "); + } + return $this->_interface->$name(...$arguments); + } +} \ No newline at end of file diff --git a/src/Utils/Coroutine/Handlers/CoroutineInterface.php b/src/Utils/Coroutine/Handlers/CoroutineInterface.php new file mode 100644 index 0000000..378e5b9 --- /dev/null +++ b/src/Utils/Coroutine/Handlers/CoroutineInterface.php @@ -0,0 +1,37 @@ +_promise = []; + } + + /** @inheritdoc */ + public function __destruct() + { + $this->_promise = []; + } + + /** @inheritdoc */ + public function create(\Closure $func): string + { + $promise = \Co\async(function () use (&$promise, $func) { + call_user_func($func); + // 移除协程id及promise + unset($this->_promise[spl_object_hash($promise)]); + }); + return spl_object_hash($promise); + } + + /** @inheritdoc */ + public function query(string $id): mixed + { + return $this->_promise[$id] ?? null; + } +} diff --git a/src/Utils/Coroutine/Handlers/SwooleCoroutine.php b/src/Utils/Coroutine/Handlers/SwooleCoroutine.php new file mode 100644 index 0000000..f863082 --- /dev/null +++ b/src/Utils/Coroutine/Handlers/SwooleCoroutine.php @@ -0,0 +1,47 @@ +_promise = []; + } + + /** @inheritdoc */ + public function __destruct() + { + $this->_promise = []; + } + + /** @inheritdoc */ + public function create(\Closure $func): string + { + while (1) { + if ($coroutine = Coroutine::create($func)) { + break; + } + } + return (string)$coroutine; + } + + /** @inheritdoc */ + public function query(string $id): bool + { + return false; + } +} diff --git a/src/Utils/Coroutine/Handlers/SwowCoroutine.php b/src/Utils/Coroutine/Handlers/SwowCoroutine.php new file mode 100644 index 0000000..c32cbf5 --- /dev/null +++ b/src/Utils/Coroutine/Handlers/SwowCoroutine.php @@ -0,0 +1,43 @@ +_promise = []; + } + + /** @inheritdoc */ + public function __destruct() + { + $this->_promise = []; + } + + /** @inheritdoc */ + public function create(\Closure $func): string + { + $coroutine = Coroutine::run($func); + return (string)$coroutine->getId(); + } + + /** @inheritdoc */ + public function query(string $id): ?Coroutine + { + return Coroutine::get((int)$id); + } +}