diff --git a/Abstracts/AsyncServer.php b/Abstracts/AsyncServer.php index 6ce2757..d24e11b 100644 --- a/Abstracts/AsyncServer.php +++ b/Abstracts/AsyncServer.php @@ -11,7 +11,9 @@ use Kiri\Server\Config as SConfig; use Kiri\Server\Constant; use Kiri\Server\Events\OnServerBeforeStart; use Kiri\Server\Events\OnShutdown; +use Kiri\Server\Events\OnWorkerStart; use Kiri\Server\Handler\OnServer; +use Kiri\Server\HotReload; use Kiri\Server\ServerInterface; use Kiri\Server\Task\TaskInterface; use Kiri\Server\Task\Task; diff --git a/HotReload.php b/HotReload.php index 777dc4c..6cc838f 100644 --- a/HotReload.php +++ b/HotReload.php @@ -5,7 +5,9 @@ namespace Kiri\Server; use Exception; use Kiri\Di\Context; +use Kiri\Router\Router; use Kiri\Server\Abstracts\BaseProcess; +use Kiri\Server\Events\OnWorkerStart; use ReflectionException; use Swoole\Coroutine; use Swoole\Event; @@ -31,6 +33,14 @@ class HotReload extends BaseProcess protected mixed $inotify = null; + /** + * @param Router $router + */ + public function __construct(public Router $router) + { + on(OnWorkerStart::class, [$this->router, 'scan_build_route']); + } + /** * @return string */ diff --git a/Server.php b/Server.php index 05a001e..946db90 100644 --- a/Server.php +++ b/Server.php @@ -14,7 +14,7 @@ use Kiri\Server\Events\OnWorkerStop; use Kiri\Server\Abstracts\AsyncServer; use Psr\Container\ContainerExceptionInterface; use Psr\Container\NotFoundExceptionInterface; -use ReflectionException; +use Kiri\Server\Events\OnServerBeforeStart; use Swoole\Timer; @@ -50,12 +50,13 @@ class Server /** - * @param $process - * @throws Exception + * @return void */ - public function addProcess($process): void + public function init(): void { - $this->manager->addProcess($process); + on(OnWorkerStop::class, [Timer::class, 'clearAll'], 9999); + on(OnWorkerStart::class, [$this, 'setWorkerName']); + on(OnTaskerStart::class, [$this, 'setTaskerName']); } @@ -65,16 +66,11 @@ class Server */ public function start(): void { - on(OnWorkerStop::class, [Timer::class, 'clearAll'], 9999); - on(OnWorkerStart::class, [$this, 'setWorkerName']); - on(OnTaskerStart::class, [$this, 'setTaskerName']); - if (\config('reload.hot') === false) { - $this->router->scan_build_route(); + if (\config('reload.hot', false) === true) { + $this->manager->addProcess(HotReload::class); } else { - on(OnWorkerStart::class, [$this->router, 'scan_build_route']); - $this->addProcess(HotReload::class); + $this->router->scan_build_route(); } - $this->manager->initCoreServers(\config('server', []), $this->daemon); $this->manager->start(); }