|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author Fran López <[email protected]> |
|
4
|
|
|
* @version 1.0 |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
namespace PSFS; |
|
8
|
|
|
|
|
9
|
|
|
use PSFS\base\config\Config; |
|
10
|
|
|
use PSFS\base\events\CloseSessionEvent; |
|
11
|
|
|
use PSFS\base\exception\AdminCredentialsException; |
|
12
|
|
|
use PSFS\base\exception\ApiException; |
|
13
|
|
|
use PSFS\base\exception\RouterException; |
|
14
|
|
|
use PSFS\base\exception\SecurityException; |
|
15
|
|
|
use PSFS\base\Logger; |
|
16
|
|
|
use PSFS\base\Request; |
|
17
|
|
|
use PSFS\base\Singleton; |
|
18
|
|
|
use PSFS\base\types\helpers\EventHelper; |
|
19
|
|
|
use PSFS\base\types\helpers\I18nHelper; |
|
20
|
|
|
use PSFS\base\types\helpers\Inspector; |
|
21
|
|
|
use PSFS\base\types\helpers\RequestHelper; |
|
22
|
|
|
use PSFS\base\types\traits\SystemTrait; |
|
23
|
|
|
use PSFS\controller\ConfigController; |
|
24
|
|
|
use PSFS\controller\UserController; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Class Dispatcher |
|
28
|
|
|
* @package PSFS |
|
29
|
|
|
*/ |
|
30
|
|
|
class Dispatcher extends Singleton |
|
31
|
|
|
{ |
|
32
|
|
|
use SystemTrait; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @Injectable |
|
36
|
|
|
* @var \PSFS\base\Security $security |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $security; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @Injectable |
|
42
|
|
|
* @var \PSFS\base\Router $router |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $router; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @Injectable |
|
48
|
|
|
* @var \PSFS\base\config\Config $config |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $config; |
|
51
|
|
|
|
|
52
|
|
|
private $actualUri; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Initializer method |
|
56
|
|
|
* @throws base\exception\GeneratorException |
|
57
|
|
|
*/ |
|
58
|
1 |
|
public function init() |
|
59
|
|
|
{ |
|
60
|
1 |
|
Config::getInstance(); |
|
61
|
1 |
|
Inspector::stats('[Dispatcher] Dispatcher init', Inspector::SCOPE_DEBUG); |
|
62
|
1 |
|
parent::init(); |
|
63
|
1 |
|
$this->initiateStats(); |
|
64
|
1 |
|
I18nHelper::setLocale(); |
|
65
|
1 |
|
$this->bindWarningAsExceptions(); |
|
66
|
1 |
|
$this->actualUri = Request::getInstance()->getServer('REQUEST_URI'); |
|
67
|
1 |
|
Inspector::stats('[Dispatcher] Dispatcher init end', Inspector::SCOPE_DEBUG); |
|
68
|
1 |
|
EventHelper::addEvent(EventHelper::EVENT_END_REQUEST, CloseSessionEvent::class); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Run method |
|
73
|
|
|
* @param string $uri |
|
74
|
|
|
* @return string HTML |
|
75
|
|
|
* @throws base\exception\GeneratorException |
|
76
|
|
|
*/ |
|
77
|
6 |
|
public function run($uri = null) |
|
78
|
|
|
{ |
|
79
|
6 |
|
Inspector::stats('[Dispatcher] Begin runner', Inspector::SCOPE_DEBUG); |
|
80
|
|
|
|
|
81
|
|
|
try { |
|
82
|
6 |
|
if ($this->config->isConfigured()) { |
|
83
|
|
|
// Check CORS for requests |
|
84
|
5 |
|
RequestHelper::checkCORS(); |
|
85
|
5 |
|
if (!Request::getInstance()->isFile()) { |
|
86
|
5 |
|
return $this->router->execute($uri ?? $this->actualUri); |
|
87
|
|
|
} |
|
88
|
|
|
} else { |
|
89
|
1 |
|
return ConfigController::getInstance()->config(); |
|
90
|
|
|
} |
|
91
|
4 |
|
} catch (AdminCredentialsException $a) { |
|
92
|
|
|
return UserController::showAdminManager(); |
|
93
|
4 |
|
} catch (SecurityException $s) { |
|
94
|
1 |
|
return $this->security->notAuthorized($this->actualUri); |
|
95
|
3 |
|
} catch (RouterException $r) { |
|
96
|
1 |
|
return $this->router->httpNotFound($r); |
|
97
|
2 |
|
} catch (ApiException $a) { |
|
98
|
|
|
return $this->router->httpNotFound($a, true); |
|
99
|
2 |
|
} catch (\Exception $e) { |
|
100
|
2 |
|
return $this->handleException($e); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
return $this->router->httpNotFound(); |
|
104
|
|
|
|
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
2 |
|
protected function handleException(\Exception $exception): string |
|
108
|
|
|
{ |
|
109
|
2 |
|
Inspector::stats('[Dispatcher] Starting dump exception', Inspector::SCOPE_DEBUG); |
|
110
|
|
|
|
|
111
|
2 |
|
$error = [ |
|
112
|
2 |
|
"error" => $exception->getMessage(), |
|
113
|
2 |
|
"file" => $exception->getFile(), |
|
114
|
2 |
|
"line" => $exception->getLine(), |
|
115
|
2 |
|
]; |
|
116
|
|
|
|
|
117
|
2 |
|
Logger::log('Throwing exception', LOG_ERR, $error); |
|
118
|
2 |
|
unset($error); |
|
119
|
|
|
|
|
120
|
2 |
|
return $this->router->httpNotFound($exception); |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
Config::initialize(); |
|
125
|
|
|
|