|
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
|
|
|
use Jaxon\Plugin\PluginManager; |
|
34
|
|
|
use Jaxon\Plugin\ResponsePlugin; |
|
35
|
|
|
use Jaxon\Response\Plugin\DataBag\DataBagContext; |
|
36
|
|
|
use Jaxon\Response\Plugin\JQuery\DomSelector; |
|
37
|
|
|
use Jaxon\Utils\Translation\Translator; |
|
38
|
|
|
use Jaxon\Exception\RequestException; |
|
39
|
|
|
|
|
40
|
|
|
use function array_keys; |
|
41
|
|
|
use function array_map; |
|
42
|
|
|
use function array_merge; |
|
43
|
|
|
use function count; |
|
44
|
|
|
use function is_array; |
|
45
|
|
|
use function is_integer; |
|
46
|
|
|
use function json_encode; |
|
47
|
|
|
use function trim; |
|
48
|
|
|
|
|
49
|
|
|
class Response extends AbstractResponse |
|
|
|
|
|
|
50
|
|
|
{ |
|
51
|
|
|
use Traits\DomTrait; |
|
52
|
|
|
use Traits\JsTrait; |
|
53
|
|
|
use Traits\DomTreeTrait; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @var Translator |
|
57
|
|
|
*/ |
|
58
|
|
|
protected $xTranslator; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @var PluginManager |
|
62
|
|
|
*/ |
|
63
|
|
|
protected $xPluginManager; |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* The commands that will be sent to the browser in the response |
|
67
|
|
|
* |
|
68
|
|
|
* @var array |
|
69
|
|
|
*/ |
|
70
|
|
|
protected $aCommands = []; |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* A string, array or integer value to be returned to the caller when using 'synchronous' mode requests. |
|
74
|
|
|
* See <jaxon->setMode> for details. |
|
75
|
|
|
* |
|
76
|
|
|
* @var mixed |
|
77
|
|
|
*/ |
|
78
|
|
|
protected $xReturnValue; |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* The constructor |
|
82
|
|
|
* |
|
83
|
|
|
* @param Translator $xTranslator |
|
|
|
|
|
|
84
|
|
|
* @param PluginManager $xPluginManager |
|
|
|
|
|
|
85
|
|
|
*/ |
|
86
|
|
|
public function __construct(Translator $xTranslator, PluginManager $xPluginManager) |
|
|
|
|
|
|
87
|
|
|
{ |
|
88
|
|
|
$this->xTranslator = $xTranslator; |
|
|
|
|
|
|
89
|
|
|
$this->xPluginManager = $xPluginManager; |
|
90
|
|
|
} |
|
|
|
|
|
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Create a new Jaxon response object |
|
94
|
|
|
* |
|
95
|
|
|
* @return Response |
|
96
|
|
|
*/ |
|
97
|
|
|
public function newResponse(): Response |
|
98
|
|
|
{ |
|
99
|
|
|
return new Response($this->xTranslator, $this->xPluginManager); |
|
100
|
|
|
} |
|
|
|
|
|
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Get the content type, which is always set to 'application/json' |
|
104
|
|
|
* |
|
105
|
|
|
* @return string |
|
106
|
|
|
*/ |
|
107
|
|
|
public function getContentType(): string |
|
108
|
|
|
{ |
|
109
|
|
|
return 'application/json'; |
|
110
|
|
|
} |
|
|
|
|
|
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Provides access to registered response plugins |
|
114
|
|
|
* |
|
115
|
|
|
* Pass the plugin name as the first argument and the plugin object will be returned. |
|
116
|
|
|
* |
|
117
|
|
|
* @param string $sName The name of the plugin |
|
|
|
|
|
|
118
|
|
|
* |
|
119
|
|
|
* @return null|ResponsePlugin |
|
120
|
|
|
*/ |
|
121
|
|
|
public function plugin(string $sName): ?ResponsePlugin |
|
122
|
|
|
{ |
|
123
|
|
|
return $this->xPluginManager->getResponsePlugin($sName, $this); |
|
124
|
|
|
} |
|
|
|
|
|
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Magic PHP function |
|
128
|
|
|
* |
|
129
|
|
|
* Used to permit plugins to be called as if they were native members of the Response instance. |
|
130
|
|
|
* |
|
131
|
|
|
* @param string $sPluginName The name of the plugin |
|
|
|
|
|
|
132
|
|
|
* |
|
133
|
|
|
* @return null|ResponsePlugin |
|
134
|
|
|
*/ |
|
135
|
|
|
public function __get(string $sPluginName) |
|
136
|
|
|
{ |
|
137
|
|
|
return $this->plugin($sPluginName); |
|
138
|
|
|
} |
|
|
|
|
|
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Create a JQuery DomSelector, and link it to the current response. |
|
142
|
|
|
* |
|
143
|
|
|
* This is a shortcut to the JQuery plugin. |
|
144
|
|
|
* |
|
145
|
|
|
* @param string $sPath The jQuery selector path |
|
146
|
|
|
* @param string $sContext A context associated to the selector |
|
|
|
|
|
|
147
|
|
|
* |
|
148
|
|
|
* @return DomSelector |
|
149
|
|
|
*/ |
|
150
|
|
|
public function jq(string $sPath = '', string $sContext = ''): DomSelector |
|
151
|
|
|
{ |
|
152
|
|
|
return $this->plugin('jquery')->selector($sPath, $sContext); |
|
|
|
|
|
|
153
|
|
|
} |
|
|
|
|
|
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* @param string $sName |
|
|
|
|
|
|
157
|
|
|
* |
|
158
|
|
|
* @return DataBagContext |
|
159
|
|
|
*/ |
|
160
|
|
|
public function bag(string $sName): DataBagContext |
|
161
|
|
|
{ |
|
162
|
|
|
return $this->plugin('bags')->bag($sName);; |
|
|
|
|
|
|
163
|
|
|
} |
|
|
|
|
|
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Add a response command to the array of commands that will be sent to the browser |
|
167
|
|
|
* |
|
168
|
|
|
* @param array $aAttributes Associative array of attributes that will describe the command |
|
|
|
|
|
|
169
|
|
|
* @param mixed $mData The data to be associated with this command |
|
|
|
|
|
|
170
|
|
|
* |
|
171
|
|
|
* @return Response |
|
172
|
|
|
*/ |
|
173
|
|
|
public function addCommand(array $aAttributes, $mData): Response |
|
174
|
|
|
{ |
|
175
|
|
|
$aAttributes = array_map(function($xAttribute) { |
|
176
|
|
|
return is_integer($xAttribute) ? $xAttribute : trim((string)$xAttribute, " \t"); |
|
177
|
|
|
}, $aAttributes); |
|
178
|
|
|
|
|
179
|
|
|
$aAttributes['data'] = $mData; |
|
180
|
|
|
$this->aCommands[] = $aAttributes; |
|
|
|
|
|
|
181
|
|
|
|
|
182
|
|
|
return $this; |
|
183
|
|
|
} |
|
|
|
|
|
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Add a response command to the array of commands that will be sent to the browser |
|
187
|
|
|
* |
|
188
|
|
|
* @param string $sName The command name |
|
|
|
|
|
|
189
|
|
|
* @param array $aAttributes Associative array of attributes that will describe the command |
|
|
|
|
|
|
190
|
|
|
* @param mixed $mData The data to be associated with this command |
|
|
|
|
|
|
191
|
|
|
* @param bool $bRemoveEmpty If true, remove empty attributes |
|
|
|
|
|
|
192
|
|
|
* |
|
193
|
|
|
* @return Response |
|
194
|
|
|
*/ |
|
195
|
|
|
protected function _addCommand(string $sName, array $aAttributes, $mData, bool $bRemoveEmpty = false): Response |
|
196
|
|
|
{ |
|
197
|
|
|
$mData = is_array($mData) ? array_map(function($sData) { |
|
198
|
|
|
return trim((string)$sData, " \t\n"); |
|
199
|
|
|
}, $mData) : trim((string)$mData, " \t\n"); |
|
200
|
|
|
|
|
201
|
|
|
if($bRemoveEmpty) |
|
202
|
|
|
{ |
|
203
|
|
|
foreach(array_keys($aAttributes) as $sAttr) |
|
204
|
|
|
{ |
|
205
|
|
|
if($aAttributes[$sAttr] === '') |
|
206
|
|
|
{ |
|
207
|
|
|
unset($aAttributes[$sAttr]); |
|
208
|
|
|
} |
|
209
|
|
|
} |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
$aAttributes['cmd'] = $sName; |
|
213
|
|
|
return $this->addCommand($aAttributes, $mData); |
|
214
|
|
|
} |
|
|
|
|
|
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* Clear all the commands already added to the response |
|
218
|
|
|
* |
|
219
|
|
|
* @return Response |
|
220
|
|
|
*/ |
|
221
|
|
|
public function clearCommands(): Response |
|
222
|
|
|
{ |
|
223
|
|
|
$this->aCommands = []; |
|
224
|
|
|
return $this; |
|
225
|
|
|
} |
|
|
|
|
|
|
226
|
|
|
|
|
227
|
|
|
/** |
|
228
|
|
|
* Add a response command that is generated by a plugin |
|
229
|
|
|
* |
|
230
|
|
|
* @param ResponsePlugin $xPlugin The plugin object |
|
|
|
|
|
|
231
|
|
|
* @param array $aAttributes The attributes for this response command |
|
|
|
|
|
|
232
|
|
|
* @param mixed $mData The data to be sent with this command |
|
|
|
|
|
|
233
|
|
|
* |
|
234
|
|
|
* @return Response |
|
235
|
|
|
*/ |
|
236
|
|
|
public function addPluginCommand(ResponsePlugin $xPlugin, array $aAttributes, $mData): Response |
|
237
|
|
|
{ |
|
238
|
|
|
$aAttributes['plg'] = $xPlugin->getName(); |
|
239
|
|
|
return $this->addCommand($aAttributes, $mData); |
|
240
|
|
|
} |
|
|
|
|
|
|
241
|
|
|
|
|
242
|
|
|
/** |
|
243
|
|
|
* Merge the response commands from the specified <Response> object with |
|
244
|
|
|
* the response commands in this <Response> object |
|
245
|
|
|
* |
|
246
|
|
|
* @param Response|array $mCommands The <Response> object |
|
|
|
|
|
|
247
|
|
|
* @param bool $bBefore Add the new commands to the beginning of the list |
|
|
|
|
|
|
248
|
|
|
* |
|
249
|
|
|
* @return void |
|
250
|
|
|
* @throws RequestException |
|
251
|
|
|
*/ |
|
252
|
|
|
public function appendResponse($mCommands, bool $bBefore = false) |
|
253
|
|
|
{ |
|
254
|
|
|
if($mCommands instanceof Response) |
|
255
|
|
|
{ |
|
256
|
|
|
$this->xReturnValue = $mCommands->xReturnValue; |
|
257
|
|
|
$aCommands = $mCommands->aCommands; |
|
|
|
|
|
|
258
|
|
|
} |
|
259
|
|
|
elseif(is_array($mCommands)) |
|
|
|
|
|
|
260
|
|
|
{ |
|
261
|
|
|
$aCommands = $mCommands; |
|
262
|
|
|
} |
|
263
|
|
|
else |
|
264
|
|
|
{ |
|
265
|
|
|
throw new RequestException($this->xTranslator->trans('errors.response.data.invalid')); |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
$this->aCommands = ($bBefore) ? |
|
|
|
|
|
|
269
|
|
|
array_merge($aCommands, $this->aCommands) : |
|
|
|
|
|
|
270
|
|
|
array_merge($this->aCommands, $aCommands); |
|
271
|
|
|
} |
|
|
|
|
|
|
272
|
|
|
|
|
273
|
|
|
/** |
|
274
|
|
|
* Get the commands in the response |
|
275
|
|
|
* |
|
276
|
|
|
* @return array |
|
277
|
|
|
*/ |
|
278
|
|
|
public function getCommands(): array |
|
279
|
|
|
{ |
|
280
|
|
|
return $this->aCommands; |
|
281
|
|
|
} |
|
|
|
|
|
|
282
|
|
|
|
|
283
|
|
|
/** |
|
284
|
|
|
* Get the number of commands in the response |
|
285
|
|
|
* |
|
286
|
|
|
* @return int |
|
287
|
|
|
*/ |
|
288
|
|
|
public function getCommandCount(): int |
|
289
|
|
|
{ |
|
290
|
|
|
return count($this->aCommands); |
|
291
|
|
|
} |
|
|
|
|
|
|
292
|
|
|
|
|
293
|
|
|
/** |
|
294
|
|
|
* Stores a value that will be passed back as part of the response |
|
295
|
|
|
* |
|
296
|
|
|
* When making synchronous requests, the calling javascript can obtain this value |
|
297
|
|
|
* immediately as the return value of the <jaxon.call> javascript function |
|
298
|
|
|
* |
|
299
|
|
|
* @param mixed $value Any value |
|
|
|
|
|
|
300
|
|
|
* |
|
301
|
|
|
* @return Response |
|
302
|
|
|
*/ |
|
303
|
|
|
public function setReturnValue($value): Response |
|
304
|
|
|
{ |
|
305
|
|
|
$this->xReturnValue = $value; |
|
306
|
|
|
return $this; |
|
307
|
|
|
} |
|
|
|
|
|
|
308
|
|
|
|
|
309
|
|
|
/** |
|
310
|
|
|
* Return the output, generated from the commands added to the response, that will be sent to the browser |
|
311
|
|
|
* |
|
312
|
|
|
* @return string |
|
313
|
|
|
*/ |
|
314
|
|
|
public function getOutput(): string |
|
315
|
|
|
{ |
|
316
|
|
|
$aResponse = ['jxnobj' => []]; |
|
317
|
|
|
if(($this->xReturnValue)) |
|
318
|
|
|
{ |
|
319
|
|
|
$aResponse['jxnrv'] = $this->xReturnValue; |
|
320
|
|
|
} |
|
321
|
|
|
foreach($this->aCommands as $xCommand) |
|
322
|
|
|
{ |
|
323
|
|
|
$aResponse['jxnobj'][] = $xCommand; |
|
324
|
|
|
} |
|
325
|
|
|
|
|
326
|
|
|
return json_encode($aResponse); |
|
327
|
|
|
} |
|
|
|
|
|
|
328
|
|
|
} |
|
329
|
|
|
|