extend event compatibility

This commit is contained in:
whale
2020-03-09 00:33:04 +08:00
parent efce1767e6
commit 66a50f0a87
8 changed files with 54 additions and 79 deletions

View File

@@ -5,13 +5,15 @@ namespace ZM\Event\CQ;
use Co;
use Framework\Console;
use Framework\ZMBuf;
use ZM\Annotation\CQ\CQAfter;
use ZM\Annotation\CQ\CQBefore;
use ZM\Annotation\CQ\CQCommand;
use ZM\Annotation\CQ\CQMessage;
use ZM\Connection\ConnectionManager;
use ZM\Connection\WSConnection;
use ZM\Exception\WaitTimeoutException;
use ZM\Http\Response;
use ZM\ModBase;
use ZM\ModHandleType;
@@ -20,25 +22,21 @@ class MessageEvent
private $function_call = false;
private $data;
private $circle;
/**
* @var \ZM\Event\Swoole\MessageEvent
*/
private $swoole_event;
/** @var WSConnection|Response */
private $connection;
public function __construct($data, \ZM\Event\Swoole\MessageEvent $event, $circle = 0) {
public function __construct($data, $conn_or_response, $circle = 0) {
$this->data = $data;
$this->swoole_event = $event;
$this->connection = $conn_or_response;
$this->circle = $circle;
}
public function onBefore() {
foreach (ZMBuf::$events[CQBefore::class][CQMessage::class] ?? [] as $v) {
foreach (ZMBuf::$events[CQBefore::class]["message"] ?? [] as $v) {
$c = $v->class;
$class = new $c([
"data" => $this->data,
"frame" => $this->swoole_event->frame,
"server" => $this->swoole_event->server,
"connection" => ConnectionManager::get($this->swoole_event->frame->fd)
"connection" => $this->connection
], ModHandleType::CQ_MESSAGE);
$r = call_user_func_array([$class, $v->method], []);
if (!$r || $class->block_continue) return false;
@@ -80,9 +78,7 @@ class MessageEvent
if (!isset($obj[$c]))
$obj[$c] = new $c([
"data" => $this->data,
"frame" => $this->swoole_event->frame,
"server" => $this->swoole_event->server,
"connection" => ConnectionManager::get($this->swoole_event->frame->fd)
"connection" => $this->connection
], ModHandleType::CQ_MESSAGE);
if ($word[0] != "" && $v->match == $word[0]) {
$r = call_user_func([$obj[$c], $v->method], $word);
@@ -110,9 +106,7 @@ class MessageEvent
if (!isset($obj[$c]))
$obj[$c] = new $c([
"data" => $this->data,
"frame" => $this->swoole_event->frame,
"server" => $this->swoole_event->server,
"connection" => ConnectionManager::get($this->swoole_event->frame->fd)
"connection" => $this->connection
], ModHandleType::CQ_MESSAGE);
$r = call_user_func([$obj[$c], $v->method], $this->data["message"]);
if (is_string($r)) $obj[$c]->reply($r);
@@ -129,13 +123,11 @@ class MessageEvent
* 在调用完事件后执行的
*/
public function onAfter() {
foreach (ZMBuf::$events[CQAfter::class][CQMessage::class] ?? [] as $v) {
foreach (ZMBuf::$events[CQAfter::class]["message"] ?? [] as $v) {
$c = $v->class;
$class = new $c([
"data" => $this->data,
"frame" => $this->swoole_event->frame,
"server" => $this->swoole_event->server,
"connection" => ConnectionManager::get($this->swoole_event->frame->fd)
"connection" => $this->connection
], ModHandleType::CQ_MESSAGE);
$r = call_user_func_array([$class, $v->method], []);
if (!$r || $class->block_continue) return false;

View File

@@ -8,6 +8,7 @@ use Framework\ZMBuf;
use ZM\Annotation\CQ\CQBefore;
use ZM\Annotation\CQ\CQMetaEvent;
use ZM\Connection\ConnectionManager;
use ZM\Connection\CQConnection;
use ZM\Exception\WaitTimeoutException;
use ZM\ModBase;
use ZM\ModHandleType;
@@ -15,25 +16,23 @@ use ZM\ModHandleType;
class MetaEvent
{
private $data;
/** @var \ZM\Event\Swoole\MessageEvent */
private $swoole_event;
/** @var CQConnection */
private $connection;
private $circle;
public function __construct($data, \ZM\Event\Swoole\MessageEvent $event, $circle = 0) {
public function __construct($data, $connection, $circle = 0) {
$this->data = $data;
$this->swoole_event = $event;
$this->connection = $connection;
$this->circle = $circle;
}
public function onBefore() {
foreach (ZMBuf::$events[CQBefore::class][CQMetaEvent::class] ?? [] as $v) {
foreach (ZMBuf::$events[CQBefore::class]["meta_event"] ?? [] as $v) {
$c = $v->class;
/** @var CQMetaEvent $v */
$class = new $c([
"data" => $this->data,
"frame" => $this->swoole_event->frame,
"server" => $this->swoole_event->server,
"connection" => ConnectionManager::get($this->swoole_event->frame->fd)
"connection" => $this->connection
], ModHandleType::CQ_META_EVENT);
$r = call_user_func_array([$class, $v->method], []);
if (!$r || $class->block_continue) return false;
@@ -55,9 +54,7 @@ class MetaEvent
if (!isset($obj[$c]))
$obj[$c] = new $c([
"data" => $this->data,
"frame" => $this->swoole_event->frame,
"server" => $this->swoole_event->server,
"connection" => ConnectionManager::get($this->swoole_event->frame->fd)
"connection" => $this->connection
], ModHandleType::CQ_META_EVENT);
$r = call_user_func([$obj[$c], $v->method]);
if (is_string($r)) $obj[$c]->reply($r);

View File

@@ -8,7 +8,7 @@ use Framework\ZMBuf;
use ZM\Annotation\CQ\CQAfter;
use ZM\Annotation\CQ\CQBefore;
use ZM\Annotation\CQ\CQNotice;
use ZM\Connection\ConnectionManager;
use ZM\Connection\CQConnection;
use ZM\Exception\WaitTimeoutException;
use ZM\ModBase;
use ZM\ModHandleType;
@@ -16,25 +16,23 @@ use ZM\ModHandleType;
class NoticeEvent
{
private $data;
/** @var \ZM\Event\Swoole\MessageEvent */
private $swoole_event;
/** @var CQConnection */
private $connection;
private $circle;
public function __construct($data, \ZM\Event\Swoole\MessageEvent $event, $circle = 0) {
public function __construct($data, $connection, $circle = 0) {
$this->data = $data;
$this->swoole_event = $event;
$this->connection = $connection;
$this->circle = $circle;
}
public function onBefore() {
foreach (ZMBuf::$events[CQBefore::class][CQNotice::class] ?? [] as $v) {
foreach (ZMBuf::$events[CQBefore::class]["notice"] ?? [] as $v) {
$c = $v->class;
/** @var CQNotice $v */
$class = new $c([
"data" => $this->data,
"frame" => $this->swoole_event->frame,
"server" => $this->swoole_event->server,
"connection" => ConnectionManager::get($this->swoole_event->frame->fd)
"connection" => $this->connection
], ModHandleType::CQ_NOTICE);
$r = call_user_func_array([$class, $v->method], []);
if (!$r || $class->block_continue) return false;
@@ -57,9 +55,7 @@ class NoticeEvent
if (!isset($obj[$c]))
$obj[$c] = new $c([
"data" => $this->data,
"frame" => $this->swoole_event->frame,
"server" => $this->swoole_event->server,
"connection" => ConnectionManager::get($this->swoole_event->frame->fd)
"connection" => $this->connection
], ModHandleType::CQ_NOTICE);
$r = call_user_func([$obj[$c], $v->method]);
if (is_string($r)) $obj[$c]->reply($r);
@@ -72,13 +68,11 @@ class NoticeEvent
}
public function onAfter() {
foreach (ZMBuf::$events[CQAfter::class][CQNotice::class] ?? [] as $v) {
foreach (ZMBuf::$events[CQAfter::class]["notice"] ?? [] as $v) {
$c = $v->class;
$class = new $c([
"data" => $this->data,
"frame" => $this->swoole_event->frame,
"server" => $this->swoole_event->server,
"connection" => ConnectionManager::get($this->swoole_event->frame->fd)
"connection" => $this->connection
], ModHandleType::CQ_NOTICE);
$r = call_user_func_array([$class, $v->method], []);
if (!$r || $class->block_continue) return false;

View File

@@ -8,7 +8,7 @@ use Framework\ZMBuf;
use ZM\Annotation\CQ\CQAfter;
use ZM\Annotation\CQ\CQBefore;
use ZM\Annotation\CQ\CQRequest;
use ZM\Connection\ConnectionManager;
use ZM\Connection\CQConnection;
use ZM\Exception\WaitTimeoutException;
use ZM\ModBase;
use ZM\ModHandleType;
@@ -16,25 +16,23 @@ use ZM\ModHandleType;
class RequestEvent
{
private $data;
/** @var \ZM\Event\Swoole\MessageEvent */
private $swoole_event;
/** @var CQConnection */
private $connection;
private $circle;
public function __construct($data, \ZM\Event\Swoole\MessageEvent $event, $circle = 0) {
public function __construct($data, $connection, $circle = 0) {
$this->data = $data;
$this->swoole_event = $event;
$this->connection = $connection;
$this->circle = $circle;
}
public function onBefore() {
foreach (ZMBuf::$events[CQBefore::class][CQRequest::class] ?? [] as $v) {
foreach (ZMBuf::$events[CQBefore::class]["request"] ?? [] as $v) {
$c = $v->class;
/** @var CQRequest $v */
$class = new $c([
"data" => $this->data,
"frame" => $this->swoole_event->frame,
"server" => $this->swoole_event->server,
"connection" => ConnectionManager::get($this->swoole_event->frame->fd)
"connection" => $this->connection
], ModHandleType::CQ_REQUEST);
$r = call_user_func_array([$class, $v->method], []);
if (!$r || $class->block_continue) return false;
@@ -58,9 +56,7 @@ class RequestEvent
if (!isset($obj[$c]))
$obj[$c] = new $c([
"data" => $this->data,
"frame" => $this->swoole_event->frame,
"server" => $this->swoole_event->server,
"connection" => ConnectionManager::get($this->swoole_event->frame->fd)
"connection" => $this->connection
], ModHandleType::CQ_REQUEST);
$r = call_user_func([$obj[$c], $v->method]);
if (is_string($r)) $obj[$c]->reply($r);
@@ -73,13 +69,11 @@ class RequestEvent
}
public function onAfter() {
foreach (ZMBuf::$events[CQAfter::class][CQRequest::class] ?? [] as $v) {
foreach (ZMBuf::$events[CQAfter::class]["request"] ?? [] as $v) {
$c = $v->class;
$class = new $c([
"data" => $this->data,
"frame" => $this->swoole_event->frame,
"server" => $this->swoole_event->server,
"connection" => ConnectionManager::get($this->swoole_event->frame->fd)
"connection" => $this->connection
], ModHandleType::CQ_REQUEST);
$r = call_user_func_array([$class, $v->method], []);
if (!$r || $class->block_continue) return false;