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