From e8b0f8da81bcf8d73317bcc72a3ea0397c9dd587 Mon Sep 17 00:00:00 2001 From: "as2252258@163.com" Date: Tue, 23 Mar 2021 02:38:20 +0800 Subject: [PATCH] modify --- HttpServer/Server.php | 963 ++++++++++++++------------- Rpc/Service.php | 9 +- System/Abstracts/BaseApplication.php | 680 +++++++++---------- 3 files changed, 838 insertions(+), 814 deletions(-) diff --git a/HttpServer/Server.php b/HttpServer/Server.php index 5d26d280..6a310c7b 100644 --- a/HttpServer/Server.php +++ b/HttpServer/Server.php @@ -18,6 +18,8 @@ use HttpServer\Service\Packet; use HttpServer\Service\Websocket; use Exception; use ReflectionException; +use Rpc\Producer; +use Rpc\Service; use Snowflake\Abstracts\Config; use Snowflake\Core\Json; use Snowflake\Error\LoggerProcess; @@ -49,477 +51,496 @@ defined('PID_PATH') or define('PID_PATH', APP_PATH . 'storage/server.pid'); */ class Server extends HttpService { - use Action; - - const HTTP = 'HTTP'; - const TCP = 'TCP'; - const PACKAGE = 'PACKAGE'; - const WEBSOCKET = 'WEBSOCKET'; - - private array $listening = []; - private array $server = [ - 'HTTP' => [SWOOLE_TCP, Http::class], - 'TCP' => [SWOOLE_TCP, Receive::class], - 'PACKAGE' => [SWOOLE_UDP, Packet::class], - 'WEBSOCKET' => [SWOOLE_SOCK_TCP, Websocket::class], - ]; - - private Packet|Websocket|Receive|null|Http $baseServer = null; - - public int $daemon = 0; - - - private array $listenTypes = []; - - - private array $process = [ - 'biomonitoring' => Biomonitoring::class, - 'logger_process' => LoggerProcess::class - ]; - - private array $params = []; - - - /** - * @param $name - * @param $process - * @param array $params - */ - public function addProcess($name, $process, $params = []) - { - $this->process[$name] = $process; - $this->params[$name] = $params; - } - - - /** - * @return array - */ - public function getProcesses(): array - { - return $this->process ?? []; - } - - - /** - * @param array $configs - * @return Packet|Websocket|Receive|Http|null - * @throws Exception - */ - public function initCore(array $configs): Packet|Websocket|Receive|Http|null - { - $this->orders($configs); - return $this->getServer(); - } - - - /** - * @param $configs - * @return Packet|Websocket|Receive|Http|null - * @throws Exception - */ - private function orders($configs): Packet|Websocket|Receive|Http|null - { - $servers = $this->sortServers($configs); - foreach ($servers as $server) { - $this->create($server); - if (!$this->baseServer) { - return null; - } - } - return $this->baseServer; - } - - - /** - * @return string start server - * - * start server - * @throws ConfigException - * @throws Exception - */ - public function start(): string - { - $configs = Config::get('servers', true); - - $baseServer = $this->initCore($configs); - if (!$baseServer) { - return 'ok'; - } - - Runtime::enableCoroutine(true, SWOOLE_HOOK_ALL ^ SWOOLE_HOOK_BLOCKING_FUNCTION); - - Coroutine::set(['enable_deadlock_check' => false]); - - return $baseServer->start(); - } - - - /** - * @param $host - * @param $Port - * @return Packet|Websocket|Receive|Http|null - * @throws Exception - */ - public function error_stop($host, $Port): Packet|Websocket|Receive|Http|null - { - $this->error(sprintf('Port %s::%d is already.', $host, $Port)); - if ($this->baseServer) { - $this->baseServer->shutdown(); - } else { - $this->shutdown(); - } - return $this->baseServer; - } - - - /** - * @return bool - * @throws ConfigException - * @throws Exception - */ - public function isRunner(): bool - { - $port = $this->sortServers(Config::get('servers')); - if (empty($port)) { - return false; - } - foreach ($port as $value) { - if (Snowflake::getPlatform()->isLinux()) { - exec('netstat -tunlp | grep ' . $value['port'], $output); - } else { - exec('lsof -i :' . $value['port'] . ' | grep -i "LISTEN"', $output); - } - if (!empty($output)) { - unset($output); - return true; - } - } - return false; - } - - - /** - * @return void - * - * start server - * @throws Exception - */ - public function shutdown() - { - $this->stop($this); - } - - - /** - * @throws ConfigException - * @throws Exception - */ - public function onProcessListener(): void - { - if (!($this->baseServer instanceof \Swoole\Server)) { - return; - } - - $processes = Config::get('processes'); - if (!empty($processes) && is_array($processes)) { - $this->deliveryProcess(merge($processes, $this->process)); - } else { - $this->deliveryProcess($this->process); - } - } - - - /** - * @param $processes - * @throws Exception - */ - private function deliveryProcess($processes) - { - $application = Snowflake::app(); - if (empty($processes) || !is_array($processes)) { - return; - } - foreach ($processes as $name => $process) { - $this->debug(sprintf('Process %s', $process)); - if (!is_string($process)) { - continue; - } - $system = new $process(Snowflake::app(), $name, true); - if (isset($this->params[$name])) { - $system->write(Json::encode($this->params[$name])); - } - $this->baseServer->addProcess($system); - $application->set($process, $system); - } - } - - - /** - * @param $daemon - * @return Server - */ - public function setDaemon($daemon): static - { - if (!in_array($daemon, [0, 1])) { - return $this; - } - $this->daemon = $daemon; - return $this; - } - - - /** - * @return Packet|Websocket|Receive|Http|null - */ - public function getServer(): Packet|Websocket|Receive|Http|null - { - return $this->baseServer; - } - - - /** - * @param $config - * @return mixed - * @throws Exception - */ - private function create($config): mixed - { - $settings = Config::get('settings', false, []); - if (!isset($this->server[$config['type']])) { - throw new Exception('Unknown server type(' . $config['type'] . ').'); - } - $server = $this->dispatchCreate($config, $settings); - if (isset($config['events'])) { - $this->createEventListen($config); - } - return $server; - } - - - /** - * @param $config - * @throws Exception - */ - protected function createEventListen($config) - { - if (!is_array($config['events'])) { - return; - } - $event = Snowflake::app()->getEvent(); - foreach ($config['events'] as $name => $_event) { - $event->on('listen ' . $config['port'] . ' ' . $name, $_event); - } - } - - /** - * @param $config - * @param $settings - * @return \Swoole\Server|Packet|Receive|Http|Websocket|null - * @throws NotFindClassException - * @throws ReflectionException - * @throws Exception - */ - private function dispatchCreate($config, $settings): \Swoole\Server|Packet|Receive|Http|Websocket|null - { - if ($this->isUse($config['port'])) { - return $this->error_stop($config['host'], $config['port']); - } - if (!($this->baseServer instanceof \Swoole\Server)) { - return $this->parseServer($config, $settings); - } - return $this->addListener($config); - } - - - /** - * @param $config - * @return Http|Packet|Receive|Websocket|null - * @throws NotFindClassException - * @throws ReflectionException - * @throws Exception - */ - private function addListener($config): Packet|Websocket|Receive|Http|null - { - $newListener = $this->baseServer->addlistener($config['host'], $config['port'], $config['mode']); - if (!$newListener) { - exit($this->addError(sprintf('Listen %s::%d fail.', $config['host'], $config['port']))); - } - - if (isset($config['settings']) && is_array($config['settings'])) { - $newListener->set($config['settings']); - } - $this->onListenerBind($config, $this->baseServer); - - return $this->baseServer; - } - - - /** - * @param $config - * @param $settings - * @return Packet|Websocket|Receive|Http|null - * @throws Exception - */ - private function parseServer($config, $settings): Packet|Websocket|Receive|Http|null - { - $class = $this->dispatch($config['type']); - if (isset($config['settings']) && !empty($config['settings'])) { - $settings = array_merge($settings, $config['settings']); - } - $this->baseServer = new $class($config['host'], $config['port'], SWOOLE_PROCESS, $config['mode']); - $settings['daemonize'] = $this->daemon; - if (!isset($settings['pid_file'])) { - $settings['pid_file'] = PID_PATH; - } - $this->debug(sprintf('Check listen %s::%d -> ok', $config['host'], $config['port'])); - - $this->onLoadWebsocketHandler(); - if ($this->baseServer instanceof Http) { - $this->onLoadHttpHandler(); - } - - $this->baseServer->set($settings); - - $this->onProcessListener(); - - return $this->baseServer; - } - - - /** - * @param $config - * @param $newListener - * @return Packet|Websocket|Receive|Http|null - * @throws NotFindClassException - * @throws ReflectionException - * @throws ComponentException - * @throws Exception - */ - private function onListenerBind($config, $newListener): Packet|Websocket|Receive|Http|null - { - if (!in_array($config['type'], [self::HTTP, self::TCP, self::PACKAGE])) { - throw new Exception('Unknown server type(' . $config['type'] . ').'); - } - if (in_array($config['type'], $this->listenTypes)) { - return $this->baseServer; - } - if ($config['type'] == self::HTTP) { - $this->onBind($newListener, 'request', [Snowflake::createObject(OnRequest::class), 'onHandler']); - } else { - $this->noHttp($newListener, $config); - } - $this->debug(sprintf('Check listen %s::%d -> ok', $config['host'], $config['port'])); - $this->listenTypes[] = $config['type']; - return $this->baseServer; - } - - - /** - * @param $newListener - * @param $config - * @throws NotFindClassException - * @throws ReflectionException - * @throws Exception - */ - private function noHttp($newListener, $config) - { - $this->onBind($newListener, 'connect', [Snowflake::createObject(OnConnect::class), 'onHandler']); - $this->onBind($newListener, 'close', [Snowflake::createObject(OnClose::class), 'onHandler']); - if ($config['type'] == self::TCP) { - $this->onBind($newListener, 'receive', [$class = new OnReceive(), 'onHandler']); - } else { - $this->onBind($newListener, 'packet', [$class = new OnPacket(), 'onHandler']); - } - $class->host = $config['host']; - $class->port = $config['port']; - } - - - /** - * @param $server - * @param $name - * @param $callback - * @throws Exception - */ - private function onBind($server, $name, $callback) - { - if (in_array($name, $this->listening)) { - return; - } - array_push($this->listening, $name); - if ($name === 'request') { - $this->onLoadHttpHandler(); - } - $server->on($name, $callback); - } - - - /** - * Load router handler - * @throws Exception - */ - public function onLoadHttpHandler() - { - $event = Snowflake::app()->getEvent(); - $event->on(Event::SERVER_WORKER_START, function () { - router()->loadRouterSetting(); - - $annotation = Snowflake::app()->getAttributes(); - $annotation->instanceDirectoryFiles(CONTROLLER_PATH); - }); - } - - - /** - * @throws Exception - */ - public function onLoadWebsocketHandler() - { - $event = Snowflake::app()->getEvent(); - $event->on(Event::SERVER_WORKER_START, function () { - $annotation = Snowflake::app()->getAttributes(); - $annotation->instanceDirectoryFiles(SOCKET_PATH); - }); - } - - - /** - * @param $type - * @return string - */ - private function dispatch($type): string - { - return match ($type) { - self::HTTP => Http::class, - self::WEBSOCKET => Websocket::class, - self::PACKAGE => Packet::class, - default => Receive::class - }; - } - - /** - * @param $servers - * @return array - */ - private function sortServers($servers): array - { - $array = []; - foreach ($servers as $server) { - switch ($server['type']) { - case self::WEBSOCKET: - array_unshift($array, $server); - break; - case self::HTTP: - case self::PACKAGE | self::TCP: - $array[] = $server; - break; - default: - $array[] = $server; - } - } - return $array; - } + use Action; + + const HTTP = 'HTTP'; + const TCP = 'TCP'; + const PACKAGE = 'PACKAGE'; + const WEBSOCKET = 'WEBSOCKET'; + + private array $listening = []; + private array $server = [ + 'HTTP' => [SWOOLE_TCP, Http::class], + 'TCP' => [SWOOLE_TCP, Receive::class], + 'PACKAGE' => [SWOOLE_UDP, Packet::class], + 'WEBSOCKET' => [SWOOLE_SOCK_TCP, Websocket::class], + ]; + + private Packet|Websocket|Receive|null|Http $baseServer = null; + + public int $daemon = 0; + + + private array $listenTypes = []; + + + private array $process = [ + 'biomonitoring' => Biomonitoring::class, + 'logger_process' => LoggerProcess::class + ]; + + private array $params = []; + + + /** + * @param $name + * @param $process + * @param array $params + */ + public function addProcess($name, $process, $params = []) + { + $this->process[$name] = $process; + $this->params[$name] = $params; + } + + + /** + * @return array + */ + public function getProcesses(): array + { + return $this->process ?? []; + } + + + /** + * @param array $configs + * @return Packet|Websocket|Receive|Http|null + * @throws Exception + */ + public function initCore(array $configs): Packet|Websocket|Receive|Http|null + { + $this->orders($configs); + return $this->getServer(); + } + + + /** + * @param $configs + * @return Packet|Websocket|Receive|Http|null + * @throws Exception + */ + private function orders($configs): Packet|Websocket|Receive|Http|null + { + $servers = $this->sortServers($configs); + foreach ($servers as $server) { + $this->create($server); + if (!$this->baseServer) { + return null; + } + } + return $this->baseServer; + } + + + /** + * @return string start server + * + * start server + * @throws ConfigException + * @throws Exception + */ + public function start(): string + { + $configs = Config::get('servers', true); + + $baseServer = $this->initCore($configs); + if (!$baseServer) { + return 'ok'; + } + + Runtime::enableCoroutine(true, SWOOLE_HOOK_ALL ^ SWOOLE_HOOK_BLOCKING_FUNCTION); + + Coroutine::set(['enable_deadlock_check' => false]); + + return $baseServer->start(); + } + + + /** + * @param $host + * @param $Port + * @return Packet|Websocket|Receive|Http|null + * @throws Exception + */ + public function error_stop($host, $Port): Packet|Websocket|Receive|Http|null + { + $this->error(sprintf('Port %s::%d is already.', $host, $Port)); + if ($this->baseServer) { + $this->baseServer->shutdown(); + } else { + $this->shutdown(); + } + return $this->baseServer; + } + + + /** + * @return bool + * @throws ConfigException + * @throws Exception + */ + public function isRunner(): bool + { + $port = $this->sortServers(Config::get('servers')); + if (empty($port)) { + return false; + } + foreach ($port as $value) { + if (Snowflake::getPlatform()->isLinux()) { + exec('netstat -tunlp | grep ' . $value['port'], $output); + } else { + exec('lsof -i :' . $value['port'] . ' | grep -i "LISTEN"', $output); + } + if (!empty($output)) { + unset($output); + return true; + } + } + return false; + } + + + /** + * @return void + * + * start server + * @throws Exception + */ + public function shutdown() + { + $this->stop($this); + } + + + /** + * @throws ConfigException + * @throws Exception + */ + public function onProcessListener(): void + { + if (!($this->baseServer instanceof \Swoole\Server)) { + return; + } + + $processes = Config::get('processes'); + if (!empty($processes) && is_array($processes)) { + $this->deliveryProcess(merge($processes, $this->process)); + } else { + $this->deliveryProcess($this->process); + } + } + + + /** + * @param $processes + * @throws Exception + */ + private function deliveryProcess($processes) + { + $application = Snowflake::app(); + if (empty($processes) || !is_array($processes)) { + return; + } + foreach ($processes as $name => $process) { + $this->debug(sprintf('Process %s', $process)); + if (!is_string($process)) { + continue; + } + $system = new $process(Snowflake::app(), $name, true); + if (isset($this->params[$name])) { + $system->write(Json::encode($this->params[$name])); + } + $this->baseServer->addProcess($system); + $application->set($process, $system); + } + } + + + /** + * @param $daemon + * @return Server + */ + public function setDaemon($daemon): static + { + if (!in_array($daemon, [0, 1])) { + return $this; + } + $this->daemon = $daemon; + return $this; + } + + + /** + * @return Packet|Websocket|Receive|Http|null + */ + public function getServer(): Packet|Websocket|Receive|Http|null + { + return $this->baseServer; + } + + + /** + * @param $config + * @return mixed + * @throws Exception + */ + private function create($config): mixed + { + $settings = Config::get('settings', false, []); + if (!isset($this->server[$config['type']])) { + throw new Exception('Unknown server type(' . $config['type'] . ').'); + } + $server = $this->dispatchCreate($config, $settings); + if (isset($config['events'])) { + $this->createEventListen($config); + } + return $server; + } + + + /** + * @param $config + * @throws Exception + */ + protected function createEventListen($config) + { + if (!is_array($config['events'])) { + return; + } + $event = Snowflake::app()->getEvent(); + foreach ($config['events'] as $name => $_event) { + $event->on('listen ' . $config['port'] . ' ' . $name, $_event); + } + } + + /** + * @param $config + * @param $settings + * @return \Swoole\Server|Packet|Receive|Http|Websocket|null + * @throws NotFindClassException + * @throws ReflectionException + * @throws Exception + */ + private function dispatchCreate($config, $settings): \Swoole\Server|Packet|Receive|Http|Websocket|null + { + if ($this->isUse($config['port'])) { + return $this->error_stop($config['host'], $config['port']); + } + if (!($this->baseServer instanceof \Swoole\Server)) { + return $this->parseServer($config, $settings); + } + return $this->addListener($config); + } + + + /** + * @param $config + * @return Http|Packet|Receive|Websocket|null + * @throws NotFindClassException + * @throws ReflectionException + * @throws Exception + */ + private function addListener($config): Packet|Websocket|Receive|Http|null + { + $newListener = $this->baseServer->addlistener($config['host'], $config['port'], $config['mode']); + if (!$newListener) { + exit($this->addError(sprintf('Listen %s::%d fail.', $config['host'], $config['port']))); + } + + if (isset($config['settings']) && is_array($config['settings'])) { + $newListener->set($config['settings']); + } + $this->onListenerBind($config, $this->baseServer); + + return $this->startRpcService(); + } + + + /** + * @return Http|Packet|Receive|Websocket|null + * @throws ComponentException + * @throws ConfigException + * @throws NotFindClassException + * @throws ReflectionException + */ + private function startRpcService() + { + $rpcService = Config::get('rpc.service', false, []); + if (is_array($rpcService) && !empty($rpcService)) { + /** @var Service $service */ + $service = Snowflake::app()->get('rpc-service'); + $service->instance($this->baseServer); + } + return $this->baseServer; + } + + + /** + * @param $config + * @param $settings + * @return Packet|Websocket|Receive|Http|null + * @throws Exception + */ + private function parseServer($config, $settings): Packet|Websocket|Receive|Http|null + { + $class = $this->dispatch($config['type']); + if (isset($config['settings']) && !empty($config['settings'])) { + $settings = array_merge($settings, $config['settings']); + } + $this->baseServer = new $class($config['host'], $config['port'], SWOOLE_PROCESS, $config['mode']); + $settings['daemonize'] = $this->daemon; + if (!isset($settings['pid_file'])) { + $settings['pid_file'] = PID_PATH; + } + $this->debug(sprintf('Check listen %s::%d -> ok', $config['host'], $config['port'])); + + $this->onLoadWebsocketHandler(); + if ($this->baseServer instanceof Http) { + $this->onLoadHttpHandler(); + } + + $this->baseServer->set($settings); + + $this->onProcessListener(); + + return $this->baseServer; + } + + + /** + * @param $config + * @param $newListener + * @return Packet|Websocket|Receive|Http|null + * @throws NotFindClassException + * @throws ReflectionException + * @throws ComponentException + * @throws Exception + */ + private function onListenerBind($config, $newListener): Packet|Websocket|Receive|Http|null + { + if (!in_array($config['type'], [self::HTTP, self::TCP, self::PACKAGE])) { + throw new Exception('Unknown server type(' . $config['type'] . ').'); + } + if (in_array($config['type'], $this->listenTypes)) { + return $this->baseServer; + } + if ($config['type'] == self::HTTP) { + $this->onBind($newListener, 'request', [Snowflake::createObject(OnRequest::class), 'onHandler']); + } else { + $this->noHttp($newListener, $config); + } + $this->debug(sprintf('Check listen %s::%d -> ok', $config['host'], $config['port'])); + $this->listenTypes[] = $config['type']; + return $this->baseServer; + } + + + /** + * @param $newListener + * @param $config + * @throws NotFindClassException + * @throws ReflectionException + * @throws Exception + */ + private function noHttp($newListener, $config) + { + $this->onBind($newListener, 'connect', [Snowflake::createObject(OnConnect::class), 'onHandler']); + $this->onBind($newListener, 'close', [Snowflake::createObject(OnClose::class), 'onHandler']); + if ($config['type'] == self::TCP) { + $this->onBind($newListener, 'receive', [$class = new OnReceive(), 'onHandler']); + } else { + $this->onBind($newListener, 'packet', [$class = new OnPacket(), 'onHandler']); + } + $class->host = $config['host']; + $class->port = $config['port']; + } + + + /** + * @param $server + * @param $name + * @param $callback + * @throws Exception + */ + private function onBind($server, $name, $callback) + { + if (in_array($name, $this->listening)) { + return; + } + array_push($this->listening, $name); + if ($name === 'request') { + $this->onLoadHttpHandler(); + } + $server->on($name, $callback); + } + + + /** + * Load router handler + * @throws Exception + */ + public function onLoadHttpHandler() + { + $event = Snowflake::app()->getEvent(); + $event->on(Event::SERVER_WORKER_START, function () { + router()->loadRouterSetting(); + + $annotation = Snowflake::app()->getAttributes(); + $annotation->instanceDirectoryFiles(CONTROLLER_PATH); + }); + } + + + /** + * @throws Exception + */ + public function onLoadWebsocketHandler() + { + $event = Snowflake::app()->getEvent(); + $event->on(Event::SERVER_WORKER_START, function () { + $annotation = Snowflake::app()->getAttributes(); + $annotation->instanceDirectoryFiles(SOCKET_PATH); + }); + } + + + /** + * @param $type + * @return string + */ + private function dispatch($type): string + { + return match ($type) { + self::HTTP => Http::class, + self::WEBSOCKET => Websocket::class, + self::PACKAGE => Packet::class, + default => Receive::class + }; + } + + /** + * @param $servers + * @return array + */ + private function sortServers($servers): array + { + $array = []; + foreach ($servers as $server) { + switch ($server['type']) { + case self::WEBSOCKET: + array_unshift($array, $server); + break; + case self::HTTP: + case self::PACKAGE | self::TCP: + $array[] = $server; + break; + default: + $array[] = $server; + } + } + return $array; + } } diff --git a/Rpc/Service.php b/Rpc/Service.php index 214d8e5e..7c20c115 100644 --- a/Rpc/Service.php +++ b/Rpc/Service.php @@ -3,9 +3,12 @@ namespace Rpc; +use HttpServer\Service\Http; +use HttpServer\Service\Packet; +use HttpServer\Service\Receive; +use HttpServer\Service\Websocket; use Snowflake\Abstracts\Component; use Snowflake\Abstracts\Config; -use Snowflake\Snowflake; /** @@ -19,14 +22,12 @@ class Service extends Component /** * @throws \Snowflake\Exception\ConfigException */ - public function instance(): void + public function instance(Packet|Websocket|Receive|null|Http $server): void { $services = Config::get('rpc.service', false, []); if (empty($services)) { return; } - - $server = Snowflake::app()->getSwoole(); foreach ($services as $service) { $mode = $service['mode'] ?? SWOOLE_SOCK_TCP6; $rpcServer = $server->addlistener($service['host'], $service['port'], $mode); diff --git a/System/Abstracts/BaseApplication.php b/System/Abstracts/BaseApplication.php index ff673492..ae2ee314 100644 --- a/System/Abstracts/BaseApplication.php +++ b/System/Abstracts/BaseApplication.php @@ -50,404 +50,406 @@ use Swoole\Table; abstract class BaseApplication extends Service { - use TraitApplication; + use TraitApplication; - /** - * @var string - */ - public string $storage = APP_PATH . '/storage'; + /** + * @var string + */ + public string $storage = APP_PATH . '/storage'; - public string $envPath = APP_PATH . '/.env'; + public string $envPath = APP_PATH . '/.env'; - /** - * Init constructor. - * - * @param array $config - * - * @throws - */ - public function __construct(array $config = []) - { - Snowflake::init($this); + /** + * Init constructor. + * + * @param array $config + * + * @throws + */ + public function __construct(array $config = []) + { + Snowflake::init($this); - $this->moreComponents(); - $this->parseInt($config); - $this->parseEvents($config); - $this->initErrorHandler(); - $this->enableEnvConfig(); + $this->moreComponents(); + $this->parseInt($config); + $this->parseEvents($config); + $this->initErrorHandler(); + $this->enableEnvConfig(); - parent::__construct($config); - } + parent::__construct($config); + } - /** - * @return array - */ - public function enableEnvConfig(): array - { - if (!file_exists($this->envPath)) { - return []; - } - $lines = $this->readLinesFromFile($this->envPath); - foreach ($lines as $line) { - if (!$this->isComment($line) && $this->looksLikeSetter($line)) { - [$key, $value] = explode('=', $line); - putenv(trim($key) . '=' . trim($value)); - } - } - return $lines; - } + /** + * @return array + */ + public function enableEnvConfig(): array + { + if (!file_exists($this->envPath)) { + return []; + } + $lines = $this->readLinesFromFile($this->envPath); + foreach ($lines as $line) { + if (!$this->isComment($line) && $this->looksLikeSetter($line)) { + [$key, $value] = explode('=', $line); + putenv(trim($key) . '=' . trim($value)); + } + } + return $lines; + } - /** - * Read lines from the file, auto detecting line endings. - * - * @param string $filePath - * - * @return array - */ - protected function readLinesFromFile(string $filePath): array - { - // Read file into an array of lines with auto-detected line endings - $autodetect = ini_get('auto_detect_line_endings'); - ini_set('auto_detect_line_endings', '1'); - $lines = file($filePath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); - ini_set('auto_detect_line_endings', $autodetect); + /** + * Read lines from the file, auto detecting line endings. + * + * @param string $filePath + * + * @return array + */ + protected function readLinesFromFile(string $filePath): array + { + // Read file into an array of lines with auto-detected line endings + $autodetect = ini_get('auto_detect_line_endings'); + ini_set('auto_detect_line_endings', '1'); + $lines = file($filePath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); + ini_set('auto_detect_line_endings', $autodetect); - return $lines; - } + return $lines; + } - /** - * Determine if the line in the file is a comment, e.g. begins with a #. - * - * @param string $line - * - * @return bool - */ - protected function isComment(string $line): bool - { - $line = ltrim($line); + /** + * Determine if the line in the file is a comment, e.g. begins with a #. + * + * @param string $line + * + * @return bool + */ + protected function isComment(string $line): bool + { + $line = ltrim($line); - return isset($line[0]) && $line[0] === '#'; - } + return isset($line[0]) && $line[0] === '#'; + } - /** - * Determine if the given line looks like it's setting a variable. - * - * @param string $line - * - * @return bool - */ - #[Pure] protected function looksLikeSetter(string $line): bool - { - return str_contains($line, '='); - } + /** + * Determine if the given line looks like it's setting a variable. + * + * @param string $line + * + * @return bool + */ + #[Pure] protected function looksLikeSetter(string $line): bool + { + return str_contains($line, '='); + } - /** - * @param $config - * - * @throws - */ - public function parseInt($config) - { - foreach ($config as $key => $value) { - Config::set($key, $value); - } - if ($storage = Config::get('storage', false, 'storage')) { - if (!str_contains($storage, APP_PATH)) { - $storage = APP_PATH . $storage . '/'; - } - if (!is_dir($storage)) { - mkdir($storage); - } - if (!is_dir($storage) || !is_writeable($storage)) { - throw new InitException("Directory {$storage} does not have write permission"); - } - } - } + /** + * @param $config + * + * @throws + */ + public function parseInt($config) + { + foreach ($config as $key => $value) { + Config::set($key, $value); + } + if ($storage = Config::get('storage', false, 'storage')) { + if (!str_contains($storage, APP_PATH)) { + $storage = APP_PATH . $storage . '/'; + } + if (!is_dir($storage)) { + mkdir($storage); + } + if (!is_dir($storage) || !is_writeable($storage)) { + throw new InitException("Directory {$storage} does not have write permission"); + } + } + } - /** - * @param $config - * - * @throws - */ - public function parseEvents($config) - { - if (!isset($config['events']) || !is_array($config['events'])) { - return; - } - $event = Snowflake::app()->getEvent(); - foreach ($config['events'] as $key => $value) { - if (is_string($value)) { - if (!class_exists($value)) { - throw new InitException("Class {$value} does not exists."); - } - $value = Snowflake::createObject($value); - } else if (is_array($value) && !is_callable($value, true)) { - throw new InitException("Class does not hav callback."); - } - $event->on($key, $value); - } - } + /** + * @param $config + * + * @throws + */ + public function parseEvents($config) + { + if (!isset($config['events']) || !is_array($config['events'])) { + return; + } + $event = Snowflake::app()->getEvent(); + foreach ($config['events'] as $key => $value) { + if (is_string($value)) { + if (!class_exists($value)) { + throw new InitException("Class {$value} does not exists."); + } + $value = Snowflake::createObject($value); + } else if (is_array($value) && !is_callable($value, true)) { + throw new InitException("Class does not hav callback."); + } + $event->on($key, $value); + } + } - /** - * @param $name - * @return mixed - * @throws Exception - */ - public function clone($name): mixed - { - return clone $this->get($name); - } + /** + * @param $name + * @return mixed + * @throws Exception + */ + public function clone($name): mixed + { + return clone $this->get($name); + } - /** - * - * @throws Exception - */ - public function initErrorHandler() - { - $this->get('error')->register(); - } + /** + * + * @throws Exception + */ + public function initErrorHandler() + { + $this->get('error')->register(); + } - /** - * @return mixed - */ - public function getLocalIps(): mixed - { - return swoole_get_local_ip(); - } + /** + * @return mixed + */ + public function getLocalIps(): mixed + { + return swoole_get_local_ip(); + } - /** - * @return mixed - */ - public function getFirstLocal(): mixed - { - return current($this->getLocalIps()); - } + /** + * @return mixed + */ + public function getFirstLocal(): mixed + { + return current($this->getLocalIps()); + } - /** - * @return Logger - * @throws Exception - */ - public function getLogger(): Logger - { - return $this->get('logger'); - } + /** + * @return Logger + * @throws Exception + */ + public function getLogger(): Logger + { + return $this->get('logger'); + } - /** - * @return Producer - * @throws Exception - */ - public function getKafka(): Producer - { - return $this->get('kafka'); - } + /** + * @return Producer + * @throws Exception + */ + public function getKafka(): Producer + { + return $this->get('kafka'); + } - /** - * @return \Redis|Redis - * @throws Exception - */ - public function getRedis(): Redis|\Redis - { - return $this->get('redis'); - } + /** + * @return \Redis|Redis + * @throws Exception + */ + public function getRedis(): Redis|\Redis + { + return $this->get('redis'); + } - /** - * @param $ip - * @return bool - */ - public function isLocal($ip): bool - { - return $this->getFirstLocal() == $ip; - } + /** + * @param $ip + * @return bool + */ + public function isLocal($ip): bool + { + return $this->getFirstLocal() == $ip; + } - /** - * @return ErrorHandler - * @throws Exception - */ - public function getError(): ErrorHandler - { - return $this->get('error'); - } + /** + * @return ErrorHandler + * @throws Exception + */ + public function getError(): ErrorHandler + { + return $this->get('error'); + } - /** - * @return Connection - * @throws ComponentException - * @throws NotFindClassException - * @throws ReflectionException - */ - public function getMysqlFromPool(): Connection - { - return $this->get('pool')->getDb(); - } + /** + * @return Connection + * @throws ComponentException + * @throws NotFindClassException + * @throws ReflectionException + */ + public function getMysqlFromPool(): Connection + { + return $this->get('pool')->getDb(); + } - /** - * @return SRedis - * @throws NotFindClassException - * @throws ReflectionException - * @throws ComponentException - */ - public function getRedisFromPool(): SRedis - { - return $this->get('pool')->getRedis(); - } + /** + * @return SRedis + * @throws NotFindClassException + * @throws ReflectionException + * @throws ComponentException + */ + public function getRedisFromPool(): SRedis + { + return $this->get('pool')->getRedis(); + } - /** - * @return Response - * @throws Exception - */ - public function getResponse(): Response - { - return $this->get('response'); - } + /** + * @return Response + * @throws Exception + */ + public function getResponse(): Response + { + return $this->get('response'); + } - /** - * @return Request - * @throws Exception - */ - public function getRequest(): Request - { - return $this->get('request'); - } + /** + * @return Request + * @throws Exception + */ + public function getRequest(): Request + { + return $this->get('request'); + } - /** - * @param $name - * @return Table - * @throws Exception - */ - public function getTable($name): Table - { - return $this->get($name); - } + /** + * @param $name + * @return Table + * @throws Exception + */ + public function getTable($name): Table + { + return $this->get($name); + } - /** - * @return Config - * @throws Exception - */ - public function getConfig(): Config - { - return $this->get('config'); - } + /** + * @return Config + * @throws Exception + */ + public function getConfig(): Config + { + return $this->get('config'); + } - /** - * @return Router - * @throws Exception - */ - public function getRouter(): Router - { - return $this->get('router'); - } + /** + * @return Router + * @throws Exception + */ + public function getRouter(): Router + { + return $this->get('router'); + } - /** - * @return Event - * @throws Exception - */ - public function getEvent(): Event - { - return $this->get('event'); - } + /** + * @return Event + * @throws Exception + */ + public function getEvent(): Event + { + return $this->get('event'); + } - /** - * @return Jwt - * @throws Exception - */ - public function getJwt(): Jwt - { - return $this->get('jwt'); - } + /** + * @return Jwt + * @throws Exception + */ + public function getJwt(): Jwt + { + return $this->get('jwt'); + } - /** - * @return Server - * @throws Exception - */ - public function getServer(): Server - { - return $this->get('server'); - } + /** + * @return Server + * @throws Exception + */ + public function getServer(): Server + { + return $this->get('server'); + } - /** - * @return Http|Packet|Receive|Websocket|null - * @throws Exception - */ - public function getSwoole(): Packet|Websocket|Receive|Http|null - { - return $this->getServer()->getServer(); - } + /** + * @return Http|Packet|Receive|Websocket|null + * @throws Exception + */ + public function getSwoole(): Packet|Websocket|Receive|Http|null + { + return $this->getServer()->getServer(); + } - /** - * @return SAnnotation - * @throws Exception - */ - public function getAttributes(): SAnnotation - { - return $this->get('attributes'); - } + /** + * @return SAnnotation + * @throws Exception + */ + public function getAttributes(): SAnnotation + { + return $this->get('attributes'); + } - /** - * @return Async - * @throws Exception - */ - public function getAsync(): Async - { - return $this->get('async'); - } + /** + * @return Async + * @throws Exception + */ + public function getAsync(): Async + { + return $this->get('async'); + } - /** - * @return ObjectPool - * @throws Exception - */ - public function getObject(): ObjectPool - { - return $this->get('object'); - } + /** + * @return ObjectPool + * @throws Exception + */ + public function getObject(): ObjectPool + { + return $this->get('object'); + } - /** - * @throws Exception - */ - protected function moreComponents(): void - { - $this->setComponents([ - 'error' => ['class' => ErrorHandler::class], - 'event' => ['class' => Event::class], - 'connections' => ['class' => Connection::class], - 'redis_connections' => ['class' => SRedis::class], - 'pool' => ['class' => SPool::class], - 'response' => ['class' => Response::class], - 'request' => ['class' => Request::class], - 'config' => ['class' => Config::class], - 'logger' => ['class' => Logger::class], - 'attributes' => ['class' => SAnnotation::class], - 'router' => ['class' => Router::class], - 'redis' => ['class' => Redis::class], - 'jwt' => ['class' => Jwt::class], - 'async' => ['class' => Async::class], - 'filter' => ['class' => HttpFilter::class], - 'crontab' => ['class' => Crontab::class], - 'object' => ['class' => ObjectPool::class], - 'goto' => ['class' => BaseGoto::class], - 'http2' => ['class' => Http2::class], - ]); - } + /** + * @throws Exception + */ + protected function moreComponents(): void + { + $this->setComponents([ + 'error' => ['class' => ErrorHandler::class], + 'event' => ['class' => Event::class], + 'connections' => ['class' => Connection::class], + 'redis_connections' => ['class' => SRedis::class], + 'pool' => ['class' => SPool::class], + 'response' => ['class' => Response::class], + 'request' => ['class' => Request::class], + 'config' => ['class' => Config::class], + 'logger' => ['class' => Logger::class], + 'attributes' => ['class' => SAnnotation::class], + 'router' => ['class' => Router::class], + 'redis' => ['class' => Redis::class], + 'jwt' => ['class' => Jwt::class], + 'async' => ['class' => Async::class], + 'filter' => ['class' => HttpFilter::class], + 'crontab' => ['class' => Crontab::class], + 'object' => ['class' => ObjectPool::class], + 'goto' => ['class' => BaseGoto::class], + 'rpc' => ['class' => \Rpc\Producer::class], + 'rpc-service' => ['class' => \Rpc\Service::class], + 'http2' => ['class' => Http2::class], + ]); + } }