/** * {@inheritDoc} */ public function getListenersForClass($instance) { // ignore classes which don't implement the subscriber if (!$instance instanceof EventSubscriber) { return array(); } // grab all the entries returned by getEventListeners() and parse them $listeners = array(); foreach ($instance->getEventListeners() as $i => $item) { // check the definition is in a valid format if (!is_array($item)) { throw Exception::invalidListenerDefinition(get_class($instance)); } // check all data is present if (!isset($item['event'])) { throw Exception::incompleteListenerDefinition(get_class($instance), 'missing event field'); } if (!isset($item['method'])) { throw Exception::incompleteListenerDefinition(get_class($instance), 'missing method field'); } // get the information $listener = array('event' => $item['event'], 'namespace' => null, 'method' => $item['method']); if (isset($item['namespace'])) { $listener['namespace'] = $item['namespace']; } $listeners[] = $listener; } return $listeners; }
/** * Automatically maps event listeners associated with a class using the driver provided to this instance and * registers them. * * @param object $instance * @throws \BedRest\Events\MappingException */ public function addClassListeners($instance) { if (!$this->driver) { throw Exception::noDriver(); } foreach ($this->driver->getListenersForClass($instance) as $listener) { $this->addListener($listener['event'], array($instance, $listener['method'])); } }
/** * Constructor. * * Takes an optional list of drivers to add to the chain. * * @param array $drivers * @throws \BedRest\Events\Exception */ public function __construct(array $drivers = array()) { foreach ($drivers as $driver) { if (!$driver instanceof Driver) { if (is_object($driver)) { throw Exception::invalidDriverSupplied(get_class($driver)); } else { throw Exception::invalidDriverSupplied(gettype($driver)); } } } $this->drivers = $drivers; }
/** * {@inheritDoc} */ public function getListenersForClass($instance) { $className = get_class($instance); $listeners = array(); // do some reflection to get the methods $reflClass = new \ReflectionClass($className); $reflMethods = $reflClass->getMethods(\ReflectionMethod::IS_PUBLIC); // process each method's annotations foreach ($reflMethods as $reflMethod) { $annotations = $this->reader->getMethodAnnotations($reflMethod); foreach ($annotations as $annotation) { // get the listeners if (get_class($annotation) == self::ANNOTATION_LISTENER) { if (empty($annotation->event)) { throw Exception::incompleteListenerDefinition(get_class($instance), 'missing event field'); } $listeners[] = array('event' => $annotation->event, 'namespace' => $annotation->namespace, 'method' => $reflMethod->getName()); } } } return $listeners; }