* SharedEventManager::getInstance()->connect( * array('My\Resource\AbstractResource', 'My\Resource\EntityResource'), * 'getOne', * function ($e) use ($cache) { * if (!$id = $e->getParam('id', false)) { * return; * } * if (!$data = $cache->load(get_class($resource) . '::getOne::' . $id )) { * return; * } * return $data; * } * ); * * * @param string|array $id Identifier(s) for event emitting component(s) * @param string $event * @param callback $callback PHP Callback * @param int $priority Priority at which listener should execute * @return void */ public function attach($id, $event, $callback, $priority = 1) { $ids = (array) $id; foreach ($ids as $id) { if (!array_key_exists($id, $this->identifiers)) { $this->identifiers[$id] = new Zend_EventManager_EventManager(); } $this->identifiers[$id]->attach($event, $callback, $priority); } } /** * Detach a listener from an event offered by a given resource * * @param string|int $id * @param Zend_Stdlib_CallbackHandler $listener * @return bool Returns true if event and listener found, and unsubscribed; returns false if either event or listener not found */ public function detach($id, Zend_Stdlib_CallbackHandler $listener) { if (!array_key_exists($id, $this->identifiers)) { return false; } return $this->identifiers[$id]->detach($listener); } /** * Retrieve all registered events for a given resource * * @param string|int $id * @return array */ public function getEvents($id) { if (!array_key_exists($id, $this->identifiers)) { return false; } return $this->identifiers[$id]->getEvents(); } /** * Retrieve all listeners for a given identifier and event * * @param string|int $id * @param string|int $event * @return false|Zend_Stdlib_PriorityQueue */ public function getListeners($id, $event) { if (!array_key_exists($id, $this->identifiers)) { return false; } return $this->identifiers[$id]->getListeners($event); } /** * Clear all listeners for a given identifier, optionally for a specific event * * @param string|int $id * @param null|string $event * @return bool */ public function clearListeners($id, $event = null) { if (!array_key_exists($id, $this->identifiers)) { return false; } if (null === $event) { unset($this->identifiers[$id]); return true; } return $this->identifiers[$id]->clearListeners($event); } }