/** * {@inheritDoc} */ public function attach($eventName, callable $listener, $priority = 1) { $options = []; $options['priority'] = $priority; $this->eventManager->on($eventName, $options, $listener); return $this; }
/** * Load a single event class attached to Crud. * * @param string $name Name * @return \Crud\Listener\BaseListener * @throws \Crud\Error\Exception\ListenerNotConfiguredException * @throws \Crud\Error\Exception\MissingListenerException */ protected function _loadListener($name) { if (!isset($this->_listenerInstances[$name])) { $config = $this->config('listeners.' . $name); if (empty($config)) { throw new ListenerNotConfiguredException(sprintf('Listener "%s" is not configured', $name)); } $className = App::classname($config['className'], 'Listener', 'Listener'); if (empty($className)) { throw new MissingListenerException('Could not find listener class: ' . $config['className']); } $this->_listenerInstances[$name] = new $className($this->_controller); unset($config['className']); $this->_listenerInstances[$name]->config($config); $this->_eventManager->on($this->_listenerInstances[$name]); if (is_callable([$this->_listenerInstances[$name], 'setup'])) { $this->_listenerInstances[$name]->setup(); } } return $this->_listenerInstances[$name]; }
/** * Tests off'ing all listeners for an event */ public function testRemoveAllListeners() { $manager = new EventManager(); $manager->on('fake.event', ['AClass', 'aMethod']); $manager->on('another.event', ['priority' => 1], 'fakeFunction'); $manager->off('fake.event'); $expected = [['callable' => 'fakeFunction']]; $this->assertEquals($expected, $manager->listeners('another.event')); $this->assertEquals([], $manager->listeners('fake.event')); }
/** * Adds a new listener to an event. * * @param null $eventKey * @param array $options * @param null $callable * @return void */ public function on($eventKey = null, $options = [], $callable = null) { parent::on($eventKey, $options, $callable); if (is_object($callable)) { $key = get_class($callable); $this->_listenersMap[$key] = $callable; } }