事件总线
2026/10/26大约 1 分钟
事件总线(EventRegistry)
L2 行为扩展:插件订阅基座的标准业务事件,做旁路增强,不侵入主流程。app\basic\EventRegistry 是带优先级 + owner 的轻量发布订阅,与基座既有的 Webman Event 互补、互不干扰。
监听(写在 boot)
use app\basic\EventRegistry;
// 方式一:boot() 内注册
EventRegistry::listen(
'file.uploaded',
[MyListener::class, 'handle'], // callable 或 [Class, 'method']
priority: 10, // 越大越先执行,默认 0
owner: 'am_demo' // 归属,卸载/禁用时 forgetByOwner 清理
);// 方式二:插件 config/event.php 声明(格式同基座 config/event.php)
// 由 PluginManager 聚合,owner 自动记为插件名
return [
'user.created' => [
[plugin\am_demo\app\event\Listener::class, 'onUserCreated'],
],
];触发(基座侧)
$returns = EventRegistry::emit('file.uploaded', [
'sid' => $uploaderSid,
'file' => $fileData,
]);- 监听器接收
payload数组作为唯一参数。 - 单个监听器异常被记录日志并隔离,不影响其它监听器与主流程。
- 无监听或开关关闭(
event_enabled)时emit为空操作。
判定与清理
| 方法 | 作用 |
|---|---|
has($event) | 是否已有监听 |
forgetByOwner($owner) | 禁用/卸载时按归属清理(基座自动调用) |
标准事件点
常见业务事件(清单以基座实际 emit 点为准):user.created、user.loginLog、file.uploaded、notify.sent 等。订阅前先确认事件名与载荷结构。
进程级
监听是进程级静态状态,必须在 boot() 内注册(或写 config/event.php)。详见生命周期。
