_source = $source; $this->addIds($ids); return; } /** * Add acceptable ID (or reset). * * @param array $ids Accepted ID. * @return void */ public function addIds(array $ids) { foreach ($ids as $id) { $this->_callables[$id] = []; } return; } /** * Attach a callable to a listenable component. * * @param string $listenerId Listener ID. * @param mixed $callable Callable. * @return \Hoa\Event\Listener * @throws \Hoa\Event\Exception */ public function attach($listenerId, $callable) { if (false === $this->listenerExists($listenerId)) { throw new Exception( 'Cannot listen %s because it is not defined.', 0, $listenerId ); } $callable = xcallable($callable); $this->_callables[$listenerId][$callable->getHash()] = $callable; return $this; } /** * Detach a callable from a listenable component. * * @param string $listenerId Listener ID. * @param mixed $callable Callable. * @return \Hoa\Event\Listener */ public function detach($listenerId, $callable) { unset($this->_callables[$listenerId][xcallable($callable)->getHash()]); return $this; } /** * Detach all callables from a listenable component. * * @param string $listenerId Listener ID. * @return \Hoa\Event\Listener */ public function detachAll($listenerId) { unset($this->_callables[$listenerId]); return $this; } /** * Check if a listener exists. * * @param string $listenerId Listener ID. * @return bool */ public function listenerExists($listenerId) { return array_key_exists($listenerId, $this->_callables); } /** * Send/fire a bucket to a listener. * * @param string $listenerId Listener ID. * @param \Hoa\Event\Bucket $data Data. * @return array * @throws \Hoa\Event\Exception */ public function fire($listenerId, Bucket $data) { if (false === $this->listenerExists($listenerId)) { throw new Exception( 'Cannot fire on %s because it is not defined.', 1, $listenerId ); } $data->setSource($this->_source); $out = []; foreach ($this->_callables[$listenerId] as $callable) { $out[] = $callable($data); } return $out; } }