From b61987d43ad6afc140a9027298ae531a00d24f4b Mon Sep 17 00:00:00 2001 From: "as2252258@163.com" Date: Fri, 9 Apr 2021 02:45:33 +0800 Subject: [PATCH] modify --- HttpServer/Events/OnWorkerStart.php | 23 +++++---- HttpServer/Events/Pipeline.php | 73 +++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+), 9 deletions(-) create mode 100644 HttpServer/Events/Pipeline.php diff --git a/HttpServer/Events/OnWorkerStart.php b/HttpServer/Events/OnWorkerStart.php index 4efd45c2..204923b5 100644 --- a/HttpServer/Events/OnWorkerStart.php +++ b/HttpServer/Events/OnWorkerStart.php @@ -47,17 +47,22 @@ class OnWorkerStart extends Callback $runtime = file_get_contents(storage('runtime.php')); $annotation = Snowflake::app()->getAnnotation(); $annotation->setLoader(unserialize($runtime)); - if ($isWorker === true) { - $annotation->runtime(CONTROLLER_PATH); - $annotation->runtime(APP_PATH, CONTROLLER_PATH); + return (new Pipeline()) + ->if($isWorker, function ($annotation, $server) { + $annotation->runtime(CONTROLLER_PATH); + $annotation->runtime(APP_PATH, CONTROLLER_PATH); - name($this->server->worker_pid, 'Worker.' . $this->server->worker_id); - } else { - $annotation->runtime(MODEL_PATH); + name($server->worker_pid, 'Worker.' . $server->worker_id); + }) + ->else(function ($annotation, $server) { + $annotation->runtime(MODEL_PATH); - name($this->server->worker_pid, 'Task.' . $this->server->worker_id); - } - return $annotation; + name($server->worker_pid, 'Task.' . $server->worker_id); + }) + ->catch(function (\Throwable $throwable) { + $this->addError($throwable->getMessage()); + }) + ->exec($annotation, $this->server); } diff --git a/HttpServer/Events/Pipeline.php b/HttpServer/Events/Pipeline.php new file mode 100644 index 00000000..e78696a0 --- /dev/null +++ b/HttpServer/Events/Pipeline.php @@ -0,0 +1,73 @@ +condition = $condition; + $this->if = $handler; + return $this; + } + + + /** + * @param \Closure $handler + * @return $this + */ + public function else(\Closure $handler): static + { + $this->else = $handler; + return $this; + } + + + /** + * @param \Closure $handler + * @return $this + */ + public function catch(\Closure $handler): static + { + $this->catch = $handler; + return $this; + } + + + /** + * @param $argv + * @return mixed + */ + public function exec(...$argv) + { + try { + if ($this->condition === false) { + call_user_func($this->else, ...$argv); + } else { + call_user_func($this->if, ...$argv); + } + return $argv; + } catch (\Throwable $exception) { + call_user_func($this->catch, $exception, ...$argv); + return $argv; + } + } + +}