[xoops-cvslog 3334] CVS update: xoops2jp/html/kernel

Back to archive index

Minahito minah****@users*****
2006年 6月 22日 (木) 14:31:41 JST


Index: xoops2jp/html/kernel/XCube_Delegate.class.php
diff -u /dev/null xoops2jp/html/kernel/XCube_Delegate.class.php:1.1.2.1
--- /dev/null	Thu Jun 22 14:31:41 2006
+++ xoops2jp/html/kernel/XCube_Delegate.class.php	Thu Jun 22 14:31:41 2006
@@ -0,0 +1,295 @@
+<?php
+/**
+ * @version $Id
+ */
+ 
+
+/**
+ * This class is an expression of reference in delegation mechanism for PHP4.
+ */
+class XCube_Ref
+{
+	var $_mObject = null;
+
+	function XCube_Ref(&$obj)
+	{
+		$this->_mObject =& $obj;
+	}
+
+	function &getObject()
+	{
+		return $this->_mObject;
+	}
+}
+
+/**
+ * This is simple mechanism for common delegation in XCube.
+ * 
+ * [Notice]
+ * This is the candidate as new delegate style, which has foolish name to escape
+ * conflict with old XCube_Delegate. After replacing, we'll change all.
+ */
+class XCube_NewDelegate
+{
+	var $_mSignatures = array();
+	var $_mCallbacks = array();
+	var $_mCallbackInstances = array();
+	var $_mCallbackPaths = array();
+	
+	var $_mHasCheckSignatures = false;
+	
+	/**
+	 * If register() is failed, this flag become true.
+	 */
+	var $_mIsLazyRegister = false;
+	var $_mLazyRegisterName = null;
+	
+	function XCube_NewDelegate()
+	{
+		if (func_num_args() > 0) {
+			$this->_setSignatures(func_get_args());
+		}
+	}
+	
+	function _setSignatures($args)
+	{
+		$this->_mSignatures = $args;
+		for ($i=0 ; $i<count($args) ; $i++) {
+			$idx = strpos($this->_mSignatures[$i], " &");
+			if ($idx > 0) {
+				$this->_mSignatures[$i] = substr($this->_mSignatures[$i], 0, $idx);
+			}
+		}
+	}
+	
+	/**
+	 * Register this object to delegate manager of root.
+	 *
+	 * @access public
+	 * @param $delegateName string
+	 * @return bool
+	 */
+	function register($delegateName)
+	{
+		$root =& XCube_Root::getSingleton();
+		if ($root->mDelegateManager != null) {
+			$this->_mIsLazyRegister = false;
+			$this->_mLazyRegisterName = null;
+		
+			return $root->mDelegateManager->register($delegateName, $this);
+		}
+		
+		$this->_mIsLazyRegister = true;
+		$this->_mLazyRegisterName = $delegateName;
+
+		return false;
+	}
+
+	/**
+	 * Connect static functions or static member functions to this object.
+	 *
+	 * @access public
+	 * @param $func callback
+	 */
+	function add($func, $path = null)
+	{
+		if (strstr($func, "::")) {
+			$tmp = explode("::", $func);
+			$this->_mCallbackInstances[] = $tmp[0];
+			$this->_mCallbacks[] = $tmp[1];
+			$this->_mCallbackPaths[] = $path;
+		}
+		else {
+			$this->_mCallbackInstances[] = null;
+			$this->_mCallbacks[] = $func;
+			$this->_mCallbackPaths[] = $path;
+		}
+	}
+	
+	/**
+	 * Conect member functions(=instance method) to this object.
+	 *
+	 * @access public
+	 * @param $obj Object
+	 * @param $method string
+	 */
+	function addMethod(&$obj, $method)
+	{
+		$this->_mCallbackInstances[] =& $obj;
+		$this->_mCallbacks[] = $method;
+		$this->_mCallbackPaths[] = null;
+	}
+
+	/**
+	 * Call connected functions.
+	 *
+	 * @access public
+	 */
+	function call()
+	{
+		$args = func_get_args();
+		$num = func_num_args();
+		
+		if ($this->_mIsLazyRegister) {
+			$this->register($this->_mLazyRegisterName);
+		}
+		
+		if ($this->_mHasCheckSignatures) {
+			if (count($this->_mSignatures) != $num) {
+				return false;
+			}
+		}
+		
+		for ($i=0 ; $i<$num ;$i++) {
+			if (is_a($args[$i], "XCube_Ref")) {
+				$args[$i] =& $args[$i]->getObject();
+			}
+			
+			if ($this->_mHasCheckSignatures) {
+				if (!isset($this->_mSignatures[$i])) {
+					return false;
+				}
+				
+				switch ($this->_mSignatures[$i]) {
+					case "void":
+						break;
+					
+					case "bool":
+						if (!empty($args[$i])) {
+							$args[$i] = $args[$i] ? true : false;
+						}
+						break;
+
+					case "int":
+						if (!empty($args[$i])) {
+							$args[$i] = intval($args[$i]);
+						}
+						break;
+					
+					case "float":
+						if (!empty($args[$i])) {
+							$args[$i] = floatval($args[$i]);
+						}
+						break;
+
+					case "string":
+						if (!empty($args[$i]) && is_string($args[$i])) {
+							return;
+						}
+						break;
+					
+					default:
+						if (!is_a($args[$i], $this->_mSignatures[$i])) {
+							return;
+						}
+				}
+			}
+		
+			$param[] = '$args[' . $i . ']';
+		}
+		
+		$argstr = "(" . join($param, ",") . ");";
+
+		//
+		// We have to use eval in the case of an instance method because 
+		// call_user_func() can't handle references rightly.
+		//		
+		foreach (array_keys($this->_mCallbackInstances) as $key) {
+			if (is_object($this->_mCallbackInstances[$key]) && method_exists($this->_mCallbackInstances[$key], $this->_mCallbacks[$key])) {
+				eval('$this->_mCallbackInstances[$key]->' . $this->_mCallbacks[$key] . $argstr);
+			}
+			else {
+				$path = $this->_mCallbackPaths[$key];
+				if ($path != null && file_exists($path)) {
+					require_once $path;
+				}
+				if ($this->_mCallbackInstances[$key] != null && is_callable(array($this->_mCallbackInstances[$key], $this->_mCallbacks[$key]))) {
+						call_user_func_array(array($this->_mCallbackInstances[$key], $this->_mCallbacks[$key]), $args);
+					}
+				else if(function_exists($this->_mCallbacks[$key])) {
+					call_user_func_array($this->_mCallbacks[$key], $args);
+				}
+			}
+		}
+	}
+}
+
+/**
+ * This is the proxy for un-registed delegate objects.
+ */
+class XCube_DelegateManager
+{
+	var $_mCallbacks = array();
+	var $_mCallbackInstances = array();
+	var $_mCallbackPaths = array();
+	
+	var $_mDelegates = array();
+
+	function XCube_DelegateManager()
+	{
+	}
+	
+	function register($name, &$delegate)
+	{
+		if (!isset($this->_mDelegates[$name])) {
+			$this->_mDelegates[$name] =& $delegate;
+			
+			if (isset($this->_mCallbackInstances[$name])) {
+				foreach (array_keys($this->_mCallbackInstances[$name]) as $key => $val) {
+					if ($val != null) {
+						$delegate->addMethod($this->_mCallbackInstances[$name][$key], $this->_mCallbacks[$name][$key], $this->_mCallbackPaths[$name][$key]);
+					}
+					else {
+						$delegate->add($this->_mCallbacks[$name][$key], $this->_mCallbackPaths[$name][$key]);
+					}
+				}
+				unset($this->_mCallbackInstances[$name]);
+			}
+			
+			return true;
+		}
+		else {
+			return false;
+		}
+	}
+	
+	/**
+	 * Connect static functions or static member functions to this object.
+	 *
+	 * @access public
+	 * @param $func callback
+	 * @param $path string If $path is specified, load first it before execute callback.
+	 */
+	function add($name, $func, $path = null)
+	{
+		if (isset($this->_mDelegates[$name])) {
+			$this->_mDelegates[$name]->add($func, $path);
+		}
+		else {
+			$this->_mCallbackInstances[$name][] = null;
+			$this->_mCallbacks[$name][] = $func;
+			$this->_mCallbackPaths[$name][] = $path;
+		}
+	}
+
+	/**
+	 * Conect member functions(=instance method) to this object.
+	 *
+	 * @access public
+	 * @param $obj Object
+	 * @param $method string
+	 */
+	function addMethod($name, &$obj, $method)
+	{
+		if (isset($this->_mDelegates[$name])) {
+			$this->_mDelegates[$name]->addMethod($obj, $method);
+		}
+		else {
+			$this->_mCallbackInstances[$name][] =& $obj;
+			$this->_mCallbacks[$name][] = $method;
+			$this->_mCallbackPaths[$name][] = null;
+		}
+	}
+}
+
+?>
\ No newline at end of file


xoops-cvslog メーリングリストの案内
Back to archive index