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