dispatcher.php
来自「一款可以和GOOGLE媲美的开源统计系统,运用AJAX.功能强大. 无色提示:」· PHP 代码 · 共 478 行 · 第 1/2 页
PHP
478 行
* @param object Notification associated object * @param string Notification name * @param array Optional user information * @param bool Whether the notification is pending * @param bool Whether you want the notification to bubble up * @return object The notification object */ function &post(&$object, $nName, $info = array(), $pending = true, $bubble = true) { $notification = new $this->_notificationClass($object, $nName, $info); return $this->postNotification($notification, $pending, $bubble); } /** * Posts the {@link Event_Notification} object * * @access public * @param object The Notification object * @param bool Whether to post the notification immediately * @param bool Whether you want the notification to bubble up * @see Event_Dispatcher::post() * @return object The notification object */ function &postNotification(&$notification, $pending = true, $bubble = true) { $nName = $notification->getNotificationName(); if ($pending === true) { $this->_pending[$nName][] =& $notification; } $objClass = get_class($notification->getNotificationObject()); // Find the registered observers if (isset($this->_ro[$nName])) { foreach (array_keys($this->_ro[$nName]) as $k) { $rObserver =& $this->_ro[$nName][$k]; if ($notification->isNotificationCancelled()) { return $notification; } if (empty($rObserver['class']) || strcasecmp($rObserver['class'], $objClass) == 0) { call_user_func_array($rObserver['callback'], array(&$notification)); $notification->increaseNotificationCount(); } } } // Notify globally registered observers if (isset($this->_ro[EVENT_DISPATCHER_GLOBAL])) { foreach (array_keys($this->_ro[EVENT_DISPATCHER_GLOBAL]) as $k) { $rObserver =& $this->_ro[EVENT_DISPATCHER_GLOBAL][$k]; if ($notification->isNotificationCancelled()) { return $notification; } if (empty($rObserver['class']) || strcasecmp($rObserver['class'], $objClass) == 0) { call_user_func_array($rObserver['callback'], array(&$notification)); $notification->increaseNotificationCount(); } } } if ($bubble === false) { return $notification; } // Notify in nested dispatchers foreach (array_keys($this->_nestedDispatchers) as $nested) { $notification =& $this->_nestedDispatchers[$nested]->postNotification($notification, $pending); } return $notification; } /** * Removes a registered observer that correspond to the given criteria * * @access public * @param mixed A PHP callback * @param string Notification name * @param string Contained object class * @return bool True if an observer was removed, false otherwise */ function removeObserver($callback, $nName = EVENT_DISPATCHER_GLOBAL, $class = null) { if (is_array($callback)) { if (is_object($callback[0])) { $reg = get_class($callback[0]).'::'.$callback[1]; } else { $reg = $callback[0].'::'.$callback[1]; } } else { $reg = $callback; } $removed = false; if (isset($this->_ro[$nName][$reg])) { if (!empty($class)) { if (strcasecmp($this->_ro[$nName][$reg]['class'], $class) == 0) { unset($this->_ro[$nName][$reg]); $removed = true; } } else { unset($this->_ro[$nName][$reg]); $removed = true; } } if (isset($this->_ro[$nName]) && count($this->_ro[$nName]) == 0) { unset($this->_ro[$nName]); } return $removed; } /** * Check, whether the specified observer has been registered with the * dispatcher * * @access public * @param mixed A PHP callback * @param string Notification name * @param string Contained object class * @return bool True if the observer has been registered, false otherwise */ function observerRegistered($callback, $nName = EVENT_DISPATCHER_GLOBAL, $class = null) { if (is_array($callback)) { if (is_object($callback[0])) { $reg = get_class($callback[0]).'::'.$callback[1]; } else { $reg = $callback[0].'::'.$callback[1]; } } else { $reg = $callback; } if (!isset($this->_ro[$nName][$reg])) { return false; } if (empty($class)) { return true; } if (strcasecmp($this->_ro[$nName][$reg]['class'], $class) == 0) { return true; } return false; } /** * Get all observers, that have been registered for a notification * * @access public * @param string Notification name * @param string Contained object class * @return array List of all observers */ function getObservers($nName = EVENT_DISPATCHER_GLOBAL, $class = null) { $observers = array(); if (!isset($this->_ro[$nName])) { return $observers; } foreach ($this->_ro[$nName] as $reg => $observer) { if ($class == null || $observer['class'] == null || strcasecmp($observer['class'], $class) == 0) { $observers[] = $reg; } } return $observers; } /** * Get the name of the dispatcher. * * The name is the unique identifier of a dispatcher. * * @access public * @return string name of the dispatcher */ function getName() { return $this->_name; } /** * add a new nested dispatcher * * Notifications will be broadcasted to this dispatcher as well, which * allows you to create event bubbling. * * @access public * @param Event_Dispatcher The nested dispatcher */ function addNestedDispatcher(&$dispatcher) { $name = $dispatcher->getName(); $this->_nestedDispatchers[$name] =& $dispatcher; } /** * remove a nested dispatcher * * @access public * @param Event_Dispatcher Dispatcher to remove * @return boolean */ function removeNestedDispatcher($dispatcher) { if (is_object($dispatcher)) { $dispatcher = $dispatcher->getName(); } if (!isset($this->_nestedDispatchers[$dispatcher])) { return false; } unset($this->_nestedDispatchers[$dispatcher]); return true; } /** * Changes the class used for notifications * * You may call this method on an object to change it for a single * dispatcher or statically, to set the default for all dispatchers * that will be created. * * @access public * @param string name of the notification class * @return boolean */ function setNotificationClass($class) { if (isset($this) && is_a($this, 'Event_Dispatcher')) { $this->_notificationClass = $class; return true; } $GLOBALS['_Event_Dispatcher']['NotificationClass'] = $class; return true; }}?>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?