diff --git a/src/Event/EventManager.php b/src/Event/EventManager.php index ec444f0d592..d48605b3401 100644 --- a/src/Event/EventManager.php +++ b/src/Event/EventManager.php @@ -22,7 +22,7 @@ * can create multiple instances of this object to manage local events or keep a single instance * and pass it around to manage all events in your app. */ -class EventManager +class EventManager implements EventManagerInterface { /** diff --git a/src/Event/EventManagerInterface.php b/src/Event/EventManagerInterface.php new file mode 100644 index 00000000000..aa12935bb1a --- /dev/null +++ b/src/Event/EventManagerInterface.php @@ -0,0 +1,109 @@ +on($listener); + * ``` + * + * Binding with no options: + * + * ``` + * $eventManager->on('Model.beforeSave', $callable); + * ``` + * + * Binding with options: + * + * ``` + * $eventManager->on('Model.beforeSave', ['priority' => 90], $callable); + * ``` + * + * @param string|\Cake\Event\EventListenerInterface|null $eventKey The event unique identifier name + * with which the callback will be associated. If $eventKey is an instance of + * Cake\Event\EventListenerInterface its events will be bound using the `implementedEvents` methods. + * + * @param array|callable $options Either an array of options or the callable you wish to + * bind to $eventKey. If an array of options, the `priority` key can be used to define the order. + * Priorities are treated as queues. Lower values are called before higher ones, and multiple attachments + * added to the same priority queue will be treated in the order of insertion. + * + * @param callable|null $callable The callable function you want invoked. + * + * @return $this + * @throws \InvalidArgumentException When event key is missing or callable is not an + * instance of Cake\Event\EventListenerInterface. + */ + public function on($eventKey = null, $options = [], $callable = null); + + /** + * Remove a listener from the active listeners. + * + * Remove a EventListenerInterface entirely: + * + * ``` + * $manager->off($listener); + * ``` + * + * Remove all listeners for a given event: + * + * ``` + * $manager->off('My.event'); + * ``` + * + * Remove a specific listener: + * + * ``` + * $manager->off('My.event', $callback); + * ``` + * + * Remove a callback from all events: + * + * ``` + * $manager->off($callback); + * ``` + * + * @param string|\Cake\Event\EventListenerInterface $eventKey The event unique identifier name + * with which the callback has been associated, or the $listener you want to remove. + * @param callable|null $callable The callback you want to detach. + * @return $this + */ + public function off($eventKey, $callable = null); + + /** + * Dispatches a new event to all configured listeners + * + * @param string|\Cake\Event\EventInterface $event the event key name or instance of Event + * @return \Cake\Event\Event + * @triggers $event + */ + public function dispatch($event); + + /** + * Returns a list of all listeners for an eventKey in the order they should be called + * + * @param string $eventKey Event key. + * @return array + */ + public function listeners($eventKey); +}