From 5f68ae9e48dff4e0c962fb78535bf5897681e5f9 Mon Sep 17 00:00:00 2001 From: xl Date: Fri, 26 May 2023 11:16:43 +0800 Subject: [PATCH] qqq --- Abstracts/TraitServer.php | 297 +++++++++++++++++++------------------- Handler/OnRequest.php | 1 - 2 files changed, 146 insertions(+), 152 deletions(-) diff --git a/Abstracts/TraitServer.php b/Abstracts/TraitServer.php index 5cd170f..b4c32d0 100644 --- a/Abstracts/TraitServer.php +++ b/Abstracts/TraitServer.php @@ -17,175 +17,170 @@ trait TraitServer { - private array $_process = []; + private array $_process = []; - /** - * @param string|array|BaseProcess $class - * @return void - * @throws Exception - */ - public function addProcess(string|array|BaseProcess $class): void - { - if (!is_array($class)) { - $class = [$class]; - } - foreach ($class as $name) { - if (is_string($name)) { - $name = Kiri::getDi()->get($name); - } - if (isset($this->_process[$name->getName()])) { - throw new Exception('Process(' . $name->getName() . ') is exists.'); - } - $this->_process[$name->getName()] = $this->genProcess($name); - } - } + /** + * @param string|array|BaseProcess $class + * @return void + * @throws Exception + */ + public function addProcess(string|array|BaseProcess $class): void + { + if (!is_array($class)) { + $class = [$class]; + } + foreach ($class as $name) { + if (is_string($name)) { + $name = Kiri::getDi()->get($name); + } + if (isset($this->_process[$name->getName()])) { + throw new Exception('Process(' . $name->getName() . ') is exists.'); + } + $this->_process[$name->getName()] = $this->genProcess($name); + } + } - /** - * @param BaseProcess $name - * @return Process - */ - private function genProcess(BaseProcess $name): Process - { - return new Process(function (Process $process) use ($name) { - $process->name($name->getName()); - $name->onSigterm()->process($process); - }, - $name->getRedirectStdinAndStdout(), - $name->getPipeType(), - $name->isEnableCoroutine()); - } + /** + * @param BaseProcess $name + * @return Process + */ + private function genProcess(BaseProcess $name): Process + { + return new Process(function (Process $process) use ($name) { + $process->name($name->getName()); + $name->onSigterm()->process($process); + }, + $name->getRedirectStdinAndStdout(), + $name->getPipeType(), + $name->isEnableCoroutine()); + } - /** - * @return void - * @throws Exception - */ - public function onSignal(): void - { - $signal = \config('signal', []); - $this->onPcntlSignal(SIGINT, [$this, 'onSigint']); - foreach ($signal as $sig => $value) { - if (is_array($value) && is_string($value[0])) { - $value[0] = \Kiri::getDi()->get($value[0]); - } - if (!is_callable($value, true)) { - throw new Exception('Register signal callback must can exec.'); - } - $this->onPcntlSignal($sig, $value); - } - } + /** + * @return void + * @throws Exception + */ + public function onSignal(): void + { + $signal = \config('signal', []); + $this->onPcntlSignal(SIGINT, [$this, 'onSigint']); + foreach ($signal as $sig => $value) { + if (is_array($value) && is_string($value[0])) { + $value[0] = \Kiri::getDi()->get($value[0]); + } + if (!is_callable($value, true)) { + throw new Exception('Register signal callback must can exec.'); + } + $this->onPcntlSignal($sig, $value); + } + } - /** - * @param $signal - * @param $callback - * @return void - */ - private function onPcntlSignal($signal, $callback): void - { -// if (get_called_class() != CoroutineServer::class) { - pcntl_signal(SIGINT, [$this, 'onSigint']); -// } else { -// Coroutine::create(static function () use ($signal, $callback) { -// $data = Coroutine::waitSignal($signal); -// if ($data) { -// $callback($signal, [true]); -// } -// }); -// } - } + /** + * @param $signal + * @param $callback + * @return void + */ + private function onPcntlSignal($signal, $callback): void + { + pcntl_signal(SIGINT, [$this, 'onSigint']); + } - /** - * @return array - */ - public function getProcess(): array - { - return $this->_process; - } + /** + * @return array + */ + public function getProcess(): array + { + return $this->_process; + } - /** - * @param array $ports - * @return array - */ - public function sortService(array $ports): array - { - $array = []; - foreach ($ports as $port) { - if ($port['type'] == Constant::SERVER_TYPE_WEBSOCKET) { - array_unshift($array, $port); - } else if ($port['type'] == Constant::SERVER_TYPE_HTTP) { - if (!empty($array) && $array[0]['type'] == Constant::SERVER_TYPE_WEBSOCKET) { - $array[] = $port; - } else { - array_unshift($array, $port); - } - } else { - $array[] = $port; - } - } - return $array; - } + /** + * @param array $ports + * @return array + * @throws ReflectionException + */ + public function sortService(array $ports): array + { + $array = []; + foreach ($ports as $port) { + $array = $this->sort($array, $port); + } + return $array; + } - /** - * @param array $ports - * @return array - * @throws ReflectionException - */ - public function genConfigService(array $ports): array - { - $array = []; - $ports = $ports['ports'] ?? []; - foreach ($ports as $port) { - $config = \Kiri::getDi()->make(Config::class, [], $port); - if ($port['type'] == Constant::SERVER_TYPE_WEBSOCKET) { - array_unshift($array, $config); - } else if ($port['type'] == Constant::SERVER_TYPE_HTTP) { - if (!empty($array) && $array[0]['type'] == Constant::SERVER_TYPE_WEBSOCKET) { - $array[] = $config; - } else { - array_unshift($array, $config); - } - } else { - $array[] = $config; - } - } - return $array; - } + /** + * @param array $ports + * @return array + * @throws ReflectionException + */ + public function genConfigService(array $ports): array + { + $array = []; + $ports = $ports['ports'] ?? []; + foreach ($ports as $port) { + $array = $this->sort($array, $port); + } + return $array; + } - /** - * @param $type - * @return string|null - */ - public function getServerClass($type): ?string - { - return match ($type) { - Constant::SERVER_TYPE_BASE, Constant::SERVER_TYPE_TCP, - Constant::SERVER_TYPE_UDP => Server::class, - Constant::SERVER_TYPE_HTTP => HServer::class, - Constant::SERVER_TYPE_WEBSOCKET => WServer::class, - default => null - }; - } + /** + * @param array $array + * @param $port + * @return array + * @throws ReflectionException + */ + private function sort(array $array, $port): array + { + $config = created(Config::class, [], $port); + if ($port['type'] == Constant::SERVER_TYPE_WEBSOCKET) { + array_unshift($array, $config); + } else if ($port['type'] == Constant::SERVER_TYPE_HTTP) { + if (!empty($array) && $array[0]['type'] == Constant::SERVER_TYPE_WEBSOCKET) { + $array[] = $config; + } else { + array_unshift($array, $config); + } + } else { + $array[] = $config; + } + return $array; + } - /** - * @param $type - * @return string|null - */ - public function getCoroutineServerClass($type): ?string - { - return match ($type) { - Constant::SERVER_TYPE_BASE, Constant::SERVER_TYPE_TCP, Constant::SERVER_TYPE_UDP => Coroutine\Server::class, - Constant::SERVER_TYPE_HTTP, Constant::SERVER_TYPE_WEBSOCKET => Coroutine\Http\Server::class, - default => null - }; - } + /** + * @param $type + * @return string|null + */ + public function getServerClass($type): ?string + { + return match ($type) { + Constant::SERVER_TYPE_BASE, Constant::SERVER_TYPE_TCP, + Constant::SERVER_TYPE_UDP => Server::class, + Constant::SERVER_TYPE_HTTP => HServer::class, + Constant::SERVER_TYPE_WEBSOCKET => WServer::class, + default => null + }; + } + + + /** + * @param $type + * @return string|null + */ + public function getCoroutineServerClass($type): ?string + { + return match ($type) { + Constant::SERVER_TYPE_BASE, Constant::SERVER_TYPE_TCP, Constant::SERVER_TYPE_UDP => Coroutine\Server::class, + Constant::SERVER_TYPE_HTTP, Constant::SERVER_TYPE_WEBSOCKET => Coroutine\Http\Server::class, + default => null + }; + } } diff --git a/Handler/OnRequest.php b/Handler/OnRequest.php index 2bd3bc2..4e32207 100644 --- a/Handler/OnRequest.php +++ b/Handler/OnRequest.php @@ -22,7 +22,6 @@ use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\UriInterface; use Swoole\Http\Request; use Swoole\Http\Response; -use Kiri\Di\Inject\Service; use const Kiri\Router\ROUTER_TYPE_HTTP; class OnRequest implements OnRequestInterface