1: <?php
2: /*
3: File: xajaxCallableObject.inc.php
4:
5: Contains the xajaxCallableObject class
6:
7: Title: xajaxCallableObject class
8:
9: Please see <copyright.inc.php> for a detailed description, copyright
10: and license information.
11: */
12:
13: /*
14: @package xajax
15: @version $Id: xajaxCallableObject.inc.php 362 2007-05-29 15:32:24Z calltoconstruct $
16: @copyright Copyright (c) 2005-2007 by Jared White & J. Max Wilson
17: @copyright Copyright (c) 2008-2010 by Joseph Woolley, Steffen Konerow, Jared White & J. Max Wilson
18: @license http://www.xajaxproject.org/bsd_license.txt BSD License
19: */
20:
21: /*
22: Class: xajaxCallableObject
23:
24: A class that stores a reference to an object whose methods can be called from
25: the client via a xajax request. <xajax> will call
26: <xajaxCallableObject->generateClientScript> so that stub functions can be
27: generated and sent to the browser.
28: */
29: final class xajaxCallableObject
30: {
31: /*
32: Object: obj
33:
34: A reference to the callable object.
35: */
36: private $obj;
37:
38: /*
39: Array: aConfiguration
40:
41: An associative array that will contain configuration options for zero
42: or more of the objects methods. These configuration options will
43: define the call options for each request. The call options will be
44: passed to the client browser when the function stubs are generated.
45: */
46: private $aConfiguration;
47:
48: /*
49: Function: xajaxCallableObject
50:
51: Constructs and initializes the <xajaxCallableObject>
52:
53: obj - (object): The object to reference.
54: */
55: public function __construct($obj)
56: {
57: $this->obj = $obj;
58: $this->aConfiguration = array();
59: }
60:
61: /*
62: Function: getName
63:
64: Returns the name of this callable object. This is typically the
65: class name of the object.
66: */
67: public function getName()
68: {
69: return get_class($this->obj);
70: }
71:
72: /*
73: Function: configure
74:
75: Used to set configuration options / call options for each method.
76:
77: sMethod - (string): The name of the method.
78: sName - (string): The name of the configuration option.
79: sValue - (string): The value to be set.
80: */
81: public function configure($sMethod, $sName, $sValue)
82: {
83: $sMethod = strtolower($sMethod);
84:
85: if (false == isset($this->aConfiguration[$sMethod]))
86: $this->aConfiguration[$sMethod] = array();
87:
88: $this->aConfiguration[$sMethod][$sName] = $sValue;
89: }
90:
91: /*
92: Function: generateRequests
93:
94: Produces an array of <xajaxRequest> objects, one for each method
95: exposed by this callable object.
96:
97: sXajaxPrefix - (string): The prefix to be prepended to the
98: javascript function names; this will correspond to the name
99: used for the function stubs that are generated by the
100: <xajaxCallableObject->generateClientScript> call.
101: */
102: public function generateRequests($sXajaxPrefix)
103: {
104: $aRequests = array();
105:
106: $sClass = get_class($this->obj);
107:
108: foreach (get_class_methods($this->obj) as $sMethodName)
109: {
110: $bInclude = true;
111: // exclude magic __call, __construct, __destruct methods
112: if (2 < strlen($sMethodName))
113: if ("__" == substr($sMethodName, 0, 2))
114: $bInclude = false;
115: // exclude constructor
116: if ($sClass == $sMethodName)
117: $bInclude = false;
118: if ($bInclude)
119: $aRequests[strtolower($sMethodName)] =
120: new xajaxRequest("{$sXajaxPrefix}{$sClass}.{$sMethodName}");
121: }
122:
123: return $aRequests;
124: }
125:
126: /*
127: Function: generateClientScript
128:
129: Called by <xajaxCallableObject->generateClientScript> while <xajax> is
130: generating the javascript to be sent to the browser.
131:
132: sXajaxPrefix - (string): The prefix to be prepended to the
133: javascript function names.
134: */
135: public function generateClientScript($sXajaxPrefix)
136: {
137: $sClass = get_class($this->obj);
138:
139: echo "{$sXajaxPrefix}{$sClass} = {};\n";
140:
141: foreach (get_class_methods($this->obj) as $sMethodName)
142: {
143: $bInclude = true;
144: // exclude magic __call, __construct, __destruct methods
145: if (2 < strlen($sMethodName))
146: if ("__" == substr($sMethodName, 0, 2))
147: $bInclude = false;
148: // exclude constructor
149: if ($sClass == $sMethodName)
150: $bInclude = false;
151: if ($bInclude)
152: {
153: echo "{$sXajaxPrefix}{$sClass}.{$sMethodName} = function() { ";
154: echo "return xajax.request( ";
155: echo "{ xjxcls: '{$sClass}', xjxmthd: '{$sMethodName}' }, ";
156: echo "{ parameters: arguments";
157:
158: $sSeparator = ", ";
159: if (isset($this->aConfiguration['*']))
160: foreach ($this->aConfiguration['*'] as $sKey => $sValue)
161: echo "{$sSeparator}{$sKey}: {$sValue}";
162: if (isset($this->aConfiguration[strtolower($sMethodName)]))
163: foreach ($this->aConfiguration[strtolower($sMethodName)] as $sKey => $sValue)
164: echo "{$sSeparator}{$sKey}: {$sValue}";
165:
166: echo " } ); ";
167: echo "};\n";
168: }
169: }
170: }
171:
172: /*
173: Function: isClass
174:
175: Determins if the specified class name matches the class name of the
176: object referenced by <xajaxCallableObject->obj>.
177:
178: sClass - (string): The name of the class to check.
179:
180: Returns:
181:
182: boolean - True of the specified class name matches the class of
183: the object being referenced; false otherwise.
184: */
185: public function isClass($sClass)
186: {
187: if (get_class($this->obj) === $sClass)
188: return true;
189: return false;
190: }
191:
192: /*
193: Function: hasMethod
194:
195: Determines if the specified method name is one of the methods of the
196: object referenced by <xajaxCallableObject->obj>.
197:
198: sMethod - (object): The name of the method to check.
199:
200: Returns:
201:
202: boolean - True of the referenced object contains the specified method,
203: false otherwise.
204: */
205: public function hasMethod($sMethod)
206: {
207: return method_exists($this->obj, $sMethod) || method_exists($this->obj, "__call");
208: }
209:
210: /*
211: Function: call
212:
213: Call the specified method of the object being referenced using the specified
214: array of arguments.
215:
216: sMethod - (string): The name of the method to call.
217: aArgs - (array): The arguments to pass to the method.
218: */
219: public function call($sMethod, $aArgs)
220: {
221: $objResponseManager = xajaxResponseManager::getInstance();
222: $objResponseManager->append(
223: call_user_func_array(
224: array($this->obj, $sMethod),
225: $aArgs
226: )
227: );
228: }
229: }
230: