2021-07-04 15:45:30 +08:00
|
|
|
|
<?php
|
|
|
|
|
|
|
2022-03-15 18:05:33 +08:00
|
|
|
|
declare(strict_types=1);
|
2021-07-04 15:45:30 +08:00
|
|
|
|
|
|
|
|
|
|
namespace ZM\Event;
|
|
|
|
|
|
|
|
|
|
|
|
use ZM\Annotation\AnnotationBase;
|
|
|
|
|
|
|
|
|
|
|
|
class EventTracer
|
|
|
|
|
|
{
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 获取当前注解事件的注解类,如CQCommand对象
|
2022-03-15 18:05:33 +08:00
|
|
|
|
* @return null|AnnotationBase
|
2021-07-04 15:45:30 +08:00
|
|
|
|
*/
|
2022-03-15 18:05:33 +08:00
|
|
|
|
public static function getCurrentEvent()
|
|
|
|
|
|
{
|
2021-07-04 15:45:30 +08:00
|
|
|
|
$list = debug_backtrace();
|
|
|
|
|
|
foreach ($list as $v) {
|
2022-03-15 18:05:33 +08:00
|
|
|
|
if ((($v['object'] ?? null) instanceof EventDispatcher) && $v['function'] == 'dispatchEvent') {
|
|
|
|
|
|
return $v['args'][0];
|
2021-07-04 15:45:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 获取当前注解事件的中间件列表
|
2022-03-15 18:05:33 +08:00
|
|
|
|
* @return null|array|mixed
|
2021-07-04 15:45:30 +08:00
|
|
|
|
*/
|
2022-03-15 18:05:33 +08:00
|
|
|
|
public static function getCurrentEventMiddlewares()
|
|
|
|
|
|
{
|
2021-07-04 15:45:30 +08:00
|
|
|
|
$current_event = self::getCurrentEvent();
|
2022-03-15 18:05:33 +08:00
|
|
|
|
if (!isset($current_event->class, $current_event->method)) {
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
2021-07-04 15:45:30 +08:00
|
|
|
|
return EventManager::$middleware_map[$current_event->class][$current_event->method] ?? [];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-03-15 18:05:33 +08:00
|
|
|
|
public static function getEventTraceList()
|
|
|
|
|
|
{
|
2021-07-04 15:45:30 +08:00
|
|
|
|
$result = [];
|
|
|
|
|
|
$list = debug_backtrace();
|
|
|
|
|
|
foreach ($list as $v) {
|
2022-03-15 18:05:33 +08:00
|
|
|
|
if ((($v['object'] ?? null) instanceof EventDispatcher) && $v['function'] == 'dispatchEvent') {
|
|
|
|
|
|
$result[] = $v['args'][0];
|
2021-07-04 15:45:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return $result;
|
|
|
|
|
|
}
|
2022-03-15 18:05:33 +08:00
|
|
|
|
}
|