|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Ajax.php |
|
5
|
|
|
* |
|
6
|
|
|
* The Jaxon library. |
|
7
|
|
|
* |
|
8
|
|
|
* @package jaxon-core |
|
|
|
|
|
|
9
|
|
|
* @author Thierry Feuzeu <[email protected]> |
|
10
|
|
|
* @copyright 2022 Thierry Feuzeu <[email protected]> |
|
11
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License |
|
12
|
|
|
* @link https://github.com/jaxon-php/jaxon-core |
|
13
|
|
|
*/ |
|
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
namespace Jaxon\App\Traits; |
|
16
|
|
|
|
|
17
|
|
|
use Jaxon\App\Config\ConfigManager; |
|
18
|
|
|
use Jaxon\App\I18n\Translator; |
|
19
|
|
|
use Jaxon\Di\Container; |
|
20
|
|
|
use Jaxon\Exception\SetupException; |
|
21
|
|
|
use Jaxon\Plugin\Manager\PluginManager; |
|
22
|
|
|
use Jaxon\Plugin\Package; |
|
23
|
|
|
use Jaxon\Plugin\Request\CallableClass\CallableRegistry; |
|
24
|
|
|
use Jaxon\Plugin\ResponsePlugin; |
|
25
|
|
|
use Jaxon\Request\Factory\Factory; |
|
26
|
|
|
use Jaxon\Request\Factory\RequestFactory; |
|
27
|
|
|
use Jaxon\Request\Handler\CallbackManager; |
|
28
|
|
|
use Jaxon\Response\Manager\ResponseManager; |
|
29
|
|
|
use Jaxon\Utils\Http\UriException; |
|
30
|
|
|
use Psr\Log\LoggerInterface; |
|
31
|
|
|
|
|
32
|
|
|
use function trim; |
|
33
|
|
|
|
|
34
|
|
|
trait AjaxTrait |
|
|
|
|
|
|
35
|
|
|
{ |
|
36
|
|
|
/** |
|
37
|
|
|
* @var Container |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $xContainer = null; |
|
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var ConfigManager |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $xConfigManager; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var ResponseManager |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $xResponseManager; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @var PluginManager |
|
53
|
|
|
*/ |
|
54
|
|
|
protected $xPluginManager; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @var CallableRegistry |
|
58
|
|
|
*/ |
|
59
|
|
|
protected $xCallableRegistry; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @var Translator |
|
63
|
|
|
*/ |
|
64
|
|
|
protected $xTranslator; |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @return Container |
|
68
|
|
|
*/ |
|
69
|
|
|
public function di(): ?Container |
|
|
|
|
|
|
70
|
|
|
{ |
|
71
|
|
|
return $this->xContainer; |
|
72
|
|
|
} |
|
|
|
|
|
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @return LoggerInterface |
|
76
|
|
|
*/ |
|
77
|
|
|
public function logger(): LoggerInterface |
|
78
|
|
|
{ |
|
79
|
|
|
return $this->di()->getLogger(); |
|
80
|
|
|
} |
|
|
|
|
|
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @return Translator |
|
84
|
|
|
*/ |
|
85
|
|
|
public function translator(): Translator |
|
86
|
|
|
{ |
|
87
|
|
|
return $this->xTranslator; |
|
88
|
|
|
} |
|
|
|
|
|
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Set the value of a config option |
|
92
|
|
|
* |
|
93
|
|
|
* @param string $sName The option name |
|
|
|
|
|
|
94
|
|
|
* @param mixed $sValue The option value |
|
|
|
|
|
|
95
|
|
|
* |
|
96
|
|
|
* @return void |
|
97
|
|
|
*/ |
|
98
|
|
|
public function setOption(string $sName, $sValue) |
|
99
|
|
|
{ |
|
100
|
|
|
$this->xConfigManager->setOption($sName, $sValue); |
|
101
|
|
|
} |
|
|
|
|
|
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Get the value of a config option |
|
105
|
|
|
* |
|
106
|
|
|
* @param string $sName The option name |
|
107
|
|
|
* @param mixed $xDefault The default value, to be returned if the option is not defined |
|
|
|
|
|
|
108
|
|
|
* |
|
109
|
|
|
* @return mixed |
|
110
|
|
|
*/ |
|
111
|
|
|
public function getOption(string $sName, $xDefault = null) |
|
112
|
|
|
{ |
|
113
|
|
|
return $this->xConfigManager->getOption($sName, $xDefault); |
|
114
|
|
|
} |
|
|
|
|
|
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Check the presence of a config option |
|
118
|
|
|
* |
|
119
|
|
|
* @param string $sName The option name |
|
|
|
|
|
|
120
|
|
|
* |
|
121
|
|
|
* @return bool |
|
122
|
|
|
*/ |
|
123
|
|
|
public function hasOption(string $sName): bool |
|
124
|
|
|
{ |
|
125
|
|
|
return $this->xConfigManager->hasOption($sName); |
|
126
|
|
|
} |
|
|
|
|
|
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Get the configured character encoding |
|
130
|
|
|
* |
|
131
|
|
|
* @return string |
|
132
|
|
|
*/ |
|
133
|
|
|
public function getCharacterEncoding(): string |
|
134
|
|
|
{ |
|
135
|
|
|
return trim($this->getOption('core.encoding', '')); |
|
136
|
|
|
} |
|
|
|
|
|
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Get the content type of the HTTP response |
|
140
|
|
|
* |
|
141
|
|
|
* @return string |
|
142
|
|
|
*/ |
|
143
|
|
|
public function getContentType(): string |
|
144
|
|
|
{ |
|
145
|
|
|
return $this->xResponseManager->getContentType(); |
|
146
|
|
|
} |
|
|
|
|
|
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Get the factory for request and parameter factories |
|
150
|
|
|
* |
|
151
|
|
|
* @return Factory |
|
152
|
|
|
*/ |
|
153
|
|
|
public function factory(): Factory |
|
154
|
|
|
{ |
|
155
|
|
|
return $this->di()->getFactory(); |
|
156
|
|
|
} |
|
|
|
|
|
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Get an instance of a registered class |
|
160
|
|
|
* |
|
161
|
|
|
* @param string $sClassName The class name |
|
162
|
|
|
* |
|
163
|
|
|
* @return mixed |
|
164
|
|
|
* @throws SetupException |
|
165
|
|
|
*/ |
|
166
|
|
|
public function cl(string $sClassName) |
|
167
|
|
|
{ |
|
168
|
|
|
$sClassName = trim($sClassName); |
|
|
|
|
|
|
169
|
|
|
$xCallableClass = $this->xCallableRegistry->getCallableObject($sClassName); |
|
170
|
|
|
return !$xCallableClass ? null : $xCallableClass->getRegisteredObject(); |
|
|
|
|
|
|
171
|
|
|
} |
|
|
|
|
|
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Get the request factory to a registered class |
|
175
|
|
|
* |
|
176
|
|
|
* @param string $sClassName The class name |
|
177
|
|
|
* |
|
178
|
|
|
* @return RequestFactory|null |
|
179
|
|
|
* @throws SetupException |
|
180
|
|
|
*/ |
|
181
|
|
|
public function rq(string $sClassName = ''): ?RequestFactory |
|
182
|
|
|
{ |
|
183
|
|
|
$sClassName = trim($sClassName); |
|
184
|
|
|
return $this->factory()->request($sClassName); |
|
185
|
|
|
} |
|
|
|
|
|
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* Get the request factory to a registered class |
|
189
|
|
|
* |
|
190
|
|
|
* @param string $sClassName The class name |
|
191
|
|
|
* |
|
192
|
|
|
* @return RequestFactory|null |
|
193
|
|
|
* @throws SetupException |
|
194
|
|
|
*/ |
|
195
|
|
|
public function request(string $sClassName = ''): ?RequestFactory |
|
196
|
|
|
{ |
|
197
|
|
|
return $this->rq($sClassName); |
|
198
|
|
|
} |
|
|
|
|
|
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* Get the HTML tags to include Jaxon javascript files into the page. |
|
202
|
|
|
* |
|
203
|
|
|
* @return string |
|
204
|
|
|
*/ |
|
205
|
|
|
public function getJs(): string |
|
206
|
|
|
{ |
|
207
|
|
|
return $this->di()->getCodeGenerator()->getJs(); |
|
208
|
|
|
} |
|
|
|
|
|
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* Get the HTML tags to include Jaxon javascript files into the page. |
|
212
|
|
|
* |
|
213
|
|
|
* @return string the javascript code |
|
214
|
|
|
*/ |
|
215
|
|
|
public function js(): string |
|
216
|
|
|
{ |
|
217
|
|
|
return $this->di()->getCodeGenerator()->getJs(); |
|
218
|
|
|
} |
|
|
|
|
|
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* Get the HTML tags to include Jaxon CSS code and files into the page. |
|
222
|
|
|
* |
|
223
|
|
|
* @return string |
|
224
|
|
|
*/ |
|
225
|
|
|
public function getCss(): string |
|
226
|
|
|
{ |
|
227
|
|
|
return $this->di()->getCodeGenerator()->getCss(); |
|
228
|
|
|
} |
|
|
|
|
|
|
229
|
|
|
|
|
230
|
|
|
/** |
|
231
|
|
|
* Get the HTML tags to include Jaxon CSS code and files into the page. |
|
232
|
|
|
* |
|
233
|
|
|
* @return string |
|
234
|
|
|
*/ |
|
235
|
|
|
public function css(): string |
|
236
|
|
|
{ |
|
237
|
|
|
return $this->di()->getCodeGenerator()->getCss(); |
|
238
|
|
|
} |
|
|
|
|
|
|
239
|
|
|
|
|
240
|
|
|
/** |
|
241
|
|
|
* Returns the js header and wrapper code to be printed into the page |
|
242
|
|
|
* |
|
243
|
|
|
* The javascript code returned by this function depends on the plugins |
|
244
|
|
|
* that are included and the functions and classes that are registered. |
|
245
|
|
|
* |
|
246
|
|
|
* @param bool $bIncludeJs Also get the js code |
|
|
|
|
|
|
247
|
|
|
* @param bool $bIncludeCss Also get the css code |
|
|
|
|
|
|
248
|
|
|
* |
|
249
|
|
|
* @return string |
|
250
|
|
|
* @throws UriException |
|
251
|
|
|
*/ |
|
252
|
|
|
public function getScript(bool $bIncludeJs = false, bool $bIncludeCss = false): string |
|
253
|
|
|
{ |
|
254
|
|
|
return $this->di()->getCodeGenerator()->getScript($bIncludeJs, $bIncludeCss); |
|
255
|
|
|
} |
|
|
|
|
|
|
256
|
|
|
|
|
257
|
|
|
/** |
|
258
|
|
|
* Returns the js header and wrapper code to be printed into the page |
|
259
|
|
|
* |
|
260
|
|
|
* @param bool $bIncludeJs Also get the js code |
|
|
|
|
|
|
261
|
|
|
* @param bool $bIncludeCss Also get the css code |
|
|
|
|
|
|
262
|
|
|
* |
|
263
|
|
|
* @return string the javascript code |
|
264
|
|
|
* @throws UriException |
|
265
|
|
|
*/ |
|
266
|
|
|
public function script(bool $bIncludeJs = false, bool $bIncludeCss = false): string |
|
267
|
|
|
{ |
|
268
|
|
|
return $this->di()->getCodeGenerator()->getScript($bIncludeJs, $bIncludeCss); |
|
269
|
|
|
} |
|
|
|
|
|
|
270
|
|
|
|
|
271
|
|
|
/** |
|
272
|
|
|
* Determine if a call is a jaxon request or a page load request |
|
273
|
|
|
* |
|
274
|
|
|
* @return bool |
|
275
|
|
|
*/ |
|
276
|
|
|
public function canProcessRequest(): bool |
|
277
|
|
|
{ |
|
278
|
|
|
return $this->di()->getRequestHandler()->canProcessRequest(); |
|
279
|
|
|
} |
|
|
|
|
|
|
280
|
|
|
|
|
281
|
|
|
/** |
|
282
|
|
|
* Get a registered response plugin |
|
283
|
|
|
* |
|
284
|
|
|
* @param string $sName The name of the plugin |
|
|
|
|
|
|
285
|
|
|
* |
|
286
|
|
|
* @return ResponsePlugin|null |
|
287
|
|
|
*/ |
|
288
|
|
|
public function plugin(string $sName): ?ResponsePlugin |
|
289
|
|
|
{ |
|
290
|
|
|
return $this->xPluginManager->getResponsePlugin($sName); |
|
291
|
|
|
} |
|
|
|
|
|
|
292
|
|
|
|
|
293
|
|
|
/** |
|
294
|
|
|
* Get a package instance |
|
295
|
|
|
* |
|
296
|
|
|
* @param string $sClassName The package class name |
|
|
|
|
|
|
297
|
|
|
* |
|
298
|
|
|
* @return Package|null |
|
299
|
|
|
*/ |
|
300
|
|
|
public function package(string $sClassName): ?Package |
|
301
|
|
|
{ |
|
302
|
|
|
return $this->di()->getPackageManager()->getPackage($sClassName); |
|
303
|
|
|
} |
|
|
|
|
|
|
304
|
|
|
|
|
305
|
|
|
/** |
|
306
|
|
|
* Get the callback manager |
|
307
|
|
|
* |
|
308
|
|
|
* @return CallbackManager |
|
309
|
|
|
*/ |
|
310
|
|
|
public function callback(): CallbackManager |
|
311
|
|
|
{ |
|
312
|
|
|
return $this->di()->getCallbackManager(); |
|
313
|
|
|
} |
|
|
|
|
|
|
314
|
|
|
} |
|
315
|
|
|
|