|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Ajax.php |
|
5
|
|
|
* |
|
6
|
|
|
* The Jaxon class uses a modular plug-in system to facilitate the processing |
|
7
|
|
|
* of special Ajax requests made by a PHP page. |
|
8
|
|
|
* It generates Javascript that the page must include in order to make requests. |
|
9
|
|
|
* It handles the output of response commands (see <Jaxon\Response\Response>). |
|
10
|
|
|
* Many flags and settings can be adjusted to effect the behavior of the Jaxon class |
|
11
|
|
|
* as well as the client-side javascript. |
|
12
|
|
|
* |
|
13
|
|
|
* @package jaxon-core |
|
|
|
|
|
|
14
|
|
|
* @author Jared White |
|
|
|
|
|
|
15
|
|
|
* @author J. Max Wilson |
|
|
|
|
|
|
16
|
|
|
* @author Joseph Woolley |
|
|
|
|
|
|
17
|
|
|
* @author Steffen Konerow |
|
|
|
|
|
|
18
|
|
|
* @author Thierry Feuzeu <[email protected]> |
|
19
|
|
|
* @copyright Copyright (c) 2005-2007 by Jared White & J. Max Wilson |
|
|
|
|
|
|
20
|
|
|
* @copyright Copyright (c) 2008-2010 by Joseph Woolley, Steffen Konerow, Jared White & J. Max Wilson |
|
|
|
|
|
|
21
|
|
|
* @copyright 2016 Thierry Feuzeu <[email protected]> |
|
22
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License |
|
23
|
|
|
* @link https://github.com/jaxon-php/jaxon-core |
|
24
|
|
|
*/ |
|
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
namespace Jaxon\App; |
|
27
|
|
|
|
|
28
|
|
|
use Jaxon\Jaxon; |
|
29
|
|
|
use Jaxon\Di\Container; |
|
30
|
|
|
use Jaxon\App\Config\ConfigManager; |
|
31
|
|
|
use Jaxon\App\Dialog\Library\DialogLibraryManager; |
|
32
|
|
|
use Jaxon\App\I18n\Translator; |
|
33
|
|
|
use Jaxon\App\Session\SessionInterface; |
|
34
|
|
|
use Jaxon\App\Traits\AjaxSendTrait; |
|
35
|
|
|
use Jaxon\App\Traits\AjaxTrait; |
|
36
|
|
|
use Jaxon\App\View\ViewRenderer; |
|
37
|
|
|
use Jaxon\Exception\RequestException; |
|
38
|
|
|
use Jaxon\Exception\SetupException; |
|
39
|
|
|
use Jaxon\Plugin\Manager\PluginManager; |
|
40
|
|
|
use Jaxon\Request\Factory\Psr\PsrFactory; |
|
41
|
|
|
use Jaxon\Request\Upload\UploadHandlerInterface; |
|
42
|
|
|
use Jaxon\Response\Manager\ResponseManager; |
|
43
|
|
|
use Jaxon\Response\ResponseInterface; |
|
44
|
|
|
use Jaxon\Utils\Template\TemplateEngine; |
|
45
|
|
|
|
|
46
|
|
|
use function trim; |
|
47
|
|
|
|
|
48
|
|
|
class Ajax |
|
|
|
|
|
|
49
|
|
|
{ |
|
50
|
|
|
use AjaxTrait; |
|
51
|
|
|
use AjaxSendTrait; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @var Ajax |
|
55
|
|
|
*/ |
|
56
|
|
|
private static $xInstance = null; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @var Bootstrap |
|
60
|
|
|
*/ |
|
61
|
|
|
protected $xBootstrap; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param Container $xContainer |
|
|
|
|
|
|
65
|
|
|
* |
|
66
|
|
|
* @return void |
|
67
|
|
|
*/ |
|
68
|
|
|
private function init(Container $xContainer) |
|
|
|
|
|
|
69
|
|
|
{ |
|
70
|
|
|
$this->xContainer = $xContainer; |
|
71
|
|
|
// Set the attributes from the container |
|
72
|
|
|
$this->xBootstrap = $xContainer->g(Bootstrap::class); |
|
|
|
|
|
|
73
|
|
|
$this->xTranslator = $xContainer->g(Translator::class); |
|
|
|
|
|
|
74
|
|
|
$this->xConfigManager = $xContainer->g(ConfigManager::class); |
|
|
|
|
|
|
75
|
|
|
$this->xPluginManager = $xContainer->g(PluginManager::class); |
|
|
|
|
|
|
76
|
|
|
$this->xResponseManager = $xContainer->g(ResponseManager::class); |
|
77
|
|
|
} |
|
|
|
|
|
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @return Ajax |
|
81
|
|
|
*/ |
|
82
|
|
|
public static function getInstance(): Ajax |
|
83
|
|
|
{ |
|
84
|
|
|
if(self::$xInstance === null) |
|
85
|
|
|
{ |
|
86
|
|
|
// First call: create and initialize the instances. |
|
87
|
|
|
self::$xInstance = new Ajax(); |
|
88
|
|
|
self::$xInstance->init(new Container(self::$xInstance)); |
|
89
|
|
|
return self::$xInstance; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
// Call the on boot callbacks on each call to the jaxon() function, except the first. |
|
93
|
|
|
self::$xInstance->xBootstrap->onBoot(); |
|
94
|
|
|
return self::$xInstance; |
|
95
|
|
|
} |
|
|
|
|
|
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* The constructor |
|
99
|
|
|
*/ |
|
100
|
|
|
private function __construct() |
|
101
|
|
|
{} |
|
|
|
|
|
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @return string |
|
105
|
|
|
*/ |
|
106
|
|
|
public function getVersion(): string |
|
107
|
|
|
{ |
|
108
|
|
|
return Jaxon::VERSION; |
|
109
|
|
|
} |
|
|
|
|
|
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Read the options from the file, if provided, and return the config |
|
113
|
|
|
* |
|
114
|
|
|
* @param string $sConfigFile The full path to the config file |
|
|
|
|
|
|
115
|
|
|
* @param string $sConfigSection The section of the config file to be loaded |
|
116
|
|
|
* |
|
117
|
|
|
* @return ConfigManager |
|
118
|
|
|
* @throws SetupException |
|
119
|
|
|
*/ |
|
120
|
|
|
public function config(string $sConfigFile = '', string $sConfigSection = ''): ConfigManager |
|
121
|
|
|
{ |
|
122
|
|
|
if(!empty(($sConfigFile = trim($sConfigFile)))) |
|
|
|
|
|
|
123
|
|
|
{ |
|
124
|
|
|
$this->xConfigManager->load($sConfigFile, trim($sConfigSection)); |
|
125
|
|
|
} |
|
126
|
|
|
return $this->xConfigManager; |
|
127
|
|
|
} |
|
|
|
|
|
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Get the global Response object |
|
131
|
|
|
* |
|
132
|
|
|
* @return ResponseInterface |
|
133
|
|
|
*/ |
|
134
|
|
|
public function getResponse(): ResponseInterface |
|
135
|
|
|
{ |
|
136
|
|
|
return $this->xResponseManager->getResponse(); |
|
137
|
|
|
} |
|
|
|
|
|
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Create a new Jaxon response object |
|
141
|
|
|
* |
|
142
|
|
|
* @return ResponseInterface |
|
143
|
|
|
*/ |
|
144
|
|
|
public function newResponse(): ResponseInterface |
|
145
|
|
|
{ |
|
146
|
|
|
return $this->di()->newResponse(); |
|
147
|
|
|
} |
|
|
|
|
|
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Register a plugin |
|
151
|
|
|
* |
|
152
|
|
|
* Below is a table for priorities and their description: |
|
153
|
|
|
* - 0 to 999: Plugins that are part of or extensions to the jaxon core |
|
154
|
|
|
* - 1000 to 8999: User created plugins, typically, these plugins don't care about order |
|
155
|
|
|
* - 9000 to 9999: Plugins that generally need to be last or near the end of the plugin list |
|
156
|
|
|
* |
|
157
|
|
|
* @param string $sClassName The plugin class |
|
|
|
|
|
|
158
|
|
|
* @param string $sPluginName The plugin name |
|
|
|
|
|
|
159
|
|
|
* @param integer $nPriority The plugin priority, used to order the plugins |
|
|
|
|
|
|
160
|
|
|
* |
|
161
|
|
|
* @return void |
|
162
|
|
|
* @throws SetupException |
|
163
|
|
|
*/ |
|
164
|
|
|
public function registerPlugin(string $sClassName, string $sPluginName, int $nPriority = 1000) |
|
165
|
|
|
{ |
|
166
|
|
|
$this->xPluginManager->registerPlugin($sClassName, $sPluginName, $nPriority); |
|
167
|
|
|
} |
|
|
|
|
|
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Register a package |
|
171
|
|
|
* |
|
172
|
|
|
* @param string $sClassName The package class |
|
|
|
|
|
|
173
|
|
|
* @param array $xPkgOptions The user provided package options |
|
|
|
|
|
|
174
|
|
|
* |
|
175
|
|
|
* @return void |
|
176
|
|
|
* @throws SetupException |
|
177
|
|
|
*/ |
|
178
|
|
|
public function registerPackage(string $sClassName, array $xPkgOptions = []) |
|
179
|
|
|
{ |
|
180
|
|
|
$this->di()->getPackageManager()->registerPackage($sClassName, $xPkgOptions); |
|
181
|
|
|
} |
|
|
|
|
|
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* @return UploadHandlerInterface|null |
|
185
|
|
|
*/ |
|
186
|
|
|
public function upload(): ?UploadHandlerInterface |
|
187
|
|
|
{ |
|
188
|
|
|
return $this->di()->getUploadHandler(); |
|
189
|
|
|
} |
|
|
|
|
|
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* Register request handlers, including functions, callable classes and directories. |
|
193
|
|
|
* |
|
194
|
|
|
* @param string $sType The type of request handler being registered |
|
|
|
|
|
|
195
|
|
|
* Options include: |
|
|
|
|
|
|
196
|
|
|
* - Jaxon::CALLABLE_FUNCTION: a function declared at global scope |
|
|
|
|
|
|
197
|
|
|
* - Jaxon::CALLABLE_CLASS: a class who's methods are to be registered |
|
|
|
|
|
|
198
|
|
|
* - Jaxon::CALLABLE_DIR: a directory containing classes to be registered |
|
|
|
|
|
|
199
|
|
|
* @param string $sName |
|
|
|
|
|
|
200
|
|
|
* When registering a function, this is the name of the function |
|
201
|
|
|
* When registering a callable class, this is the class name |
|
202
|
|
|
* When registering a callable directory, this is the full path to the directory |
|
203
|
|
|
* @param array|string $xOptions The related options |
|
|
|
|
|
|
204
|
|
|
* |
|
205
|
|
|
* @return void |
|
206
|
|
|
* @throws SetupException |
|
207
|
|
|
*/ |
|
208
|
|
|
public function register(string $sType, string $sName, $xOptions = []) |
|
209
|
|
|
{ |
|
210
|
|
|
$this->xPluginManager->registerCallable($sType, $sName, $xOptions); |
|
211
|
|
|
} |
|
|
|
|
|
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* If this is a jaxon request, call the requested PHP function, build the response and send it back to the browser |
|
215
|
|
|
* |
|
216
|
|
|
* This is the main server side engine for Jaxon. |
|
217
|
|
|
* It handles all the incoming requests, including the firing of events and handling of the response. |
|
218
|
|
|
* If your RequestURI is the same as your web page, then this function should be called before ANY |
|
219
|
|
|
* headers or HTML is output from your script. |
|
220
|
|
|
* |
|
221
|
|
|
* This function may exit after the request is processed, if the 'core.process.exit' option is set to true. |
|
222
|
|
|
* |
|
223
|
|
|
* @return void |
|
224
|
|
|
* |
|
225
|
|
|
* @throws RequestException |
|
226
|
|
|
* @see <AjaxTrait::canProcessRequest> |
|
227
|
|
|
*/ |
|
228
|
|
|
public function processRequest() |
|
229
|
|
|
{ |
|
230
|
|
|
// Process the jaxon request |
|
231
|
|
|
$this->di()->getRequestHandler()->processRequest(); |
|
232
|
|
|
|
|
233
|
|
|
$this->sendResponse(); |
|
234
|
|
|
} |
|
|
|
|
|
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* @return PsrFactory |
|
238
|
|
|
*/ |
|
239
|
|
|
public function psr(): PsrFactory |
|
240
|
|
|
{ |
|
241
|
|
|
return $this->di()->getPsrFactory(); |
|
242
|
|
|
} |
|
|
|
|
|
|
243
|
|
|
|
|
244
|
|
|
/** |
|
245
|
|
|
* @return AppInterface |
|
246
|
|
|
*/ |
|
247
|
|
|
public function app(): AppInterface |
|
248
|
|
|
{ |
|
249
|
|
|
return $this->di()->getApp(); |
|
250
|
|
|
} |
|
|
|
|
|
|
251
|
|
|
|
|
252
|
|
|
/** |
|
253
|
|
|
* @return TemplateEngine |
|
254
|
|
|
*/ |
|
255
|
|
|
public function template(): TemplateEngine |
|
256
|
|
|
{ |
|
257
|
|
|
return $this->di()->getTemplateEngine(); |
|
258
|
|
|
} |
|
|
|
|
|
|
259
|
|
|
|
|
260
|
|
|
/** |
|
261
|
|
|
* @return ViewRenderer |
|
262
|
|
|
*/ |
|
263
|
|
|
public function view(): ViewRenderer |
|
264
|
|
|
{ |
|
265
|
|
|
return $this->di()->getViewRenderer(); |
|
266
|
|
|
} |
|
|
|
|
|
|
267
|
|
|
|
|
268
|
|
|
/** |
|
269
|
|
|
* @return DialogLibraryManager |
|
270
|
|
|
*/ |
|
271
|
|
|
public function dialog(): DialogLibraryManager |
|
272
|
|
|
{ |
|
273
|
|
|
return $this->di()->getDialogLibraryManager(); |
|
274
|
|
|
} |
|
|
|
|
|
|
275
|
|
|
|
|
276
|
|
|
/** |
|
277
|
|
|
* @return SessionInterface|null |
|
278
|
|
|
*/ |
|
279
|
|
|
public function session(): ?SessionInterface |
|
280
|
|
|
{ |
|
281
|
|
|
return $this->di()->getSessionManager(); |
|
282
|
|
|
} |
|
|
|
|
|
|
283
|
|
|
|
|
284
|
|
|
/** |
|
285
|
|
|
* @return void |
|
286
|
|
|
* @throws SetupException |
|
287
|
|
|
*/ |
|
288
|
|
|
public function reset() |
|
289
|
|
|
{ |
|
290
|
|
|
self::$xInstance = null; |
|
291
|
|
|
// Need to register the default plugins. |
|
292
|
|
|
self::getInstance()->di()->getPluginManager()->registerPlugins(); |
|
293
|
|
|
} |
|
|
|
|
|
|
294
|
|
|
} |
|
295
|
|
|
|