类 EventManager
事件管理器负责跟踪事件监听器,将正确的数据传递给它们,并在关联事件触发时按正确顺序触发它们。您可以创建此对象的多个实例来管理本地事件,或保留单个实例并将其传递以管理应用程序中的所有事件。
属性摘要
- 
        $_eventList protectedCake\Event\EventList|null事件列表对象。 
- 
        $_generalManager protected staticCake\Event\EventManager|null全局可用的实例,用于分派从任何范围附加的事件 
- 
        $_isGlobal protectedbool内部标志,用于区分通用管理器和单例 
- 
        $_listeners protectedarray与之关联的监听器回调列表 
- 
        $_trackEvents protectedbool如果存在事件列表对象,则启用将事件自动添加到事件列表对象中。 
- 
        $defaultPriority public staticint新附加的监听器的默认优先级队列值 
方法摘要
- 
          __debugInfo() public调试友好的对象属性。 
- 
          _attachSubscriber() protected辅助函数,将 Cake\Event\EventListenerInterface 类实例的所有实现回调附加为此管理器上的各个方法 
- 
          _callListener() protected调用监听器。 
- 
          _detachSubscriber() protected辅助函数,用于帮助分离由实现 EventListenerInterface 的对象提供的全部监听器 
- 
          addEventToList() public如果事件列表对象存在,则将事件添加到列表中。 
- 
          dispatch() public将新事件分派给所有配置的监听器 
- 
          getEventList() public返回事件列表。 
- 
          instance() public static返回 Cake\Event\EventManager 的全局可用实例,这用于分派从其他管理器创建范围之外附加的事件。通常用于创建钩子系统或类间通信 
- 
          isTrackingEvents() public返回此管理器是否设置为跟踪事件 
- 
          listeners() public返回特定事件的全部监听器,按调用顺序排列 
- 
          matchingListeners() public返回与指定模式匹配的监听器 
- 
          normalizeHandler() protected构建单个标准化处理程序。 
- 
          normalizeHandlers() protected构建标准化处理程序数组。 
- 
          off() public从活动监听器中删除监听器。 
- 
          on() public将新监听器添加到事件。 
- 
          prioritisedListeners() public返回指定事件键的监听器,按优先级索引 
- 
          setEventList() public启用分派事件的列表。 
- 
          trackEvents() public在运行时启用/禁用事件跟踪。 
- 
          unsetEventList() public禁用分派事件的列表。 
方法详细信息
_attachSubscriber() ¶ protected
_attachSubscriber(Cake\Event\EventListenerInterface $subscriber): void辅助函数,将 Cake\Event\EventListenerInterface 类实例的所有实现回调附加为此管理器上的各个方法
参数
- 
                Cake\Event\EventListenerInterface$subscriber
- 事件监听器。 
返回
void_callListener() ¶ protected
_callListener(callable $listener, Cake\Event\EventInterface<TSubject> $event): mixed调用监听器。
参数
- 
                callable$listener
- 要触发的监听器。 
- 
                Cake\Event\EventInterface<TSubject>$event
- 事件实例。 
返回
mixed_detachSubscriber() ¶ protected
_detachSubscriber(Cake\Event\EventListenerInterface $subscriber, string|null $eventKey = null): void辅助函数,用于帮助分离由实现 EventListenerInterface 的对象提供的全部监听器
参数
- 
                Cake\Event\EventListenerInterface$subscriber
- 要分离的订阅者 
- 
                string|null$eventKey optional
- 可选的事件键名称,用于取消订阅监听器 
返回
voidaddEventToList() ¶ public
addEventToList(Cake\Event\EventInterface<TSubject> $event): $this如果事件列表对象存在,则将事件添加到列表中。
参数
- 
                Cake\Event\EventInterface<TSubject>$event
- 要添加到列表中的事件。 
返回
$thisdispatch() ¶ public
dispatch(Cake\Event\EventInterface<TSubject>|string $event): Cake\Event\EventInterface<TSubject>将新事件分派给所有配置的监听器
参数
- 
                Cake\Event\EventInterface<TSubject>|string$event
返回
Cake\Event\EventInterface<TSubject>getEventList() ¶ public
getEventList(): Cake\Event\EventList|null返回事件列表。
返回
Cake\Event\EventList|nullinstance() ¶ public static
instance(Cake\Event\EventManager|null $manager = null): Cake\Event\EventManager返回 Cake\Event\EventManager 的全局可用实例,这用于分派从其他管理器创建范围之外附加的事件。通常用于创建钩子系统或类间通信
如果使用第一个参数调用,它将被设置为全局可用实例
参数
- 
                Cake\Event\EventManager|null$manager optional
- 事件管理器实例。 
返回
Cake\Event\EventManagerlisteners() ¶ public
listeners(string $eventKey): array返回特定事件的全部监听器,按调用顺序排列
参数
- 
                string$eventKey
返回
arraymatchingListeners() ¶ public
matchingListeners(string $eventKeyPattern): array返回与指定模式匹配的监听器
参数
- 
                string$eventKeyPattern
- 要匹配的模式。 
返回
arraynormalizeHandler() ¶ protected
normalizeHandler(Cake\Event\EventListenerInterface $subscriber, Closure|array|string $handler): array构建单个标准化处理程序。
标准化处理程序是一个包含以下键的数组
- callable- 事件处理程序闭包- settings- 事件处理程序设置
 
参数
- 
                Cake\Event\EventListenerInterface$subscriber
- 事件订阅者 
- 
                Closure|array|string$handler
- 事件处理程序 
返回
arraynormalizeHandlers() ¶ protected
normalizeHandlers(Cake\Event\EventListenerInterface $subscriber, Closure|array|string $handlers): array构建标准化处理程序数组。
标准化处理程序是一个包含以下键的数组
- callable- 事件处理程序闭包- settings- 事件处理程序设置
 
参数
- 
                Cake\Event\EventListenerInterface$subscriber
- 事件订阅者 
- 
                Closure|array|string$handlers
- 事件处理程序 
返回
arrayoff() ¶ public
off(Cake\Event\EventListenerInterface|callable|string $eventKey, Cake\Event\EventListenerInterface|callable|null $callable = null): $this从活动监听器中删除监听器。
完全删除 EventListenerInterface
$manager->off($listener);删除给定事件的全部监听器
$manager->off('My.event');删除特定监听器
$manager->off('My.event', $callback);从所有事件中删除回调
$manager->off($callback);参数
- 
                Cake\Event\EventListenerInterface|callable|string$eventKey
- 
                Cake\Event\EventListenerInterface|callable|null$callable optional
返回
$thison() ¶ public
on(Cake\Event\EventListenerInterface|string $eventKey, callable|array $options = [], callable|null $callable = null): $this将新监听器添加到事件。
一个可变参数接口,用于添加模拟 jQuery.on() 的监听器。
绑定 EventListenerInterface
$eventManager->on($listener);无选项绑定
$eventManager->on('Model.beforeSave', $callable);使用选项绑定
$eventManager->on('Model.beforeSave', ['priority' => 90], $callable);参数
- 
                Cake\Event\EventListenerInterface|string$eventKey
- 
                callable|array$options optional
- 
                callable|null$callable optional
返回
$thisprioritisedListeners() ¶ public
prioritisedListeners(string $eventKey): array返回指定事件键的监听器,按优先级索引
参数
- 
                string$eventKey
- 事件键。 
返回
arraysetEventList() ¶ public
setEventList(Cake\Event\EventList $eventList): $this启用分派事件的列表。
参数
- 
                Cake\Event\EventList$eventList
- 要使用的事件列表对象。 
返回
$thistrackEvents() ¶ public
trackEvents(bool $enabled): $this在运行时启用/禁用事件跟踪。
参数
- 
                bool$enabled
- 启用/禁用它,值为真或假。 
返回
$this