@@ -4,26 +4,26 @@ |
||
4 | 4 | |
5 | 5 | use Exception; |
6 | 6 | |
7 | -trait CheckTrait{ |
|
7 | +trait CheckTrait { |
|
8 | 8 | |
9 | 9 | protected function check_protocol(string $expected, string $current) |
10 | 10 | { |
11 | - if($expected !== $current){ |
|
12 | - throw new Exception('Page not found.',404); |
|
11 | + if ($expected !== $current) { |
|
12 | + throw new Exception('Page not found.', 404); |
|
13 | 13 | } |
14 | 14 | } |
15 | 15 | |
16 | - protected function check_name(string $route_name){ |
|
17 | - if(!array_key_exists($route_name,$this->routers)){ |
|
18 | - throw new Exception('Page not found.',404); |
|
16 | + protected function check_name(string $route_name) { |
|
17 | + if (!array_key_exists($route_name, $this->routers)) { |
|
18 | + throw new Exception('Page not found.', 404); |
|
19 | 19 | } |
20 | 20 | } |
21 | 21 | |
22 | 22 | protected function check_filtering(array $route) |
23 | 23 | { |
24 | - $filters = (is_array($route['filters'])) ? $route['filters'] : [ $route['filters'] ]; |
|
24 | + $filters = (is_array($route['filters'])) ? $route['filters'] : [$route['filters']]; |
|
25 | 25 | |
26 | - foreach($filters as $filter){ |
|
26 | + foreach ($filters as $filter) { |
|
27 | 27 | $this->filter->filtering($filter); |
28 | 28 | } |
29 | 29 | } |
@@ -6,7 +6,7 @@ discard block |
||
6 | 6 | |
7 | 7 | use Exception; |
8 | 8 | |
9 | -class Router{ |
|
9 | +class Router { |
|
10 | 10 | use Helper, CheckTrait; |
11 | 11 | |
12 | 12 | private static $instance = null; |
@@ -25,7 +25,7 @@ discard block |
||
25 | 25 | |
26 | 26 | public static function create(): Router |
27 | 27 | { |
28 | - if(!defined('ROUTER_CONFIG')){ |
|
28 | + if (!defined('ROUTER_CONFIG')) { |
|
29 | 29 | throw new Exception("Information for loading routes has not been defined."); |
30 | 30 | } |
31 | 31 | |
@@ -35,7 +35,7 @@ discard block |
||
35 | 35 | |
36 | 36 | public static function getInstance(): Router |
37 | 37 | { |
38 | - if(is_null(self::$instance)){ |
|
38 | + if (is_null(self::$instance)) { |
|
39 | 39 | self::$instance = new self(); |
40 | 40 | } |
41 | 41 | return self::$instance; |
@@ -44,8 +44,8 @@ discard block |
||
44 | 44 | private static function import(string $path): Router |
45 | 45 | { |
46 | 46 | foreach (scandir($path) as $routeFile) { |
47 | - if(pathinfo($path.DIRECTORY_SEPARATOR.$routeFile, PATHINFO_EXTENSION) === 'php'){ |
|
48 | - require_once($path. DIRECTORY_SEPARATOR .$routeFile); |
|
47 | + if (pathinfo($path.DIRECTORY_SEPARATOR.$routeFile, PATHINFO_EXTENSION) === 'php') { |
|
48 | + require_once($path.DIRECTORY_SEPARATOR.$routeFile); |
|
49 | 49 | } |
50 | 50 | } |
51 | 51 | |
@@ -77,12 +77,12 @@ discard block |
||
77 | 77 | return self::getInstance()->set($uri, $controll, $protocol); |
78 | 78 | } |
79 | 79 | |
80 | - public function set($url , $role , $protocol = null): Router |
|
80 | + public function set($url, $role, $protocol = null): Router |
|
81 | 81 | { |
82 | - $url = (substr($url,0,1) !=='/' and strlen($url) > 0) ? "/{$url}" : $url; |
|
82 | + $url = (substr($url, 0, 1) !== '/' and strlen($url) > 0) ? "/{$url}" : $url; |
|
83 | 83 | |
84 | - foreach($this->routers as $key => $value){ |
|
85 | - if( md5($this->prefix . $value['url'] . $value['protocol'] ) === md5( $url . $protocol ) ){ |
|
84 | + foreach ($this->routers as $key => $value) { |
|
85 | + if (md5($this->prefix.$value['url'].$value['protocol']) === md5($url.$protocol)) { |
|
86 | 86 | throw new Exception("There is already a route with the url {$url} and with the {$protocol} protocol configured."); |
87 | 87 | } |
88 | 88 | } |
@@ -100,9 +100,9 @@ discard block |
||
100 | 100 | return self::getInstance(); |
101 | 101 | } |
102 | 102 | |
103 | - public static function group(string $prefix,$callback): Router |
|
103 | + public static function group(string $prefix, $callback): Router |
|
104 | 104 | { |
105 | - self::getInstance()->prefix = (substr($prefix,0,1) !== '/') ? "/{$prefix}" : $prefix; |
|
105 | + self::getInstance()->prefix = (substr($prefix, 0, 1) !== '/') ? "/{$prefix}" : $prefix; |
|
106 | 106 | self::getInstance()->group = sha1(date('d/m/Y h:m:i')); |
107 | 107 | $callback(); |
108 | 108 | self::getInstance()->group = null; |
@@ -114,14 +114,14 @@ discard block |
||
114 | 114 | public static function name(string $name): Router |
115 | 115 | { |
116 | 116 | |
117 | - if(self::getInstance()->lastReturn){ |
|
117 | + if (self::getInstance()->lastReturn) { |
|
118 | 118 | throw new Exception("There is no reason to call a {$name} route group."); |
119 | 119 | } |
120 | 120 | |
121 | 121 | $currentRoute = end(self::getInstance()->routers); |
122 | 122 | |
123 | - foreach(self::getInstance()->routers as $key => $value){ |
|
124 | - if(array_key_exists($name, self::getInstance()->routers)){ |
|
123 | + foreach (self::getInstance()->routers as $key => $value) { |
|
124 | + if (array_key_exists($name, self::getInstance()->routers)) { |
|
125 | 125 | throw new Exception("There is already a route with the name {$name} configured."); |
126 | 126 | } |
127 | 127 | } |
@@ -153,28 +153,28 @@ discard block |
||
153 | 153 | |
154 | 154 | public function dispatch(?string $route_name = null): bool |
155 | 155 | { |
156 | - if(!is_null($route_name)){ |
|
156 | + if (!is_null($route_name)) { |
|
157 | 157 | return $this->byName($route_name); |
158 | 158 | } |
159 | 159 | |
160 | 160 | $currentProtocol = $this->getProtocol(); |
161 | 161 | |
162 | - foreach(array_reverse($this->routers) as $r => $route){ |
|
163 | - if(is_array($route['protocol'])){ |
|
164 | - foreach($route['protocol'] as $protocol){ |
|
165 | - if($protocol !== $currentProtocol){ |
|
162 | + foreach (array_reverse($this->routers) as $r => $route) { |
|
163 | + if (is_array($route['protocol'])) { |
|
164 | + foreach ($route['protocol'] as $protocol) { |
|
165 | + if ($protocol !== $currentProtocol) { |
|
166 | 166 | continue; |
167 | 167 | } |
168 | 168 | } |
169 | - }else{ |
|
170 | - if($route['protocol'] !== $currentProtocol){ |
|
169 | + }else { |
|
170 | + if ($route['protocol'] !== $currentProtocol) { |
|
171 | 171 | continue; |
172 | 172 | } |
173 | 173 | } |
174 | 174 | |
175 | 175 | $route_loop = explode( |
176 | 176 | '/', |
177 | - (substr($route['url'],strlen($route['url'])-1,1) === '/') |
|
177 | + (substr($route['url'], strlen($route['url'])-1, 1) === '/') |
|
178 | 178 | ? substr($route['url'], 0, -1) |
179 | 179 | : $route['url'] |
180 | 180 | ); |
@@ -188,29 +188,29 @@ discard block |
||
188 | 188 | : $_SERVER['REQUEST_URI'] |
189 | 189 | );*/ |
190 | 190 | |
191 | - if(count($route_loop) !== count($route_request)){ |
|
191 | + if (count($route_loop) !== count($route_request)) { |
|
192 | 192 | continue; |
193 | 193 | } |
194 | 194 | |
195 | - for($rr = 0; $rr < count($route_loop); $rr++){ |
|
196 | - $param = (substr($route_loop[$rr],0,1)==='{'); |
|
195 | + for ($rr = 0; $rr < count($route_loop); $rr++) { |
|
196 | + $param = (substr($route_loop[$rr], 0, 1) === '{'); |
|
197 | 197 | |
198 | - if($param){ |
|
199 | - $param_name = substr($route_loop[$rr],1,strlen($route_loop[$rr])-2); |
|
198 | + if ($param) { |
|
199 | + $param_name = substr($route_loop[$rr], 1, strlen($route_loop[$rr])-2); |
|
200 | 200 | $data[$param_name] = $route_request[$rr]; |
201 | 201 | } |
202 | 202 | |
203 | - if(!$param and $route_loop[$rr] !== $route_request[$rr]){ |
|
203 | + if (!$param and $route_loop[$rr] !== $route_request[$rr]) { |
|
204 | 204 | continue 2; |
205 | 205 | } |
206 | 206 | } |
207 | 207 | |
208 | - if(!empty($route['filters'])){ |
|
209 | - if(is_array($route['filters'])){ |
|
210 | - foreach($route['filters'] as $filter){ |
|
208 | + if (!empty($route['filters'])) { |
|
209 | + if (is_array($route['filters'])) { |
|
210 | + foreach ($route['filters'] as $filter) { |
|
211 | 211 | $this->filter->filtering($filter); |
212 | 212 | } |
213 | - }else{ |
|
213 | + }else { |
|
214 | 214 | $this->filter->filtering($route['filters']); |
215 | 215 | } |
216 | 216 | } |
@@ -219,25 +219,25 @@ discard block |
||
219 | 219 | return true; |
220 | 220 | } |
221 | 221 | |
222 | - throw new Exception('Page not found.',404); |
|
222 | + throw new Exception('Page not found.', 404); |
|
223 | 223 | } |
224 | 224 | |
225 | 225 | public static function filter($filters): Router |
226 | 226 | { |
227 | - if(self::getInstance()->lastReturn !== null){ |
|
227 | + if (self::getInstance()->lastReturn !== null) { |
|
228 | 228 | $currentGroup = end(self::getInstance()->routers)['group']; |
229 | 229 | |
230 | 230 | foreach (self::getInstance()->routers as $key => $value) { |
231 | 231 | |
232 | - if($value['group'] === $currentGroup){ |
|
233 | - $currentRoute = self::getInstance()->addFilter(self::getInstance()->routers[$key],$filters); |
|
232 | + if ($value['group'] === $currentGroup) { |
|
233 | + $currentRoute = self::getInstance()->addFilter(self::getInstance()->routers[$key], $filters); |
|
234 | 234 | self::getInstance()->routers[$key] = $currentRoute; |
235 | 235 | } |
236 | 236 | |
237 | 237 | } |
238 | 238 | |
239 | - }else{ |
|
240 | - self::getInstance()->routers[count(self::getInstance()->routers)-1] = self::getInstance()->addFilter(end(self::getInstance()->routers),$filters); |
|
239 | + }else { |
|
240 | + self::getInstance()->routers[count(self::getInstance()->routers)-1] = self::getInstance()->addFilter(end(self::getInstance()->routers), $filters); |
|
241 | 241 | } |
242 | 242 | |
243 | 243 | return self::getInstance(); |
@@ -245,18 +245,18 @@ discard block |
||
245 | 245 | |
246 | 246 | public static function addFilter(array $route, $filter): array |
247 | 247 | { |
248 | - if(is_null($route['filters'])){ |
|
248 | + if (is_null($route['filters'])) { |
|
249 | 249 | $route['filters'] = $filter; |
250 | 250 | return $route; |
251 | 251 | } |
252 | 252 | |
253 | 253 | $filters = (is_array($filter)) ? $filter : [0 => $filter]; |
254 | 254 | |
255 | - if(is_array($route['filters'])){ |
|
255 | + if (is_array($route['filters'])) { |
|
256 | 256 | foreach ($route['filters'] as $key => $value) { |
257 | 257 | $filters[] = $value; |
258 | 258 | } |
259 | - }else{ |
|
259 | + }else { |
|
260 | 260 | $filters[] = $route['filters']; |
261 | 261 | } |
262 | 262 | |
@@ -269,37 +269,37 @@ discard block |
||
269 | 269 | $data = $this->getData(); |
270 | 270 | |
271 | 271 | foreach ($data['GET'] as $name => $value) { |
272 | - $controll = str_replace('{'.$name.'}',$value,$controll); |
|
272 | + $controll = str_replace('{'.$name.'}', $value, $controll); |
|
273 | 273 | } |
274 | 274 | |
275 | - $d = explode(':',$controll); |
|
275 | + $d = explode(':', $controll); |
|
276 | 276 | |
277 | - if(count($d) != 2){ |
|
277 | + if (count($d) != 2) { |
|
278 | 278 | throw new Exception("Controller {$controll} badly set."); |
279 | 279 | } |
280 | 280 | |
281 | - if(!class_exists('Controllers\\' . ucfirst($d[0]))){ |
|
281 | + if (!class_exists('Controllers\\'.ucfirst($d[0]))) { |
|
282 | 282 | throw new Exception("No controller {$d[0]} found."); |
283 | 283 | } |
284 | 284 | |
285 | - if(!method_exists('Controllers\\' . ucfirst($d[0]), $d[1])){ |
|
285 | + if (!method_exists('Controllers\\'.ucfirst($d[0]), $d[1])) { |
|
286 | 286 | throw new Exception("No method {$d[1]} found for {$d[0]}."); |
287 | 287 | } |
288 | 288 | |
289 | - $controller = 'Controllers\\' . ucfirst($d[0]); |
|
289 | + $controller = 'Controllers\\'.ucfirst($d[0]); |
|
290 | 290 | $controller = new $controller(); |
291 | 291 | $method = $d[1]; |
292 | 292 | |
293 | - if( ( $this->getProtocol() == 'form') ){ |
|
293 | + if (($this->getProtocol() == 'form')) { |
|
294 | 294 | $this->ControllerForm($controller, $method, $data['POST']); |
295 | 295 | }else { |
296 | 296 | $controller->$method($data); |
297 | 297 | } |
298 | 298 | } |
299 | 299 | |
300 | - public function ControllerForm($controller, string $method, array $values){ |
|
301 | - if(Validator::execute($values)){ |
|
302 | - if(!array_key_exists('role',$this->getData()['POST'])){ |
|
300 | + public function ControllerForm($controller, string $method, array $values) { |
|
301 | + if (Validator::execute($values)) { |
|
302 | + if (!array_key_exists('role', $this->getData()['POST'])) { |
|
303 | 303 | throw new Exception('O servidor não conseguiu identificar a finalidade deste formulário.'); |
304 | 304 | } |
305 | 305 |
@@ -166,7 +166,7 @@ discard block |
||
166 | 166 | continue; |
167 | 167 | } |
168 | 168 | } |
169 | - }else{ |
|
169 | + } else{ |
|
170 | 170 | if($route['protocol'] !== $currentProtocol){ |
171 | 171 | continue; |
172 | 172 | } |
@@ -210,7 +210,7 @@ discard block |
||
210 | 210 | foreach($route['filters'] as $filter){ |
211 | 211 | $this->filter->filtering($filter); |
212 | 212 | } |
213 | - }else{ |
|
213 | + } else{ |
|
214 | 214 | $this->filter->filtering($route['filters']); |
215 | 215 | } |
216 | 216 | } |
@@ -236,7 +236,7 @@ discard block |
||
236 | 236 | |
237 | 237 | } |
238 | 238 | |
239 | - }else{ |
|
239 | + } else{ |
|
240 | 240 | self::getInstance()->routers[count(self::getInstance()->routers)-1] = self::getInstance()->addFilter(end(self::getInstance()->routers),$filters); |
241 | 241 | } |
242 | 242 | |
@@ -256,7 +256,7 @@ discard block |
||
256 | 256 | foreach ($route['filters'] as $key => $value) { |
257 | 257 | $filters[] = $value; |
258 | 258 | } |
259 | - }else{ |
|
259 | + } else{ |
|
260 | 260 | $filters[] = $route['filters']; |
261 | 261 | } |
262 | 262 | |
@@ -292,7 +292,7 @@ discard block |
||
292 | 292 | |
293 | 293 | if( ( $this->getProtocol() == 'form') ){ |
294 | 294 | $this->ControllerForm($controller, $method, $data['POST']); |
295 | - }else { |
|
295 | + } else { |
|
296 | 296 | $controller->$method($data); |
297 | 297 | } |
298 | 298 | } |