diff --git a/Server/Worker/OnServerWorker.php b/Server/Worker/OnServerWorker.php index d9932ca3..c2f38561 100644 --- a/Server/Worker/OnServerWorker.php +++ b/Server/Worker/OnServerWorker.php @@ -5,20 +5,18 @@ namespace Server\Worker; use Annotation\Annotation; use Annotation\Inject; use Exception; +use Kiri\Abstracts\Config; +use Kiri\Core\Help; +use Kiri\Events\EventDispatch; +use Kiri\Exception\ConfigException; +use Kiri\Kiri; +use Kiri\Runtime; use ReflectionException; -use Server\Constant; use Server\Events\OnAfterWorkerStart; use Server\Events\OnWorkerError; use Server\Events\OnWorkerExit; use Server\Events\OnWorkerStart; use Server\Events\OnWorkerStop; -use Kiri\Abstracts\Config; -use Kiri\Core\Help; -use Kiri\Events\EventDispatch; -use Kiri\Exception\ConfigException; -use Kiri\Exception\NotFindClassException; -use Kiri\Runtime; -use Kiri\Kiri; use Swoole\Server; use Swoole\Timer; @@ -45,18 +43,37 @@ class OnServerWorker extends \Server\Abstracts\Server */ public function onWorkerStart(Server $server, int $workerId) { - $this->_setConfigCache($workerId); + $this->eventDispatch->dispatch(new OnWorkerStart($server, $workerId)); + + $this->onWorkerStartInit($server, $workerId); + + $this->eventDispatch->dispatch(new OnAfterWorkerStart()); + } + + + /** + * @param Server $server + * @param int $workerId + * @throws ConfigException + * @throws ReflectionException + * @throws Exception + */ + private function onWorkerStartInit(Server $server, int $workerId) + { + putenv('state=start'); + putenv('worker=' . $workerId); + + $serialize = file_get_contents(storage(Runtime::CONFIG_NAME)); + if (!empty($serialize)) { + Config::sets(unserialize($serialize)); + } + $annotation = Kiri::app()->getAnnotation(); $annotation->read(APP_PATH . 'app', 'App', $workerId < $server->setting['worker_num'] ? [] : [CONTROLLER_PATH] ); - - $this->eventDispatch->dispatch(new OnWorkerStart($server, $workerId)); - $this->workerInitExecutor($server, $annotation, $workerId); -// $this->interpretDirectory($annotation); - - $this->eventDispatch->dispatch(new OnAfterWorkerStart()); + $this->interpretDirectory($annotation); } @@ -113,23 +130,6 @@ class OnServerWorker extends \Server\Abstracts\Server } - /** - * @param $worker_id - * @throws Exception - */ - private function _setConfigCache($worker_id) - { - putenv('state=start'); - putenv('worker=' . $worker_id); - - $serialize = file_get_contents(storage(Runtime::CONFIG_NAME)); - if (empty($serialize)) { - return; - } - Config::sets(unserialize($serialize)); - } - - /** * @param Server $server * @param int $workerId diff --git a/http-helper/Route/Router.php b/http-helper/Route/Router.php index 017e96a8..84afd8bc 100644 --- a/http-helper/Route/Router.php +++ b/http-helper/Route/Router.php @@ -548,20 +548,20 @@ class Router extends HttpService implements RouterInterface public function _loader() { $this->loadRouteDir(APP_PATH . 'routes'); -// $classes = Kiri::getAnnotation()->runtime(CONTROLLER_PATH); -// -// $di = Kiri::getDi(); -// foreach ($classes as $class) { -// $methods = $di->getMethodAttribute($class); -// foreach ($methods as $method => $attribute) { -// if (empty($attribute)) { -// continue; -// } -// foreach ($attribute as $item) { -// $item->execute($class, $method); -// } -// } -// } + $classes = Kiri::getAnnotation()->runtime(CONTROLLER_PATH); + + $di = Kiri::getDi(); + foreach ($classes as $class) { + $methods = $di->getMethodAttribute($class); + foreach ($methods as $method => $attribute) { + if (empty($attribute)) { + continue; + } + foreach ($attribute as $item) { + $item->execute($class, $method); + } + } + } } /**