NobuNobu
nobun****@users*****
2006年 6月 27日 (火) 01:17:37 JST
Index: xoops2jp/html/kernel/XCube_Delegate.class.php diff -u xoops2jp/html/kernel/XCube_Delegate.class.php:1.1.2.3 xoops2jp/html/kernel/XCube_Delegate.class.php:1.1.2.4 --- xoops2jp/html/kernel/XCube_Delegate.class.php:1.1.2.3 Mon Jun 26 12:22:54 2006 +++ xoops2jp/html/kernel/XCube_Delegate.class.php Tue Jun 27 01:17:37 2006 @@ -1,6 +1,6 @@ <?php /** - * @version $Id: XCube_Delegate.class.php,v 1.1.2.3 2006/06/26 03:22:54 minahito Exp $ + * @version $Id: XCube_Delegate.class.php,v 1.1.2.4 2006/06/26 16:17:37 nobunobu Exp $ */ @@ -170,7 +170,7 @@ $priority = XCUBE_DELEGATE_PRIORITY_NORMAL; $filepath = null; if ($param2 !== null && is_int($param2)) { - $priority = XCUBE_DELEGATE_PRIORITY_NORMAL; + $priority = $param2; $filepath = ($param3 !== null && is_string($param3)) ? $param3 : null; } elseif ($param2 !== null && is_string($param2)) { @@ -185,6 +185,32 @@ } /** + * Disconnect a function from this object. + * @access public + * @return void + */ + function delete($callback) + { + $count = count($this->_mCallbacks); + ksort($this->_mPriorities); + foreach(array_keys($this->_mCallbacks) as $key) { + if ((!is_array($callback)&&($this->_mCallbacks[$key] === $callback))||(is_array($callback)&&((gettype($this->_mCallbacks[$key][0]) === gettype($callback[0]))&&((!is_object($callback[0])&&($this->_mCallbacks[$key][0] === $callback[0])))||((is_object($callback[0])&&(get_class($this->_mCallbacks[$key][0]) === get_class($callback[0])))))&&($this->_mCallbacks[$key][1] === $callback[1]))) { + $this->_mCallbacks = + array_merge(array_slice($this->_mCallbacks, 0, $key), + array_slice($this->_mCallbacks, $key+1)); + + $this->_mPriorities = + array_merge(array_slice($this->_mPriorities, 0, $key), + array_slice($this->_mPriorities, $key+1)); + + $this->_mCallbackPaths = + array_merge(array_slice($this->_mCallbackPaths, 0, $key), + array_slice($this->_mCallbackPaths, $key+1)); + } + } + } + + /** * Call connected functions. * * @access public @@ -260,7 +286,7 @@ } asort($this->_mPriorities); - + // // We have to use eval in the case of an instance method because // call_user_func() can't handle references rightly. @@ -361,6 +387,133 @@ $this->_mCallbackParameters[$name][] = array('0' => $param3, '1' => $param4); } } + + /** + * Disconnect a function from the delegate that have the specified name. + * + * @access public + * @param $name register name + * + * @see XCube_NewDelegate::delete() + */ + function delete($name, $callback) + { + if (isset($this->_mDelegates[$name])) { + $this->_mDelegates[$name]->delete($callback); + } + else { + if (isset($this->_mCallbacks[$name])) { + $count = count($this->_mCallbacks[$name]); + foreach(array_keys($this->_mCallbacks[$name]) as $key) { + if ((!is_array($callback)&&($this->_mCallbacks[$name][$key] === $callback))||(is_array($callback)&&((gettype($this->_mCallbacks[$key][$name][0]) === gettype($callback[0]))&&((!is_object($callback[0])&&($this->_mCallbacks[$key][$name][0] === $callback[0])))||((is_object($callback[0])&&(get_class($this->_mCallbacks[$key][$name][0]) === get_class($callback[0])))))&&($this->_mCallbacks[$key][$name][1] === $callback[1]))) { + $this->_mCallbacks[$name] = + array_merge(array_slice($this->_mCallbacks[$name], 0, $key), + array_slice($this->_mCallbacks[$name], $key+1)); + $this->_mCallbackParameters[$name] = + array_merge(array_slice($this->_mCallbackParameters[$name], 0, $key), + array_slice($this->_mCallbackParameters[$name], $key+1)); + } + } + } + } + } + + + /** + * + * @access public + * @return array of XCube_NewDelegate + */ + function getDelegates() + { + return $this->_mDelegates; + } } +/** + * Delegate Utility functions + * XCube_DelegateUtils::call("Delegate Name"[, fuction args...]); + * XCube_DelegateUtils::raiseEvent("Event Name"[, fuction params...]); + * $string = XCube_DelegateUtils::applyStringFilter("Filter Name", $string, [, option params...]); + */ + +class XCube_DelegateUtils +{ + /** + * Calls a Delegate function quickly + * + * @access public + * @param 1st : Delaget Name + * @param 2nd and more : Delegate function parameters + * @return bool + */ + function call() + { + $args = func_get_args(); + $num = func_num_args(); + if ($num > 0) { + $delegateName = $args[0]; + if ($num > 1) { + array_shift($args); + } + } else { + return false; + } + $root =& XCube_Root::getSingleton(); + if ($root->mDelegateManager != null) { + $delegates = $root->mDelegateManager->getDelegates(); + if (isset($delegates[$delegateName])) { + $delegate =& $delegates[$delegateName]; + } else { + $delegate =& new XCube_NewDelegate; + $root->mDelegateManager->register($delegateName, $delegate); + } + } + return call_user_func_array(array(&$delegate,'call'),$args); + } + + /** + * Alias of call method + * + * @access public + * @param 1st Delaget Name + * @param 2nd and more : Delegate function parameters + * @return bool + */ + function raiseEvent() + { + $args = func_get_args(); + $num = func_num_args(); + if ($num > 0) { + return call_user_func_array(array('XCube_DelegateUtils','call'),$args); + } + } + + /** + * Calls a delegate string filter function + * + * @access public + * @param 1st : Delaget Name + * @param 2nd : String + * @param 3rd and more : Optional function paramaters + * @return string + */ + function applyStringFilter() + { + $args = func_get_args(); + $num = func_num_args(); + if ($num > 2) { + $delegateName = $args[0]; + $string = $args[1]; + if (!empty($strint) && is_string($string)) { + return ""; + } + $args[1] =& new XCube_Ref($string); + call_user_func_array(array('XCube_DelegateUtils','call'),$args); + return $string; + } else { + return ""; + } + } +} ?> \ No newline at end of file