@@ -79,23 +79,23 @@ discard block |
||
79 | 79 | |
80 | 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 | + } |
|
89 | 89 | |
90 | - $route = [ |
|
91 | - 'url' => $this->prefix.$url, |
|
92 | - 'role' => $role, |
|
93 | - 'protocol' => $protocol, |
|
94 | - 'filters' => null, |
|
90 | + $route = [ |
|
91 | + 'url' => $this->prefix.$url, |
|
92 | + 'role' => $role, |
|
93 | + 'protocol' => $protocol, |
|
94 | + 'filters' => null, |
|
95 | 95 | 'group' => self::getInstance()->group |
96 | - ]; |
|
96 | + ]; |
|
97 | 97 | |
98 | - $this->routers[] = $route; |
|
98 | + $this->routers[] = $route; |
|
99 | 99 | |
100 | 100 | return self::getInstance(); |
101 | 101 | } |
@@ -169,7 +169,7 @@ discard block |
||
169 | 169 | return $this->byName($route_name); |
170 | 170 | } |
171 | 171 | |
172 | - $currentProtocol = $this->getProtocol(); |
|
172 | + $currentProtocol = $this->getProtocol(); |
|
173 | 173 | |
174 | 174 | foreach(array_reverse($this->routers) as $r => $route){ |
175 | 175 | if(is_array($route['protocol'])){ |
@@ -178,13 +178,13 @@ discard block |
||
178 | 178 | continue; |
179 | 179 | } |
180 | 180 | } |
181 | - }else{ |
|
182 | - if($route['protocol'] !== $currentProtocol){ |
|
181 | + }else{ |
|
182 | + if($route['protocol'] !== $currentProtocol){ |
|
183 | 183 | continue; |
184 | 184 | } |
185 | - } |
|
185 | + } |
|
186 | 186 | |
187 | - $route_loop = explode( |
|
187 | + $route_loop = explode( |
|
188 | 188 | '/', |
189 | 189 | (substr($route['url'],strlen($route['url'])-1,1) === '/') |
190 | 190 | ? substr($route['url'], 0, -1) |
@@ -193,45 +193,45 @@ discard block |
||
193 | 193 | |
194 | 194 | /* ONLY FOR DEBUG CONDITION */ |
195 | 195 | $route_request = $route_loop; |
196 | - /*$route_request = explode( |
|
196 | + /*$route_request = explode( |
|
197 | 197 | '/', |
198 | 198 | (substr($_SERVER['REQUEST_URI'],strlen($_SERVER['REQUEST_URI'])-1,1) === '/') |
199 | 199 | ? substr($_SERVER['REQUEST_URI'], 0, -1) |
200 | 200 | : $_SERVER['REQUEST_URI'] |
201 | 201 | );*/ |
202 | 202 | |
203 | - if(count($route_loop) !== count($route_request)){ |
|
203 | + if(count($route_loop) !== count($route_request)){ |
|
204 | 204 | continue; |
205 | 205 | } |
206 | 206 | |
207 | - for($rr = 0; $rr < count($route_loop); $rr++){ |
|
208 | - $param = (substr($route_loop[$rr],0,1)==='{'); |
|
207 | + for($rr = 0; $rr < count($route_loop); $rr++){ |
|
208 | + $param = (substr($route_loop[$rr],0,1)==='{'); |
|
209 | 209 | |
210 | - if($param){ |
|
210 | + if($param){ |
|
211 | 211 | $param_name = substr($route_loop[$rr],1,strlen($route_loop[$rr])-2); |
212 | - $data[$param_name] = $route_request[$rr]; |
|
213 | - } |
|
212 | + $data[$param_name] = $route_request[$rr]; |
|
213 | + } |
|
214 | 214 | |
215 | - if(!$param and $route_loop[$rr] !== $route_request[$rr]){ |
|
215 | + if(!$param and $route_loop[$rr] !== $route_request[$rr]){ |
|
216 | 216 | continue 2; |
217 | 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 | - } |
|
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 | 229 | |
230 | 230 | $this->Controller($route['role']); |
231 | - return true; |
|
232 | - } |
|
231 | + return true; |
|
232 | + } |
|
233 | 233 | |
234 | - throw new Exception('Page not found.',404); |
|
234 | + throw new Exception('Page not found.',404); |
|
235 | 235 | } |
236 | 236 | |
237 | 237 | public static function filter($filters): Router |
@@ -319,7 +319,7 @@ discard block |
||
319 | 319 | } |
320 | 320 | |
321 | 321 | public function ControllerForm($controller, string $method, array $values){ |
322 | - if(Validator::execute($values)){ |
|
322 | + if(Validator::execute($values)){ |
|
323 | 323 | if(!array_key_exists('role',$this->getData()['POST'])){ |
324 | 324 | throw new Exception('O servidor não conseguiu identificar a finalidade deste formulário.'); |
325 | 325 | } |
@@ -6,7 +6,7 @@ discard block |
||
6 | 6 | |
7 | 7 | use Exception; |
8 | 8 | |
9 | -class Router{ |
|
9 | +class Router { |
|
10 | 10 | use Helper; |
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 | } |
@@ -139,22 +139,22 @@ discard block |
||
139 | 139 | { |
140 | 140 | $currentProtocol = $this->getProtocol(); |
141 | 141 | |
142 | - if(!array_key_exists($route_name,$this->routers)){ |
|
143 | - throw new Exception('Page not found.'.$route_name,404); |
|
142 | + if (!array_key_exists($route_name, $this->routers)) { |
|
143 | + throw new Exception('Page not found.'.$route_name, 404); |
|
144 | 144 | } |
145 | 145 | |
146 | 146 | $route = $this->routers[$route_name]; |
147 | 147 | |
148 | - if($route['protocol']!==$currentProtocol){ |
|
149 | - throw new Exception('Page not found.'.$route_name,404); |
|
148 | + if ($route['protocol'] !== $currentProtocol) { |
|
149 | + throw new Exception('Page not found.'.$route_name, 404); |
|
150 | 150 | } |
151 | 151 | |
152 | - if(!empty($route['filters'])){ |
|
153 | - if(is_array($route['filters'])){ |
|
154 | - foreach($route['filters'] as $filter){ |
|
152 | + if (!empty($route['filters'])) { |
|
153 | + if (is_array($route['filters'])) { |
|
154 | + foreach ($route['filters'] as $filter) { |
|
155 | 155 | $this->filter->filtering($filter); |
156 | 156 | } |
157 | - }else{ |
|
157 | + }else { |
|
158 | 158 | $this->filter->filtering($route['filters']); |
159 | 159 | } |
160 | 160 | } |
@@ -165,28 +165,28 @@ discard block |
||
165 | 165 | |
166 | 166 | public function dispatch(?string $route_name = null): bool |
167 | 167 | { |
168 | - if(!is_null($route_name)){ |
|
168 | + if (!is_null($route_name)) { |
|
169 | 169 | return $this->byName($route_name); |
170 | 170 | } |
171 | 171 | |
172 | 172 | $currentProtocol = $this->getProtocol(); |
173 | 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){ |
|
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 | 178 | continue; |
179 | 179 | } |
180 | 180 | } |
181 | - }else{ |
|
182 | - if($route['protocol'] !== $currentProtocol){ |
|
181 | + }else { |
|
182 | + if ($route['protocol'] !== $currentProtocol) { |
|
183 | 183 | continue; |
184 | 184 | } |
185 | 185 | } |
186 | 186 | |
187 | 187 | $route_loop = explode( |
188 | 188 | '/', |
189 | - (substr($route['url'],strlen($route['url'])-1,1) === '/') |
|
189 | + (substr($route['url'], strlen($route['url'])-1, 1) === '/') |
|
190 | 190 | ? substr($route['url'], 0, -1) |
191 | 191 | : $route['url'] |
192 | 192 | ); |
@@ -200,29 +200,29 @@ discard block |
||
200 | 200 | : $_SERVER['REQUEST_URI'] |
201 | 201 | );*/ |
202 | 202 | |
203 | - if(count($route_loop) !== count($route_request)){ |
|
203 | + if (count($route_loop) !== count($route_request)) { |
|
204 | 204 | continue; |
205 | 205 | } |
206 | 206 | |
207 | - for($rr = 0; $rr < count($route_loop); $rr++){ |
|
208 | - $param = (substr($route_loop[$rr],0,1)==='{'); |
|
207 | + for ($rr = 0; $rr < count($route_loop); $rr++) { |
|
208 | + $param = (substr($route_loop[$rr], 0, 1) === '{'); |
|
209 | 209 | |
210 | - if($param){ |
|
211 | - $param_name = substr($route_loop[$rr],1,strlen($route_loop[$rr])-2); |
|
210 | + if ($param) { |
|
211 | + $param_name = substr($route_loop[$rr], 1, strlen($route_loop[$rr])-2); |
|
212 | 212 | $data[$param_name] = $route_request[$rr]; |
213 | 213 | } |
214 | 214 | |
215 | - if(!$param and $route_loop[$rr] !== $route_request[$rr]){ |
|
215 | + if (!$param and $route_loop[$rr] !== $route_request[$rr]) { |
|
216 | 216 | continue 2; |
217 | 217 | } |
218 | 218 | } |
219 | 219 | |
220 | - if(!empty($route['filters'])){ |
|
221 | - if(is_array($route['filters'])){ |
|
222 | - foreach($route['filters'] as $filter){ |
|
220 | + if (!empty($route['filters'])) { |
|
221 | + if (is_array($route['filters'])) { |
|
222 | + foreach ($route['filters'] as $filter) { |
|
223 | 223 | $this->filter->filtering($filter); |
224 | 224 | } |
225 | - }else{ |
|
225 | + }else { |
|
226 | 226 | $this->filter->filtering($route['filters']); |
227 | 227 | } |
228 | 228 | } |
@@ -231,23 +231,23 @@ discard block |
||
231 | 231 | return true; |
232 | 232 | } |
233 | 233 | |
234 | - throw new Exception('Page not found.',404); |
|
234 | + throw new Exception('Page not found.', 404); |
|
235 | 235 | } |
236 | 236 | |
237 | 237 | public static function filter($filters): Router |
238 | 238 | { |
239 | - if(self::getInstance()->lastReturn !== null){ |
|
239 | + if (self::getInstance()->lastReturn !== null) { |
|
240 | 240 | $currentGroup = end(self::getInstance()->routers)['group']; |
241 | 241 | |
242 | 242 | foreach (self::getInstance()->routers as $key => $value) { |
243 | - if($value['group'] === $currentGroup){ |
|
244 | - $currentRoute = self::getInstance()->addFilter(self::getInstance()->routers[$key],$filters); |
|
243 | + if ($value['group'] === $currentGroup) { |
|
244 | + $currentRoute = self::getInstance()->addFilter(self::getInstance()->routers[$key], $filters); |
|
245 | 245 | self::getInstance()->routers[$key] = $currentRoute; |
246 | 246 | } |
247 | 247 | } |
248 | 248 | |
249 | - }else{ |
|
250 | - $currentRoute = self::getInstance()->addFilter(end(self::getInstance()->routers),$filters); |
|
249 | + }else { |
|
250 | + $currentRoute = self::getInstance()->addFilter(end(self::getInstance()->routers), $filters); |
|
251 | 251 | self::getInstance()->routers[count(self::getInstance()->routers)-1] = $currentRoute; |
252 | 252 | } |
253 | 253 | |
@@ -256,18 +256,18 @@ discard block |
||
256 | 256 | |
257 | 257 | public static function addFilter(array $route, $filter): array |
258 | 258 | { |
259 | - if(is_null($route['filters'])){ |
|
259 | + if (is_null($route['filters'])) { |
|
260 | 260 | $route['filters'] = $filter; |
261 | 261 | return $route; |
262 | 262 | } |
263 | 263 | |
264 | 264 | $filters = (is_array($filter)) ? $filter : [0 => $filter]; |
265 | 265 | |
266 | - if(is_array($route['filters'])){ |
|
266 | + if (is_array($route['filters'])) { |
|
267 | 267 | foreach ($route['filters'] as $key => $value) { |
268 | 268 | $filters[] = $value; |
269 | 269 | } |
270 | - }else{ |
|
270 | + }else { |
|
271 | 271 | $filters[] = $route['filters']; |
272 | 272 | } |
273 | 273 | |
@@ -288,39 +288,39 @@ discard block |
||
288 | 288 | $data = $this->getData(); |
289 | 289 | |
290 | 290 | foreach ($data['GET'] as $name => $value) { |
291 | - $controll = str_replace('{'.$name.'}',$value,$controll); |
|
291 | + $controll = str_replace('{'.$name.'}', $value, $controll); |
|
292 | 292 | } |
293 | 293 | |
294 | - $d = explode(':',$controll); |
|
294 | + $d = explode(':', $controll); |
|
295 | 295 | |
296 | - if(count($d) != 2){ |
|
296 | + if (count($d) != 2) { |
|
297 | 297 | throw new Exception("Controller {$controll} badly set."); |
298 | 298 | } |
299 | 299 | |
300 | - if(!class_exists('Controllers\\' . ucfirst($d[0]))){ |
|
300 | + if (!class_exists('Controllers\\'.ucfirst($d[0]))) { |
|
301 | 301 | throw new Exception("No controller {$d[0]} found."); |
302 | 302 | } |
303 | 303 | |
304 | - if(!method_exists('Controllers\\' . ucfirst($d[0]), $d[1])){ |
|
304 | + if (!method_exists('Controllers\\'.ucfirst($d[0]), $d[1])) { |
|
305 | 305 | throw new Exception("No method {$d[1]} found for {$d[0]}."); |
306 | 306 | } |
307 | 307 | |
308 | - $controller = 'Controllers\\' . ucfirst($d[0]); |
|
308 | + $controller = 'Controllers\\'.ucfirst($d[0]); |
|
309 | 309 | $controller = new $controller(); |
310 | 310 | $method = $d[1]; |
311 | 311 | |
312 | - $isForm = ( $this->getProtocol() == 'form'); |
|
312 | + $isForm = ($this->getProtocol() == 'form'); |
|
313 | 313 | |
314 | - if($isform){ |
|
314 | + if ($isform) { |
|
315 | 315 | $this->ControllerForm($controller, $method, $data['POST']); |
316 | 316 | }else { |
317 | 317 | $controller->$method($data); |
318 | 318 | } |
319 | 319 | } |
320 | 320 | |
321 | - public function ControllerForm($controller, string $method, array $values){ |
|
322 | - if(Validator::execute($values)){ |
|
323 | - if(!array_key_exists('role',$this->getData()['POST'])){ |
|
321 | + public function ControllerForm($controller, string $method, array $values) { |
|
322 | + if (Validator::execute($values)) { |
|
323 | + if (!array_key_exists('role', $this->getData()['POST'])) { |
|
324 | 324 | throw new Exception('O servidor não conseguiu identificar a finalidade deste formulário.'); |
325 | 325 | } |
326 | 326 |
@@ -154,7 +154,7 @@ discard block |
||
154 | 154 | foreach($route['filters'] as $filter){ |
155 | 155 | $this->filter->filtering($filter); |
156 | 156 | } |
157 | - }else{ |
|
157 | + } else{ |
|
158 | 158 | $this->filter->filtering($route['filters']); |
159 | 159 | } |
160 | 160 | } |
@@ -178,7 +178,7 @@ discard block |
||
178 | 178 | continue; |
179 | 179 | } |
180 | 180 | } |
181 | - }else{ |
|
181 | + } else{ |
|
182 | 182 | if($route['protocol'] !== $currentProtocol){ |
183 | 183 | continue; |
184 | 184 | } |
@@ -222,7 +222,7 @@ discard block |
||
222 | 222 | foreach($route['filters'] as $filter){ |
223 | 223 | $this->filter->filtering($filter); |
224 | 224 | } |
225 | - }else{ |
|
225 | + } else{ |
|
226 | 226 | $this->filter->filtering($route['filters']); |
227 | 227 | } |
228 | 228 | } |
@@ -246,7 +246,7 @@ discard block |
||
246 | 246 | } |
247 | 247 | } |
248 | 248 | |
249 | - }else{ |
|
249 | + } else{ |
|
250 | 250 | $currentRoute = self::getInstance()->addFilter(end(self::getInstance()->routers),$filters); |
251 | 251 | self::getInstance()->routers[count(self::getInstance()->routers)-1] = $currentRoute; |
252 | 252 | } |
@@ -267,7 +267,7 @@ discard block |
||
267 | 267 | foreach ($route['filters'] as $key => $value) { |
268 | 268 | $filters[] = $value; |
269 | 269 | } |
270 | - }else{ |
|
270 | + } else{ |
|
271 | 271 | $filters[] = $route['filters']; |
272 | 272 | } |
273 | 273 | |
@@ -313,7 +313,7 @@ discard block |
||
313 | 313 | |
314 | 314 | if($isform){ |
315 | 315 | $this->ControllerForm($controller, $method, $data['POST']); |
316 | - }else { |
|
316 | + } else { |
|
317 | 317 | $controller->$method($data); |
318 | 318 | } |
319 | 319 | } |