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

80 lines
2.5 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\CQBefore;
use ZM\Annotation\CQ\CQMetaEvent;
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 MetaEvent
{
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]["meta_event"] ?? [] 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[CQMetaEvent::class] ?? [] as $v) {
/** @var CQMetaEvent $v */
if (
($v->meta_event_type == '' || ($v->meta_event_type != '' && $v->meta_event_type == $this->data["meta_event_type"])) &&
($v->sub_type == 0 || ($v->sub_type != 0 && $v->sub_type == $this->data["sub_type"]))) {
$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_META_EVENT);
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-06 17:26:50 +08:00
}