Total Complexity | 66 |
Total Lines | 321 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 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 |
||
9 | class Router{ |
||
10 | use Helper; |
||
11 | |||
12 | private static $instance = null; |
||
13 | private array $routers = []; |
||
14 | private ?string $prefix = null; |
||
15 | private $protocol = null; |
||
|
|||
16 | private $filtersSet = null; |
||
17 | private $filter = null; |
||
18 | private $group = false; |
||
19 | private $lastReturn = null; |
||
20 | |||
21 | public function __construct() |
||
22 | { |
||
23 | return $this; |
||
24 | } |
||
25 | |||
26 | public static function create(): Router |
||
27 | { |
||
28 | if(!defined('ROUTER_CONFIG')){ |
||
29 | throw new Exception("Information for loading routes has not been defined."); |
||
30 | } |
||
31 | |||
32 | self::import(ROUTER_CONFIG['path']); |
||
33 | return self::getInstance(); |
||
34 | } |
||
35 | |||
36 | public static function getInstance(): Router |
||
37 | { |
||
38 | if(is_null(self::$instance)){ |
||
39 | self::$instance = new self(); |
||
40 | } |
||
41 | return self::$instance; |
||
42 | } |
||
43 | |||
44 | private static function import(string $path): Router |
||
45 | { |
||
46 | foreach (scandir($path) as $routeFile) { |
||
47 | if(pathinfo($path.DIRECTORY_SEPARATOR.$routeFile, PATHINFO_EXTENSION) === 'php'){ |
||
48 | require_once($path. DIRECTORY_SEPARATOR .$routeFile); |
||
49 | } |
||
50 | } |
||
51 | |||
52 | return self::getInstance(); |
||
53 | } |
||
54 | |||
55 | public static function form(string $uri, string $controll): Router |
||
56 | { |
||
57 | return self::getInstance()->add($uri, $controll, 'form'); |
||
58 | } |
||
59 | |||
60 | public static function get(string $uri, string $controll): Router |
||
61 | { |
||
62 | return self::getInstance()->add($uri, $controll, 'get'); |
||
63 | } |
||
64 | |||
65 | public static function post(string $uri, string $controll): Router |
||
66 | { |
||
67 | return self::getInstance()->add($uri, $controll, 'post'); |
||
68 | } |
||
69 | |||
70 | public static function ajax(string $uri, string $controll): Router |
||
71 | { |
||
72 | return self::getInstance()->add($uri, $controll, 'ajax'); |
||
73 | } |
||
74 | |||
75 | public static function add(string $uri, string $controll, string $protocol): Router |
||
76 | { |
||
77 | return self::getInstance()->set($uri, $controll, $protocol); |
||
78 | } |
||
79 | |||
80 | public function set($url , $role , $protocol = null): Router |
||
101 | } |
||
102 | |||
103 | public static function group(string $prefix,$callback): Router |
||
104 | { |
||
105 | self::getInstance()->prefix = (substr($prefix,0,1) !== '/') ? "/{$prefix}" : $prefix; |
||
106 | self::getInstance()->group = sha1(date('d/m/Y h:m:i')); |
||
107 | $callback(); |
||
108 | self::getInstance()->group = null; |
||
109 | self::getInstance()->prefix = null; |
||
110 | self::getInstance()->lastReturn = true; |
||
111 | return self::getInstance(); |
||
112 | } |
||
113 | |||
114 | public static function name(string $name): Router |
||
136 | } |
||
137 | |||
138 | public function byName(string $route_name) |
||
139 | { |
||
140 | $currentProtocol = $this->getProtocol(); |
||
141 | |||
142 | if(!array_key_exists($route_name,$this->routers)){ |
||
143 | throw new Exception('Page not found.'.$route_name,404); |
||
144 | } |
||
145 | |||
146 | $route = $this->routers[$route_name]; |
||
147 | |||
148 | if($route['protocol']!==$currentProtocol){ |
||
149 | throw new Exception('Page not found.'.$route_name,404); |
||
150 | } |
||
151 | |||
152 | if(!empty($route['filters'])){ |
||
153 | if(is_array($route['filters'])){ |
||
154 | foreach($route['filters'] as $filter){ |
||
155 | $this->filter->filtering($filter); |
||
156 | } |
||
157 | }else{ |
||
158 | $this->filter->filtering($route['filters']); |
||
159 | } |
||
160 | } |
||
161 | |||
162 | $this->Controller($route['role']); |
||
163 | return true; |
||
164 | } |
||
165 | |||
166 | public function dispatch(?string $route_name = null): bool |
||
167 | { |
||
168 | if(!is_null($route_name)){ |
||
169 | return $this->byName($route_name); |
||
170 | } |
||
171 | |||
172 | $currentProtocol = $this->getProtocol(); |
||
173 | |||
174 | foreach(array_reverse($this->routers) as $r => $route){ |
||
175 | if(is_array($route['protocol'])){ |
||
176 | foreach($route['protocol'] as $protocol){ |
||
177 | if($protocol !== $currentProtocol){ |
||
178 | continue; |
||
179 | } |
||
180 | } |
||
181 | }else{ |
||
182 | if($route['protocol'] !== $currentProtocol){ |
||
183 | continue; |
||
184 | } |
||
185 | } |
||
186 | |||
187 | $route_loop = explode( |
||
188 | '/', |
||
189 | (substr($route['url'],strlen($route['url'])-1,1) === '/') |
||
190 | ? substr($route['url'], 0, -1) |
||
191 | : $route['url'] |
||
192 | ); |
||
193 | |||
194 | /* ONLY FOR DEBUG CONDITION */ |
||
195 | $route_request = $route_loop; |
||
196 | /*$route_request = explode( |
||
197 | '/', |
||
198 | (substr($_SERVER['REQUEST_URI'],strlen($_SERVER['REQUEST_URI'])-1,1) === '/') |
||
199 | ? substr($_SERVER['REQUEST_URI'], 0, -1) |
||
200 | : $_SERVER['REQUEST_URI'] |
||
201 | );*/ |
||
202 | |||
203 | if(count($route_loop) !== count($route_request)){ |
||
204 | continue; |
||
205 | } |
||
206 | |||
207 | for($rr = 0; $rr < count($route_loop); $rr++){ |
||
208 | $param = (substr($route_loop[$rr],0,1)==='{'); |
||
209 | |||
210 | if($param){ |
||
211 | $param_name = substr($route_loop[$rr],1,strlen($route_loop[$rr])-2); |
||
212 | $data[$param_name] = $route_request[$rr]; |
||
213 | } |
||
214 | |||
215 | if(!$param and $route_loop[$rr] !== $route_request[$rr]){ |
||
216 | continue 2; |
||
217 | } |
||
218 | } |
||
219 | |||
220 | if(!empty($route['filters'])){ |
||
221 | if(is_array($route['filters'])){ |
||
222 | foreach($route['filters'] as $filter){ |
||
223 | $this->filter->filtering($filter); |
||
224 | } |
||
225 | }else{ |
||
226 | $this->filter->filtering($route['filters']); |
||
227 | } |
||
228 | } |
||
229 | |||
230 | $this->Controller($route['role']); |
||
231 | return true; |
||
232 | } |
||
233 | |||
234 | throw new Exception('Page not found.',404); |
||
235 | } |
||
236 | |||
237 | public static function filter($filters): Router |
||
238 | { |
||
239 | if(self::getInstance()->lastReturn !== null){ |
||
240 | $currentGroup = end(self::getInstance()->routers)['group']; |
||
241 | |||
242 | foreach (self::getInstance()->routers as $key => $value) { |
||
243 | if($value['group'] === $currentGroup){ |
||
244 | $currentRoute = self::getInstance()->addFilter(self::getInstance()->routers[$key],$filters); |
||
245 | self::getInstance()->routers[$key] = $currentRoute; |
||
246 | } |
||
247 | } |
||
248 | |||
249 | }else{ |
||
250 | $currentRoute = self::getInstance()->addFilter(end(self::getInstance()->routers),$filters); |
||
251 | self::getInstance()->routers[count(self::getInstance()->routers)-1] = $currentRoute; |
||
252 | } |
||
253 | |||
254 | return self::getInstance(); |
||
255 | } |
||
256 | |||
257 | public static function addFilter(array $route, $filter): array |
||
258 | { |
||
259 | if(is_null($route['filters'])){ |
||
260 | $route['filters'] = $filter; |
||
261 | return $route; |
||
262 | } |
||
263 | |||
264 | $filters = (is_array($filter)) ? $filter : [0 => $filter]; |
||
265 | |||
266 | if(is_array($route['filters'])){ |
||
267 | foreach ($route['filters'] as $key => $value) { |
||
268 | $filters[] = $value; |
||
269 | } |
||
270 | }else{ |
||
271 | $filters[] = $route['filters']; |
||
272 | } |
||
273 | |||
274 | $route['filters'] = $filters; |
||
275 | return $route; |
||
276 | } |
||
277 | |||
278 | |||
279 | |||
280 | |||
281 | |||
282 | |||
283 | |||
284 | |||
285 | |||
286 | public function Controller(string $controll): void |
||
318 | } |
||
319 | } |
||
320 | |||
321 | public function ControllerForm($controller, string $method, array $values){ |
||
322 | if(Validator::execute($values)){ |
||
334 |