1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Fran López <[email protected]> |
4
|
|
|
* @version 1.0 |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace PSFS; |
8
|
|
|
|
9
|
|
|
use PSFS\base\exception\AdminCredentialsException; |
10
|
|
|
use PSFS\base\exception\RouterException; |
11
|
|
|
use PSFS\base\exception\SecurityException; |
12
|
|
|
use PSFS\base\Logger; |
13
|
|
|
use PSFS\base\Request; |
14
|
|
|
use PSFS\base\Security; |
15
|
|
|
use PSFS\base\Singleton; |
16
|
|
|
use PSFS\base\types\helpers\I18nHelper; |
17
|
|
|
use PSFS\base\types\helpers\RequestHelper; |
18
|
|
|
use PSFS\base\types\traits\SystemTrait; |
19
|
|
|
use PSFS\controller\ConfigController; |
20
|
|
|
use PSFS\controller\UserController; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class Dispatcher |
24
|
|
|
* @package PSFS |
25
|
|
|
*/ |
26
|
|
|
class Dispatcher extends Singleton |
27
|
|
|
{ |
28
|
|
|
use SystemTrait; |
29
|
|
|
/** |
30
|
|
|
* @Inyectable |
31
|
|
|
* @var \PSFS\base\Security $security |
32
|
|
|
*/ |
33
|
|
|
protected $security; |
34
|
|
|
/** |
35
|
|
|
* @Inyectable |
36
|
|
|
* @var \PSFS\base\Router $router |
37
|
|
|
*/ |
38
|
|
|
protected $router; |
39
|
|
|
/** |
40
|
|
|
* @Inyectable |
41
|
|
|
* @var \PSFS\base\config\Config $config |
42
|
|
|
*/ |
43
|
|
|
protected $config; |
44
|
|
|
|
45
|
|
|
private $actualUri; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Initializer method |
49
|
|
|
*/ |
50
|
1 |
|
public function init() |
51
|
|
|
{ |
52
|
1 |
|
Logger::log('Dispatcher init'); |
53
|
1 |
|
parent::init(); |
54
|
1 |
|
$this->initiateStats(); |
55
|
1 |
|
I18nHelper::setLocale(); |
56
|
1 |
|
$this->bindWarningAsExceptions(); |
57
|
1 |
|
$this->actualUri = Request::getInstance()->getServer("REQUEST_URI"); |
58
|
1 |
|
Logger::log('End dispatcher init'); |
59
|
1 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Run method |
63
|
|
|
* @return string HTML |
64
|
|
|
*/ |
65
|
4 |
|
public function run() |
66
|
|
|
{ |
67
|
4 |
|
Logger::log('Begin runner'); |
68
|
|
|
try { |
69
|
4 |
|
if ($this->config->isConfigured()) { |
70
|
|
|
//Check CORS for requests |
71
|
4 |
|
RequestHelper::checkCORS(); |
72
|
4 |
|
if (!Request::getInstance()->isFile()) { |
73
|
4 |
|
return $this->router->execute($this->actualUri); |
74
|
|
|
} |
75
|
|
|
} else { |
76
|
|
|
return ConfigController::getInstance()->config(); |
77
|
|
|
} |
78
|
3 |
|
} catch (AdminCredentialsException $a) { |
79
|
|
|
return UserController::showAdminManager(); |
80
|
3 |
|
} catch (SecurityException $s) { |
81
|
1 |
|
return $this->security->notAuthorized($this->actualUri); |
82
|
2 |
|
} catch (RouterException $r) { |
83
|
1 |
|
return $this->router->httpNotFound($r); |
84
|
1 |
|
} catch (\Exception $e) { |
85
|
1 |
|
return $this->dumpException($e); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Method that convert an exception to html |
91
|
|
|
* |
92
|
|
|
* @param \Exception $e |
93
|
|
|
* |
94
|
|
|
* @return string HTML |
95
|
|
|
*/ |
96
|
1 |
|
protected function dumpException(\Exception $e) |
|
|
|
|
97
|
|
|
{ |
98
|
1 |
|
Logger::log('Starting dump exception'); |
99
|
1 |
|
$ex = (NULL !== $e->getPrevious()) ? $e->getPrevious() : $e; |
|
|
|
|
100
|
|
|
$error = array( |
101
|
1 |
|
"error" => $ex->getMessage(), |
102
|
1 |
|
"file" => $ex->getFile(), |
103
|
1 |
|
"line" => $ex->getLine(), |
104
|
|
|
); |
105
|
1 |
|
Logger::log('Throwing exception', LOG_ERR, $error); |
106
|
1 |
|
unset($error); |
107
|
|
|
|
108
|
1 |
|
return $this->router->httpNotFound($ex); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
} |
112
|
|
|
|
Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.