Files
zhamao-framework/src/ZM/Event/CQ/RequestEvent.php

104 lines
3.3 KiB
PHP
Raw Normal View History

2020-03-02 16:14:20 +08:00
<?php
namespace ZM\Event\CQ;
2020-05-08 16:37:38 +08:00
use Doctrine\Common\Annotations\AnnotationException;
2020-03-02 16:14:20 +08:00
use Framework\ZMBuf;
use ZM\Annotation\CQ\CQAfter;
use ZM\Annotation\CQ\CQBefore;
use ZM\Annotation\CQ\CQRequest;
2020-03-09 00:33:04 +08:00
use ZM\Connection\CQConnection;
2020-05-08 16:37:38 +08:00
use ZM\Event\EventHandler;
2020-03-02 16:14:20 +08:00
use ZM\Exception\WaitTimeoutException;
use ZM\ModBase;
use ZM\ModHandleType;
class RequestEvent
{
private $data;
2020-03-09 00:33:04 +08:00
/** @var CQConnection */
private $connection;
2020-03-02 16:14:20 +08:00
private $circle;
2020-03-09 00:33:04 +08:00
public function __construct($data, $connection, $circle = 0) {
2020-03-02 16:14:20 +08:00
$this->data = $data;
2020-03-09 00:33:04 +08:00
$this->connection = $connection;
2020-03-02 16:14:20 +08:00
$this->circle = $circle;
}
2020-05-08 16:37:38 +08:00
/**
* @return bool
* @throws AnnotationException
*/
2020-03-02 16:14:20 +08:00
public function onBefore() {
2020-03-09 00:33:04 +08:00
foreach (ZMBuf::$events[CQBefore::class]["request"] ?? [] as $v) {
2020-03-02 16:14:20 +08:00
$c = $v->class;
2020-05-08 16:37:38 +08:00
EventHandler::callWithMiddleware(
$c,
$v->method,
["data" => context()->getData(), "connection" => $this->connection],
[],
function ($r) {
if(!$r) context()->setCache("block_continue", true);
}
);
if(context()->getCache("block_continue") === true) return false;
2020-03-02 16:14:20 +08:00
}
return true;
}
2020-05-08 16:37:38 +08:00
/**
* @throws AnnotationException
*/
2020-03-02 16:14:20 +08:00
public function onActivate() {
try {
/** @var ModBase[] $obj */
$obj = [];
foreach (ZMBuf::$events[CQRequest::class] ?? [] as $v) {
/** @var CQRequest $v */
if (
($v->request_type == '' || ($v->request_type != '' && $v->request_type == $this->data["request_type"])) &&
($v->sub_type == 0 || ($v->sub_type != 0 && $v->sub_type == $this->data["sub_type"])) &&
($v->user_id == 0 || ($v->user_id != 0 && $v->user_id == ($this->data["user_id"] ?? 0))) &&
($v->comment == 0 || ($v->comment != 0 && $v->comment == ($this->data["comment"] ?? 0)))) {
$c = $v->class;
if (!isset($obj[$c]))
$obj[$c] = new $c([
"data" => $this->data,
2020-03-09 00:33:04 +08:00
"connection" => $this->connection
2020-03-02 16:14:20 +08:00
], ModHandleType::CQ_REQUEST);
2020-05-08 16:37:38 +08:00
EventHandler::callWithMiddleware($obj[$c],$v->method, [], [], function($r) {
if (is_string($r)) context()->reply($r);
});
2020-05-06 17:26:50 +08:00
if (context()->getCache("block_continue") === true) return;
2020-03-02 16:14:20 +08:00
}
}
} catch (WaitTimeoutException $e) {
$e->module->finalReply($e->getMessage());
}
}
2020-05-08 16:37:38 +08:00
/**
* @return bool
* @throws AnnotationException
*/
2020-03-02 16:14:20 +08:00
public function onAfter() {
2020-03-09 00:33:04 +08:00
foreach (ZMBuf::$events[CQAfter::class]["request"] ?? [] as $v) {
2020-03-02 16:14:20 +08:00
$c = $v->class;
2020-05-08 16:37:38 +08:00
EventHandler::callWithMiddleware(
$c,
$v->method,
["data" => context()->getData(), "connection" => $this->connection],
[],
function ($r) {
if(!$r) context()->setCache("block_continue", true);
}
);
if(context()->getCache("block_continue") === true) return false;
2020-03-02 16:14:20 +08:00
}
return true;
}
2020-05-06 17:26:50 +08:00
}