1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the O2System PHP Framework package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
* |
8
|
|
|
* @author Steeve Andrian Salim |
9
|
|
|
* @copyright Copyright (c) Steeve Andrian Salim |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
// ------------------------------------------------------------------------ |
13
|
|
|
|
14
|
|
|
namespace O2System; |
15
|
|
|
|
16
|
|
|
// ------------------------------------------------------------------------ |
17
|
|
|
|
18
|
|
|
/* |
19
|
|
|
* --------------------------------------------------------------- |
20
|
|
|
* ERROR REPORTING |
21
|
|
|
* --------------------------------------------------------------- |
22
|
|
|
* |
23
|
|
|
* Different environments will require different levels of error reporting. |
24
|
|
|
* By default development will show errors but testing and live will hide them. |
25
|
|
|
*/ |
26
|
|
|
switch (strtoupper(ENVIRONMENT)) { |
|
|
|
|
27
|
|
|
case 'DEVELOPMENT': |
28
|
|
|
error_reporting(-1); |
29
|
|
|
ini_set('display_errors', 1); |
30
|
|
|
break; |
31
|
|
|
case 'TESTING': |
32
|
|
|
case 'PRODUCTION': |
33
|
|
|
ini_set('display_errors', 0); |
34
|
|
|
error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED & ~E_STRICT & ~E_USER_NOTICE & ~E_USER_DEPRECATED); |
35
|
|
|
break; |
36
|
|
|
default: |
37
|
|
|
header('HTTP/1.1 503 Service Unavailable.', true, 503); |
38
|
|
|
echo 'The application environment is not set correctly.'; |
39
|
|
|
exit(1); // EXIT_ERROR |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/* |
43
|
|
|
*--------------------------------------------------------------- |
44
|
|
|
* VENDOR PATH |
45
|
|
|
*--------------------------------------------------------------- |
46
|
|
|
* |
47
|
|
|
* RealPath to vendor folder. |
48
|
|
|
* |
49
|
|
|
* WITH TRAILING SLASH! |
50
|
|
|
*/ |
51
|
|
|
if ( ! defined('PATH_VENDOR')) { |
52
|
|
|
define('PATH_VENDOR', PATH_ROOT . 'vendor' . DIRECTORY_SEPARATOR); |
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/* |
56
|
|
|
*--------------------------------------------------------------- |
57
|
|
|
* FRAMEWORK PATH |
58
|
|
|
*--------------------------------------------------------------- |
59
|
|
|
* |
60
|
|
|
* RealPath to framework folder. |
61
|
|
|
* |
62
|
|
|
* WITH TRAILING SLASH! |
63
|
|
|
*/ |
64
|
|
|
if ( ! defined('PATH_FRAMEWORK')) { |
65
|
|
|
define('PATH_FRAMEWORK', __DIR__ . DIRECTORY_SEPARATOR); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/* |
69
|
|
|
*--------------------------------------------------------------- |
70
|
|
|
* APP PATH |
71
|
|
|
*--------------------------------------------------------------- |
72
|
|
|
* |
73
|
|
|
* RealPath to application folder. |
74
|
|
|
* |
75
|
|
|
* WITH TRAILING SLASH! |
76
|
|
|
*/ |
77
|
|
|
if ( ! defined('PATH_APP')) { |
78
|
|
|
define('PATH_APP', PATH_ROOT . DIR_APP . DIRECTORY_SEPARATOR); |
|
|
|
|
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/* |
82
|
|
|
*--------------------------------------------------------------- |
83
|
|
|
* PUBLIC PATH |
84
|
|
|
*--------------------------------------------------------------- |
85
|
|
|
* |
86
|
|
|
* RealPath to public folder. |
87
|
|
|
* |
88
|
|
|
* WITH TRAILING SLASH! |
89
|
|
|
*/ |
90
|
|
|
if ( ! defined('PATH_PUBLIC')) { |
91
|
|
|
define('PATH_PUBLIC', PATH_ROOT . DIR_PUBLIC . DIRECTORY_SEPARATOR); |
|
|
|
|
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/* |
95
|
|
|
*--------------------------------------------------------------- |
96
|
|
|
* CACHE PATH |
97
|
|
|
*--------------------------------------------------------------- |
98
|
|
|
* |
99
|
|
|
* RealPath to writable caching folder. |
100
|
|
|
* |
101
|
|
|
* WITH TRAILING SLASH! |
102
|
|
|
*/ |
103
|
|
|
if ( ! defined('PATH_CACHE')) { |
104
|
|
|
define('PATH_CACHE', PATH_ROOT . DIR_CACHE . DIRECTORY_SEPARATOR); |
|
|
|
|
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/* |
108
|
|
|
*--------------------------------------------------------------- |
109
|
|
|
* STORAGE PATH |
110
|
|
|
*--------------------------------------------------------------- |
111
|
|
|
* |
112
|
|
|
* RealPath to writable storage folder. |
113
|
|
|
* |
114
|
|
|
* WITH TRAILING SLASH! |
115
|
|
|
*/ |
116
|
|
|
if ( ! defined('PATH_STORAGE')) { |
117
|
|
|
define('PATH_STORAGE', PATH_ROOT . DIR_STORAGE . DIRECTORY_SEPARATOR); |
|
|
|
|
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/* |
121
|
|
|
*--------------------------------------------------------------- |
122
|
|
|
* RESOURCES PATH |
123
|
|
|
*--------------------------------------------------------------- |
124
|
|
|
* |
125
|
|
|
* RealPath to writable resources folder. |
126
|
|
|
* |
127
|
|
|
* WITH TRAILING SLASH! |
128
|
|
|
*/ |
129
|
|
|
if ( ! defined('PATH_RESOURCES')) { |
130
|
|
|
define('PATH_RESOURCES', PATH_ROOT . DIR_RESOURCES . DIRECTORY_SEPARATOR); |
|
|
|
|
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
|
134
|
|
|
/* |
135
|
|
|
*--------------------------------------------------------------- |
136
|
|
|
* FRAMEWORK CONSTANTS |
137
|
|
|
*--------------------------------------------------------------- |
138
|
|
|
*/ |
139
|
|
|
require __DIR__ . '/Config/Constants.php'; |
140
|
|
|
|
141
|
|
|
/* |
142
|
|
|
*--------------------------------------------------------------- |
143
|
|
|
* FRAMEWORK HELPERS |
144
|
|
|
*--------------------------------------------------------------- |
145
|
|
|
*/ |
146
|
|
|
require __DIR__ . '/Helpers/Framework.php'; |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Class Framework |
150
|
|
|
* |
151
|
|
|
* @package O2System |
152
|
|
|
*/ |
153
|
|
|
class Framework extends Kernel |
154
|
|
|
{ |
155
|
|
|
/** |
156
|
|
|
* Framework Container Models |
157
|
|
|
* |
158
|
|
|
* @var Framework\Containers\Models |
159
|
|
|
*/ |
160
|
|
|
public $models; |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Framework Container Modules |
164
|
|
|
* |
165
|
|
|
* @var Framework\Containers\Modules |
166
|
|
|
*/ |
167
|
|
|
public $modules; |
168
|
|
|
|
169
|
|
|
// ------------------------------------------------------------------------ |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Framework::__construct |
173
|
|
|
* |
174
|
|
|
* @return Framework |
175
|
|
|
*/ |
176
|
|
|
protected function __construct() |
177
|
|
|
{ |
178
|
|
|
parent::__construct(); |
179
|
|
|
|
180
|
|
|
if (profiler() !== false) { |
|
|
|
|
181
|
|
|
profiler()->watch('Starting O2System Framework'); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
// Add App Views Folder |
185
|
|
|
output()->addFilePath(PATH_APP); |
186
|
|
|
|
187
|
|
|
$this->services->load(Framework\Services\Hooks::class); |
188
|
|
|
|
189
|
|
|
if (profiler() !== false) { |
|
|
|
|
190
|
|
|
profiler()->watch('Starting Framework Services'); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
$services = [ |
194
|
|
|
'Containers\Globals' => 'globals', |
195
|
|
|
'Containers\Environment' => 'environment', |
196
|
|
|
'Services\Loader' => 'loader', |
197
|
|
|
'Services\Config' => 'config' |
198
|
|
|
]; |
199
|
|
|
|
200
|
|
|
foreach ($services as $className => $classOffset) { |
201
|
|
|
$this->services->load($className, $classOffset); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
// Instantiate Models Container |
205
|
|
|
if (profiler() !== false) { |
|
|
|
|
206
|
|
|
profiler()->watch('Starting Models Container'); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
$this->models = new Framework\Containers\Models(); |
210
|
|
|
|
211
|
|
|
// Instantiate Cache Service |
212
|
|
|
if (profiler() !== false) { |
|
|
|
|
213
|
|
|
profiler()->watch('Starting Cache Service'); |
214
|
|
|
} |
215
|
|
|
$this->services->add(new Framework\Services\Cache(config('cache', true)), 'cache'); |
|
|
|
|
216
|
|
|
|
217
|
|
|
// Instantiate Modules Container |
218
|
|
|
if (profiler() !== false) { |
|
|
|
|
219
|
|
|
profiler()->watch('Starting Modules Container'); |
220
|
|
|
} |
221
|
|
|
$this->modules = new Framework\Containers\Modules(); |
222
|
|
|
|
223
|
|
|
if (profiler() !== false) { |
|
|
|
|
224
|
|
|
profiler()->watch('Starting O2System Framework Hooks Pre System'); |
225
|
|
|
} |
226
|
|
|
hooks()->callEvent(Framework\Services\Hooks::PRE_SYSTEM); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
// ------------------------------------------------------------------------ |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Framework::__reconstruct |
233
|
|
|
*/ |
234
|
|
|
protected function __reconstruct() |
235
|
|
|
{ |
236
|
|
|
// Modules default app |
237
|
|
|
if (null !== ($defaultApp = config('app'))) { |
|
|
|
|
238
|
|
|
if (false !== ($defaultModule = modules()->getApp($defaultApp))) { |
|
|
|
|
239
|
|
|
// Register Domain App Module Namespace |
240
|
|
|
loader()->addNamespace($defaultModule->getNamespace(), $defaultModule->getRealPath()); |
241
|
|
|
|
242
|
|
|
// Push Domain App Module |
243
|
|
|
modules()->push($defaultModule); |
244
|
|
|
} elseif (false !== ($defaultModule = modules()->getModule($defaultApp))) { |
|
|
|
|
245
|
|
|
// Register Path Module Namespace |
246
|
|
|
loader()->addNamespace($defaultModule->getNamespace(), $defaultModule->getRealPath()); |
247
|
|
|
|
248
|
|
|
// Push Path Module |
249
|
|
|
modules()->push($defaultModule); |
250
|
|
|
} |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
if (profiler() !== false) { |
|
|
|
|
254
|
|
|
profiler()->watch('Calling Hooks Service: Post System'); |
255
|
|
|
} |
256
|
|
|
hooks()->callEvent(Framework\Services\Hooks::POST_SYSTEM); |
257
|
|
|
|
258
|
|
|
if (is_cli()) { |
259
|
|
|
$this->cliHandler(); |
260
|
|
|
} else { |
261
|
|
|
$this->httpHandler(); |
262
|
|
|
} |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
// ------------------------------------------------------------------------ |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* Framework::cliHandler |
269
|
|
|
* |
270
|
|
|
* @return void |
271
|
|
|
*/ |
272
|
|
|
private function cliHandler() |
273
|
|
|
{ |
274
|
|
|
// Instantiate CLI Router Service |
275
|
|
|
$this->services->load(Framework\Cli\Router::class); |
|
|
|
|
276
|
|
|
|
277
|
|
|
if (profiler() !== false) { |
|
|
|
|
278
|
|
|
profiler()->watch('Parse Router Request'); |
279
|
|
|
} |
280
|
|
|
router()->parseRequest(); |
281
|
|
|
|
282
|
|
|
if ($commander = router()->getCommander()) { |
|
|
|
|
283
|
|
|
if ($commander instanceof Kernel\Cli\Router\Datastructures\Commander) { |
284
|
|
|
// Autoload Language |
285
|
|
|
language()->loadFile($commander->getParameter()); |
286
|
|
|
language()->loadFile($commander->getRequestMethod()); |
287
|
|
|
language()->loadFile($commander->getParameter() . '/' . $commander->getRequestMethod()); |
288
|
|
|
|
289
|
|
|
// Autoload Model |
290
|
|
|
foreach ($this->modules as $module) { |
291
|
|
|
if (in_array($module->getType(), ['KERNEL', 'FRAMEWORK'])) { |
292
|
|
|
continue; |
293
|
|
|
} |
294
|
|
|
$module->loadModel(); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
// Autoload Model |
298
|
|
|
$modelClassName = str_replace('Commanders', 'Models', $commander->getName()); |
299
|
|
|
|
300
|
|
|
if (class_exists($modelClassName)) { |
301
|
|
|
models()->load($modelClassName, 'commander'); |
|
|
|
|
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
// Initialize Controller |
305
|
|
|
if (profiler() !== false) { |
|
|
|
|
306
|
|
|
profiler()->watch('Calling Hooks Service: Pre Commander'); |
307
|
|
|
} |
308
|
|
|
hooks()->callEvent(Framework\Services\Hooks::PRE_COMMANDER); |
309
|
|
|
|
310
|
|
|
if (profiler() !== false) { |
|
|
|
|
311
|
|
|
profiler()->watch('Instantiating Requested Commander: ' . $commander->getClass()); |
312
|
|
|
} |
313
|
|
|
$requestCommander = $commander->getInstance(); |
314
|
|
|
|
315
|
|
|
if (profiler() !== false) { |
|
|
|
|
316
|
|
|
profiler()->watch('Calling Hooks Service: Post Commander'); |
317
|
|
|
} |
318
|
|
|
hooks()->callEvent(Framework\Services\Hooks::POST_COMMANDER); |
319
|
|
|
|
320
|
|
|
if (profiler() !== false) { |
|
|
|
|
321
|
|
|
profiler()->watch('Execute Requested Commander: ' . $commander->getClass()); |
322
|
|
|
} |
323
|
|
|
$requestCommander->execute(); |
324
|
|
|
|
325
|
|
|
exit(EXIT_SUCCESS); |
|
|
|
|
326
|
|
|
} |
327
|
|
|
} |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
// ------------------------------------------------------------------------ |
331
|
|
|
|
332
|
|
|
/** |
333
|
|
|
* Framework::httpHandler |
334
|
|
|
* |
335
|
|
|
* @return void |
336
|
|
|
*/ |
337
|
|
|
private function httpHandler() |
338
|
|
|
{ |
339
|
|
|
// Instantiate Http Router Service |
340
|
|
|
$this->services->load(Framework\Http\Router::class); |
341
|
|
|
|
342
|
|
|
if (profiler() !== false) { |
|
|
|
|
343
|
|
|
profiler()->watch('Parse Router Request'); |
344
|
|
|
} |
345
|
|
|
router()->parseRequest(); |
346
|
|
|
|
347
|
|
|
if (config()->loadFile('session') === true) { |
|
|
|
|
348
|
|
|
|
349
|
|
|
// Instantiate Session Service |
350
|
|
|
$session = new Session(config('session', true)); |
|
|
|
|
351
|
|
|
$session->setLogger($this->services->get('logger')); |
352
|
|
|
|
353
|
|
|
if ( ! $session->isStarted()) { |
354
|
|
|
$session->start(); |
355
|
|
|
} |
356
|
|
|
|
357
|
|
|
$this->services->add($session, 'session'); |
358
|
|
|
|
359
|
|
|
if ($session->has('language') and $this->services->has('language')) { |
360
|
|
|
language()->setDefault($session->get('language')); |
361
|
|
|
} else { |
362
|
|
|
$session->set('language', language()->getDefault()); |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
if (config('security')->protection[ 'csrf' ] === true) { |
|
|
|
|
366
|
|
|
$csrfProtection = new Security\Protections\Csrf(); |
367
|
|
|
$this->services->add($csrfProtection, 'csrfProtection'); |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
if (config('security')->protection[ 'xss' ] === true) { |
371
|
|
|
$csrfProtection = new Security\Protections\Xss(); |
|
|
|
|
372
|
|
|
$this->services->add($xssProtection, 'xssProtection'); |
|
|
|
|
373
|
|
|
} |
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
if (config()->loadFile('view') === true) { |
377
|
|
|
// Instantiate Http UserAgent Service |
378
|
|
|
$this->services->load(Framework\Http\UserAgent::class); |
379
|
|
|
|
380
|
|
|
// Instantiate Http View Service |
381
|
|
|
$this->services->load(Framework\Http\Parser::class); |
382
|
|
|
|
383
|
|
|
// Instantiate Http View Service |
384
|
|
|
$this->services->load(Framework\Http\View::class); |
385
|
|
|
|
386
|
|
|
// Instantiate Http Presenter Service |
387
|
|
|
$this->services->load(Framework\Http\Presenter::class); |
388
|
|
|
presenter()->initialize(); |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
// Instantiate Http Middleware Service |
392
|
|
|
$this->services->load(Framework\Http\Middleware::class); |
393
|
|
|
|
394
|
|
|
if (profiler() !== false) { |
|
|
|
|
395
|
|
|
profiler()->watch('Running Middleware Service: Pre Controller'); |
396
|
|
|
} |
397
|
|
|
middleware()->run(); |
398
|
|
|
|
399
|
|
|
if (false !== ($controller = $this->services->get('controller'))) { |
400
|
|
|
if ($controller instanceof Kernel\Http\Router\Datastructures\Controller) { |
401
|
|
|
$controllerParameter = dash($controller->getParameter()); |
402
|
|
|
$controllerRequestMethod = dash($controller->getRequestMethod()); |
403
|
|
|
|
404
|
|
|
// Autoload Language |
405
|
|
|
if ($this->services->has('language')) { |
406
|
|
|
language()->loadFile($controller->getParameter()); |
407
|
|
|
language()->loadFile($controller->getRequestMethod()); |
408
|
|
|
language()->loadFile($controller->getParameter() . '/' . $controller->getRequestMethod()); |
409
|
|
|
} |
410
|
|
|
|
411
|
|
|
// Autoload Model |
412
|
|
|
foreach ($this->modules as $module) { |
413
|
|
|
if (in_array($module->getType(), ['KERNEL', 'FRAMEWORK'])) { |
414
|
|
|
continue; |
415
|
|
|
} |
416
|
|
|
$module->loadModel(); |
417
|
|
|
} |
418
|
|
|
|
419
|
|
|
// Autoload Model |
420
|
|
|
$modelClassName = str_replace(['Controllers', 'Presenters'], 'Models', $controller->getName()); |
421
|
|
|
|
422
|
|
|
if (class_exists($modelClassName)) { |
423
|
|
|
$this->models->register('controller', new $modelClassName()); |
|
|
|
|
424
|
|
|
} |
425
|
|
|
|
426
|
|
|
// Autoload Assets |
427
|
|
|
if (config()->loadFile('view') === true) { |
428
|
|
|
$controllerAssets = []; |
429
|
|
|
$controllerAssetsDirs = []; |
430
|
|
|
|
431
|
|
|
// Autoload Assets |
432
|
|
|
foreach ($this->modules as $module) { |
433
|
|
|
if (in_array($module->getType(), ['KERNEL', 'FRAMEWORK'])) { |
434
|
|
|
continue; |
435
|
|
|
} |
436
|
|
|
|
437
|
|
|
$controllerAssets[] = $module->getParameter(); |
438
|
|
|
$controllerAssetsDirs[] = $module->getParameter(); |
439
|
|
|
} |
440
|
|
|
|
441
|
|
|
$controllerAssets = array_reverse($controllerAssets); |
442
|
|
|
$controllerAssetsDirs = array_reverse($controllerAssetsDirs); |
443
|
|
|
|
444
|
|
|
$controllerAssets[] = $controllerParameter; |
445
|
|
|
$controllerAssetsDirs[] = $controllerParameter; |
446
|
|
|
|
447
|
|
|
$controllerAssets[] = $controllerRequestMethod; |
448
|
|
|
|
449
|
|
|
foreach ($controllerAssetsDirs as $controllerAssetsDir) { |
450
|
|
|
$controllerAssets[] = $controllerAssetsDir . '/' . $controllerParameter; |
451
|
|
|
$controllerAssets[] = $controllerAssetsDir . '/' . $controllerRequestMethod; |
452
|
|
|
$controllerAssets[] = $controllerAssetsDir . '/' . $controllerParameter . '/' . $controllerRequestMethod; |
453
|
|
|
} |
454
|
|
|
|
455
|
|
|
// Autoload Presenter |
456
|
|
|
$presenterClassName = str_replace('Controllers', 'Presenters', $controller->getName()); |
457
|
|
|
|
458
|
|
|
if (class_exists($presenterClassName)) { |
459
|
|
|
$presenterClassObject = new $presenterClassName(); |
460
|
|
|
if ($presenterClassObject instanceof Framework\Http\Presenter) { |
461
|
|
|
$this->services->add($presenterClassObject, 'presenter'); |
462
|
|
|
} |
463
|
|
|
} |
464
|
|
|
|
465
|
|
|
// Re-Initialize Presenter |
466
|
|
|
presenter()->initialize()->assets->loadFiles( |
|
|
|
|
467
|
|
|
[ |
468
|
|
|
'css' => $controllerAssets, |
469
|
|
|
'js' => $controllerAssets, |
470
|
|
|
] |
471
|
|
|
); |
472
|
|
|
} |
473
|
|
|
|
474
|
|
|
// Initialize Controller |
475
|
|
|
if (profiler() !== false) { |
|
|
|
|
476
|
|
|
profiler()->watch('Calling Hooks Service: Pre Controller'); |
477
|
|
|
} |
478
|
|
|
|
479
|
|
|
hooks()->callEvent(Framework\Services\Hooks::PRE_CONTROLLER); |
480
|
|
|
|
481
|
|
|
if (profiler() !== false) { |
|
|
|
|
482
|
|
|
profiler()->watch('Instantiating Requested Controller: ' . $controller->getClass()); |
483
|
|
|
} |
484
|
|
|
$requestController = $controller->getInstance(); |
485
|
|
|
|
486
|
|
|
if (method_exists($requestController, '__reconstruct')) { |
487
|
|
|
$requestController->__reconstruct(); |
488
|
|
|
} elseif (method_exists($requestController, 'initialize')) { |
489
|
|
|
$requestController->initialize(); |
490
|
|
|
} |
491
|
|
|
|
492
|
|
|
$this->services->add($requestController, 'controller'); |
493
|
|
|
|
494
|
|
|
if (profiler() !== false) { |
|
|
|
|
495
|
|
|
profiler()->watch('Calling Middleware Service: Post Controller'); |
496
|
|
|
} |
497
|
|
|
hooks()->callEvent(Framework\Services\Hooks::POST_CONTROLLER); |
498
|
|
|
|
499
|
|
|
$requestMethod = $controller->getRequestMethod(); |
500
|
|
|
$requestMethodArgs = $controller->getRequestMethodArgs(); |
501
|
|
|
|
502
|
|
|
// Call the requested controller method |
503
|
|
|
if (profiler() !== false) { |
|
|
|
|
504
|
|
|
profiler()->watch('Execute Requested Controller Method'); |
505
|
|
|
} |
506
|
|
|
ob_start(); |
507
|
|
|
$requestControllerOutput = $requestController->__call($requestMethod, $requestMethodArgs); |
508
|
|
|
|
509
|
|
|
if (empty($requestControllerOutput)) { |
510
|
|
|
$requestControllerOutput = ob_get_contents(); |
511
|
|
|
ob_end_clean(); |
512
|
|
|
} elseif (is_bool($requestControllerOutput)) { |
513
|
|
|
if ($requestController instanceof Framework\Http\Controllers\Restful) { |
514
|
|
|
$requestController->sendError($requestControllerOutput); |
515
|
|
|
} else { |
516
|
|
|
if ($requestControllerOutput === true) { |
517
|
|
|
output()->sendError(200); |
518
|
|
|
exit(EXIT_SUCCESS); |
|
|
|
|
519
|
|
|
} elseif ($requestControllerOutput === false) { |
|
|
|
|
520
|
|
|
output()->sendError(204); |
521
|
|
|
exit(EXIT_ERROR); |
|
|
|
|
522
|
|
|
} |
523
|
|
|
} |
524
|
|
|
} elseif (is_array($requestControllerOutput) or is_object($requestControllerOutput)) { |
525
|
|
|
if ($requestController instanceof Framework\Http\Controllers\Restful) { |
526
|
|
|
$requestController->sendPayload($requestControllerOutput); |
527
|
|
|
} else { |
528
|
|
|
output()->send($requestControllerOutput); |
|
|
|
|
529
|
|
|
} |
530
|
|
|
} elseif (is_numeric($requestControllerOutput)) { |
531
|
|
|
output()->sendError($requestControllerOutput); |
532
|
|
|
} |
533
|
|
|
|
534
|
|
|
if (empty($requestControllerOutput) or $requestControllerOutput === '') { |
535
|
|
|
if ($requestController instanceof Framework\Http\Controllers\Restful) { |
536
|
|
|
$requestController->sendError(204); |
537
|
|
|
} elseif (config()->loadFile('view') === true) { |
538
|
|
|
$filenames = [ |
539
|
|
|
$controllerRequestMethod, |
540
|
|
|
$controllerParameter . DIRECTORY_SEPARATOR . $controllerRequestMethod, |
541
|
|
|
]; |
542
|
|
|
|
543
|
|
|
if ($controllerRequestMethod === 'index') { |
544
|
|
|
array_unshift($filenames, $controllerParameter); |
545
|
|
|
} |
546
|
|
|
|
547
|
|
|
foreach ($filenames as $filename) { |
548
|
|
|
if (view()->getFilePath($filename)) { |
549
|
|
|
view()->load($filename); |
550
|
|
|
} |
551
|
|
|
} |
552
|
|
|
|
553
|
|
|
if (presenter()->partials->offsetExists('content')) { |
|
|
|
|
554
|
|
|
view()->render(); |
555
|
|
|
exit(EXIT_SUCCESS); |
|
|
|
|
556
|
|
|
} |
557
|
|
|
} |
558
|
|
|
|
559
|
|
|
// Send default error 204 - No Content |
560
|
|
|
output()->sendError(204); |
561
|
|
|
} elseif (is_string($requestControllerOutput)) { |
562
|
|
|
if (is_json($requestControllerOutput)) { |
563
|
|
|
output()->setContentType('application/json'); |
|
|
|
|
564
|
|
|
output()->send($requestControllerOutput); |
565
|
|
|
} elseif (is_serialized($requestControllerOutput)) { |
566
|
|
|
output()->send($requestControllerOutput); |
567
|
|
|
} elseif (config('presenter', true)->enabled === true) { |
|
|
|
|
568
|
|
|
presenter()->partials->offsetSet('content', $requestControllerOutput); |
569
|
|
|
view()->render(); |
570
|
|
|
} else { |
571
|
|
|
output()->send($requestControllerOutput); |
572
|
|
|
} |
573
|
|
|
exit(EXIT_SUCCESS); |
|
|
|
|
574
|
|
|
} |
575
|
|
|
} |
576
|
|
|
} |
577
|
|
|
|
578
|
|
|
// Show Error (404) Page Not Found |
579
|
|
|
output()->sendError(404); |
580
|
|
|
} |
581
|
|
|
} |
582
|
|
|
|