/**
  * @param EventListenerInterface $eventListener
  * @return string
  */
 public function getListenerClassName(EventListenerInterface $eventListener)
 {
     if ($eventListener instanceof EventListenerProxyInterface) {
         $listenerType = $eventListener->getTargetType();
     } else {
         $listenerType = get_class($eventListener);
     }
     return $listenerType;
 }
 /**
  * Register listener
  *
  * @param EventListenerInterface the listener instance
  * @return EventDispatcher
  */
 public function addListener(EventListenerInterface $listener)
 {
     $events = $listener->getEventMap();
     foreach ($events as $eventName => $eventData) {
         // process event data
         if (is_array($eventData)) {
             $method = (string) $eventData[0];
             $priority = isset($eventData[1]) ? (int) $eventData[1] : 0;
         } else {
             $method = (string) $eventData;
             $priority = 0;
         }
         // add to map
         $this->sortMap[$eventName] = false;
         $this->eventMap[$eventName][] = array(0, $priority, $listener, $method);
     }
     return $this;
 }
Ejemplo n.º 3
0
 /**
  * Auxiliary function to help detach all listeners provided by an object implementing EventListenerInterface
  *
  * @param \Cake\Event\EventListenerInterface $subscriber the subscriber to be detached
  * @param string $eventKey optional event key name to unsubscribe the listener from
  * @return void
  */
 protected function _detachSubscriber(EventListenerInterface $subscriber, $eventKey = null)
 {
     $events = (array) $subscriber->implementedEvents();
     if (!empty($eventKey) && empty($events[$eventKey])) {
         return;
     } elseif (!empty($eventKey)) {
         $events = array($eventKey => $events[$eventKey]);
     }
     foreach ($events as $key => $function) {
         if (is_array($function)) {
             if (is_numeric(key($function))) {
                 foreach ($function as $handler) {
                     $handler = isset($handler['callable']) ? $handler['callable'] : $handler;
                     $this->detach(array($subscriber, $handler), $key);
                 }
                 continue;
             }
             $function = $function['callable'];
         }
         $this->detach(array($subscriber, $function), $key);
     }
 }