1 | <?php |
||
27 | class Dispatcher extends Singleton |
||
28 | { |
||
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\Request $parser |
||
42 | */ |
||
43 | protected $parser; |
||
44 | /** |
||
45 | * @Inyectable |
||
46 | * @var \PSFS\base\Logger $log |
||
47 | */ |
||
48 | protected $log; |
||
49 | /** |
||
50 | * @Inyectable |
||
51 | * @var \PSFS\base\config\Config $config |
||
52 | */ |
||
53 | protected $config; |
||
54 | |||
55 | protected $ts; |
||
56 | protected $mem; |
||
57 | protected $locale = "es_ES"; |
||
58 | |||
59 | private $actualUri; |
||
60 | |||
61 | /** |
||
62 | * Initializer method |
||
63 | */ |
||
64 | 1 | public function init() |
|
65 | { |
||
66 | 1 | Logger::log('Dispatcher init'); |
|
67 | 1 | parent::init(); |
|
68 | 1 | $this->initiateStats(); |
|
69 | 1 | $this->setLocale(); |
|
70 | 1 | $this->bindWarningAsExceptions(); |
|
71 | 1 | $this->actualUri = $this->parser->getServer("REQUEST_URI"); |
|
72 | 1 | Logger::log('End dispatcher init'); |
|
73 | 1 | } |
|
74 | |||
75 | /** |
||
76 | * Method that assign the locale to the request |
||
77 | * @return $this |
||
78 | */ |
||
79 | 1 | private function setLocale() |
|
80 | { |
||
81 | 1 | $this->locale = $this->config->get("default_language"); |
|
82 | 1 | Logger::log('Set locale to project [' . $this->locale . ']'); |
|
83 | // Load translations |
||
84 | 1 | putenv("LC_ALL=" . $this->locale); |
|
85 | 1 | setlocale(LC_ALL, $this->locale); |
|
86 | // Load the locale path |
||
87 | 1 | $locale_path = BASE_DIR . DIRECTORY_SEPARATOR . 'locale'; |
|
88 | 1 | Logger::log('Set locale dir ' . $locale_path); |
|
89 | 1 | GeneratorHelper::createDir($locale_path); |
|
90 | 1 | bindtextdomain('translations', $locale_path); |
|
91 | 1 | textdomain('translations'); |
|
92 | 1 | bind_textdomain_codeset('translations', 'UTF-8'); |
|
93 | |||
94 | 1 | return $this; |
|
95 | } |
||
96 | |||
97 | /** |
||
98 | * Run method |
||
99 | * @return string HTML |
||
100 | */ |
||
101 | 5 | public function run() |
|
102 | { |
||
103 | 5 | Logger::log('Begin runner'); |
|
104 | try { |
||
105 | 5 | if ($this->config->isConfigured()) { |
|
106 | 4 | if (!$this->parser->isFile()) { |
|
107 | 4 | return $this->router->execute($this->actualUri); |
|
108 | } |
||
109 | } else { |
||
110 | 1 | return ConfigController::getInstance()->config(); |
|
111 | } |
||
112 | 4 | } catch (AdminCredentialsException $a) { |
|
113 | return UserController::showAdminManager(); |
||
114 | 4 | } catch (ConfigException $c) { |
|
115 | return $this->dumpException($c); |
||
116 | 4 | } catch (SecurityException $s) { |
|
117 | 1 | return $this->security->notAuthorized($this->actualUri); |
|
118 | 3 | } catch (UserAuthException $u) { |
|
119 | $this->redirectToHome(); |
||
120 | 3 | } catch (RouterException $r) { |
|
121 | 1 | return $this->router->httpNotFound($r); |
|
122 | 2 | } catch (\Exception $e) { |
|
123 | 2 | return $this->dumpException($e); |
|
124 | } |
||
125 | } |
||
126 | |||
127 | private function redirectToHome() |
||
131 | |||
132 | /** |
||
133 | * Method that convert an exception to html |
||
134 | * |
||
135 | * @param \Exception $e |
||
136 | * |
||
137 | * @return string HTML |
||
138 | */ |
||
139 | 2 | protected function dumpException(\Exception $e) |
|
140 | { |
||
141 | 2 | Logger::log('Starting dump exception'); |
|
142 | 2 | $ex = (NULL !== $e->getPrevious()) ? $e->getPrevious() : $e; |
|
143 | $error = array( |
||
144 | 2 | "error" => $ex->getMessage(), |
|
145 | 2 | "file" => $ex->getFile(), |
|
146 | 2 | "line" => $ex->getLine(), |
|
147 | ); |
||
148 | 2 | Logger::log('Throwing exception', LOG_ERR, $error); |
|
149 | 2 | unset($error); |
|
150 | |||
151 | 2 | return $this->router->httpNotFound($ex); |
|
152 | } |
||
153 | |||
154 | /** |
||
155 | * Method that returns the memory used at this specific moment |
||
156 | * |
||
157 | * @param $unit string |
||
158 | * |
||
159 | * @return int |
||
160 | */ |
||
161 | 1 | public function getMem($unit = "Bytes") |
|
177 | |||
178 | /** |
||
179 | * Method that returns the seconds spent with the script |
||
180 | * @return double |
||
181 | */ |
||
182 | 3 | public function getTs() |
|
186 | |||
187 | /** |
||
188 | * Debug function to catch warnings as exceptions |
||
189 | */ |
||
190 | 1 | protected function bindWarningAsExceptions() |
|
201 | |||
202 | /** |
||
203 | * Stats initializer |
||
204 | */ |
||
205 | 1 | private function initiateStats() |
|
215 | |||
216 | } |
||
217 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.