diff --git a/Abstracts/AsyncServer.php b/Abstracts/AsyncServer.php index 974a38b..a16d562 100644 --- a/Abstracts/AsyncServer.php +++ b/Abstracts/AsyncServer.php @@ -218,8 +218,7 @@ class AsyncServer implements ServerInterface */ public function start(): void { - $processManager = Kiri::getDi()->get(EventDispatch::class); - $processManager->dispatch(new OnServerBeforeStart()); + event(new OnServerBeforeStart()); $this->server->start(); } diff --git a/Abstracts/BaseProcess.php b/Abstracts/BaseProcess.php index 88a787b..f52d3ea 100644 --- a/Abstracts/BaseProcess.php +++ b/Abstracts/BaseProcess.php @@ -90,6 +90,7 @@ abstract class BaseProcess implements OnProcessInterface /** * @param $data + * @throws \ReflectionException */ protected function onShutdown($data): void { diff --git a/Abstracts/DoWhile.php b/Abstracts/DoWhile.php deleted file mode 100644 index f252063..0000000 --- a/Abstracts/DoWhile.php +++ /dev/null @@ -1,36 +0,0 @@ - \Swoole\Coroutine\Server::class, - Constant::SERVER_TYPE_HTTP, Constant::SERVER_TYPE_WEBSOCKET => \Swoole\Coroutine\Http\Server::class, + 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/Broadcast/Broadcast.php b/Broadcast/Broadcast.php index 0567311..8d4b48b 100644 --- a/Broadcast/Broadcast.php +++ b/Broadcast/Broadcast.php @@ -5,6 +5,7 @@ namespace Kiri\Server\Broadcast; use Kiri; use Kiri\Server\ServerInterface; use Kiri\Server\Abstracts\ProcessManager; +use ReflectionException; class Broadcast { @@ -13,6 +14,7 @@ class Broadcast /** * @param $message * @return void + * @throws ReflectionException */ public function broadcast($message): void { diff --git a/Handler/OnPipeMessage.php b/Handler/OnPipeMessage.php index 55e3e00..6be23ab 100644 --- a/Handler/OnPipeMessage.php +++ b/Handler/OnPipeMessage.php @@ -20,7 +20,7 @@ class OnPipeMessage extends Server * @param mixed $message * @throws Exception */ - public function onPipeMessage(\Swoole\Server $server, int $src_worker_id, mixed $message) + public function onPipeMessage(\Swoole\Server $server, int $src_worker_id, mixed $message): void { if (is_string($message)) { $message = unserialize($message); diff --git a/Handler/OnServer.php b/Handler/OnServer.php index c7e649e..1b9afb8 100644 --- a/Handler/OnServer.php +++ b/Handler/OnServer.php @@ -32,12 +32,11 @@ class OnServer extends Server * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface */ - public function onStart(SServer $server) + public function onStart(SServer $server): void { \Kiri::setProcessName(sprintf('start[%d].server', $server->master_pid)); - $dispatch = \Kiri::getDi()->get(EventDispatch::class); - $dispatch->dispatch(new OnStart($server)); + event(new OnStart($server)); } @@ -47,52 +46,20 @@ class OnServer extends Server * @throws NotFoundExceptionInterface * @throws ReflectionException */ - public function onBeforeShutdown(SServer $server) + public function onBeforeShutdown(SServer $server): void { - $dispatch = \Kiri::getDi()->get(EventDispatch::class); - $dispatch->dispatch(new OnBeforeShutdown($server)); + event(new OnBeforeShutdown($server)); } /** * @param SServer $server - * @throws ContainerExceptionInterface - * @throws NotFoundExceptionInterface - * @throws ReflectionException + * @throws */ public function onShutdown(SServer $server): void { @unlink(storage('.swoole.pid')); - $dispatch = \Kiri::getDi()->get(EventDispatch::class); - $dispatch->dispatch(new OnShutdown($server)); + event(new OnShutdown($server)); } - - /** - * @param SServer $server - * @throws ContainerExceptionInterface - * @throws NotFoundExceptionInterface - * @throws ReflectionException - */ - public function onBeforeReload(SServer $server) - { - $dispatch = \Kiri::getDi()->get(EventDispatch::class); - $dispatch->dispatch(new OnBeforeReload($server)); - } - - - /** - * @param SServer $server - * @throws ContainerExceptionInterface - * @throws NotFoundExceptionInterface - * @throws ReflectionException - */ - public function onAfterReload(SServer $server) - { - $dispatch = \Kiri::getDi()->get(EventDispatch::class); - $dispatch->dispatch(new OnAfterReload($server)); - } - - - } diff --git a/Handler/OnServerManager.php b/Handler/OnServerManager.php index d25b208..7398e5b 100644 --- a/Handler/OnServerManager.php +++ b/Handler/OnServerManager.php @@ -24,16 +24,14 @@ class OnServerManager extends Server /** * @param \Swoole\Server $server - * @throws ConfigException * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface|ReflectionException */ - public function onManagerStart(\Swoole\Server $server) + public function onManagerStart(\Swoole\Server $server): void { Kiri::setProcessName(sprintf('manger process[%d]', $server->manager_pid)); - $dispatch = \Kiri::getDi()->get(EventDispatch::class); - $dispatch->dispatch(new OnManagerStart($server)); + event(new OnManagerStart($server)); } @@ -45,8 +43,7 @@ class OnServerManager extends Server */ public function onManagerStop(\Swoole\Server $server): void { - $dispatch = \Kiri::getDi()->get(EventDispatch::class); - $dispatch->dispatch(new OnManagerStop($server)); + event(new OnManagerStop($server)); } diff --git a/Handler/OnServerWorker.php b/Handler/OnServerWorker.php index ef7c109..42557b8 100644 --- a/Handler/OnServerWorker.php +++ b/Handler/OnServerWorker.php @@ -58,10 +58,9 @@ class OnServerWorker extends \Kiri\Server\Abstracts\Server * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface|ReflectionException */ - public function onWorkerStop(Server $server, int $workerId) + public function onWorkerStop(Server $server, int $workerId): void { - $dispatch = \Kiri::getDi()->get(EventDispatch::class); - $dispatch->dispatch(new OnWorkerStop($server, $workerId)); + event(new OnWorkerStop($server, $workerId)); } @@ -71,10 +70,9 @@ class OnServerWorker extends \Kiri\Server\Abstracts\Server * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface|ReflectionException */ - public function onWorkerExit(Server $server, int $workerId) + public function onWorkerExit(Server $server, int $workerId): void { - $dispatch = \Kiri::getDi()->get(EventDispatch::class); - $dispatch->dispatch(new OnWorkerExit($server, $workerId)); + event(new OnWorkerExit($server, $workerId)); } @@ -88,7 +86,7 @@ class OnServerWorker extends \Kiri\Server\Abstracts\Server * @throws NotFoundExceptionInterface * @throws Exception */ - public function onWorkerError(Server $server, int $worker_id, int $worker_pid, int $exit_code, int $signal) + public function onWorkerError(Server $server, int $worker_id, int $worker_pid, int $exit_code, int $signal): void { $dispatch = \Kiri::getDi()->get(EventDispatch::class); $dispatch->dispatch(new OnWorkerError($server, $worker_id, $worker_pid, $exit_code, $signal)); @@ -97,7 +95,7 @@ class OnServerWorker extends \Kiri\Server\Abstracts\Server $worker_id, $worker_pid, $signal, $exit_code, swoole_strerror(swoole_last_error(), 9) ); - \Kiri::getLogger()->error($message); + error($message); $this->system_mail($message); } @@ -107,7 +105,7 @@ class OnServerWorker extends \Kiri\Server\Abstracts\Server * @param $messageContent * @throws Exception */ - protected function system_mail($messageContent) + protected function system_mail($messageContent): void { try { $email = Config::get('email', ['enable' => false]); diff --git a/ServerCommand.php b/ServerCommand.php index 047f3b6..c91205b 100644 --- a/ServerCommand.php +++ b/ServerCommand.php @@ -6,7 +6,6 @@ namespace Kiri\Server; use Exception; use Kiri; -use Kiri\Abstracts\Config; use Kiri\Exception\ConfigException; use Psr\Container\ContainerExceptionInterface; use Psr\Container\NotFoundExceptionInterface; @@ -56,17 +55,13 @@ class ServerCommand extends Command */ public function execute(InputInterface $input, OutputInterface $output): int { - $value = match ($input->getArgument('action')) { + return match ($input->getArgument('action')) { 'restart' => $this->restart($input), 'stop' => $this->stop(), 'start' => $this->start($input), default => throw new Exception('I don\'t know what I want to do.') }; - if ($input->getOption('daemon') != 1) { - file_put_contents(storage('.swoole.pid'), 0); - } - return $value; } diff --git a/ServerProviders.php b/ServerProviders.php index 7f42dce..868b841 100644 --- a/ServerProviders.php +++ b/ServerProviders.php @@ -24,7 +24,7 @@ class ServerProviders extends Providers * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface */ - public function onImport(LocalService $application) + public function onImport(LocalService $application): void { $server = $this->container->get(ServerCommand::class); diff --git a/WorkerStatus.php b/WorkerStatus.php deleted file mode 100644 index 02f0d3e..0000000 --- a/WorkerStatus.php +++ /dev/null @@ -1,33 +0,0 @@ -enum = $enum; - } - - - /** - * @param StatusEnum $enum - * @return bool - */ - public function is(StatusEnum $enum): bool - { - return $this->enum == $enum; - } - - -}