示例#1
0
文件: Widget.php 项目: cwcw/cms
 /**
  * @see Engine/Events/Event/Streamwide_Engine_Events_Event_Dispatcher#dispatchEvent()
  */
 public function dispatchEvent(Streamwide_Event_Interface $event)
 {
     $eventType = $event->getEventType();
     if (!$this->isAllowedEventType($eventType)) {
         return null;
     }
     // check to see if we have any context parameters set
     if (null !== $this->_contextParams) {
         $areContextParamsOptionsSet = is_array($this->_contextParamsOptions) && !empty($this->_contextParamsOptions);
         foreach (new ArrayIterator($this->_contextParams) as $key => $value) {
             $clashSafeName = sprintf('__%s__', $key);
             // do we have options for the current context parameter?
             $hasOptions = $areContextParamsOptionsSet && array_key_exists($key, $this->_contextParamsOptions) && is_array($this->_contextParamsOptions[$key]) && !empty($this->_contextParamsOptions[$key]);
             if ($hasOptions) {
                 $options = $this->_contextParamsOptions[$key];
                 if (array_key_exists('forEvent', $options)) {
                     if (!is_array($options['forEvent'])) {
                         $options['forEvent'] = array($options['forEvent']);
                     }
                     if (!in_array($eventType, $options['forEvent'])) {
                         // if the current context parameter is not allowed to be injected in
                         // the current event object skip to the next context parameter
                         continue;
                     }
                 }
             }
             // empty options or no options set default
             // to injecting the context parameter into all
             // events dispatched by the widget
             $event->setParam($clashSafeName, $value);
         }
     }
     $event->setEventSource($this);
     return $this->_eventDispatcher->dispatchEvent($event);
 }
示例#2
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);
     }
 }