1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the O2System 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::$config |
157
|
|
|
* |
158
|
|
|
* Framework Container Config |
159
|
|
|
* |
160
|
|
|
* @var Framework\Containers\Config |
161
|
|
|
*/ |
162
|
|
|
public $config; |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Framework::$globals |
166
|
|
|
* |
167
|
|
|
* Framework Container Globals |
168
|
|
|
* |
169
|
|
|
* @var Framework\Containers\Globals |
170
|
|
|
*/ |
171
|
|
|
public $globals; |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Framework::$environment |
175
|
|
|
* |
176
|
|
|
* Framework Container Environment |
177
|
|
|
* |
178
|
|
|
* @var Framework\Containers\Environment |
179
|
|
|
*/ |
180
|
|
|
public $environment; |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Framework::$models |
184
|
|
|
* |
185
|
|
|
* Framework Container Models |
186
|
|
|
* |
187
|
|
|
* @var Framework\Containers\Models |
188
|
|
|
*/ |
189
|
|
|
public $models; |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Framework::$modules |
193
|
|
|
* |
194
|
|
|
* Framework Container Modules |
195
|
|
|
* |
196
|
|
|
* @var Framework\Containers\Modules |
197
|
|
|
*/ |
198
|
|
|
public $modules; |
199
|
|
|
|
200
|
|
|
// ------------------------------------------------------------------------ |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Framework::__construct |
204
|
|
|
*/ |
205
|
|
|
protected function __construct() |
206
|
|
|
{ |
207
|
|
|
parent::__construct(); |
208
|
|
|
|
209
|
|
|
if (profiler() !== false) { |
|
|
|
|
210
|
|
|
profiler()->watch('Starting O2System Framework'); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
// Add App Views Folder |
214
|
|
|
output()->addFilePath(PATH_APP); |
215
|
|
|
|
216
|
|
|
if (profiler() !== false) { |
|
|
|
|
217
|
|
|
profiler()->watch('Starting Framework Services'); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
$services = [ |
221
|
|
|
'Services\Hooks' => 'hooks', |
222
|
|
|
'Services\Shutdown' => 'shutdown', |
223
|
|
|
'Services\Logger' => 'logger', |
224
|
|
|
'Services\Loader' => 'loader', |
225
|
|
|
]; |
226
|
|
|
|
227
|
|
|
foreach ($services as $className => $classOffset) { |
228
|
|
|
$this->services->load($className, $classOffset); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
// Instantiate Config Container |
232
|
|
|
if (profiler() !== false) { |
|
|
|
|
233
|
|
|
profiler()->watch('Starting Config Container'); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
$this->config = new Framework\Containers\Config(); |
237
|
|
|
|
238
|
|
|
// Instantiate Globals Container |
239
|
|
|
if (profiler() !== false) { |
|
|
|
|
240
|
|
|
profiler()->watch('Starting Globals Container'); |
241
|
|
|
} |
242
|
|
|
$this->globals = new Framework\Containers\Globals(); |
243
|
|
|
|
244
|
|
|
// Instantiate Environment Container |
245
|
|
|
if (profiler() !== false) { |
|
|
|
|
246
|
|
|
profiler()->watch('Starting Environment Container'); |
247
|
|
|
} |
248
|
|
|
$this->environment = new Framework\Containers\Environment(); |
249
|
|
|
|
250
|
|
|
// Instantiate Models Container |
251
|
|
|
if (profiler() !== false) { |
|
|
|
|
252
|
|
|
profiler()->watch('Starting Models Container'); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
$this->models = new Framework\Containers\Models(); |
256
|
|
|
|
257
|
|
|
// Instantiate Modules Container |
258
|
|
|
if (profiler() !== false) { |
|
|
|
|
259
|
|
|
profiler()->watch('Starting Modules Container'); |
260
|
|
|
} |
261
|
|
|
$this->modules = new Framework\Containers\Modules(); |
262
|
|
|
|
263
|
|
|
if (config()->loadFile('cache') === true) { |
|
|
|
|
264
|
|
|
// Instantiate Cache Service |
265
|
|
|
if (profiler() !== false) { |
|
|
|
|
266
|
|
|
profiler()->watch('Starting Cache Service'); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
$this->services->add(new Framework\Services\Cache(config('cache', true)), 'cache'); |
|
|
|
|
270
|
|
|
|
271
|
|
|
// Language Service Load Registry |
272
|
|
|
if (profiler() !== false) { |
|
|
|
|
273
|
|
|
profiler()->watch('Loading Language Registry'); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
language()->loadRegistry(); |
|
|
|
|
277
|
|
|
|
278
|
|
|
// Modules Service Load Registry |
279
|
|
|
if (profiler() !== false) { |
|
|
|
|
280
|
|
|
profiler()->watch('Loading Modules Registry'); |
281
|
|
|
} |
282
|
|
|
$this->modules->loadRegistry(); |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
if (profiler() !== false) { |
|
|
|
|
286
|
|
|
profiler()->watch('Starting O2System Framework Hooks Pre System'); |
287
|
|
|
} |
288
|
|
|
hooks()->callEvent(Framework\Services\Hooks::PRE_SYSTEM); |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
// ------------------------------------------------------------------------ |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* Framework::__reconstruct |
295
|
|
|
*/ |
296
|
|
|
protected function __reconstruct() |
297
|
|
|
{ |
298
|
|
|
// Modules default app |
299
|
|
|
if (null !== ($defaultApp = config('app'))) { |
|
|
|
|
300
|
|
|
if (false !== ($defaultModule = modules()->getApp($defaultApp))) { |
|
|
|
|
301
|
|
|
// Register Domain App Module Namespace |
302
|
|
|
loader()->addNamespace($defaultModule->getNamespace(), $defaultModule->getRealPath()); |
303
|
|
|
|
304
|
|
|
// Push Domain App Module |
305
|
|
|
modules()->push($defaultModule); |
|
|
|
|
306
|
|
|
} elseif (false !== ($defaultModule = modules()->getModule($defaultApp))) { |
|
|
|
|
307
|
|
|
// Register Path Module Namespace |
308
|
|
|
loader()->addNamespace($defaultModule->getNamespace(), $defaultModule->getRealPath()); |
309
|
|
|
|
310
|
|
|
// Push Path Module |
311
|
|
|
modules()->push($defaultModule); |
312
|
|
|
} |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
if (profiler() !== false) { |
|
|
|
|
316
|
|
|
profiler()->watch('Calling Hooks Service: Post System'); |
317
|
|
|
} |
318
|
|
|
hooks()->callEvent(Framework\Services\Hooks::POST_SYSTEM); |
319
|
|
|
|
320
|
|
|
if (is_cli()) { |
321
|
|
|
$this->cliHandler(); |
322
|
|
|
} else { |
323
|
|
|
$this->httpHandler(); |
324
|
|
|
} |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
// ------------------------------------------------------------------------ |
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* Framework::cliHandler |
331
|
|
|
* |
332
|
|
|
* @return void |
333
|
|
|
* @throws \ReflectionException |
334
|
|
|
*/ |
335
|
|
|
private function cliHandler() |
336
|
|
|
{ |
337
|
|
|
// Instantiate CLI Router Service |
338
|
|
|
$this->services->load(Kernel\Cli\Router::class); |
339
|
|
|
|
340
|
|
|
if (profiler() !== false) { |
|
|
|
|
341
|
|
|
profiler()->watch('Parse Router Request'); |
342
|
|
|
} |
343
|
|
|
router()->parseRequest(); |
344
|
|
|
|
345
|
|
|
if ($commander = router()->getCommander()) { |
|
|
|
|
346
|
|
|
if ($commander instanceof Kernel\Cli\Router\DataStructures\Commander) { |
|
|
|
|
347
|
|
|
// Autoload Language |
348
|
|
|
language()->loadFile($commander->getParameter()); |
349
|
|
|
language()->loadFile($commander->getRequestMethod()); |
350
|
|
|
language()->loadFile($commander->getParameter() . '/' . $commander->getRequestMethod()); |
351
|
|
|
|
352
|
|
|
// Autoload Model |
353
|
|
|
foreach ($this->modules as $module) { |
354
|
|
|
if (in_array($module->getType(), ['KERNEL', 'FRAMEWORK'])) { |
355
|
|
|
continue; |
356
|
|
|
} |
357
|
|
|
$module->loadModel(); |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
// Autoload Model |
361
|
|
|
$modelClassName = str_replace('Commanders', 'Models', $commander->getName()); |
362
|
|
|
|
363
|
|
|
if (class_exists($modelClassName)) { |
364
|
|
|
$this->models->load($modelClassName, 'commander'); |
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
// Initialize Controller |
368
|
|
|
if (profiler() !== false) { |
|
|
|
|
369
|
|
|
profiler()->watch('Calling Hooks Service: Pre Commander'); |
370
|
|
|
} |
371
|
|
|
hooks()->callEvent(Framework\Services\Hooks::PRE_COMMANDER); |
372
|
|
|
|
373
|
|
|
if (profiler() !== false) { |
|
|
|
|
374
|
|
|
profiler()->watch('Instantiating Requested Commander: ' . $commander->getClass()); |
375
|
|
|
} |
376
|
|
|
$requestCommander = $commander->getInstance(); |
377
|
|
|
|
378
|
|
|
if (profiler() !== false) { |
|
|
|
|
379
|
|
|
profiler()->watch('Calling Hooks Service: Post Commander'); |
380
|
|
|
} |
381
|
|
|
hooks()->callEvent(Framework\Services\Hooks::POST_COMMANDER); |
382
|
|
|
|
383
|
|
|
if (profiler() !== false) { |
|
|
|
|
384
|
|
|
profiler()->watch('Execute Requested Commander: ' . $commander->getClass()); |
385
|
|
|
} |
386
|
|
|
$requestCommander->execute(); |
387
|
|
|
|
388
|
|
|
exit(EXIT_SUCCESS); |
|
|
|
|
389
|
|
|
} |
390
|
|
|
} |
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
// ------------------------------------------------------------------------ |
394
|
|
|
|
395
|
|
|
/** |
396
|
|
|
* Framework::httpHandler |
397
|
|
|
* |
398
|
|
|
* @return void |
399
|
|
|
* @throws \ReflectionException |
400
|
|
|
*/ |
401
|
|
|
private function httpHandler() |
402
|
|
|
{ |
403
|
|
|
if (config()->loadFile('session') === true) { |
404
|
|
|
|
405
|
|
|
// Instantiate Session Service |
406
|
|
|
$session = new Session(config('session', true)); |
|
|
|
|
407
|
|
|
$session->setLogger($this->services->get('logger')); |
408
|
|
|
|
409
|
|
|
if ( ! $session->isStarted()) { |
410
|
|
|
$session->start(); |
411
|
|
|
} |
412
|
|
|
|
413
|
|
|
$this->services->add($session, 'session'); |
414
|
|
|
|
415
|
|
|
if ($session->has('language') and $this->services->has('language')) { |
416
|
|
|
language()->setDefault($session->get('language')); |
417
|
|
|
} else { |
418
|
|
|
$session->set('language', language()->getDefault()); |
419
|
|
|
} |
420
|
|
|
|
421
|
|
|
if (config('security')->protection[ 'csrf' ] === true) { |
|
|
|
|
422
|
|
|
$csrfProtection = new Security\Protections\Csrf(); |
423
|
|
|
$this->services->add($csrfProtection, 'csrfProtection'); |
424
|
|
|
} |
425
|
|
|
|
426
|
|
|
if (config('security')->protection[ 'xss' ] === true) { |
427
|
|
|
$xssProtection = new Security\Protections\Xss(); |
428
|
|
|
$this->services->add($xssProtection, 'xssProtection'); |
429
|
|
|
} |
430
|
|
|
} |
431
|
|
|
|
432
|
|
|
if (config()->loadFile('view') === true) { |
433
|
|
|
// Instantiate Http UserAgent Service |
434
|
|
|
$this->services->load(Framework\Http\UserAgent::class); |
435
|
|
|
|
436
|
|
|
// Instantiate Http View Service |
437
|
|
|
$this->services->load(Framework\Http\Parser::class); |
438
|
|
|
|
439
|
|
|
// Instantiate Http View Service |
440
|
|
|
$this->services->load(Framework\Http\View::class); |
441
|
|
|
|
442
|
|
|
// Instantiate Http Presenter Service |
443
|
|
|
$this->services->load(Framework\Http\Presenter::class); |
444
|
|
|
|
445
|
|
|
// Initialize Http Presenter Service |
446
|
|
|
presenter()->initialize(); |
447
|
|
|
} |
448
|
|
|
|
449
|
|
|
// Instantiate Http Middleware Service |
450
|
|
|
$this->services->load(Framework\Http\Middleware::class); |
451
|
|
|
|
452
|
|
|
// Instantiate Http Router Service |
453
|
|
|
$this->services->load(Framework\Http\Router::class); |
454
|
|
|
|
455
|
|
|
if (profiler() !== false) { |
|
|
|
|
456
|
|
|
profiler()->watch('Parse Router Request'); |
457
|
|
|
} |
458
|
|
|
router()->parseRequest(); |
459
|
|
|
|
460
|
|
|
if (profiler() !== false) { |
|
|
|
|
461
|
|
|
profiler()->watch('Running Middleware Service: Pre Controller'); |
462
|
|
|
} |
463
|
|
|
middleware()->run(); |
464
|
|
|
|
465
|
|
|
if ($this->services->has('controller')) { |
466
|
|
|
$controller = $this->services->get('controller'); |
467
|
|
|
|
468
|
|
|
$controllerParameter = dash($controller->getParameter()); |
469
|
|
|
$controllerRequestMethod = dash($controller->getRequestMethod()); |
470
|
|
|
|
471
|
|
|
// Autoload Language |
472
|
|
|
if ($this->services->has('language')) { |
473
|
|
|
language()->loadFile($controller->getParameter()); |
474
|
|
|
language()->loadFile($controller->getRequestMethod()); |
475
|
|
|
language()->loadFile($controller->getParameter() . '/' . $controller->getRequestMethod()); |
476
|
|
|
} |
477
|
|
|
|
478
|
|
|
// Autoload Model |
479
|
|
|
foreach ($this->modules as $module) { |
480
|
|
|
if (in_array($module->getType(), ['KERNEL', 'FRAMEWORK'])) { |
481
|
|
|
continue; |
482
|
|
|
} |
483
|
|
|
$module->loadModel(); |
484
|
|
|
} |
485
|
|
|
|
486
|
|
|
// Autoload Model |
487
|
|
|
$modelClassName = str_replace(['Controllers', 'Presenters'], 'Models', $controller->getName()); |
488
|
|
|
|
489
|
|
|
if (class_exists($modelClassName)) { |
490
|
|
|
$this->models->load($modelClassName, 'controller'); |
491
|
|
|
} |
492
|
|
|
|
493
|
|
|
// Autoload Assets |
494
|
|
|
if ($this->services->has('view')) { |
495
|
|
|
$controllerAssets = []; |
496
|
|
|
$controllerAssetsDirs = []; |
497
|
|
|
|
498
|
|
|
// Autoload Assets |
499
|
|
|
foreach ($this->modules as $module) { |
500
|
|
|
if (in_array($module->getType(), ['KERNEL', 'FRAMEWORK'])) { |
501
|
|
|
continue; |
502
|
|
|
} |
503
|
|
|
|
504
|
|
|
$controllerAssets[] = $module->getParameter(); |
505
|
|
|
$controllerAssetsDirs[] = $module->getParameter(); |
506
|
|
|
} |
507
|
|
|
|
508
|
|
|
$controllerAssets = array_reverse($controllerAssets); |
509
|
|
|
$controllerAssetsDirs = array_reverse($controllerAssetsDirs); |
510
|
|
|
|
511
|
|
|
$controllerAssets[] = $controllerParameter; |
512
|
|
|
$controllerAssetsDirs[] = $controllerParameter; |
513
|
|
|
|
514
|
|
|
$controllerAssets[] = $controllerRequestMethod; |
515
|
|
|
|
516
|
|
|
foreach ($controllerAssetsDirs as $controllerAssetsDir) { |
517
|
|
|
$controllerAssets[] = $controllerAssetsDir . '/' . $controllerParameter; |
518
|
|
|
$controllerAssets[] = $controllerAssetsDir . '/' . $controllerRequestMethod; |
519
|
|
|
$controllerAssets[] = $controllerAssetsDir . '/' . $controllerParameter . '/' . $controllerRequestMethod; |
520
|
|
|
} |
521
|
|
|
|
522
|
|
|
// Autoload Presenter |
523
|
|
|
$presenterClassName = str_replace('Controllers', 'Presenters', $controller->getName()); |
524
|
|
|
|
525
|
|
|
if (class_exists($presenterClassName)) { |
526
|
|
|
$presenterClassObject = new $presenterClassName(); |
527
|
|
|
if ($presenterClassObject instanceof Framework\Http\Presenter) { |
528
|
|
|
$this->services->add($presenterClassObject, 'presenter'); |
529
|
|
|
} |
530
|
|
|
} |
531
|
|
|
|
532
|
|
|
// Autoload Assets |
533
|
|
|
presenter()->assets->loadCss($controllerAssets); |
534
|
|
|
presenter()->assets->loadJs($controllerAssets); |
535
|
|
|
} |
536
|
|
|
|
537
|
|
|
// Initialize Controller |
538
|
|
|
if (profiler() !== false) { |
|
|
|
|
539
|
|
|
profiler()->watch('Calling Hooks Service: Pre Controller'); |
540
|
|
|
} |
541
|
|
|
|
542
|
|
|
hooks()->callEvent(Framework\Services\Hooks::PRE_CONTROLLER); |
543
|
|
|
|
544
|
|
|
if (profiler() !== false) { |
|
|
|
|
545
|
|
|
profiler()->watch('Instantiating Requested Controller: ' . $controller->getClass()); |
546
|
|
|
} |
547
|
|
|
$requestController = $controller->getInstance(); |
548
|
|
|
|
549
|
|
|
if (method_exists($requestController, '__reconstruct')) { |
550
|
|
|
$requestController->__reconstruct(); |
551
|
|
|
} |
552
|
|
|
|
553
|
|
|
$this->services->add($requestController, 'controller'); |
554
|
|
|
|
555
|
|
|
if (profiler() !== false) { |
|
|
|
|
556
|
|
|
profiler()->watch('Calling Hooks Service: Post Controller'); |
557
|
|
|
} |
558
|
|
|
hooks()->callEvent(Framework\Services\Hooks::POST_CONTROLLER); |
559
|
|
|
|
560
|
|
|
if (profiler() !== false) { |
|
|
|
|
561
|
|
|
profiler()->watch('Calling Middleware Service: Post Controller'); |
562
|
|
|
} |
563
|
|
|
middleware()->run(); |
564
|
|
|
|
565
|
|
|
$requestMethod = $controller->getRequestMethod(); |
566
|
|
|
$requestMethodArgs = $controller->getRequestMethodArgs(); |
567
|
|
|
|
568
|
|
|
// Call the requested controller method |
569
|
|
|
if (profiler() !== false) { |
|
|
|
|
570
|
|
|
profiler()->watch('Execute Requested Controller Method'); |
571
|
|
|
} |
572
|
|
|
|
573
|
|
|
ob_start(); |
574
|
|
|
$requestControllerOutput = $requestController->__call($requestMethod, $requestMethodArgs); |
575
|
|
|
|
576
|
|
|
if (empty($requestControllerOutput)) { |
577
|
|
|
$requestControllerOutput = ob_get_contents(); |
578
|
|
|
ob_end_clean(); |
579
|
|
|
} |
580
|
|
|
|
581
|
|
|
if (is_numeric($requestControllerOutput)) { |
582
|
|
|
output()->sendError($requestControllerOutput); |
583
|
|
|
} elseif (is_bool($requestControllerOutput)) { |
584
|
|
|
if ($requestControllerOutput === true) { |
585
|
|
|
output()->sendError(200); |
586
|
|
|
} elseif ($requestControllerOutput === false) { |
|
|
|
|
587
|
|
|
output()->sendError(204); |
588
|
|
|
} |
589
|
|
|
} elseif (is_array($requestControllerOutput) or is_object($requestControllerOutput)) { |
590
|
|
|
output()->sendPayload($requestControllerOutput); |
|
|
|
|
591
|
|
|
} elseif ($requestController instanceof Framework\Http\Controllers\Restful) { |
592
|
|
|
if (empty($requestControllerOutput) or $requestControllerOutput === '') { |
593
|
|
|
$requestController->sendError(204); |
594
|
|
|
} elseif (is_string($requestControllerOutput)) { |
595
|
|
|
if (is_json($requestControllerOutput)) { |
596
|
|
|
output()->setContentType('application/json'); |
|
|
|
|
597
|
|
|
} else { |
598
|
|
|
output()->setContentType('text/plain'); |
599
|
|
|
} |
600
|
|
|
|
601
|
|
|
echo $requestControllerOutput; |
602
|
|
|
} |
603
|
|
|
} elseif (is_string($requestControllerOutput)) { |
604
|
|
|
if (is_json($requestControllerOutput)) { |
605
|
|
|
output()->setContentType('application/json'); |
606
|
|
|
} elseif ($this->services->has('view')) { |
607
|
|
|
if (empty($requestControllerOutput) or $requestControllerOutput === '') { |
608
|
|
|
$filenames = [ |
609
|
|
|
$controllerRequestMethod, |
610
|
|
|
$controllerParameter . DIRECTORY_SEPARATOR . $controllerRequestMethod, |
611
|
|
|
]; |
612
|
|
|
|
613
|
|
|
if ($controllerRequestMethod === 'index') { |
614
|
|
|
array_unshift($filenames, $controllerParameter); |
615
|
|
|
} |
616
|
|
|
|
617
|
|
|
foreach ($filenames as $filename) { |
618
|
|
|
if (false !== ($filePath = view()->getFilePath($filename))) { |
619
|
|
|
view()->load($filePath); |
620
|
|
|
break; |
621
|
|
|
} |
622
|
|
|
} |
623
|
|
|
} else { |
624
|
|
|
presenter()->partials->offsetSet('content', $requestControllerOutput); |
625
|
|
|
} |
626
|
|
|
|
627
|
|
|
if (presenter()->partials->offsetExists('content')) { |
628
|
|
|
$htmlOutput = view()->render(); |
629
|
|
|
|
630
|
|
|
if (empty($htmlOutput)) { |
631
|
|
|
output()->sendError(204); |
632
|
|
|
} else { |
633
|
|
|
output()->setContentType('text/html'); |
634
|
|
|
output()->send($htmlOutput); |
|
|
|
|
635
|
|
|
} |
636
|
|
|
} else { |
637
|
|
|
output()->sendError(204); |
638
|
|
|
} |
639
|
|
|
} elseif (empty($requestControllerOutput) or $requestControllerOutput === '') { |
640
|
|
|
output()->sendError(204); |
641
|
|
|
} else { |
642
|
|
|
output()->setContentType('text/plain'); |
643
|
|
|
output()->send($requestControllerOutput); |
644
|
|
|
} |
645
|
|
|
} |
646
|
|
|
} else { |
647
|
|
|
// Show Error (404) Page Not Found |
648
|
|
|
output()->sendError(404); |
649
|
|
|
} |
650
|
|
|
} |
651
|
|
|
} |
652
|
|
|
|