diff --git a/README.md b/README.md index 38153373..82c21bc3 100644 --- a/README.md +++ b/README.md @@ -64,4 +64,4 @@ Pages:[https://framework.zhamao.xin/](https://framework.zhamao.xin/) 本项目在更行内容时,请及时关注 GitHub 动态,更新前请将自己的模块代码做好备份。 -项目框架采用 Apache-2.0 协议开源,在分发或重写修改等操作时需遵守协议。项目模块部分(`Module` 文件夹) 在非借鉴框架内代码时可不遵守 Apache-2.0 协议进行分发和修改(声明版权)。 \ No newline at end of file +项目框架采用 Apache-2.0 协议开源,在分发或重写修改等操作时需遵守协议。项目模块部分(`Module` 文件夹) 在非借鉴框架内代码时可不遵守 Apache-2.0 协议进行分发和修改(声明版权)。 diff --git a/src/Framework/ZMBuf.php b/src/Framework/ZMBuf.php index 8ebb3e92..053c53b9 100755 --- a/src/Framework/ZMBuf.php +++ b/src/Framework/ZMBuf.php @@ -51,6 +51,7 @@ class ZMBuf public static $atomics; public static $req_mapping = []; public static $config = []; + public static $context = []; static function get($name, $default = null) { return self::$cache[$name] ?? $default; @@ -115,4 +116,4 @@ class ZMBuf self::$atomics[$k] = new Atomic($v); } } -} \ No newline at end of file +} diff --git a/src/Framework/global_functions.php b/src/Framework/global_functions.php index 2a4d0026..09c1609e 100644 --- a/src/Framework/global_functions.php +++ b/src/Framework/global_functions.php @@ -1,5 +1,8 @@ $v) { + if(!Co::exists($c)) unset(ZMBuf::$context[$c]); + } +} + +function context(){ + $cid = Co::getCid(); + if(isset(ZMBuf::$context[$cid])) { + return new Context(ZMBuf::$context[$cid], $cid); + } else return null; +} diff --git a/src/Module/Example/Hello.php b/src/Module/Example/Hello.php index f9163917..47cd7d02 100644 --- a/src/Module/Example/Hello.php +++ b/src/Module/Example/Hello.php @@ -6,6 +6,7 @@ namespace Module\Example; use Framework\Console; use ZM\Annotation\CQ\CQCommand; +use ZM\Annotation\Http\Middleware; use ZM\Annotation\Http\RequestMapping; use ZM\Annotation\Swoole\SwooleEventAt; use ZM\Connection\CQConnection; @@ -33,6 +34,7 @@ class Hello extends ModBase /** * @RequestMapping("/test/ping") + * @Middleware("timer") */ public function pong(){ return "pong"; @@ -45,4 +47,4 @@ class Hello extends ModBase Console::info("Unknown connection , I will close it."); $this->connection->close(); } -} \ No newline at end of file +} diff --git a/src/Module/Middleware/TimerMiddleware.php b/src/Module/Middleware/TimerMiddleware.php new file mode 100644 index 00000000..e78e8b67 --- /dev/null +++ b/src/Module/Middleware/TimerMiddleware.php @@ -0,0 +1,38 @@ +starttime = microtime(true); + return true; + } + + /** + * @After() + */ + public function onAfter() { + Console::info("Using " . round((microtime(true) - $this->starttime) * 1000, 2) . " ms."); + } + + public function getName() { return "timer"; } +} diff --git a/src/ZM/Annotation/AnnotationParser.php b/src/ZM/Annotation/AnnotationParser.php index 236e87dc..330a3f11 100644 --- a/src/ZM/Annotation/AnnotationParser.php +++ b/src/ZM/Annotation/AnnotationParser.php @@ -5,6 +5,7 @@ namespace ZM\Annotation; use Doctrine\Common\Annotations\AnnotationException; use Doctrine\Common\Annotations\AnnotationReader; +use Framework\Console; use Framework\ZMBuf; use ReflectionClass; use ReflectionException; @@ -61,24 +62,30 @@ class AnnotationParser } elseif ($vs instanceof InitBuffer) { ZMBuf::set($vs->buf_name, []); } elseif ($vs instanceof MiddlewareClass) { + Console::info("正在注册中间件 ".$vs->class); $result = [ - "class" => "\\".$reflection_class->getName() + "class" => "\\" . $reflection_class->getName() ]; foreach ($methods as $vss) { + if ($vss->getName() == "getName") { + /** @var MiddlewareInterface $tmp */ + $tmp = new $v(); + $result["name"] = $tmp->getName(); + continue; + } $method_annotations = $reader->getMethodAnnotations($vss); foreach ($method_annotations as $vsss) { if ($vss instanceof Rule) $vss = self::registerRuleEvent($vsss, $vss, $reflection_class); else $vss = self::registerMethod($vsss, $vss, $reflection_class); - echo get_class($vsss).PHP_EOL; + //echo get_class($vsss) . PHP_EOL; if ($vsss instanceof Before) $result["before"] = $vsss->method; if ($vsss instanceof After) $result["after"] = $vsss->method; if ($vsss instanceof HandleException) { - $result["exception"] = $vsss->class_name; - $result["exception_method"] = $vsss->method; + $result["exceptions"][$vsss->class_name] = $vsss->method; } } } - ZMBuf::$events[MiddlewareClass::class]["\\".$reflection_class->getName()] = $result; + ZMBuf::$events[MiddlewareClass::class][$result["name"]] = $result; continue 2; } } @@ -100,7 +107,7 @@ class AnnotationParser elseif ($vss instanceof CQBefore) ZMBuf::$events[CQBefore::class][$vss->cq_event][] = $vss; elseif ($vss instanceof CQAfter) ZMBuf::$events[CQAfter::class][$vss->cq_event][] = $vss; elseif ($vss instanceof OnStart) ZMBuf::$events[OnStart::class][] = $vss; - elseif ($vss instanceof Middleware){ + elseif ($vss instanceof Middleware) { ZMBuf::$events[MiddlewareInterface::class][$vss->class][$vss->method] = $vss->middleware; } } @@ -307,4 +314,4 @@ class AnnotationParser $tree[] = &$items[$item['id']]; return $tree; } -} \ No newline at end of file +} diff --git a/src/ZM/Event/EventHandler.php b/src/ZM/Event/EventHandler.php index d1bbdde1..63f5d493 100644 --- a/src/ZM/Event/EventHandler.php +++ b/src/ZM/Event/EventHandler.php @@ -5,6 +5,7 @@ namespace ZM\Event; use Co; +use Error; use Exception; use Framework\Console; use Framework\ZMBuf; @@ -28,6 +29,9 @@ class EventHandler ZMUtil::stop(); return; + } catch(Error $e) { + var_export($e); + ZMUtil::stop(); } break; case "message": @@ -112,4 +116,4 @@ class EventHandler ZMBuf::unsetByValue("sent_api", $req["echo"]); } } -} \ No newline at end of file +} diff --git a/src/ZM/Event/Swoole/RequestEvent.php b/src/ZM/Event/Swoole/RequestEvent.php index 919b7b36..6e56be3a 100644 --- a/src/ZM/Event/Swoole/RequestEvent.php +++ b/src/ZM/Event/Swoole/RequestEvent.php @@ -5,6 +5,7 @@ namespace ZM\Event\Swoole; use Closure; +use Doctrine\Common\Annotations\AnnotationException; use Exception; use Framework\ZMBuf; use Swoole\Http\Request; @@ -87,10 +88,13 @@ class RequestEvent implements SwooleEvent return $this; } + set_coroutine_params(["request" => $this->request, "response" => $this->response, "params" => $params]); + if (in_array(strtoupper($this->request->server["request_method"]), $node["request_method"] ?? [])) { //判断目标方法在不在里面 $c_name = $node["class"]; if (isset(ZMBuf::$events[MiddlewareInterface::class][$c_name][$node["method"]])) { $middleware = ZMBuf::$events[MiddlewareInterface::class][$c_name][$node["method"]]; + if(!isset(ZMBuf::$events[MiddlewareClass::class][$middleware])) throw new AnnotationException("Annotation parse error: Unknown MiddlewareClass named \"{$middleware}\"!"); $middleware = ZMBuf::$events[MiddlewareClass::class][$middleware]; $before = $middleware["class"]; $r = new $before(); @@ -107,11 +111,14 @@ class RequestEvent implements SwooleEvent if (is_string($result) && !$this->response->isEnd()) $this->response->end($result); if (!$this->response->isEnd()) goto eventCall; } catch (Exception $e) { - if (!isset($middleware["exception"])) throw $e; - if ($e instanceof $middleware["exception"]) { - call_user_func([$r, $middleware["exception_method"]], $e, $this->request, $this->response, $params); - return $this; - } else throw $e; + if (!isset($middleware["exceptions"])) throw $e; + foreach($middleware["exceptions"] as $name => $method) { + if($e instanceof $name) { + call_user_func([$r, $method], $e, $this->request, $this->response, $params); + return $this; + } + } + throw $e; } } if (isset($middleware["after"])) { @@ -178,4 +185,4 @@ class RequestEvent implements SwooleEvent } return true; } -} \ No newline at end of file +} diff --git a/src/ZM/Http/MiddlewareInterface.php b/src/ZM/Http/MiddlewareInterface.php index f8635563..79c00865 100644 --- a/src/ZM/Http/MiddlewareInterface.php +++ b/src/ZM/Http/MiddlewareInterface.php @@ -6,5 +6,5 @@ namespace ZM\Http; interface MiddlewareInterface { - -} \ No newline at end of file + public function getName(); +} diff --git a/src/ZM/Http/Response.php b/src/ZM/Http/Response.php index 7eae9d97..39dd623d 100644 --- a/src/ZM/Http/Response.php +++ b/src/ZM/Http/Response.php @@ -157,6 +157,11 @@ class Response public function isEnd() { return $this->is_end; } + public function endWithStatus($status_code = 200, $content = null){ + $this->status($status_code); + $this->end($content); + } + /** * @param $filename * @param $offset @@ -226,4 +231,4 @@ class Response } -} \ No newline at end of file +} diff --git a/src/ZM/Utils/Context.php b/src/ZM/Utils/Context.php new file mode 100644 index 00000000..f633240f --- /dev/null +++ b/src/ZM/Utils/Context.php @@ -0,0 +1,71 @@ +server = $param0["server"]; + if (isset($param0["frame"])) $this->frame = $param0["frame"]; + if (isset($param0["data"])) $this->data = $param0["data"]; + if (isset($param0["request"])) $this->request = $param0["request"]; + if (isset($param0["response"])) $this->response = $param0["response"]; + $this->cid = $cid; + } + + /** + * @return swoole_server|null + */ + public function getServer() { + return $this->server; + } + + /** + * @return Frame|null + */ + public function getFrame() { + return $this->frame; + } + + /** + * @return array|null + */ + public function getData() { + return $this->data; + } + + /** + * @return Request|null + */ + public function getRequest() { + return $this->request; + } + + /** + * @return Response|null + */ + public function getResponse() { + return $this->response; + } + + /** + * @return int|null + */ + public function getCid() { + return $this->cid; + } +}