From 917e82064abd5de782ec711111d7e15adeac76a7 Mon Sep 17 00:00:00 2001 From: xl Date: Thu, 29 Aug 2024 17:01:07 +0800 Subject: [PATCH] eee --- Abstracts/AsyncServer.php | 15 ++++----------- Abstracts/BaseProcess.php | 16 +--------------- Abstracts/TraitServer.php | 11 ++--------- Handler/OnRequest.php | 7 +++---- Handler/OnServerWorker.php | 17 +++++------------ HotReload.php | 14 +++++++------- Task/Task.php | 8 +++----- Task/TaskExecute.php | 21 +++++++++------------ 8 files changed, 34 insertions(+), 75 deletions(-) diff --git a/Abstracts/AsyncServer.php b/Abstracts/AsyncServer.php index 71abe38..9557f29 100644 --- a/Abstracts/AsyncServer.php +++ b/Abstracts/AsyncServer.php @@ -35,13 +35,6 @@ class AsyncServer implements ServerInterface private ?Server $server = null; - /** - * @var ContainerInterface - */ - #[Container(ContainerInterface::class)] - public ContainerInterface $container; - - /** * @param array $service * @param int $daemon @@ -110,7 +103,7 @@ class AsyncServer implements ServerInterface $config->events[Constant::SHUTDOWN] = [OnServer::class, 'onShutdown']; } $this->_listenDump($config); - $this->container->bind(ServerInterface::class, $this->server); + \Kiri::getDi()->bind(ServerInterface::class, $this->server); } @@ -123,7 +116,7 @@ class AsyncServer implements ServerInterface if (!isset($this->server->setting[Constant::OPTION_TASK_WORKER_NUM])) { return; } - $this->container->get(Task::class)->initTaskWorker($this->server); + \Kiri::getDi()->get(Task::class)->initTaskWorker($this->server); } @@ -170,7 +163,7 @@ class AsyncServer implements ServerInterface */ protected function _listenDump(SConfig $config): void { - $writeln = $this->container->get(OutputInterface::class); + $writeln = \Kiri::getDi()->get(OutputInterface::class); if ($config->type == Constant::SERVER_TYPE_HTTP) { $writeln->writeln('Add http port listen ' . $config->host . '::' . $config->port); } else if ($config->type == Constant::SERVER_TYPE_WEBSOCKET) { @@ -214,7 +207,7 @@ class AsyncServer implements ServerInterface { foreach ($events as $name => $event) { if (is_array($event) && is_string($event[0])) { - $event[0] = $this->container->get($event[0]); + $event[0] = \Kiri::getDi()->get($event[0]); } $base->on($name, $event); } diff --git a/Abstracts/BaseProcess.php b/Abstracts/BaseProcess.php index 99f0525..8c18ed1 100644 --- a/Abstracts/BaseProcess.php +++ b/Abstracts/BaseProcess.php @@ -44,20 +44,6 @@ abstract class BaseProcess implements OnProcessInterface protected bool $enable_queue = false; - /** - * @var StdoutLogger - */ - #[Container(LoggerInterface::class)] - public StdoutLogger $logger; - - - /** - * @var \Kiri\Di\Container - */ - #[Container(ContainerInterface::class)] - public \Kiri\Di\Container $container; - - /** * @var string */ @@ -141,7 +127,7 @@ abstract class BaseProcess implements OnProcessInterface { $this->stop = true; $value = Context::get('waite:process:message'); - $this->logger->alert('Process ' . $this->getName() . ' stop'); + \Kiri::getLogger()->alert('Process ' . $this->getName() . ' stop'); if (!is_null($value) && Coroutine::exists((int)$value)) { Coroutine::cancel((int)$value); } diff --git a/Abstracts/TraitServer.php b/Abstracts/TraitServer.php index 6fec4de..884bd0a 100644 --- a/Abstracts/TraitServer.php +++ b/Abstracts/TraitServer.php @@ -24,13 +24,6 @@ trait TraitServer private array $_process = []; - /** - * @var StdoutLogger - */ - #[Container(LoggerInterface::class)] - public StdoutLogger $logger; - - /** * @param string|array|BaseProcess $class * @return void @@ -64,7 +57,7 @@ trait TraitServer private function genProcess(BaseProcess $name): Process { return new Process(function (Process $process) use ($name) { - $process->name('[' . \config('id','system-service') . ']' . $name->getName()); + $process->name('[' . \config('id','system-service') . '].' . $name->getName()); $name->onSigterm()->process($process); }, $name->getRedirectStdinAndStdout(), @@ -111,7 +104,7 @@ trait TraitServer public function onSigint($no, array $signInfo): void { try { - $this->logger->alert('Pid ' . getmypid() . ' get signo ' . $no); + Kiri::getLogger()->alert('Pid ' . getmypid() . ' get signo ' . $no); $this->shutdown(); } catch (\Throwable $exception) { error($exception); diff --git a/Handler/OnRequest.php b/Handler/OnRequest.php index 28649ae..7c24b4c 100644 --- a/Handler/OnRequest.php +++ b/Handler/OnRequest.php @@ -63,16 +63,15 @@ class OnRequest implements OnRequestInterface * @param DataGrip $dataGrip * @throws */ - public function __construct(public ResponseInterface $response, public ContainerInterface $container, - public DataGrip $dataGrip) + public function __construct(public ResponseInterface $response, DataGrip $dataGrip) { $this->responseEmitter = $this->response->emmit; $exception = \config('exception.http'); if (!in_array(ExceptionHandlerInterface::class, class_implements($exception))) { $exception = ExceptionHandlerDispatcher::class; } - $this->exception = $this->container->get($exception); - $this->router = $this->dataGrip->get(ROUTER_TYPE_HTTP); + $this->exception = \Kiri::getDi()->get($exception); + $this->router = $dataGrip->get(ROUTER_TYPE_HTTP); } diff --git a/Handler/OnServerWorker.php b/Handler/OnServerWorker.php index cfb7b1b..001d1ca 100644 --- a/Handler/OnServerWorker.php +++ b/Handler/OnServerWorker.php @@ -26,14 +26,6 @@ use function config; class OnServerWorker extends Kiri\Server\Abstracts\Server { - - /** - * @var EventDispatch - */ - #[Container(EventDispatch::class)] - public EventDispatch $dispatch; - - /** * @return void */ @@ -68,13 +60,14 @@ class OnServerWorker extends Kiri\Server\Abstracts\Server */ public function onWorkerStart(Server $server, int $workerId): void { - $this->dispatch->dispatch(new OnBeforeWorkerStart($server, $workerId)); + $dispatch = Kiri::getDi()->get(EventDispatch::class); + $dispatch->dispatch(new OnBeforeWorkerStart($server, $workerId)); if ($workerId < $server->setting['worker_num']) { - $this->dispatch->dispatch(new OnWorkerStart($server, $workerId)); + $dispatch->dispatch(new OnWorkerStart($server, $workerId)); } else { - $this->dispatch->dispatch(new OnTaskerStart($server, $workerId)); + $dispatch->dispatch(new OnTaskerStart($server, $workerId)); } - $this->dispatch->dispatch(new OnAfterWorkerStart($server, $workerId)); + $dispatch->dispatch(new OnAfterWorkerStart($server, $workerId)); } diff --git a/HotReload.php b/HotReload.php index e632642..2c2a964 100644 --- a/HotReload.php +++ b/HotReload.php @@ -37,9 +37,9 @@ class HotReload extends BaseProcess /** * @param Router $router */ - public function __construct(public Router $router) + public function __construct(Router $router) { - on(OnWorkerStart::class, [$this->router, 'scan_build_route']); + on(OnWorkerStart::class, [$router, 'scan_build_route']); $this->forceFile = \config('reload.forceFile', false); } @@ -61,7 +61,7 @@ class HotReload extends BaseProcess { // TODO: Implement onSigterm() method. if (Context::inCoroutine()) { - Coroutine::create(fn() => $this->onShutdown(Coroutine::waitSignal(SIGTERM | SIGINT))); + Coroutine::create(fn () => $this->onShutdown(Coroutine::waitSignal(SIGTERM | SIGINT))); } else { \pcntl_signal(SIGTERM, [$this, 'onStop']); } @@ -127,8 +127,8 @@ class HotReload extends BaseProcess } $this->watch(rtrim($dir, '/')); } - Event::add($this->inotify, fn() => $this->check()); - Event::cycle(fn() => function () { + Event::add($this->inotify, fn () => $this->check()); + Event::cycle(fn () => function () { Event::dispatch(); }, true); Event::wait(); @@ -262,7 +262,7 @@ class HotReload extends BaseProcess if (Context::exists('swoole_timer_after')) { return; } - $int = @swoole_timer_after(2000, fn() => $this->reload()); + $int = @swoole_timer_after(2000, fn () => $this->reload()); Context::set('swoole_timer_after', $int); Context::set('isReloading', true); } @@ -308,7 +308,7 @@ class HotReload extends BaseProcess */ public function trigger_reload(): void { - $this->logger->failure('Wait trigger server Reload' . PHP_EOL); + \Kiri::getLogger()->failure('Wait trigger server Reload' . PHP_EOL); di(ServerInterface::class)->reload(false); } diff --git a/Task/Task.php b/Task/Task.php index ef06186..d7d1f0a 100644 --- a/Task/Task.php +++ b/Task/Task.php @@ -25,17 +25,15 @@ class Task /** - * @param ContainerInterface $container - * @throws ContainerExceptionInterface - * @throws NotFoundExceptionInterface + * */ - public function __construct(public ContainerInterface $container) + public function __construct() { $exception = \config('exception.task'); if (!in_array(ExceptionHandlerInterface::class, class_implements($exception))) { $exception = ExceptionHandlerDispatcher::class; } - $this->exception = $this->container->get($exception); + $this->exception = Kiri::getDi()->get($exception); } diff --git a/Task/TaskExecute.php b/Task/TaskExecute.php index 48b73dc..3ceaf4f 100644 --- a/Task/TaskExecute.php +++ b/Task/TaskExecute.php @@ -8,15 +8,6 @@ use Kiri\Server\ServerInterface; class TaskExecute implements TaskInterface { - /** - * @var ServerInterface - */ - #[Container(ServerInterface::class)] - public ServerInterface $server; - - - - /** * @param mixed $data * @param float $timeout @@ -25,7 +16,9 @@ class TaskExecute implements TaskInterface */ public function taskWait(mixed $data, float $timeout = 0.5, int $dstWorkerId = -1): mixed { - return $this->server->taskwait($data, $timeout, $dstWorkerId); + $server = \Kiri::getDi()->get(ServerInterface::class); + + return $server->taskwait($data, $timeout, $dstWorkerId); } @@ -36,7 +29,9 @@ class TaskExecute implements TaskInterface */ public function taskCo(array $tasks, float $timeout = 0.5): false|array { - return $this->server->taskCo($tasks, $timeout); + $server = \Kiri::getDi()->get(ServerInterface::class); + + return $server->taskCo($tasks, $timeout); } @@ -47,7 +42,9 @@ class TaskExecute implements TaskInterface */ public function taskWaitMulti(array $tasks, float $timeout = 0.5): false|array { - return $this->server->taskWaitMulti($tasks, $timeout); + $server = \Kiri::getDi()->get(ServerInterface::class); + + return $server->taskWaitMulti($tasks, $timeout); }