示例#1
0
文件: Dispatcher.php 项目: cwcw/cms
 /**
  * Dispatches an event. This means notifying all listeners and passing them the event object
  *
  * @param Streamwide_Event_Interface $event
  * @param scalar $phpId
  * @return void
  */
 protected function _dispatchEvent(Streamwide_Event_Interface $event, $phpId)
 {
     $eventType = $event->getEventType();
     $logMessage = 'Preparing to dispatch the "%s" event in phpId "%d" from event dispatcher of class "%s"';
     Streamwide_Engine_Logger::info(sprintf($logMessage, $eventType, $phpId, $this->_class));
     $logMessage = 'The event listeners count for class "%s" is %d';
     Streamwide_Engine_Logger::debug(sprintf($logMessage, $this->_class, $this->_count));
     // search the event listener list for info that matches the php id and event type
     if (!isset($this->_list[$phpId][$eventType])) {
         $logMessage = 'Event "%s" will not be dispatched in phpId "%d" from event dispatcher of class "%s"';
         $logMessage .= ' because no event listeners subscribed to it';
         Streamwide_Engine_Logger::debug(sprintf($logMessage, $eventType, $phpId, $this->_class));
         return;
     }
     // If the event does not have a source yet, set the source to be the instance of this class,
     // this is needed because event dispatching capabilities can be aquired through composition too,
     // not only through inheritance
     if (null === $event->getEventSource()) {
         $event->setEventSource($this);
     }
     foreach (new ArrayIterator($this->_list[$phpId][$eventType]) as $offset => $listener) {
         if (!$event->isDispatchable()) {
             $logMessage = 'Breaking out of the event dispatching loop in phpId "%d" from event dispatcher of class "%s"';
             $logMessage .= ' because the event object has been marked as undispatchable';
             Streamwide_Engine_Logger::debug(sprintf($logMessage, $phpId, $this->_class));
             break;
         }
         if (!isset($this->_list[$phpId][$eventType][$offset])) {
             $forceExecution = $listener->getForceExecution();
             if ($forceExecution) {
                 $logMessage = 'Forcing execution of listener "%s" from "%s"';
                 Streamwide_Engine_Logger::debug(sprintf($logMessage, $listener->getLoggingName(), $this->_class));
             } else {
                 $logMessage = 'Could not find offset "%d" in the event listener list in class "%s" while dispatching event "%s"';
                 Streamwide_Engine_Logger::debug(sprintf($logMessage, $offset, $this->_class, $eventType));
                 continue;
             }
         }
         $specification = $listener->getNotifyFilter();
         if (!$specification->isSatisfiedBy($event)) {
             $logMessage = 'Event "%s" will not be dispatched in phpId "%d" from event dispatcher of class "%s"';
             $logMessage .= ' because the event does not satisfy the listener\'s specifications';
             Streamwide_Engine_Logger::debug(sprintf($logMessage, $eventType, $phpId, $this->_class));
             continue;
         }
         if ($listener->isAutoRemovableBeforeExecution()) {
             $removeGroup = $listener->getRemoveGroup();
             if ($removeGroup && null !== ($groupName = $listener->getGroup())) {
                 $this->_flushByGroup($groupName);
             } else {
                 unset($this->_list[$phpId][$eventType][$offset]);
                 $this->_count--;
                 $logMessage = 'Listener "%s" was automatically removed before execution from "%s"';
                 Streamwide_Engine_Logger::debug(sprintf($logMessage, $listener->getLoggingName(), $this->_class));
                 $logMessage = 'The event listeners count for class "%s" is %d';
                 Streamwide_Engine_Logger::debug(sprintf($logMessage, $this->_class, $this->_count));
             }
         }
         // execute the listener
         $listener->execute($event);
         if ($listener->isAutoRemovableAfterExecution()) {
             $removeGroup = $listener->getRemoveGroup();
             if ($removeGroup && null !== ($groupName = $listener->getGroup())) {
                 $this->_flushByGroup($groupName);
             } else {
                 unset($this->_list[$phpId][$eventType][$offset]);
                 $this->_count--;
                 $logMessage = 'Listener "%s" was automatically removed after execution from "%s"';
                 Streamwide_Engine_Logger::debug(sprintf($logMessage, $listener->getLoggingName(), $this->_class));
                 $logMessage = 'The event listeners count for class "%s" is %d';
                 Streamwide_Engine_Logger::debug(sprintf($logMessage, $this->_class, $this->_count));
             }
         }
     }
     if (isset($this->_list[$phpId][$eventType])) {
         $this->_performListMaintenance($phpId, $eventType);
     }
 }