Total Complexity | 42 |
Total Lines | 243 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like router often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use router, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
7 | class router extends themes |
||
8 | { |
||
9 | public $root; |
||
10 | public $PHP_ERROR_FILE = PHP_ERROR_FILE; |
||
11 | |||
12 | public function __construct() |
||
13 | { |
||
14 | parent::__construct(); |
||
15 | } |
||
16 | |||
17 | /** |
||
18 | * Safe redirect, support any conditions. |
||
19 | */ |
||
20 | public static function safe_redirect(string $path) |
||
21 | { |
||
22 | if (ob_get_level()) { |
||
23 | ob_end_clean(); |
||
24 | } |
||
25 | ob_start(); |
||
26 | header('HTTP/1.1 503 Service Unavailable'); |
||
27 | header('Status: 503 Service Unavailable'); |
||
28 | header('Retry-After: 3600'); |
||
29 | header("refresh:5; url=$path"); |
||
30 | |||
31 | if (!headers_sent()) { |
||
32 | header("Location: $path", true, 302); |
||
33 | } else { |
||
34 | echo '<!DOCTYPE html> |
||
35 | <html> |
||
36 | |||
37 | <head> |
||
38 | <title>Temporarily Unavailable</title> |
||
39 | <meta name="robots" content="none" /> |
||
40 | <meta http-equiv="refresh" content="5; url=' . $path . '" /> |
||
41 | </head> |
||
42 | |||
43 | <body> |
||
44 | You will redirected automatically |
||
45 | </body> |
||
46 | <script> |
||
47 | location.href = `' . $path . '`; |
||
48 | document.body.innerHTML = ""; |
||
49 | throw ""; |
||
50 | </script> |
||
51 | |||
52 | </html>'; |
||
53 | } |
||
54 | exit; |
||
|
|||
55 | } |
||
56 | |||
57 | public function redirect(string $path) |
||
58 | { |
||
59 | return self::safe_redirect($path); |
||
60 | } |
||
61 | |||
62 | public function no_direct() |
||
63 | { |
||
64 | if (1 == count(get_included_files())) { |
||
65 | header('Location: /'); // Send to index |
||
66 | die('403'); // Must include to stop PHP from continuing |
||
67 | } |
||
68 | } |
||
69 | |||
70 | public $env; |
||
71 | |||
72 | /** |
||
73 | * Define environtment |
||
74 | * * development / production. |
||
75 | * |
||
76 | * @return string |
||
77 | */ |
||
78 | public function environtment($env = 'production') |
||
79 | { |
||
80 | $env = strtolower($env); //$GLOBALS['router']['env'] |
||
81 | switch ($env) { |
||
82 | case 'development': |
||
83 | //error_reporting(-1); |
||
84 | ini_set('display_errors', 1); |
||
85 | ini_set('display_startup_errors', 1); |
||
86 | error_reporting(E_ALL); |
||
87 | ini_set('log_errors', 1); |
||
88 | ini_set('error_log', \Filemanager\file::file($this->PHP_ERROR_FILE, '')); |
||
89 | break; |
||
90 | |||
91 | case 'production': |
||
92 | ini_set('display_errors', 0); |
||
93 | if (version_compare(PHP_VERSION, '5.3', '>=')) { |
||
94 | error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED & ~E_STRICT & ~E_USER_NOTICE & ~E_USER_DEPRECATED); |
||
95 | } else { |
||
96 | error_reporting(E_ALL & ~E_NOTICE & ~E_STRICT & ~E_USER_NOTICE); |
||
97 | } |
||
98 | break; |
||
99 | } |
||
100 | if (empty($env)) { |
||
101 | if (ob_get_level()) { |
||
102 | ob_end_clean(); |
||
103 | } |
||
104 | header('HTTP/1.1 503 Service Unavailable.', true, 503); |
||
105 | echo 'The application environment is not set correctly.'; |
||
106 | exit(1); // EXIT_ERROR |
||
107 | } |
||
108 | |||
109 | file_put_contents(ROOT . '/tmp/environtment.txt', $env, true); |
||
110 | $GLOBALS['router_state']['env'] = $this->env = $env; |
||
111 | |||
112 | return $env; |
||
113 | } |
||
114 | |||
115 | public function is_production() |
||
116 | { |
||
117 | return 'production' == $this->get_env(); |
||
118 | } |
||
119 | |||
120 | private static $env_status = null; |
||
121 | |||
122 | /** |
||
123 | * Get framework environtment. |
||
124 | * |
||
125 | * @return string |
||
126 | */ |
||
127 | public static function get_env() |
||
128 | { |
||
129 | if (isset($GLOBALS['router_state']['env'])) { |
||
130 | return $GLOBALS['router_state']['env']; |
||
131 | } |
||
132 | if (!self::$env_status) { |
||
133 | self::$env_status = \Filemanager\file::get(\Filemanager\file::tmp() . '/environtment.txt'); |
||
134 | } |
||
135 | if (!self::$env_status) { |
||
136 | \Cookie\helper::del('environtment'); |
||
137 | } |
||
138 | |||
139 | return self::$env_status; |
||
140 | } |
||
141 | |||
142 | public function is_req($any, $alternative = null) |
||
143 | { |
||
144 | if (isset($_REQUEST[(string) $any])) { |
||
145 | return $_REQUEST[(string) $any]; |
||
146 | } |
||
147 | |||
148 | return $alternative; |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * check if one of the headers exists. |
||
153 | * |
||
154 | * ```php |
||
155 | * if ($this->is_reqs(['DNT', 'Connection'])) |
||
156 | * ``` |
||
157 | * |
||
158 | * @param array $anys |
||
159 | * |
||
160 | * @return bool|string |
||
161 | */ |
||
162 | public function is_reqs(array $anys) |
||
163 | { |
||
164 | foreach ($anys as $any) { |
||
165 | if (isset($_REQUEST[(string) $any])) { |
||
166 | return $_REQUEST[(string) $any]; |
||
167 | } |
||
168 | } |
||
169 | } |
||
170 | |||
171 | public function is_post($any, $alternative = null) |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * Check if header request has $any. |
||
182 | * |
||
183 | * @param string $any |
||
184 | * |
||
185 | * @return string|null |
||
186 | */ |
||
187 | public function is_header(string $any) |
||
192 | } |
||
193 | |||
194 | /** |
||
195 | * Check browser no-cache request (hard reload) |
||
196 | * |
||
197 | * @return boolean |
||
198 | */ |
||
199 | public function is_hard_reload() |
||
202 | } |
||
203 | |||
204 | /** |
||
205 | * Find router from parameter URL. |
||
206 | */ |
||
207 | public function findRoute() |
||
236 | } |
||
237 | |||
238 | public function config($router) |
||
239 | { |
||
240 | return helper::platformSlashes($this->view . '/' . $router); |
||
241 | } |
||
242 | |||
243 | public function GUID() |
||
250 | } |
||
251 | } |
||
252 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.