Chris@0: . Chris@0: */ Chris@0: Chris@0: namespace Doctrine\Common; Chris@0: Chris@0: /** Chris@0: * The EventManager is the central point of Doctrine's event listener system. Chris@0: * Listeners are registered on the manager and events are dispatched through the Chris@0: * manager. Chris@0: * Chris@0: * @link www.doctrine-project.org Chris@0: * @since 2.0 Chris@0: * @author Guilherme Blanco Chris@0: * @author Jonathan Wage Chris@0: * @author Roman Borschel Chris@0: */ Chris@0: class EventManager Chris@0: { Chris@0: /** Chris@0: * Map of registered listeners. Chris@0: * => Chris@0: * Chris@0: * @var array Chris@0: */ Chris@0: private $_listeners = []; Chris@0: Chris@0: /** Chris@0: * Dispatches an event to all registered listeners. Chris@0: * Chris@0: * @param string $eventName The name of the event to dispatch. The name of the event is Chris@0: * the name of the method that is invoked on listeners. Chris@0: * @param EventArgs|null $eventArgs The event arguments to pass to the event handlers/listeners. Chris@0: * If not supplied, the single empty EventArgs instance is used. Chris@0: * Chris@0: * @return boolean Chris@0: */ Chris@0: public function dispatchEvent($eventName, EventArgs $eventArgs = null) Chris@0: { Chris@0: if (isset($this->_listeners[$eventName])) { Chris@0: $eventArgs = $eventArgs === null ? EventArgs::getEmptyInstance() : $eventArgs; Chris@0: Chris@0: foreach ($this->_listeners[$eventName] as $listener) { Chris@0: $listener->$eventName($eventArgs); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the listeners of a specific event or all listeners. Chris@0: * Chris@0: * @param string|null $event The name of the event. Chris@0: * Chris@0: * @return array The event listeners for the specified event, or all event listeners. Chris@0: */ Chris@0: public function getListeners($event = null) Chris@0: { Chris@0: return $event ? $this->_listeners[$event] : $this->_listeners; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks whether an event has any registered listeners. Chris@0: * Chris@0: * @param string $event Chris@0: * Chris@0: * @return boolean TRUE if the specified event has any listeners, FALSE otherwise. Chris@0: */ Chris@0: public function hasListeners($event) Chris@0: { Chris@0: return isset($this->_listeners[$event]) && $this->_listeners[$event]; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Adds an event listener that listens on the specified events. Chris@0: * Chris@0: * @param string|array $events The event(s) to listen on. Chris@0: * @param object $listener The listener object. Chris@0: * Chris@0: * @return void Chris@0: */ Chris@0: public function addEventListener($events, $listener) Chris@0: { Chris@0: // Picks the hash code related to that listener Chris@0: $hash = spl_object_hash($listener); Chris@0: Chris@0: foreach ((array) $events as $event) { Chris@0: // Overrides listener if a previous one was associated already Chris@0: // Prevents duplicate listeners on same event (same instance only) Chris@0: $this->_listeners[$event][$hash] = $listener; Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Removes an event listener from the specified events. Chris@0: * Chris@0: * @param string|array $events Chris@0: * @param object $listener Chris@0: * Chris@0: * @return void Chris@0: */ Chris@0: public function removeEventListener($events, $listener) Chris@0: { Chris@0: // Picks the hash code related to that listener Chris@0: $hash = spl_object_hash($listener); Chris@0: Chris@0: foreach ((array) $events as $event) { Chris@0: // Check if actually have this listener associated Chris@0: if (isset($this->_listeners[$event][$hash])) { Chris@0: unset($this->_listeners[$event][$hash]); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Adds an EventSubscriber. The subscriber is asked for all the events it is Chris@0: * interested in and added as a listener for these events. Chris@0: * Chris@0: * @param \Doctrine\Common\EventSubscriber $subscriber The subscriber. Chris@0: * Chris@0: * @return void Chris@0: */ Chris@0: public function addEventSubscriber(EventSubscriber $subscriber) Chris@0: { Chris@0: $this->addEventListener($subscriber->getSubscribedEvents(), $subscriber); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Removes an EventSubscriber. The subscriber is asked for all the events it is Chris@0: * interested in and removed as a listener for these events. Chris@0: * Chris@0: * @param \Doctrine\Common\EventSubscriber $subscriber The subscriber. Chris@0: * Chris@0: * @return void Chris@0: */ Chris@0: public function removeEventSubscriber(EventSubscriber $subscriber) Chris@0: { Chris@0: $this->removeEventListener($subscriber->getSubscribedEvents(), $subscriber); Chris@0: } Chris@0: }