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\traits\SystemTrait; |
18
|
|
|
use PSFS\controller\ConfigController; |
19
|
|
|
use PSFS\controller\UserController; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class Dispatcher |
23
|
|
|
* @package PSFS |
24
|
|
|
*/ |
25
|
|
|
class Dispatcher extends Singleton |
26
|
|
|
{ |
27
|
|
|
use SystemTrait; |
28
|
|
|
/** |
29
|
|
|
* @Inyectable |
30
|
|
|
* @var \PSFS\base\Security $security |
31
|
|
|
*/ |
32
|
|
|
protected $security; |
33
|
|
|
/** |
34
|
|
|
* @Inyectable |
35
|
|
|
* @var \PSFS\base\Router $router |
36
|
|
|
*/ |
37
|
|
|
protected $router; |
38
|
|
|
/** |
39
|
|
|
* @Inyectable |
40
|
|
|
* @var \PSFS\base\config\Config $config |
41
|
|
|
*/ |
42
|
|
|
protected $config; |
43
|
|
|
|
44
|
|
|
private $actualUri; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Initializer method |
48
|
|
|
*/ |
49
|
1 |
|
public function init() |
50
|
|
|
{ |
51
|
1 |
|
Logger::log('Dispatcher init'); |
52
|
1 |
|
parent::init(); |
53
|
1 |
|
$this->initiateStats(); |
54
|
1 |
|
I18nHelper::setLocale(); |
55
|
1 |
|
$this->bindWarningAsExceptions(); |
56
|
1 |
|
$this->actualUri = Request::getInstance()->getServer("REQUEST_URI"); |
57
|
1 |
|
Logger::log('End dispatcher init'); |
58
|
1 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Run method |
62
|
|
|
* @return string HTML |
|
|
|
|
63
|
|
|
*/ |
64
|
5 |
|
public function run() |
65
|
|
|
{ |
66
|
5 |
|
Logger::log('Begin runner'); |
67
|
|
|
try { |
68
|
5 |
|
if ($this->config->isConfigured()) { |
69
|
4 |
|
if (!Request::getInstance()->isFile()) { |
70
|
4 |
|
return $this->router->execute($this->actualUri); |
71
|
|
|
} |
72
|
|
|
} else { |
73
|
1 |
|
return ConfigController::getInstance()->config(); |
74
|
|
|
} |
75
|
4 |
|
} catch (AdminCredentialsException $a) { |
76
|
|
|
return UserController::showAdminManager(); |
77
|
4 |
|
} catch (SecurityException $s) { |
78
|
1 |
|
return $this->security->notAuthorized($this->actualUri); |
79
|
3 |
|
} catch (RouterException $r) { |
80
|
1 |
|
return $this->router->httpNotFound($r); |
81
|
2 |
|
} catch (\Exception $e) { |
82
|
2 |
|
return $this->dumpException($e); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Method that convert an exception to html |
88
|
|
|
* |
89
|
|
|
* @param \Exception $e |
90
|
|
|
* |
91
|
|
|
* @return string HTML |
|
|
|
|
92
|
|
|
*/ |
93
|
2 |
|
protected function dumpException(\Exception $e) |
|
|
|
|
94
|
|
|
{ |
95
|
2 |
|
Logger::log('Starting dump exception'); |
96
|
2 |
|
$ex = (NULL !== $e->getPrevious()) ? $e->getPrevious() : $e; |
|
|
|
|
97
|
|
|
$error = array( |
98
|
2 |
|
"error" => $ex->getMessage(), |
99
|
2 |
|
"file" => $ex->getFile(), |
100
|
2 |
|
"line" => $ex->getLine(), |
101
|
2 |
|
); |
102
|
2 |
|
Logger::log('Throwing exception', LOG_ERR, $error); |
103
|
2 |
|
unset($error); |
104
|
|
|
|
105
|
2 |
|
return $this->router->httpNotFound($ex); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
} |
109
|
|
|
|
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.