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