1 | <?php |
---|
2 | /* |
---|
3 | File: xajaxCallableObjectPlugin.inc.php |
---|
4 | |
---|
5 | Contains the xajaxCallableObjectPlugin class |
---|
6 | |
---|
7 | Title: xajaxCallableObjectPlugin 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: xajaxCallableObjectPlugin.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 | Constant: XAJAX_CALLABLE_OBJECT |
---|
22 | Specifies that the item being registered via the <xajax->register> function is a |
---|
23 | object who's methods will be callable from the browser. |
---|
24 | */ |
---|
25 | if (!defined ('XAJAX_CALLABLE_OBJECT')) define ('XAJAX_CALLABLE_OBJECT', 'callable object'); |
---|
26 | |
---|
27 | //SkipAIO |
---|
28 | require dirname(__FILE__) . '/support/xajaxCallableObject.inc.php'; |
---|
29 | //EndSkipAIO |
---|
30 | |
---|
31 | /* |
---|
32 | Class: xajaxCallableObjectPlugin |
---|
33 | */ |
---|
34 | class xajaxCallableObjectPlugin extends xajaxRequestPlugin |
---|
35 | { |
---|
36 | /* |
---|
37 | Array: aCallableObjects |
---|
38 | */ |
---|
39 | var $aCallableObjects; |
---|
40 | |
---|
41 | /* |
---|
42 | String: sXajaxPrefix |
---|
43 | */ |
---|
44 | var $sXajaxPrefix; |
---|
45 | |
---|
46 | /* |
---|
47 | String: sDefer |
---|
48 | */ |
---|
49 | var $sDefer; |
---|
50 | |
---|
51 | var $bDeferScriptGeneration; |
---|
52 | |
---|
53 | /* |
---|
54 | String: sRequestedClass |
---|
55 | */ |
---|
56 | var $sRequestedClass; |
---|
57 | |
---|
58 | /* |
---|
59 | String: sRequestedMethod |
---|
60 | */ |
---|
61 | var $sRequestedMethod; |
---|
62 | |
---|
63 | /* |
---|
64 | Function: xajaxCallableObjectPlugin |
---|
65 | */ |
---|
66 | function xajaxCallableObjectPlugin() |
---|
67 | { |
---|
68 | $this->aCallableObjects = array(); |
---|
69 | |
---|
70 | $this->sXajaxPrefix = 'xajax_'; |
---|
71 | $this->sDefer = ''; |
---|
72 | $this->bDeferScriptGeneration = false; |
---|
73 | |
---|
74 | $this->sRequestedClass = NULL; |
---|
75 | $this->sRequestedMethod = NULL; |
---|
76 | |
---|
77 | if (!empty($_GET['xjxcls'])) $this->sRequestedClass = $_GET['xjxcls']; |
---|
78 | if (!empty($_GET['xjxmthd'])) $this->sRequestedMethod = $_GET['xjxmthd']; |
---|
79 | if (!empty($_POST['xjxcls'])) $this->sRequestedClass = $_POST['xjxcls']; |
---|
80 | if (!empty($_POST['xjxmthd'])) $this->sRequestedMethod = $_POST['xjxmthd']; |
---|
81 | } |
---|
82 | |
---|
83 | /* |
---|
84 | Function: configure |
---|
85 | */ |
---|
86 | function configure($sName, $mValue) |
---|
87 | { |
---|
88 | if ('wrapperPrefix' == $sName) { |
---|
89 | $this->sXajaxPrefix = $mValue; |
---|
90 | } else if ('scriptDefferal' == $sName) { |
---|
91 | if (true === $mValue) $this->sDefer = 'defer '; |
---|
92 | else $this->sDefer = ''; |
---|
93 | } else if ('deferScriptGeneration' == $sName) { |
---|
94 | if (true === $mValue || false === $mValue) |
---|
95 | $this->bDeferScriptGeneration = $mValue; |
---|
96 | else if ('deferred' === $mValue) |
---|
97 | $this->bDeferScriptGeneration = $mValue; |
---|
98 | } |
---|
99 | } |
---|
100 | |
---|
101 | /* |
---|
102 | Function: register |
---|
103 | */ |
---|
104 | function register($aArgs) |
---|
105 | { |
---|
106 | if (1 < count($aArgs)) |
---|
107 | { |
---|
108 | $sType = $aArgs[0]; |
---|
109 | |
---|
110 | if (XAJAX_CALLABLE_OBJECT == $sType) |
---|
111 | { |
---|
112 | $xco =& $aArgs[1]; |
---|
113 | |
---|
114 | //SkipDebug |
---|
115 | if (false === is_object($xco)) |
---|
116 | { |
---|
117 | trigger_error("To register a callable object, please provide an instance of the desired class.", E_USER_WARNING); |
---|
118 | return false; |
---|
119 | } |
---|
120 | //EndSkipDebug |
---|
121 | |
---|
122 | if (false === is_a($xco, 'xajaxCallableObject')) |
---|
123 | $xco =& new xajaxCallableObject($xco); |
---|
124 | |
---|
125 | if (2 < count($aArgs)) |
---|
126 | if (is_array($aArgs[2])) |
---|
127 | foreach ($aArgs[2] as $sKey => $aValue) |
---|
128 | foreach ($aValue as $sName => $sValue) |
---|
129 | $xco->configure($sKey, $sName, $sValue); |
---|
130 | |
---|
131 | $this->aCallableObjects[] =& $xco; |
---|
132 | |
---|
133 | return $xco->generateRequests($this->sXajaxPrefix); |
---|
134 | } |
---|
135 | } |
---|
136 | |
---|
137 | return false; |
---|
138 | } |
---|
139 | |
---|
140 | /* |
---|
141 | Function: generateClientScript |
---|
142 | */ |
---|
143 | function generateClientScript() |
---|
144 | { |
---|
145 | if (false === $this->bDeferScriptGeneration || 'deferred' === $this->bDeferScriptGeneration) |
---|
146 | { |
---|
147 | if (0 < count($this->aCallableObjects)) |
---|
148 | { |
---|
149 | $sCrLf = "\n"; |
---|
150 | |
---|
151 | print $sCrLf; |
---|
152 | print '<'; |
---|
153 | print 'script type="text/javascript" '; |
---|
154 | print $this->sDefer; |
---|
155 | print 'charset="UTF-8">'; |
---|
156 | print $sCrLf; |
---|
157 | print '/* <'; |
---|
158 | print '![CDATA[ */'; |
---|
159 | print $sCrLf; |
---|
160 | |
---|
161 | foreach(array_keys($this->aCallableObjects) as $sKey) |
---|
162 | $this->aCallableObjects[$sKey]->generateClientScript($this->sXajaxPrefix); |
---|
163 | |
---|
164 | print '/* ]]> */'; |
---|
165 | print $sCrLf; |
---|
166 | print '<'; |
---|
167 | print '/script>'; |
---|
168 | print $sCrLf; |
---|
169 | } |
---|
170 | } |
---|
171 | } |
---|
172 | |
---|
173 | /* |
---|
174 | Function: canProcessRequest |
---|
175 | */ |
---|
176 | function canProcessRequest() |
---|
177 | { |
---|
178 | if (NULL == $this->sRequestedClass) |
---|
179 | return false; |
---|
180 | if (NULL == $this->sRequestedMethod) |
---|
181 | return false; |
---|
182 | |
---|
183 | return true; |
---|
184 | } |
---|
185 | |
---|
186 | /* |
---|
187 | Function: processRequest |
---|
188 | */ |
---|
189 | function processRequest() |
---|
190 | { |
---|
191 | if (NULL == $this->sRequestedClass) |
---|
192 | return false; |
---|
193 | if (NULL == $this->sRequestedMethod) |
---|
194 | return false; |
---|
195 | |
---|
196 | $objArgumentManager =& xajaxArgumentManager::getInstance(); |
---|
197 | $aArgs = $objArgumentManager->process(); |
---|
198 | |
---|
199 | foreach (array_keys($this->aCallableObjects) as $sKey) |
---|
200 | { |
---|
201 | $xco =& $this->aCallableObjects[$sKey]; |
---|
202 | |
---|
203 | if ($xco->isClass($this->sRequestedClass)) |
---|
204 | { |
---|
205 | if ($xco->hasMethod($this->sRequestedMethod)) |
---|
206 | { |
---|
207 | $xco->call($this->sRequestedMethod, $aArgs); |
---|
208 | return true; |
---|
209 | } |
---|
210 | } |
---|
211 | } |
---|
212 | |
---|
213 | return 'Invalid request for a callable object.'; |
---|
214 | } |
---|
215 | } |
---|
216 | |
---|
217 | $objPluginManager =& xajaxPluginManager::getInstance(); |
---|
218 | $objPluginManager->registerPlugin(new xajaxCallableObjectPlugin(), 102); |
---|