add di support for event handlers

This commit is contained in:
sunxyw
2022-04-10 21:46:53 +08:00
parent 420b8e6d67
commit 0dd61a7270
3 changed files with 16 additions and 33 deletions

View File

@@ -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;
}

View File

@@ -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() . ';');