mirror of
https://github.com/zhamao-robot/zhamao-framework.git
synced 2026-07-22 16:15:34 +08:00
update to 2.5.0-b2 (build 409)
This commit is contained in:
@@ -137,17 +137,18 @@ class EventDispatcher
|
||||
if ($this->log) Console::verbose("[事件分发{$this->eid}] " . $q_c . "::" . $q_f . " 方法下的 ruleFunc 为真,继续执行方法本身 ...");
|
||||
if (isset(EventManager::$middleware_map[$q_c][$q_f])) {
|
||||
$middlewares = EventManager::$middleware_map[$q_c][$q_f];
|
||||
if ($this->log) Console::verbose("[事件分发{$this->eid}] " . $q_c . "::" . $q_f . " 方法还绑定了 Middleware:" . implode(", ", $middlewares));
|
||||
if ($this->log) Console::verbose("[事件分发{$this->eid}] " . $q_c . "::" . $q_f . " 方法还绑定了 Middleware:" . implode(", ", array_map(function($x){ return $x->middleware; }, $middlewares)));
|
||||
$before_result = true;
|
||||
$r = [];
|
||||
foreach ($middlewares as $k => $middleware) {
|
||||
if (!isset(EventManager::$middlewares[$middleware])) throw new AnnotationException("Annotation parse error: Unknown MiddlewareClass named \"{$middleware}\"!");
|
||||
$middleware_obj = EventManager::$middlewares[$middleware];
|
||||
if (!isset(EventManager::$middlewares[$middleware->middleware])) throw new AnnotationException("Annotation parse error: Unknown MiddlewareClass named \"{$middleware->middleware}\"!");
|
||||
$middleware_obj = EventManager::$middlewares[$middleware->middleware];
|
||||
$before = $middleware_obj["class"];
|
||||
//var_dump($middleware_obj);
|
||||
$r[$k] = new $before();
|
||||
$r[$k]->class = $q_c;
|
||||
$r[$k]->method = $q_f;
|
||||
$r[$k]->middleware = $middleware;
|
||||
if (isset($middleware_obj["before"])) {
|
||||
if ($this->log) Console::verbose("[事件分发{$this->eid}] Middleware 存在前置事件,执行中 ...");
|
||||
$rs = $middleware_obj["before"];
|
||||
@@ -173,7 +174,7 @@ class EventDispatcher
|
||||
}
|
||||
if ($this->log) Console::verbose("[事件分发{$this->eid}] 方法 " . $q_c . "::" . $q_f . " 执行过程中抛出了异常,正在倒序查找 Middleware 中的捕获方法 ...");
|
||||
for ($i = count($middlewares) - 1; $i >= 0; --$i) {
|
||||
$middleware_obj = EventManager::$middlewares[$middlewares[$i]];
|
||||
$middleware_obj = EventManager::$middlewares[$middlewares[$i]->middleware];
|
||||
if (!isset($middleware_obj["exceptions"])) continue;
|
||||
foreach ($middleware_obj["exceptions"] as $name => $method) {
|
||||
if ($e instanceof $name) {
|
||||
@@ -186,7 +187,7 @@ class EventDispatcher
|
||||
throw $e;
|
||||
}
|
||||
for ($i = count($middlewares) - 1; $i >= 0; --$i) {
|
||||
$middleware_obj = EventManager::$middlewares[$middlewares[$i]];
|
||||
$middleware_obj = EventManager::$middlewares[$middlewares[$i]->middleware];
|
||||
if (isset($middleware_obj["after"], $r[$i])) {
|
||||
if ($this->log) Console::verbose("[事件分发{$this->eid}] Middleware 存在后置事件,执行中 ...");
|
||||
$r[$i]->{$middleware_obj["after"]}(...$params);
|
||||
@@ -207,4 +208,18 @@ class EventDispatcher
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getEid(): int {
|
||||
return $this->eid;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getClass(): string {
|
||||
return $this->class;
|
||||
}
|
||||
}
|
||||
|
||||
45
src/ZM/Event/EventTracer.php
Normal file
45
src/ZM/Event/EventTracer.php
Normal file
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace ZM\Event;
|
||||
|
||||
|
||||
use ZM\Annotation\AnnotationBase;
|
||||
|
||||
class EventTracer
|
||||
{
|
||||
/**
|
||||
* 获取当前注解事件的注解类,如CQCommand对象
|
||||
* @return AnnotationBase|null
|
||||
*/
|
||||
public static function getCurrentEvent() {
|
||||
$list = debug_backtrace();
|
||||
foreach ($list as $v) {
|
||||
if ((($v["object"] ?? null) instanceof EventDispatcher) && $v["function"] == "dispatchEvent") {
|
||||
return $v["args"][0];
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前注解事件的中间件列表
|
||||
* @return array|mixed|null
|
||||
*/
|
||||
public static function getCurrentEventMiddlewares() {
|
||||
$current_event = self::getCurrentEvent();
|
||||
if (!isset($current_event->class, $current_event->method)) return null;
|
||||
return EventManager::$middleware_map[$current_event->class][$current_event->method] ?? [];
|
||||
}
|
||||
|
||||
public static function getEventTraceList() {
|
||||
$result = [];
|
||||
$list = debug_backtrace();
|
||||
foreach ($list as $v) {
|
||||
if ((($v["object"] ?? null) instanceof EventDispatcher) && $v["function"] == "dispatchEvent") {
|
||||
$result[] = $v["args"][0];
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,7 @@ namespace ZM\Event\SwooleEvent;
|
||||
use Swoole\Server;
|
||||
use Swoole\Timer;
|
||||
use ZM\Annotation\Swoole\SwooleHandler;
|
||||
use ZM\ConnectionManager\ManagerGM;
|
||||
use ZM\Console\Console;
|
||||
use ZM\Event\SwooleEvent;
|
||||
|
||||
@@ -19,6 +20,10 @@ class OnWorkerExit implements SwooleEvent
|
||||
{
|
||||
public function onCall(Server $server, $worker_id) {
|
||||
Timer::clearAll();
|
||||
foreach($server->connections as $v) {
|
||||
$server->close($v);
|
||||
Console::info("Closing connection #".$v);
|
||||
}
|
||||
Console::info("正在结束 Worker #".$worker_id.",进程内可能有事务在运行...");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user