mirror of
https://github.com/zhamao-robot/zhamao-framework.git
synced 2026-03-17 20:54:52 +08:00
add di support for event handlers
This commit is contained in:
parent
420b8e6d67
commit
0dd61a7270
@ -18,6 +18,7 @@ use ZM\API\TuringAPI;
|
||||
use ZM\Config\ZMConfig;
|
||||
use ZM\ConnectionManager\ConnectionObject;
|
||||
use ZM\Console\Console;
|
||||
use ZM\Context\Context;
|
||||
use ZM\Event\EventDispatcher;
|
||||
use ZM\Exception\InterruptException;
|
||||
use ZM\Module\QQBot;
|
||||
@ -69,7 +70,6 @@ class Hello
|
||||
*/
|
||||
public function hello()
|
||||
{
|
||||
zm_dump(resolve('swoole_server')->worker_id);
|
||||
return '你好啊,我是由炸毛框架构建的机器人!';
|
||||
}
|
||||
|
||||
@ -141,7 +141,6 @@ class Hello
|
||||
*/
|
||||
public function randNum()
|
||||
{
|
||||
zm_dump(resolve('swoole_server')->worker_id);
|
||||
// 获取第一个数字类型的参数
|
||||
$num1 = ctx()->getNumArg('请输入第一个数字');
|
||||
// 获取第二个数字类型的参数
|
||||
@ -192,13 +191,7 @@ class Hello
|
||||
*/
|
||||
public function onConnect(ConnectionObject $conn)
|
||||
{
|
||||
zm_dump(resolve('path.base'));
|
||||
zm_dump(resolve('path.config'));
|
||||
zm_dump(resolve('app')::$server->worker_id);
|
||||
Console::info('机器人 ' . $conn->getOption('connect_id') . ' 已连接!');
|
||||
container()->bind('cid', function () use ($conn) {
|
||||
return resolve('app')::$server->worker_id . $conn->getOption('connect_id');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@ -207,7 +200,6 @@ class Hello
|
||||
*/
|
||||
public function onDisconnect(ConnectionObject $conn)
|
||||
{
|
||||
zm_dump(resolve('cid'));
|
||||
Console::info('机器人 ' . $conn->getOption('connect_id') . ' 已断开连接!');
|
||||
}
|
||||
|
||||
@ -255,29 +247,14 @@ class Hello
|
||||
}
|
||||
|
||||
/*
|
||||
* @OnStart(-1)
|
||||
*/
|
||||
#[OnStart(-1)]
|
||||
public function initContainer()
|
||||
{
|
||||
$worker_container = new WorkerContainer();
|
||||
$worker_container->bindIf('echo', function ($container, $parameters) {
|
||||
return 'Hello, ' . $parameters[0];
|
||||
});
|
||||
container()->bind(ContextInterface::class, function () {
|
||||
return ctx();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 容器呐
|
||||
* 欢迎来到容器时代
|
||||
*
|
||||
* @CQCommand("container")
|
||||
* @param Context $context 通过依赖注入实现的
|
||||
*
|
||||
* @CQCommand("容器你好")
|
||||
*/
|
||||
#[CQCommand('container')]
|
||||
public function container(): string
|
||||
public function welcomeToContainerAge(Context $context)
|
||||
{
|
||||
resolve(ContextInterface::class)->reply('Good'); // ctx()->reply('good')
|
||||
return resolve('echo', ['hello']); // Hello, hello
|
||||
$context->reply('欢迎来到容器时代');
|
||||
}
|
||||
}
|
||||
|
||||
@ -188,7 +188,9 @@ class EventDispatcher
|
||||
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(', ', array_map(function ($x) { return $x->middleware; }, $middlewares)));
|
||||
Console::verbose("[事件分发{$this->eid}] " . $q_c . '::' . $q_f . ' 方法还绑定了 Middleware:' . implode(', ', array_map(function ($x) {
|
||||
return $x->middleware;
|
||||
}, $middlewares)));
|
||||
}
|
||||
$before_result = true;
|
||||
$r = [];
|
||||
@ -231,7 +233,7 @@ class EventDispatcher
|
||||
if ($this->log) {
|
||||
Console::verbose("[事件分发{$this->eid}] 正在执行方法 " . $q_c . '::' . $q_f . ' ...');
|
||||
}
|
||||
$this->store = $q_o->{$q_f}(...$params);
|
||||
$this->store = container()->call([$q_o, $q_f], $params);
|
||||
} catch (Exception $e) {
|
||||
if ($e instanceof InterruptException) {
|
||||
if ($this->log) {
|
||||
@ -283,7 +285,7 @@ class EventDispatcher
|
||||
if ($this->log) {
|
||||
Console::verbose("[事件分发{$this->eid}] 正在执行方法 " . $q_c . '::' . $q_f . ' ...');
|
||||
}
|
||||
$this->store = $q_o->{$q_f}(...$params);
|
||||
$this->store = container()->call([$q_o, $q_f], $params);
|
||||
$this->status = self::STATUS_NORMAL;
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -13,6 +13,7 @@ use ZM\Annotation\Swoole\OnOpenEvent;
|
||||
use ZM\Annotation\Swoole\OnSwooleEvent;
|
||||
use ZM\Annotation\Swoole\SwooleHandler;
|
||||
use ZM\Config\ZMConfig;
|
||||
use ZM\ConnectionManager\ConnectionObject;
|
||||
use ZM\ConnectionManager\ManagerGM;
|
||||
use ZM\Console\Console;
|
||||
use ZM\Context\Context;
|
||||
@ -30,6 +31,7 @@ class OnOpen implements SwooleEvent
|
||||
{
|
||||
Console::debug('Calling Swoole "open" event from fd=' . $request->fd);
|
||||
unset(Context::$context[Coroutine::getCid()]);
|
||||
|
||||
$type = strtolower($request->header['x-client-role'] ?? $request->get['type'] ?? '');
|
||||
$access_token = explode(' ', $request->header['authorization'] ?? '')[1] ?? $request->get['token'] ?? '';
|
||||
$token = ZMConfig::get('global', 'access_token');
|
||||
@ -52,6 +54,8 @@ class OnOpen implements SwooleEvent
|
||||
set_coroutine_params(['server' => $server, 'request' => $request, 'connection' => $conn, 'fd' => $request->fd]);
|
||||
$conn->setOption('connect_id', strval($request->header['x-self-id'] ?? ''));
|
||||
|
||||
container()->instance(ConnectionObject::class, $conn);
|
||||
|
||||
$dispatcher1 = new EventDispatcher(OnOpenEvent::class);
|
||||
$dispatcher1->setRuleFunction(function ($v) {
|
||||
return ctx()->getConnection()->getName() == $v->connect_type && eval('return ' . $v->getRule() . ';');
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user