update to 1.3.0 version totally.

This commit is contained in:
whale
2020-05-08 16:37:38 +08:00
parent a1b013ee53
commit acb4bdf9b4
19 changed files with 425 additions and 171 deletions

View File

@@ -53,9 +53,12 @@ class MessageEvent implements SwooleEvent
foreach (ZMBuf::$events[SwooleEventAt::class] ?? [] as $v) {
if (strtolower($v->type) == "message" && $this->parseSwooleRule($v)) {
$c = $v->class;
/** @var ModBase $class */
$class = new $c(["server" => $this->server, "frame" => $this->frame, "connection" => $conn], ModHandleType::SWOOLE_MESSAGE);
call_user_func_array([$class, $v->method], [$conn]);
EventHandler::callWithMiddleware(
$c,
$v->method,
["server" => $this->server, "frame" => $this->frame, "connection" => $conn],
[$conn]
);
if (context()->getCache("block_continue") === true) break;
}
}

View File

@@ -5,18 +5,14 @@ namespace ZM\Event\Swoole;
use Closure;
use Doctrine\Common\Annotations\AnnotationException;
use Exception;
use Framework\Console;
use Framework\ZMBuf;
use Swoole\Http\Request;
use ZM\Annotation\Http\MiddlewareClass;
use ZM\Annotation\Swoole\SwooleEventAfter;
use ZM\Annotation\Swoole\SwooleEventAt;
use ZM\Http\MiddlewareInterface;
use ZM\Event\EventHandler;
use ZM\Http\Response;
use ZM\ModBase;
use ZM\ModHandleType;
use Framework\DataProvider;
use ZM\Utils\ZMUtil;
@@ -120,57 +116,25 @@ class RequestEvent implements SwooleEvent
return $this;
}
context()->setCache("params", $params);
context()->setCache("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();
$before_result = true;
if (isset($middleware["before"])) {
$before_result = call_user_func([$r, $middleware["before"]], $this->request, $this->response, $params);
if ($before_result !== false) $before_result = true;
EventHandler::callWithMiddleware(
$c_name,
$node["method"],
["request" => $this->request, "response" => &$this->response, "params" => $params],
[$params],
function ($result) {
if (is_string($result) && !$this->response->isEnd()) $this->response->end($result);
if ($this->response->isEnd()) context()->setCache("block_continue", true);
}
if ($before_result) {
try {
/** @var ModBase $class */
$class = new $c_name(["request" => $this->request, "response" => $this->response, "params" => $params], ModHandleType::SWOOLE_REQUEST);
$result = call_user_func_array([$class, $node["method"]], [$params]);
if (is_string($result) && !$this->response->isEnd()) $this->response->end($result);
if (!$this->response->isEnd()) goto eventCall;
} catch (Exception $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"])) {
call_user_func([$r, $middleware["after"]], $this->request, $this->response, $params);
return $this;
}
} else {
/** @var ModBase $class */
$class = new $c_name(["request" => $this->request, "response" => $this->response, "params" => $params], ModHandleType::SWOOLE_REQUEST);
$r = call_user_func_array([$class, $node["method"]], [$params]);
if (is_string($r) && !$this->response->isEnd()) $this->response->end($r);
if (!$this->response->isEnd()) goto eventCall;
}
);
}
eventCall:
foreach (ZMBuf::$events[SwooleEventAt::class] ?? [] as $v) {
if (strtolower($v->type) == "request" && $this->parseSwooleRule($v)) {
$c = $v->class;
$class = new $c(["request" => $this->request, "response" => $this->response]);
$r = call_user_func_array([$class, $v->method], []);
EventHandler::callWithMiddleware($c, $v->method, ["request" => $this->request, "response" => $this->response], []);
if (context()->getCache("block_continue") === true) break;
}
}

View File

@@ -4,11 +4,13 @@
namespace ZM\Event\Swoole;
use Doctrine\Common\Annotations\AnnotationException;
use Framework\ZMBuf;
use Swoole\Server;
use ZM\Annotation\Swoole\SwooleEventAfter;
use ZM\Annotation\Swoole\SwooleEventAt;
use ZM\Connection\ConnectionManager;
use ZM\Event\EventHandler;
use ZM\ModBase;
use ZM\ModHandleType;
use ZM\Utils\ZMUtil;
@@ -26,6 +28,7 @@ class WSCloseEvent implements SwooleEvent
/**
* @inheritDoc
* @throws AnnotationException
*/
public function onActivate() {
ZMUtil::checkWait();
@@ -34,9 +37,8 @@ class WSCloseEvent implements SwooleEvent
foreach(ZMBuf::$events[SwooleEventAt::class] ?? [] as $v) {
if(strtolower($v->type) == "close" && $this->parseSwooleRule($v)) {
$c = $v->class;
$class = new $c(["server" => $this->server, "fd" => $this->fd], ModHandleType::SWOOLE_CLOSE);
call_user_func_array([$class, $v->method], []);
if($class->block_continue) break;
EventHandler::callWithMiddleware($c, $v->method, ["server" => $this->server, "fd" => $this->fd], []);
if(context()->getCache("block_continue") === true) break;
}
}
return $this;
@@ -44,14 +46,13 @@ class WSCloseEvent implements SwooleEvent
/**
* @inheritDoc
* @throws AnnotationException
*/
public function onAfter() {
foreach (ZMBuf::$events[SwooleEventAfter::class] ?? [] as $v) {
if (strtolower($v->type) == "close" && $this->parseSwooleRule($v) === true) {
$c = $v->class;
/** @var ModBase $class */
$class = new $c(["server" => $this->server, "fd" => $this->fd], ModHandleType::SWOOLE_CLOSE);
call_user_func_array([$class, $v->method], []);
EventHandler::callWithMiddleware($c, $v->method, ["server" => $this->server, "fd" => $this->fd], []);
if(context()->getCache("block_continue") === true) break;
}
}

View File

@@ -5,6 +5,7 @@ namespace ZM\Event\Swoole;
use Closure;
use Doctrine\Common\Annotations\AnnotationException;
use Framework\ZMBuf;
use Swoole\Http\Request;
use Swoole\WebSocket\Server;
@@ -14,6 +15,7 @@ use ZM\Connection\ConnectionManager;
use ZM\Connection\CQConnection;
use ZM\Connection\UnknownConnection;
use ZM\Connection\WSConnection;
use ZM\Event\EventHandler;
use ZM\ModBase;
use ZM\ModHandleType;
use ZM\Utils\ZMUtil;
@@ -40,6 +42,7 @@ class WSOpenEvent implements SwooleEvent
/**
* @inheritDoc
* @throws AnnotationException
*/
public function onActivate() {
ZMUtil::checkWait();
@@ -59,8 +62,12 @@ class WSOpenEvent implements SwooleEvent
foreach (ZMBuf::$events[SwooleEventAt::class] ?? [] as $v) {
if (strtolower($v->type) == "open" && $this->parseSwooleRule($v) === true) {
$c = $v->class;
$class = new $c(["server" => $this->server, "request" => $this->request, "connection" => $this->conn], ModHandleType::SWOOLE_OPEN);
call_user_func_array([$class, $v->method], [$this->conn]);
EventHandler::callWithMiddleware(
$c,
$v->method,
["server" => $this->server, "request" => $this->request, "connection" => $this->conn],
[$this->conn]
);
if (context()->getCache("block_continue") === true) break;
}
}

View File

@@ -22,9 +22,8 @@ use Framework\Console;
use Framework\GlobalConfig;
use Framework\ZMBuf;
use Swoole\Server;
use ZM\Event\EventHandler;
use ZM\Exception\DbException;
use ZM\ModBase;
use ZM\ModHandleType;
use Framework\DataProvider;
use ZM\Utils\SQLPool;
use ZM\Utils\ZMUtil;
@@ -90,25 +89,25 @@ class WorkerStartEvent implements SwooleEvent
return $this;
}
/**
* @return WorkerStartEvent
* @throws AnnotationException
*/
public function onAfter(): WorkerStartEvent {
foreach (ZMBuf::get("wait_start") as $v) {
Coroutine::resume($v);
}
ZMBuf::unsetCache("wait_start");
set_coroutine_params(["server" => $this->server, "worker_id" => $this->worker_id]);
foreach (ZMBuf::$events[OnStart::class] ?? [] as $v) {
$class_name = $v->class;
/** @var ModBase $class */
$class = new $class_name(["server" => $this->server, "worker_id" => $this->worker_id], ModHandleType::SWOOLE_WORKER_START);
call_user_func_array([$class, $v->method], []);
EventHandler::callWithMiddleware($class_name, $v->method, ["server" => $this->server, "worker_id" => $this->worker_id], []);
}
set_coroutine_params(["server" => $this->server, "worker_id" => $this->worker_id]);
foreach (ZMBuf::$events[SwooleEventAfter::class] ?? [] as $v) {
/** @var AnnotationBase $v */
if (strtolower($v->type) == "workerstart") {
$class_name = $v->class;
/** @var ModBase $class */
$class = new $class_name(["server" => $this->server, "worker_id" => $this->worker_id], ModHandleType::SWOOLE_WORKER_START);
call_user_func_array([$class, $v->method], []);
EventHandler::callWithMiddleware($class_name, $v->method, ["server" => $this->server, "worker_id" => $this->worker_id], []);
if (context()->getCache("block_continue") === true) break;
}
}