|
1
|
|
|
<?php |
|
2
|
|
|
|
|
|
|
|
|
|
3
|
|
|
namespace Jaxon\App; |
|
4
|
|
|
|
|
5
|
|
|
use Jaxon\Jaxon; |
|
6
|
|
|
use Jaxon\Plugin\Package; |
|
7
|
|
|
use Jaxon\Request\Factory\RequestFactory; |
|
8
|
|
|
use Jaxon\Request\Handler\CallbackManager; |
|
9
|
|
|
use Jaxon\Response\AbstractResponse; |
|
10
|
|
|
use Jaxon\Session\SessionInterface; |
|
11
|
|
|
use Jaxon\Ui\View\ViewRenderer; |
|
12
|
|
|
use Jaxon\Utils\Http\UriException; |
|
13
|
|
|
use Jaxon\Exception\RequestException; |
|
14
|
|
|
use Jaxon\Exception\SetupException; |
|
15
|
|
|
|
|
16
|
|
|
use Psr\Container\ContainerInterface; |
|
17
|
|
|
use Psr\Log\LoggerInterface; |
|
18
|
|
|
|
|
19
|
|
|
use Closure; |
|
20
|
|
|
|
|
21
|
|
|
use function trim; |
|
22
|
|
|
|
|
23
|
|
|
trait AppTrait |
|
|
|
|
|
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @var Jaxon |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $jaxon; |
|
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Get the Jaxon application bootstrapper. |
|
32
|
|
|
* |
|
33
|
|
|
* @return Bootstrap |
|
34
|
|
|
*/ |
|
35
|
|
|
protected function bootstrap(): Bootstrap |
|
|
|
|
|
|
36
|
|
|
{ |
|
37
|
|
|
return $this->jaxon->di()->getBootstrap(); |
|
38
|
|
|
} |
|
|
|
|
|
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Get the Jaxon response. |
|
42
|
|
|
* |
|
43
|
|
|
* @return AbstractResponse |
|
44
|
|
|
*/ |
|
45
|
|
|
public function ajaxResponse(): AbstractResponse |
|
46
|
|
|
{ |
|
47
|
|
|
return $this->jaxon->getResponse(); |
|
48
|
|
|
} |
|
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Get the configured character encoding |
|
52
|
|
|
* |
|
53
|
|
|
* @return string |
|
54
|
|
|
*/ |
|
55
|
|
|
public function getCharacterEncoding(): string |
|
56
|
|
|
{ |
|
57
|
|
|
return trim($this->jaxon->getOption('core.encoding', '')); |
|
58
|
|
|
} |
|
|
|
|
|
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Get an instance of a registered class |
|
62
|
|
|
* |
|
63
|
|
|
* @param string $sClassName The class name |
|
64
|
|
|
* |
|
65
|
|
|
* @return object|null |
|
66
|
|
|
* @throws SetupException |
|
67
|
|
|
*/ |
|
68
|
|
|
public function instance(string $sClassName) |
|
69
|
|
|
{ |
|
70
|
|
|
return $this->jaxon->instance($sClassName); |
|
71
|
|
|
} |
|
|
|
|
|
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Get a request to a registered class |
|
75
|
|
|
* |
|
76
|
|
|
* @param string $sClassName The class name |
|
77
|
|
|
* |
|
78
|
|
|
* @return RequestFactory|null |
|
79
|
|
|
* @throws SetupException |
|
80
|
|
|
*/ |
|
81
|
|
|
public function request(string $sClassName): ?RequestFactory |
|
82
|
|
|
{ |
|
83
|
|
|
return $this->jaxon->request($sClassName); |
|
84
|
|
|
} |
|
|
|
|
|
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Get a package instance |
|
88
|
|
|
* |
|
89
|
|
|
* @param string $sClassName The package class name |
|
|
|
|
|
|
90
|
|
|
* |
|
91
|
|
|
* @return Package|null |
|
92
|
|
|
*/ |
|
93
|
|
|
public function package(string $sClassName): ?Package |
|
94
|
|
|
{ |
|
95
|
|
|
return $this->jaxon->package($sClassName); |
|
96
|
|
|
} |
|
|
|
|
|
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Get the request callback manager |
|
100
|
|
|
* |
|
101
|
|
|
* @return CallbackManager |
|
102
|
|
|
*/ |
|
103
|
|
|
public function callback(): CallbackManager |
|
104
|
|
|
{ |
|
105
|
|
|
return $this->jaxon->callback(); |
|
106
|
|
|
} |
|
|
|
|
|
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Check if a call is a Jaxon request. |
|
110
|
|
|
* |
|
111
|
|
|
* @return bool |
|
112
|
|
|
*/ |
|
113
|
|
|
public function canProcessRequest(): bool |
|
114
|
|
|
{ |
|
115
|
|
|
return $this->jaxon->canProcessRequest(); |
|
116
|
|
|
} |
|
|
|
|
|
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Get the HTTP response |
|
120
|
|
|
* |
|
121
|
|
|
* @param string $sCode The HTTP response code |
|
|
|
|
|
|
122
|
|
|
* |
|
123
|
|
|
* @return mixed |
|
124
|
|
|
*/ |
|
125
|
|
|
abstract public function httpResponse(string $sCode = '200'); |
|
|
|
|
|
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Process an incoming Jaxon request, and return the response. |
|
129
|
|
|
* |
|
130
|
|
|
* @return mixed |
|
131
|
|
|
* @throws RequestException |
|
132
|
|
|
*/ |
|
133
|
|
|
public function processRequest() |
|
134
|
|
|
{ |
|
135
|
|
|
// Prevent the Jaxon library from sending the response or exiting |
|
136
|
|
|
$this->jaxon->setOption('core.response.send', false); |
|
137
|
|
|
$this->jaxon->setOption('core.process.exit', false); |
|
138
|
|
|
|
|
139
|
|
|
// Process the jaxon request |
|
140
|
|
|
$this->jaxon->processRequest(); |
|
141
|
|
|
|
|
142
|
|
|
// Return the response to the request |
|
143
|
|
|
return $this->httpResponse(); |
|
144
|
|
|
} |
|
|
|
|
|
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Get the HTML tags to include Jaxon CSS code and files into the page. |
|
148
|
|
|
* |
|
149
|
|
|
* @return string the javascript code |
|
150
|
|
|
*/ |
|
151
|
|
|
public function css(): string |
|
152
|
|
|
{ |
|
153
|
|
|
return $this->jaxon->getCss(); |
|
154
|
|
|
} |
|
|
|
|
|
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Get the HTML tags to include Jaxon CSS code and files into the page. |
|
158
|
|
|
* |
|
159
|
|
|
* @return string the javascript code |
|
160
|
|
|
*/ |
|
161
|
|
|
public function getCss(): string |
|
162
|
|
|
{ |
|
163
|
|
|
return $this->jaxon->getCss(); |
|
164
|
|
|
} |
|
|
|
|
|
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* Get the HTML tags to include Jaxon javascript files into the page. |
|
168
|
|
|
* |
|
169
|
|
|
* @return string the javascript code |
|
170
|
|
|
*/ |
|
171
|
|
|
public function js(): string |
|
172
|
|
|
{ |
|
173
|
|
|
return $this->jaxon->getJs(); |
|
174
|
|
|
} |
|
|
|
|
|
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* Get the HTML tags to include Jaxon javascript files into the page. |
|
178
|
|
|
* |
|
179
|
|
|
* @return string the javascript code |
|
180
|
|
|
*/ |
|
181
|
|
|
public function getJs(): string |
|
182
|
|
|
{ |
|
183
|
|
|
return $this->jaxon->getJs(); |
|
184
|
|
|
} |
|
|
|
|
|
|
185
|
|
|
|
|
186
|
|
|
/** |
|
|
|
|
|
|
187
|
|
|
* Get the javascript code to be sent to the browser. |
|
188
|
|
|
* |
|
189
|
|
|
* @return string the javascript code |
|
190
|
|
|
* @throws UriException |
|
191
|
|
|
*/ |
|
192
|
|
|
public function script(bool $bIncludeJs = false, bool $bIncludeCss = false): string |
|
193
|
|
|
{ |
|
194
|
|
|
return $this->jaxon->getScript($bIncludeJs, $bIncludeCss); |
|
195
|
|
|
} |
|
|
|
|
|
|
196
|
|
|
|
|
197
|
|
|
/** |
|
|
|
|
|
|
198
|
|
|
* Get the javascript code to be sent to the browser. |
|
199
|
|
|
* |
|
200
|
|
|
* @return string the javascript code |
|
201
|
|
|
* @throws UriException |
|
202
|
|
|
*/ |
|
203
|
|
|
public function getScript(bool $bIncludeJs = false, bool $bIncludeCss = false): string |
|
204
|
|
|
{ |
|
205
|
|
|
return $this->jaxon->getScript($bIncludeJs, $bIncludeCss); |
|
206
|
|
|
} |
|
|
|
|
|
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* Get the view renderer |
|
210
|
|
|
* |
|
211
|
|
|
* @return ViewRenderer |
|
212
|
|
|
*/ |
|
213
|
|
|
public function view(): ViewRenderer |
|
214
|
|
|
{ |
|
215
|
|
|
return $this->jaxon->view(); |
|
216
|
|
|
} |
|
|
|
|
|
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* Get the session manager |
|
220
|
|
|
* |
|
221
|
|
|
* @return SessionInterface |
|
222
|
|
|
*/ |
|
223
|
|
|
public function session(): SessionInterface |
|
224
|
|
|
{ |
|
225
|
|
|
return $this->jaxon->session(); |
|
226
|
|
|
} |
|
|
|
|
|
|
227
|
|
|
|
|
228
|
|
|
/** |
|
229
|
|
|
* Get the logger |
|
230
|
|
|
* |
|
231
|
|
|
* @return LoggerInterface |
|
232
|
|
|
*/ |
|
233
|
|
|
public function logger(): LoggerInterface |
|
234
|
|
|
{ |
|
235
|
|
|
return $this->jaxon->logger(); |
|
236
|
|
|
} |
|
|
|
|
|
|
237
|
|
|
|
|
238
|
|
|
/** |
|
239
|
|
|
* Sets a logger. |
|
240
|
|
|
* |
|
241
|
|
|
* @param LoggerInterface $logger |
|
|
|
|
|
|
242
|
|
|
* |
|
243
|
|
|
* @return void |
|
244
|
|
|
*/ |
|
245
|
|
|
public function setLogger(LoggerInterface $logger) |
|
246
|
|
|
{ |
|
247
|
|
|
$this->jaxon->setLogger($logger); |
|
248
|
|
|
} |
|
|
|
|
|
|
249
|
|
|
|
|
250
|
|
|
/** |
|
251
|
|
|
* Set the container provided by the integrated framework |
|
252
|
|
|
* |
|
253
|
|
|
* @param ContainerInterface $xContainer The container implementation |
|
|
|
|
|
|
254
|
|
|
* |
|
255
|
|
|
* @return void |
|
256
|
|
|
*/ |
|
257
|
|
|
public function setAppContainer(ContainerInterface $xContainer) |
|
258
|
|
|
{ |
|
259
|
|
|
$this->jaxon->di()->setAppContainer($xContainer); |
|
260
|
|
|
} |
|
|
|
|
|
|
261
|
|
|
|
|
262
|
|
|
/** |
|
263
|
|
|
* Add a view namespace, and set the corresponding renderer. |
|
264
|
|
|
* |
|
265
|
|
|
* @param string $sNamespace The namespace name |
|
|
|
|
|
|
266
|
|
|
* @param string $sDirectory The namespace directory |
|
|
|
|
|
|
267
|
|
|
* @param string $sExtension The extension to append to template names |
|
|
|
|
|
|
268
|
|
|
* @param string $sRenderer The corresponding renderer name |
|
|
|
|
|
|
269
|
|
|
* |
|
270
|
|
|
* @return void |
|
271
|
|
|
*/ |
|
272
|
|
|
public function addViewNamespace(string $sNamespace, string $sDirectory, string $sExtension, string $sRenderer) |
|
273
|
|
|
{ |
|
274
|
|
|
$this->jaxon->di()->getViewManager()->addNamespace($sNamespace, $sDirectory, $sExtension, $sRenderer); |
|
275
|
|
|
} |
|
|
|
|
|
|
276
|
|
|
|
|
277
|
|
|
/** |
|
278
|
|
|
* Add a view renderer with an id |
|
279
|
|
|
* |
|
280
|
|
|
* @param string $sId The unique identifier of the view renderer |
|
|
|
|
|
|
281
|
|
|
* @param Closure $xClosure A closure to create the view instance |
|
|
|
|
|
|
282
|
|
|
* |
|
283
|
|
|
* @return void |
|
284
|
|
|
*/ |
|
285
|
|
|
public function addViewRenderer(string $sId, Closure $xClosure) |
|
286
|
|
|
{ |
|
287
|
|
|
$this->jaxon->di()->getViewManager()->addRenderer($sId, $xClosure); |
|
288
|
|
|
} |
|
|
|
|
|
|
289
|
|
|
|
|
290
|
|
|
/** |
|
291
|
|
|
* Set the session manager |
|
292
|
|
|
* |
|
293
|
|
|
* @param Closure $xClosure A closure to create the session manager instance |
|
|
|
|
|
|
294
|
|
|
* |
|
295
|
|
|
* @return void |
|
296
|
|
|
*/ |
|
297
|
|
|
public function setSessionManager(Closure $xClosure) |
|
298
|
|
|
{ |
|
299
|
|
|
$this->jaxon->di()->setSessionManager($xClosure); |
|
300
|
|
|
} |
|
|
|
|
|
|
301
|
|
|
} |
|
302
|
|
|
|