listeners[$eventName])) { $this->listeners[$eventName] = array(); } $this->listeners[$eventName][] = $listener; } /** * Trigger an event * * @access public * @param string $eventName Event name * @param array $data Event data */ public function trigger($eventName, array $data) { $this->lastEvent = $eventName; $this->events[] = $eventName; if (isset($this->listeners[$eventName])) { foreach ($this->listeners[$eventName] as $listener) { $listener->execute($data); // TODO: keep an history of executed actions for unit test } } } /** * Get the last fired event * * @access public * @return string Event name */ public function getLastTriggeredEvent() { return $this->lastEvent; } /** * Get a list of triggered events * * @access public * @return array */ public function getTriggeredEvents() { return $this->events; } /** * Check if a listener bind to an event * * @access public * @param string $eventName Event name * @param mixed $instance Instance name or object itself * @return bool Yes or no */ public function hasListener($eventName, $instance) { if (isset($this->listeners[$eventName])) { foreach ($this->listeners[$eventName] as $listener) { if ($listener instanceof $instance) { return true; } } } return false; } }