1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Response.php - The Jaxon Response |
5
|
|
|
* |
6
|
|
|
* This class collects commands to be sent back to the browser in response to a jaxon request. |
7
|
|
|
* Commands are encoded and packaged in json format. |
8
|
|
|
* |
9
|
|
|
* Common commands include: |
10
|
|
|
* - <Response->assign>: Assign a value to an element's attribute. |
11
|
|
|
* - <Response->append>: Append a value on to an element's attribute. |
12
|
|
|
* - <Response->script>: Execute a portion of javascript code. |
13
|
|
|
* - <Response->call>: Execute an existing javascript function. |
14
|
|
|
* - <Response->alert>: Display an alert dialog to the user. |
15
|
|
|
* |
16
|
|
|
* Elements are identified by the value of the HTML id attribute. |
17
|
|
|
* |
18
|
|
|
* @package jaxon-core |
|
|
|
|
19
|
|
|
* @author Jared White |
|
|
|
|
20
|
|
|
* @author J. Max Wilson |
|
|
|
|
21
|
|
|
* @author Joseph Woolley |
|
|
|
|
22
|
|
|
* @author Steffen Konerow |
|
|
|
|
23
|
|
|
* @author Thierry Feuzeu <[email protected]> |
24
|
|
|
* @copyright Copyright (c) 2005-2007 by Jared White & J. Max Wilson |
|
|
|
|
25
|
|
|
* @copyright Copyright (c) 2008-2010 by Joseph Woolley, Steffen Konerow, Jared White & J. Max Wilson |
|
|
|
|
26
|
|
|
* @copyright 2016 Thierry Feuzeu <[email protected]> |
27
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License |
28
|
|
|
* @link https://github.com/jaxon-php/jaxon-core |
29
|
|
|
*/ |
|
|
|
|
30
|
|
|
|
31
|
|
|
namespace Jaxon\Response; |
32
|
|
|
|
33
|
|
|
class Response extends AbstractResponse |
|
|
|
|
34
|
|
|
{ |
35
|
|
|
use Features\DomCommands; |
36
|
|
|
use Features\JsCommands; |
37
|
|
|
use Features\DomTreeCommands; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* The commands that will be sent to the browser in the response |
41
|
|
|
* |
42
|
|
|
* @var array |
43
|
|
|
*/ |
44
|
|
|
private $aCommands = []; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* A string, array or integer value to be returned to the caller when using 'synchronous' mode requests. |
48
|
|
|
* See <jaxon->setMode> for details. |
49
|
|
|
* |
50
|
|
|
* @var mixed |
51
|
|
|
*/ |
52
|
|
|
private $returnValue; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Get the content type, which is always set to 'application/json' |
56
|
|
|
* |
57
|
|
|
* @return string |
58
|
|
|
*/ |
59
|
|
|
public function getContentType() |
|
|
|
|
60
|
|
|
{ |
61
|
|
|
return 'application/json'; |
62
|
|
|
} |
|
|
|
|
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Provides access to registered response plugins |
66
|
|
|
* |
67
|
|
|
* Pass the plugin name as the first argument and the plugin object will be returned. |
68
|
|
|
* |
69
|
|
|
* @param string $sName The name of the plugin |
|
|
|
|
70
|
|
|
* |
71
|
|
|
* @return null|\Jaxon\Plugin\Response |
72
|
|
|
*/ |
73
|
|
|
public function plugin($sName) |
74
|
|
|
{ |
75
|
|
|
$xPlugin = jaxon()->di()->getPluginManager()->getResponsePlugin($sName); |
76
|
|
|
if(!$xPlugin) |
77
|
|
|
{ |
78
|
|
|
return null; |
79
|
|
|
} |
80
|
|
|
$xPlugin->setResponse($this); |
81
|
|
|
return $xPlugin; |
82
|
|
|
} |
|
|
|
|
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Magic PHP function |
86
|
|
|
* |
87
|
|
|
* Used to permit plugins to be called as if they where native members of the Response instance. |
88
|
|
|
* |
89
|
|
|
* @param string $sPluginName The name of the plugin |
|
|
|
|
90
|
|
|
* |
91
|
|
|
* @return null|\Jaxon\Plugin\Response |
92
|
|
|
*/ |
93
|
|
|
public function __get($sPluginName) |
94
|
|
|
{ |
95
|
|
|
return $this->plugin($sPluginName); |
96
|
|
|
} |
|
|
|
|
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Create a JQuery Element with a given selector, and link it to the current response. |
100
|
|
|
* |
101
|
|
|
* This is a shortcut to the JQuery plugin. |
102
|
|
|
* |
103
|
|
|
* @param string $sSelector The jQuery selector |
|
|
|
|
104
|
|
|
* @param string $sContext A context associated to the selector |
|
|
|
|
105
|
|
|
* |
106
|
|
|
* @return \Jaxon\Response\Plugin\JQuery\Dom\Element |
107
|
|
|
*/ |
108
|
|
|
public function jq($sSelector = '', $sContext = '') |
109
|
|
|
{ |
110
|
|
|
return $this->plugin('jquery')->element($sSelector, $sContext); |
111
|
|
|
} |
|
|
|
|
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Create a JQuery Element with a given selector, and link it to the current response. |
115
|
|
|
* |
116
|
|
|
* This is a shortcut to the JQuery plugin. |
117
|
|
|
* |
118
|
|
|
* @param string $sSelector The jQuery selector |
|
|
|
|
119
|
|
|
* @param string $sContext A context associated to the selector |
|
|
|
|
120
|
|
|
* |
121
|
|
|
* @return \Jaxon\Response\Plugin\JQuery\Dom\Element |
122
|
|
|
*/ |
123
|
|
|
public function jQuery($sSelector = '', $sContext = '') |
124
|
|
|
{ |
125
|
|
|
return $this->jq($sSelector, $sContext); |
126
|
|
|
} |
|
|
|
|
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Add a response command to the array of commands that will be sent to the browser |
130
|
|
|
* |
131
|
|
|
* @param array $aAttributes Associative array of attributes that will describe the command |
|
|
|
|
132
|
|
|
* @param mixed $mData The data to be associated with this command |
|
|
|
|
133
|
|
|
* |
134
|
|
|
* @return Response |
135
|
|
|
*/ |
136
|
|
|
public function addCommand(array $aAttributes, $mData) |
137
|
|
|
{ |
138
|
|
|
array_walk($aAttributes, function(&$sAttribute) { |
139
|
|
|
if(!is_integer($sAttribute)) |
140
|
|
|
{ |
141
|
|
|
$sAttribute = trim((string)$sAttribute, " \t"); |
142
|
|
|
} |
143
|
|
|
}); |
144
|
|
|
|
145
|
|
|
/* merge commands if possible */ |
|
|
|
|
146
|
|
|
if(in_array($aAttributes['cmd'], ['js', 'ap'])) |
147
|
|
|
{ |
148
|
|
|
if(($aLastCommand = array_pop($this->aCommands))) |
|
|
|
|
149
|
|
|
{ |
150
|
|
|
if($aLastCommand['cmd'] == $aAttributes['cmd']) |
151
|
|
|
{ |
152
|
|
|
if($this->getOption('core.response.merge.js') && $aLastCommand['cmd'] == 'js') |
153
|
|
|
{ |
154
|
|
|
$mData = $aLastCommand['data'] . '; ' . $mData; |
|
|
|
|
155
|
|
|
} |
156
|
|
|
elseif($this->getOption('core.response.merge.ap') && |
157
|
|
|
$aLastCommand['cmd'] == 'ap' && |
158
|
|
|
$aLastCommand['id'] == $aAttributes['id'] && |
159
|
|
|
$aLastCommand['prop'] == $aAttributes['prop']) |
160
|
|
|
{ |
161
|
|
|
$mData = $aLastCommand['data'] . ' ' . $mData; |
|
|
|
|
162
|
|
|
} |
163
|
|
|
else |
164
|
|
|
{ |
165
|
|
|
$this->aCommands[] = $aLastCommand; |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
else |
169
|
|
|
{ |
170
|
|
|
$this->aCommands[] = $aLastCommand; |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
$aAttributes['data'] = $mData; |
175
|
|
|
$this->aCommands[] = $aAttributes; |
|
|
|
|
176
|
|
|
|
177
|
|
|
return $this; |
178
|
|
|
} |
|
|
|
|
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Add a response command to the array of commands that will be sent to the browser |
182
|
|
|
* |
183
|
|
|
* @param string $sName The command name |
|
|
|
|
184
|
|
|
* @param array $aAttributes Associative array of attributes that will describe the command |
|
|
|
|
185
|
|
|
* @param mixed $mData The data to be associated with this command |
|
|
|
|
186
|
|
|
* @param boolean $bRemoveEmpty If true, remove empty attributes |
|
|
|
|
187
|
|
|
* |
188
|
|
|
* @return Response |
189
|
|
|
*/ |
190
|
|
|
protected function _addCommand($sName, array $aAttributes, $mData, $bRemoveEmpty = false) |
191
|
|
|
{ |
192
|
|
|
if(is_array($mData)) |
193
|
|
|
{ |
194
|
|
|
array_walk($mData, function(&$sData) { |
195
|
|
|
$sData = trim((string)$sData, " \t\n"); |
196
|
|
|
}); |
197
|
|
|
} |
198
|
|
|
else |
199
|
|
|
{ |
200
|
|
|
$mData = trim((string)$mData, " \t\n"); |
|
|
|
|
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
if($bRemoveEmpty) |
204
|
|
|
{ |
205
|
|
|
foreach(array_keys($aAttributes) as $sAttr) |
206
|
|
|
{ |
207
|
|
|
if($aAttributes[$sAttr] === '') |
208
|
|
|
{ |
209
|
|
|
unset($aAttributes[$sAttr]); |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
$aAttributes['cmd'] = $sName; |
215
|
|
|
return $this->addCommand($aAttributes, $mData); |
216
|
|
|
} |
|
|
|
|
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Clear all the commands already added to the response |
220
|
|
|
* |
221
|
|
|
* @return Response |
222
|
|
|
*/ |
223
|
|
|
public function clearCommands() |
224
|
|
|
{ |
225
|
|
|
$this->aCommands[] = []; |
226
|
|
|
|
227
|
|
|
return $this; |
228
|
|
|
} |
|
|
|
|
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Add a response command that is generated by a plugin |
232
|
|
|
* |
233
|
|
|
* @param \Jaxon\Plugin\Response $xPlugin The plugin object |
|
|
|
|
234
|
|
|
* @param array $aAttributes The attributes for this response command |
|
|
|
|
235
|
|
|
* @param string $mData The data to be sent with this command |
|
|
|
|
236
|
|
|
* |
237
|
|
|
* @return Response |
238
|
|
|
*/ |
239
|
|
|
public function addPluginCommand($xPlugin, $aAttributes, $mData) |
240
|
|
|
{ |
241
|
|
|
$aAttributes['plg'] = $xPlugin->getName(); |
242
|
|
|
return $this->addCommand($aAttributes, $mData); |
243
|
|
|
} |
|
|
|
|
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* Merge the response commands from the specified <Response> object with |
247
|
|
|
* the response commands in this <Response> object |
248
|
|
|
* |
249
|
|
|
* @param AbstractResponse|array $mCommands The <Response> object |
|
|
|
|
250
|
|
|
* @param boolean $bBefore Add the new commands to the beginning of the list |
|
|
|
|
251
|
|
|
* |
252
|
|
|
* @return void |
253
|
|
|
*/ |
254
|
|
|
public function appendResponse($mCommands, $bBefore = false) |
255
|
|
|
{ |
256
|
|
|
$aCommands = []; |
|
|
|
|
257
|
|
|
if($mCommands instanceof AbstractResponse) |
258
|
|
|
{ |
259
|
|
|
$this->returnValue = $mCommands->returnValue; |
|
|
|
|
260
|
|
|
$aCommands = $mCommands->aCommands; |
|
|
|
|
261
|
|
|
} |
262
|
|
|
elseif(is_array($mCommands)) |
263
|
|
|
{ |
264
|
|
|
$aCommands = $mCommands; |
265
|
|
|
} |
266
|
|
|
else |
267
|
|
|
{ |
268
|
|
|
throw new \Jaxon\Exception\Error(jaxon_trans('errors.response.data.invalid')); |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
if(count($aCommands) > 0) |
272
|
|
|
{ |
273
|
|
|
if($bBefore) |
274
|
|
|
{ |
275
|
|
|
$this->aCommands = array_merge($aCommands, $this->aCommands); |
276
|
|
|
} |
277
|
|
|
else |
278
|
|
|
{ |
279
|
|
|
$this->aCommands = array_merge($this->aCommands, $aCommands); |
280
|
|
|
} |
281
|
|
|
} |
282
|
|
|
} |
|
|
|
|
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* Get the commands in the response |
286
|
|
|
* |
287
|
|
|
* @return array |
288
|
|
|
*/ |
289
|
|
|
public function getCommands() |
290
|
|
|
{ |
291
|
|
|
return $this->aCommands; |
292
|
|
|
} |
|
|
|
|
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* Get the number of commands in the response |
296
|
|
|
* |
297
|
|
|
* @return integer |
298
|
|
|
*/ |
299
|
|
|
public function getCommandCount() |
300
|
|
|
{ |
301
|
|
|
return count($this->aCommands); |
302
|
|
|
} |
|
|
|
|
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* Stores a value that will be passed back as part of the response |
306
|
|
|
* |
307
|
|
|
* When making synchronous requests, the calling javascript can obtain this value |
308
|
|
|
* immediately as the return value of the <jaxon.call> javascript function |
309
|
|
|
* |
310
|
|
|
* @param mixed $value Any value |
|
|
|
|
311
|
|
|
* |
312
|
|
|
* @return Response |
313
|
|
|
*/ |
314
|
|
|
public function setReturnValue($value) |
315
|
|
|
{ |
316
|
|
|
$this->returnValue = $value; |
317
|
|
|
return $this; |
318
|
|
|
} |
|
|
|
|
319
|
|
|
|
320
|
|
|
/** |
321
|
|
|
* Return the output, generated from the commands added to the response, that will be sent to the browser |
322
|
|
|
* |
323
|
|
|
* @return string |
324
|
|
|
*/ |
325
|
|
|
public function getOutput() |
326
|
|
|
{ |
327
|
|
|
$response = [ |
328
|
|
|
'jxnobj' => [], |
329
|
|
|
]; |
330
|
|
|
|
331
|
|
|
if(($this->returnValue)) |
332
|
|
|
{ |
333
|
|
|
$response['jxnrv'] = $this->returnValue; |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
foreach($this->aCommands as $xCommand) |
337
|
|
|
{ |
338
|
|
|
$response['jxnobj'][] = $xCommand; |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
return json_encode($response); |
342
|
|
|
} |
|
|
|
|
343
|
|
|
} |
344
|
|
|
|