From ff58d0faef7da1843bfb74517c9eb49eaa60cc85 Mon Sep 17 00:00:00 2001 From: "as2252258@163.com" Date: Sun, 19 Sep 2021 16:42:29 +0800 Subject: [PATCH] 111 --- composer.json | 2 - core/Abstracts/BaseApplication.php | 2 - core/AspectManager.php | 124 -------------------------- core/Proxy/AProxy.php | 8 ++ core/Proxy/AspectProxy.php | 53 +++++++++++ core/Proxy/ProxyInterface.php | 17 ++++ function.php | 16 ---- http-handler/Abstracts/Handler.php | 13 +-- http-handler/Pipeline.php | 3 +- http-handler/Router.php | 2 +- http-server/Events/OnTaskerStart.php | 23 +++++ http-server/Service/Http.php | 2 +- http-server/Worker/OnServerWorker.php | 12 ++- http-server/Worker/OnTaskerStart.php | 33 +++++++ http-server/Worker/OnWorkerStart.php | 120 ++++--------------------- http-server/Worker/WorkerStart.php | 88 ++++++++++++++++++ 16 files changed, 256 insertions(+), 262 deletions(-) delete mode 100644 core/AspectManager.php create mode 100644 core/Proxy/AProxy.php create mode 100644 core/Proxy/AspectProxy.php create mode 100644 core/Proxy/ProxyInterface.php create mode 100644 http-server/Events/OnTaskerStart.php create mode 100644 http-server/Worker/OnTaskerStart.php create mode 100644 http-server/Worker/WorkerStart.php diff --git a/composer.json b/composer.json index 28de06ba..501eba8e 100644 --- a/composer.json +++ b/composer.json @@ -35,11 +35,9 @@ "autoload": { "psr-4": { "Kiri\\": "core/", - "Http\\": "http-helper/", "Http\\Message\\": "http-message/", "Http\\Handler\\": "http-handler/", "Server\\": "http-server/", - "Console\\": "kiri-console/", "Gii\\": "kiri-gii/", "Annotation\\": "note/" }, diff --git a/core/Abstracts/BaseApplication.php b/core/Abstracts/BaseApplication.php index 3a1d3328..6f5b8cc1 100644 --- a/core/Abstracts/BaseApplication.php +++ b/core/Abstracts/BaseApplication.php @@ -16,7 +16,6 @@ use Exception; use Http\Handler\Router; use Server\Server; use Kafka\KafkaProvider; -use Kiri\AspectManager; use Kiri\Async; use Kiri\Cache\Redis; use Kiri\Di\LocalService; @@ -460,7 +459,6 @@ abstract class BaseApplication extends Component 'logger' => ['class' => Logger::class], 'annotation' => ['class' => SAnnotation::class], 'databases' => ['class' => Connection::class], - 'aop' => ['class' => AspectManager::class], 'jwt' => ['class' => Jwt::class], 'async' => ['class' => Async::class], 'kafka-container' => ['class' => KafkaProvider::class], diff --git a/core/AspectManager.php b/core/AspectManager.php deleted file mode 100644 index c6ba575b..00000000 --- a/core/AspectManager.php +++ /dev/null @@ -1,124 +0,0 @@ -getReflect(current(static::$_aop[$aopName])); - if (!$reflect->isInstantiable() || !$reflect->hasMethod('invoke')) { - throw new Exception(ASPECT_ERROR . IAspect::class); - } - $method = $reflect->getMethod('invoke'); - - return $method->invokeArgs($reflect->newInstance($handler), $params); - } - - - /** - * @param array $handler - * @return IAspect - * @throws Exception - * @throws ReflectionException - */ - public function getAop(array $handler): IAspect - { - $aopName = $handler[0]::class . '::' . $handler[1]; - - $reflect = Kiri::getDi()->get(current(static::$_aop[$aopName])); - if (!method_exists($reflect, 'invoke')) { - throw new Exception(ASPECT_ERROR . IAspect::class); - } - return $reflect; - } - - - /** - * @param $handler - * @param $params - * @return mixed - * @throws Exception - */ - private function notFound($handler, $params): mixed - { - if (!method_exists($handler[0], $handler[1])) { - return response()->close(404); - } - return call_user_func($handler, ...$params); - } - -} diff --git a/core/Proxy/AProxy.php b/core/Proxy/AProxy.php new file mode 100644 index 00000000..678caa5c --- /dev/null +++ b/core/Proxy/AProxy.php @@ -0,0 +1,8 @@ +callback instanceof \Closure) { + return call_user_func($executor->callback, ...$executor->params); + } + $controller = Kiri::getDi()->get($executor->callback[0]); + $aspect = $this->getAspect($executor->callback); + if (!is_null($aspect)) { + $aspect->before(); + $result = $aspect->invoke([$controller, $executor->callback[1]], $executor->params); + $aspect->after($result); + } else { + $result = call_user_func([$controller, $executor->callback[1]]); + } + return $result; + } + + + /** + * @param array $executor + * @return ?IAspect + */ + protected function getAspect(array $executor): ?Aspect + { + $aspect = NoteManager::getSpecify_annotation(Aspect::class, $executor[0], $executor[1]); + if (!is_null($aspect)) { + $aspect = Kiri::getDi()->get($aspect->aspect); + } + return $aspect; + } +} diff --git a/core/Proxy/ProxyInterface.php b/core/Proxy/ProxyInterface.php new file mode 100644 index 00000000..e7c413ba --- /dev/null +++ b/core/Proxy/ProxyInterface.php @@ -0,0 +1,17 @@ +get(AspectManager::class)->dispatch($handler, $params); - } -} - if (!function_exists('app')) { diff --git a/http-handler/Abstracts/Handler.php b/http-handler/Abstracts/Handler.php index 24f33369..16874267 100644 --- a/http-handler/Abstracts/Handler.php +++ b/http-handler/Abstracts/Handler.php @@ -6,6 +6,7 @@ use Http\Handler\Handler as CHl; use Http\Message\ServerRequest; use Kiri\Core\Help; use Kiri\Kiri; +use Kiri\Proxy\AspectProxy; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\MiddlewareInterface; @@ -74,16 +75,8 @@ abstract class Handler implements RequestHandlerInterface */ protected function dispatcher(ServerRequestInterface $request): mixed { - if ($this->handler->callback instanceof \Closure) { - $response = call_user_func($this->handler->callback, ...$this->handler->params); - } else { - [$controller, $action] = $this->handler->callback; - - $controller = Kiri::getDi()->get($controller); - - $response = call_user_func([$controller, $action], ...$this->handler->params); - } - if (!($response instanceof ResponseInterface)) { + $aspect = Kiri::getDi()->get(AspectProxy::class); + if (!(($response = $aspect->proxy($this->handler)) instanceof ResponseInterface)) { $response = $this->transferToResponse($response); } $response->withHeader('Run-Time', $this->_runTime($request)); diff --git a/http-handler/Pipeline.php b/http-handler/Pipeline.php index fc0b6d00..05149cab 100644 --- a/http-handler/Pipeline.php +++ b/http-handler/Pipeline.php @@ -106,8 +106,7 @@ class Pipeline * @param $destination * @param $parameters * @return Closure|array - * @throws ReflectionException - */ + */ private function aspect_caller($destination, $parameters): Closure|array { [$controller, $action] = $destination; diff --git a/http-handler/Router.php b/http-handler/Router.php index 051e1924..2316d083 100644 --- a/http-handler/Router.php +++ b/http-handler/Router.php @@ -212,7 +212,7 @@ class Router /** * @throws Exception */ - public function _loader() + public function read_files() { $this->loadRouteDir(APP_PATH . 'routes'); } diff --git a/http-server/Events/OnTaskerStart.php b/http-server/Events/OnTaskerStart.php new file mode 100644 index 00000000..e8ef690d --- /dev/null +++ b/http-server/Events/OnTaskerStart.php @@ -0,0 +1,23 @@ +handler($handler, $PsrRequest); } } catch (\Throwable $throwable) { - $PsrResponse = \response()->withStatus($throwable->getCode()) + $PsrResponse = $this->response->withStatus($throwable->getCode()) ->withContentType(\Http\Message\Response::CONTENT_TYPE_HTML) ->withBody(new Stream(jTraceEx($throwable, null, true))); } finally { diff --git a/http-server/Worker/OnServerWorker.php b/http-server/Worker/OnServerWorker.php index f323eb08..50aad95e 100644 --- a/http-server/Worker/OnServerWorker.php +++ b/http-server/Worker/OnServerWorker.php @@ -7,6 +7,7 @@ use Exception; use Kiri\Abstracts\Config; use Kiri\Core\Help; use Kiri\Events\EventDispatch; +use Kiri\Kiri; use Kiri\Runtime; use Server\Events\OnAfterWorkerStart; use Server\Events\OnBeforeWorkerStart; @@ -17,6 +18,7 @@ use Server\Events\OnWorkerStop; use Server\ServerManager; use Swoole\Server; use Swoole\Timer; +use Server\Events\OnTaskerStart as OnTaskStart; /** @@ -42,9 +44,13 @@ class OnServerWorker extends \Server\Abstracts\Server public function onWorkerStart(Server $server, int $workerId) { $this->eventDispatch->dispatch(new OnBeforeWorkerStart($workerId)); - - $this->eventDispatch->dispatch(new OnWorkerStart($server, $workerId)); - + if ($workerId < $server->setting['worker_num']) { + $this->eventDispatch->dispatch(new OnWorkerStart($server, $workerId)); + $this->setProcessName(sprintf('Worker[%d].%d', $server->worker_pid, $workerId)); + } else { + $this->eventDispatch->dispatch(new OnTaskStart($server, $workerId)); + $this->setProcessName(sprintf('Tasker[%d].%d', $server->worker_pid, $workerId)); + } $this->eventDispatch->dispatch(new OnAfterWorkerStart()); } diff --git a/http-server/Worker/OnTaskerStart.php b/http-server/Worker/OnTaskerStart.php new file mode 100644 index 00000000..d0db0199 --- /dev/null +++ b/http-server/Worker/OnTaskerStart.php @@ -0,0 +1,33 @@ +workerId < $event->server->setting['worker_num']; + + $time = microtime(true); + + ServerManager::setEnv('environmental', Kiri::TASK); + if (!is_enable_file_modification_listening()) { + $this->interpretDirectory(); + } + + $this->mixed($event, $isWorker, $time); + } + + +} diff --git a/http-server/Worker/OnWorkerStart.php b/http-server/Worker/OnWorkerStart.php index c68b3f71..203eb00b 100644 --- a/http-server/Worker/OnWorkerStart.php +++ b/http-server/Worker/OnWorkerStart.php @@ -14,111 +14,29 @@ use Psr\EventDispatcher\EventDispatcherInterface; use ReflectionException; use Server\ServerManager; -class OnWorkerStart implements EventDispatcherInterface +class OnWorkerStart extends WorkerStart implements EventDispatcherInterface { - /** - * @var Annotation - */ - #[Inject(Annotation::class)] - public Annotation $annotation; + /** + * @param object $event + * @return void + * @throws ConfigException + * @throws ReflectionException + * @throws Exception + */ + public function dispatch(object $event) + { + $isWorker = $event->workerId < $event->server->setting['worker_num']; + $time = microtime(true); - /** - * @var Router - */ - #[Inject(Router::class)] - public Router $router; - - - /** - * @param object $event - * @return void - * @throws ConfigException - * @throws ReflectionException - * @throws Exception - */ - public function dispatch(object $event) - { - $isWorker = $event->workerId < $event->server->setting['worker_num']; - - $time = microtime(true); - - $this->interpretDirectory($isWorker); - if ($isWorker) { - ServerManager::setEnv('environmental', Kiri::WORKER); - Kiri::getDi()->get(Router::class)->_loader(); - - $this->setProcessName(sprintf('Worker[%d].%d', $event->server->worker_pid, $event->workerId)); - } else { - ServerManager::setEnv('environmental', Kiri::TASK); - - $this->setProcessName(sprintf('Tasker[%d].%d', $event->server->worker_pid, $event->workerId)); - } - $this->mixed($event, Config::get('id', 'system-service'), $isWorker, $time); - } - - - /** - * @param $event - * @param $name - * @param $isWorker - * @param $time - */ - private function mixed($event, $name, $isWorker, $time) - { - echo sprintf("\033[36m[" . date('Y-m-d H:i:s') . "]\033[0m [%s]Builder %s[%d].%d use time %s.", $name, $isWorker ? 'Worker' : 'Taker', - $event->server->worker_pid, $event->workerId, round(microtime(true) - $time, 6) . 's') . PHP_EOL; - } - - - /** - * @param $prefix - * @throws ConfigException - */ - protected function setProcessName($prefix) - { - if (Kiri::getPlatform()->isMac()) { - return; - } - $name = Config::get('id', 'system-service'); - if (!empty($prefix)) { - $name .= '.' . $prefix; - } - swoole_set_process_name($name); - } - - - /** - * @throws ReflectionException - * @throws Exception - */ - private function interpretDirectory($isWorker) - { - $di = Kiri::getDi(); - -// $namespace = array_filter(explode('\\', Router::getNamespace())); -// -// $namespace = APP_PATH . implode('/', $namespace); - - $this->annotation->read(APP_PATH . 'app', 'App'); - - $fileLists = $this->annotation->read(APP_PATH . 'app'); - foreach ($fileLists->runtime(APP_PATH . 'app') as $class) { - foreach (NoteManager::getTargetNote($class) as $value) { - $value->execute($class); - } - $methods = $di->getMethodAttribute($class); - foreach ($methods as $method => $attribute) { - if (empty($attribute)) { - continue; - } - foreach ($attribute as $item) { - $item->execute($class, $method); - } - } - } - } + ServerManager::setEnv('environmental', Kiri::WORKER); + if (!is_enable_file_modification_listening()) { + $this->router->read_files(); + $this->interpretDirectory(); + } + $this->mixed($event, $isWorker, $time); + } } diff --git a/http-server/Worker/WorkerStart.php b/http-server/Worker/WorkerStart.php new file mode 100644 index 00000000..ba3dc35b --- /dev/null +++ b/http-server/Worker/WorkerStart.php @@ -0,0 +1,88 @@ +annotation->read(APP_PATH . 'app', 'App'); + + $fileLists = $this->annotation->read(APP_PATH . 'app'); + foreach ($fileLists->runtime(APP_PATH . 'app') as $class) { + foreach (NoteManager::getTargetNote($class) as $value) { + $value->execute($class); + } + $methods = $di->getMethodAttribute($class); + foreach ($methods as $method => $attribute) { + if (empty($attribute)) { + continue; + } + foreach ($attribute as $item) { + $item->execute($class, $method); + } + } + } + } + + + /** + * @param $event + * @param $isWorker + * @param $time + * @throws \Kiri\Exception\ConfigException + */ + protected function mixed($event, $isWorker, $time) + { + $name = Config::get('id', 'system-service'); + echo sprintf("\033[36m[" . date('Y-m-d H:i:s') . "]\033[0m [%s]Builder %s[%d].%d use time %s.", $name, $isWorker ? 'Worker' : 'Taker', + $event->server->worker_pid, $event->workerId, round(microtime(true) - $time, 6) . 's') . PHP_EOL; + } + + /** + * @param $prefix + * @throws ConfigException + */ + protected function setProcessName($prefix) + { + if (Kiri::getPlatform()->isMac()) { + return; + } + $name = Config::get('id', 'system-service'); + if (!empty($prefix)) { + $name .= '.' . $prefix; + } + swoole_set_process_name($name); + } + +}