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