Example #1
0
 /**
  * Get listeners for the currently triggered event.
  *
  * @param  string $eventName
  * @return callable[]
  */
 private function getListenersByEventName($eventName)
 {
     $listeners = array_merge_recursive(isset($this->events[$eventName]) ? $this->events[$eventName] : [], isset($this->events['*']) ? $this->events['*'] : [], $this->sharedManager ? $this->sharedManager->getListeners($this->identifiers, $eventName) : []);
     krsort($listeners, SORT_NUMERIC);
     $listenersForEvent = [];
     foreach ($listeners as $priority => $listenersByPriority) {
         foreach ($listenersByPriority as $listener) {
             // Performance note: after some testing, it appears that accumulating listeners and sending
             // them at the end of the method is FASTER than using generators (ie. yielding)
             $listenersForEvent[] = $listener;
         }
     }
     return $listenersForEvent;
 }
Example #2
0
 /**
  * Detach problem listeners specified by getListenersToDetach() and return an array of information that will
  * allow them to be reattached.
  *
  * @param  SharedEvents $sharedEvents Shared event manager
  * @return array
  */
 protected function detachProblemListeners(SharedEvents $sharedEvents)
 {
     // Convert the problem list from two-dimensional array to more convenient id => event => class format:
     $formattedProblems = array();
     foreach ($this->getListenersToDetach() as $current) {
         if (!isset($formattedProblems[$current['id']])) {
             $formattedProblems[$current['id']] = array();
         }
         if (!isset($formattedProblems[$current['id']][$current['event']])) {
             $formattedProblems[$current['id']][$current['event']] = array();
         }
         $formattedProblems[$current['id']][$current['event']][] = $current['class'];
     }
     // Loop through the class blacklist, detaching problem events and remembering their CallbackHandlers
     // for future reference:
     $results = array();
     foreach ($formattedProblems as $id => $eventArray) {
         $results[$id] = array();
         foreach ($eventArray as $eventName => $classArray) {
             $results[$id][$eventName] = array();
             $events = $sharedEvents->getListeners($id, $eventName);
             foreach ($events as $currentEvent) {
                 $currentCallback = $currentEvent->getCallback();
                 if (!isset($currentCallback[0])) {
                     continue;
                 }
                 foreach ($classArray as $class) {
                     if (is_a($currentCallback[0], $class)) {
                         $sharedEvents->detach($id, $currentEvent);
                         $results[$id][$eventName][] = $currentEvent;
                     }
                 }
             }
         }
     }
     return $results;
 }
 /**
  * Detach problem listeners specified by getListenersToDetach() and return an array of information that will
  * allow them to be reattached.
  *
  * @param  SharedEvents $sharedEvents Shared event manager
  * @return array
  */
 protected function detachProblemListeners(SharedEvents $sharedEvents)
 {
     // Convert the problem list from two-dimensional array to more convenient id => event => class format:
     $formattedProblems = [];
     foreach ($this->getListenersToDetach() as $current) {
         if (!isset($formattedProblems[$current['id']])) {
             $formattedProblems[$current['id']] = [];
         }
         if (!isset($formattedProblems[$current['id']][$current['event']])) {
             $formattedProblems[$current['id']][$current['event']] = [];
         }
         $formattedProblems[$current['id']][$current['event']][] = $current['class'];
     }
     // Loop through the class blacklist, detaching problem events and remembering their CallbackHandlers
     // for future reference:
     $results = [];
     foreach ($formattedProblems as $id => $eventArray) {
         $results[$id] = [];
         foreach ($eventArray as $eventName => $classArray) {
             $results[$id][$eventName] = [];
             $events = $sharedEvents->getListeners($id, $eventName);
             foreach ($events as $currentEvent) {
                 $currentCallback = $currentEvent->getCallback();
                 // If we have an array, grab the object
                 if (is_array($currentCallback)) {
                     $currentCallback = array_shift($currentCallback);
                 }
                 // This routine is only valid for object callbacks
                 if (!is_object($currentCallback)) {
                     continue;
                 }
                 foreach ($classArray as $class) {
                     if ($currentCallback instanceof $class) {
                         $sharedEvents->detach($id, $currentEvent);
                         $results[$id][$eventName][] = $currentEvent;
                     }
                 }
             }
         }
     }
     return $results;
 }