@@ -71,26 +71,26 @@ discard block |
||
71 | 71 | |
72 | 72 | public function set($url ,$walking , string $protocol): Router |
73 | 73 | { |
74 | - $url = (substr($url,0,1) !=='/' and strlen($url) > 0) ? "/{$url}" : $url; |
|
74 | + $url = (substr($url,0,1) !=='/' and strlen($url) > 0) ? "/{$url}" : $url; |
|
75 | 75 | |
76 | - foreach($this->routers as $key => $value){ |
|
77 | - if( md5($this->prefix . $value['url'] . $value['protocol'] ) === md5( $url . $protocol ) ){ |
|
76 | + foreach($this->routers as $key => $value){ |
|
77 | + if( md5($this->prefix . $value['url'] . $value['protocol'] ) === md5( $url . $protocol ) ){ |
|
78 | 78 | throw new Exception("There is already a route with the url {$url} and with the {$protocol} protocol configured."); |
79 | 79 | } |
80 | 80 | } |
81 | 81 | |
82 | 82 | $this->checkTypeRole($walking); |
83 | 83 | |
84 | - $route = [ |
|
85 | - 'url' => $this->prefix.$url, |
|
86 | - 'role' => $walking, |
|
87 | - 'protocol' => $protocol, |
|
84 | + $route = [ |
|
85 | + 'url' => $this->prefix.$url, |
|
86 | + 'role' => $walking, |
|
87 | + 'protocol' => $protocol, |
|
88 | 88 | 'filters' => null, |
89 | 89 | 'where' => null, |
90 | 90 | 'group' => self::getInstance()->group |
91 | - ]; |
|
91 | + ]; |
|
92 | 92 | |
93 | - $this->routers[] = $route; |
|
93 | + $this->routers[] = $route; |
|
94 | 94 | |
95 | 95 | return self::getInstance(); |
96 | 96 | } |
@@ -166,7 +166,7 @@ discard block |
||
166 | 166 | |
167 | 167 | $instance->getInstance()->byName($routeName); |
168 | 168 | |
169 | - $currentProtocol = $instance->getInstance()->getProtocol(); |
|
169 | + $currentProtocol = $instance->getInstance()->getProtocol(); |
|
170 | 170 | |
171 | 171 | foreach(array_reverse($instance->getInstance()->routers) as $r => $route){ |
172 | 172 | |
@@ -197,7 +197,7 @@ discard block |
||
197 | 197 | |
198 | 198 | $instance->getInstance()->currentRoute = null; |
199 | 199 | |
200 | - throw new Exception('Page not found.',404); |
|
200 | + throw new Exception('Page not found.',404); |
|
201 | 201 | } |
202 | 202 | |
203 | 203 | public static function filter($filters): Router |
@@ -4,7 +4,7 @@ discard block |
||
4 | 4 | |
5 | 5 | use Exception; |
6 | 6 | |
7 | -class Router{ |
|
7 | +class Router { |
|
8 | 8 | use Helper, CheckTrait, CheckWhere; |
9 | 9 | |
10 | 10 | private static $instance = null; |
@@ -21,7 +21,7 @@ discard block |
||
21 | 21 | |
22 | 22 | public static function create(): Router |
23 | 23 | { |
24 | - if(!self::getInstance()->instanced){ |
|
24 | + if (!self::getInstance()->instanced) { |
|
25 | 25 | self::getInstance()->checkConfig(); |
26 | 26 | self::getInstance()->import(ROUTER_CONFIG['path']); |
27 | 27 | self::getInstance()->instanced = true; |
@@ -69,12 +69,12 @@ discard block |
||
69 | 69 | return self::getInstance()->set($uri, $walking, $protocol); |
70 | 70 | } |
71 | 71 | |
72 | - public function set($url ,$walking , string $protocol): Router |
|
72 | + public function set($url, $walking, string $protocol): Router |
|
73 | 73 | { |
74 | - $url = (substr($url,0,1) !=='/' and strlen($url) > 0) ? "/{$url}" : $url; |
|
74 | + $url = (substr($url, 0, 1) !== '/' and strlen($url) > 0) ? "/{$url}" : $url; |
|
75 | 75 | |
76 | - foreach($this->routers as $key => $value){ |
|
77 | - if( md5($this->prefix . $value['url'] . $value['protocol'] ) === md5( $url . $protocol ) ){ |
|
76 | + foreach ($this->routers as $key => $value) { |
|
77 | + if (md5($this->prefix.$value['url'].$value['protocol']) === md5($url.$protocol)) { |
|
78 | 78 | throw new Exception("There is already a route with the url {$url} and with the {$protocol} protocol configured."); |
79 | 79 | } |
80 | 80 | } |
@@ -95,9 +95,9 @@ discard block |
||
95 | 95 | return self::getInstance(); |
96 | 96 | } |
97 | 97 | |
98 | - public static function group(string $prefix,$callback): Router |
|
98 | + public static function group(string $prefix, $callback): Router |
|
99 | 99 | { |
100 | - self::getInstance()->prefix = (substr($prefix,0,1) !== '/') ? "/{$prefix}" : $prefix; |
|
100 | + self::getInstance()->prefix = (substr($prefix, 0, 1) !== '/') ? "/{$prefix}" : $prefix; |
|
101 | 101 | self::getInstance()->group = sha1(date('d/m/Y h:m:i')); |
102 | 102 | $callback(); |
103 | 103 | self::getInstance()->group = null; |
@@ -108,7 +108,7 @@ discard block |
||
108 | 108 | |
109 | 109 | public static function where(): Router |
110 | 110 | { |
111 | - if(self::getInstance()->lastReturn){ |
|
111 | + if (self::getInstance()->lastReturn) { |
|
112 | 112 | throw new Exception("It is not possible to define parameter tests for groups of routes."); |
113 | 113 | } |
114 | 114 | |
@@ -119,14 +119,14 @@ discard block |
||
119 | 119 | |
120 | 120 | public static function name(string $name): Router |
121 | 121 | { |
122 | - if(self::getInstance()->lastReturn){ |
|
122 | + if (self::getInstance()->lastReturn) { |
|
123 | 123 | throw new Exception("There is no reason to call a {$name} route group."); |
124 | 124 | } |
125 | 125 | |
126 | 126 | $currentRoute = end(self::getInstance()->routers); |
127 | 127 | |
128 | - foreach(self::getInstance()->routers as $key => $value){ |
|
129 | - if(array_key_exists($name, self::getInstance()->routers)){ |
|
128 | + foreach (self::getInstance()->routers as $key => $value) { |
|
129 | + if (array_key_exists($name, self::getInstance()->routers)) { |
|
130 | 130 | throw new Exception("There is already a route with the name {$name} configured."); |
131 | 131 | } |
132 | 132 | } |
@@ -142,15 +142,15 @@ discard block |
||
142 | 142 | |
143 | 143 | private function byName(?string $routName) |
144 | 144 | { |
145 | - if(!is_null($routName)){ |
|
145 | + if (!is_null($routName)) { |
|
146 | 146 | $currentProtocol = $this->getProtocol(); |
147 | 147 | |
148 | 148 | $this->checkName($routName); |
149 | 149 | |
150 | 150 | $route = $this->routers[$routName]; |
151 | 151 | |
152 | - if(!$this->checkProtocol($route['protocol'], $currentProtocol)){ |
|
153 | - throw new Exception('Page not found.',404); |
|
152 | + if (!$this->checkProtocol($route['protocol'], $currentProtocol)) { |
|
153 | + throw new Exception('Page not found.', 404); |
|
154 | 154 | } |
155 | 155 | |
156 | 156 | $this->checkFiltering($route); |
@@ -168,23 +168,23 @@ discard block |
||
168 | 168 | |
169 | 169 | $currentProtocol = $instance->getInstance()->getProtocol(); |
170 | 170 | |
171 | - foreach(array_reverse($instance->getInstance()->routers) as $r => $route){ |
|
171 | + foreach (array_reverse($instance->getInstance()->routers) as $r => $route) { |
|
172 | 172 | |
173 | 173 | $instance->getInstance()->currentRoute = $route; |
174 | 174 | |
175 | - if(!$instance->getInstance()->checkProtocol($route['protocol'], $currentProtocol)){ |
|
175 | + if (!$instance->getInstance()->checkProtocol($route['protocol'], $currentProtocol)) { |
|
176 | 176 | continue; |
177 | 177 | } |
178 | 178 | |
179 | 179 | $instance->getInstance()->hasProtocol($route, $currentProtocol); |
180 | 180 | |
181 | - $routeLoop = $instance->getInstance()->explodeRoute( (substr($route['url'],strlen($route['url'])-1,1) === '/') , $route['url']); |
|
181 | + $routeLoop = $instance->getInstance()->explodeRoute((substr($route['url'], strlen($route['url'])-1, 1) === '/'), $route['url']); |
|
182 | 182 | |
183 | 183 | $_SERVER['REQUEST_URI'] = (array_key_exists('REQUEST_URI', $_SERVER)) ? $_SERVER['REQUEST_URI'] : ''; |
184 | 184 | |
185 | - $routeRequest = $instance->getInstance()->explodeRoute((substr($_SERVER['REQUEST_URI'],strlen($_SERVER['REQUEST_URI'])-1,1) === '/') , $_SERVER['REQUEST_URI']); |
|
185 | + $routeRequest = $instance->getInstance()->explodeRoute((substr($_SERVER['REQUEST_URI'], strlen($_SERVER['REQUEST_URI'])-1, 1) === '/'), $_SERVER['REQUEST_URI']); |
|
186 | 186 | |
187 | - if(!$instance->getInstance()->checkToHiking($route, $routeRequest, $routeLoop)){ |
|
187 | + if (!$instance->getInstance()->checkToHiking($route, $routeRequest, $routeLoop)) { |
|
188 | 188 | continue; |
189 | 189 | } |
190 | 190 | |
@@ -197,18 +197,18 @@ discard block |
||
197 | 197 | |
198 | 198 | $instance->getInstance()->currentRoute = null; |
199 | 199 | |
200 | - throw new Exception('Page not found.',404); |
|
200 | + throw new Exception('Page not found.', 404); |
|
201 | 201 | } |
202 | 202 | |
203 | 203 | public static function filter($filters): Router |
204 | 204 | { |
205 | - if(self::getInstance()->lastReturn !== null){ |
|
205 | + if (self::getInstance()->lastReturn !== null) { |
|
206 | 206 | $currentGroup = end(self::getInstance()->routers)['group']; |
207 | 207 | |
208 | 208 | foreach (self::getInstance()->routers as $key => $value) { |
209 | 209 | |
210 | - if($value['group'] === $currentGroup){ |
|
211 | - $currentRoute = self::getInstance()->addFilter(self::getInstance()->routers[$key],$filters); |
|
210 | + if ($value['group'] === $currentGroup) { |
|
211 | + $currentRoute = self::getInstance()->addFilter(self::getInstance()->routers[$key], $filters); |
|
212 | 212 | self::getInstance()->routers[$key] = $currentRoute; |
213 | 213 | } |
214 | 214 | |
@@ -219,24 +219,24 @@ discard block |
||
219 | 219 | return self::getInstance(); |
220 | 220 | } |
221 | 221 | |
222 | - self::getInstance()->routers[count(self::getInstance()->routers)-1] = self::getInstance()->addFilter(end(self::getInstance()->routers),$filters); |
|
222 | + self::getInstance()->routers[count(self::getInstance()->routers)-1] = self::getInstance()->addFilter(end(self::getInstance()->routers), $filters); |
|
223 | 223 | return self::getInstance(); |
224 | 224 | } |
225 | 225 | |
226 | 226 | public static function addFilter(array $route, $filter): array |
227 | 227 | { |
228 | - if(is_null($route['filters'])){ |
|
228 | + if (is_null($route['filters'])) { |
|
229 | 229 | $route['filters'] = $filter; |
230 | 230 | return $route; |
231 | 231 | } |
232 | 232 | |
233 | 233 | $filters = (is_array($filter)) ? $filter : [0 => $filter]; |
234 | 234 | |
235 | - if(is_array($route['filters'])){ |
|
235 | + if (is_array($route['filters'])) { |
|
236 | 236 | foreach ($route['filters'] as $key => $value) { |
237 | 237 | $filters[] = $value; |
238 | 238 | } |
239 | - }else{ |
|
239 | + }else { |
|
240 | 240 | $filters[] = $route['filters']; |
241 | 241 | } |
242 | 242 |
@@ -4,7 +4,7 @@ discard block |
||
4 | 4 | |
5 | 5 | use Exception; |
6 | 6 | |
7 | -trait CheckTrait{ |
|
7 | +trait CheckTrait { |
|
8 | 8 | use FilterTrait, CheckWhere; |
9 | 9 | |
10 | 10 | protected function checkProtocol(string $expected, string $current): bool |
@@ -12,21 +12,21 @@ discard block |
||
12 | 12 | return ($expected === $current); |
13 | 13 | } |
14 | 14 | |
15 | - protected function checkName(string $routeName){ |
|
16 | - if(!array_key_exists($routeName,$this->routers)){ |
|
15 | + protected function checkName(string $routeName) { |
|
16 | + if (!array_key_exists($routeName, $this->routers)) { |
|
17 | 17 | throw new Exception('Page not found.', 404); |
18 | 18 | } |
19 | 19 | } |
20 | 20 | |
21 | - protected function checkTypeRole($role){ |
|
22 | - if(!is_string($role) && @get_class($role) !== 'Closure' ){ |
|
21 | + protected function checkTypeRole($role) { |
|
22 | + if (!is_string($role) && @get_class($role) !== 'Closure') { |
|
23 | 23 | throw new Exception("Improperly defined route track."); |
24 | 24 | } |
25 | 25 | } |
26 | 26 | |
27 | 27 | protected function checkConfig() |
28 | 28 | { |
29 | - if(!defined('ROUTER_CONFIG')){ |
|
29 | + if (!defined('ROUTER_CONFIG')) { |
|
30 | 30 | throw new Exception("Information for loading routes has not been defined."); |
31 | 31 | } |
32 | 32 | } |
@@ -38,12 +38,12 @@ discard block |
||
38 | 38 | |
39 | 39 | protected function checkParameters(array $routeLoop, array $routeRequest): bool |
40 | 40 | { |
41 | - foreach($routeLoop as $rr => $param){ |
|
42 | - if( (substr($param,0,1) === '{') ){ |
|
43 | - $_GET[ substr($param,1,strlen($param)-2) ] = $routeRequest[$rr]; |
|
41 | + foreach ($routeLoop as $rr => $param) { |
|
42 | + if ((substr($param, 0, 1) === '{')) { |
|
43 | + $_GET[substr($param, 1, strlen($param)-2)] = $routeRequest[$rr]; |
|
44 | 44 | } |
45 | 45 | |
46 | - if($this->checkParameter($param, $routeRequest[$rr])){ |
|
46 | + if ($this->checkParameter($param, $routeRequest[$rr])) { |
|
47 | 47 | return false; |
48 | 48 | } |
49 | 49 | } |
@@ -52,22 +52,22 @@ discard block |
||
52 | 52 | |
53 | 53 | protected function checkParameter(string $routeLoop, string $routeRequest) |
54 | 54 | { |
55 | - return !( substr($routeLoop,0,1) === '{' ) and $routeLoop !== $routeRequest; |
|
55 | + return !(substr($routeLoop, 0, 1) === '{') and $routeLoop !== $routeRequest; |
|
56 | 56 | } |
57 | 57 | |
58 | 58 | protected function checkRole() |
59 | 59 | { |
60 | - if(!array_key_exists('role', $this->getData()['POST'])){ |
|
60 | + if (!array_key_exists('role', $this->getData()['POST'])) { |
|
61 | 61 | throw new Exception('O servidor não conseguiu identificar a finalidade deste formulário.'); |
62 | 62 | } |
63 | 63 | } |
64 | 64 | |
65 | 65 | protected function hasProtocol(array $route, string $currentProtocol) |
66 | 66 | { |
67 | - $protocols = ( is_array($route['protocol']) ) ? $route['protocol'] : [ $route['protocol'] ]; |
|
67 | + $protocols = (is_array($route['protocol'])) ? $route['protocol'] : [$route['protocol']]; |
|
68 | 68 | |
69 | - foreach($protocols as $protocol){ |
|
70 | - if($protocol !== $currentProtocol){ |
|
69 | + foreach ($protocols as $protocol) { |
|
70 | + if ($protocol !== $currentProtocol) { |
|
71 | 71 | continue; |
72 | 72 | } |
73 | 73 | } |
@@ -75,9 +75,9 @@ discard block |
||
75 | 75 | |
76 | 76 | protected function checkToHiking($route, $routeRequest, $routeLoop): bool |
77 | 77 | { |
78 | - if($this->checkNumparams($routeLoop, $routeRequest) || |
|
78 | + if ($this->checkNumparams($routeLoop, $routeRequest) || |
|
79 | 79 | $this->checkParameters($routeLoop, $routeRequest) || |
80 | - $this->checkWhere($route, $routeRequest)){ |
|
80 | + $this->checkWhere($route, $routeRequest)) { |
|
81 | 81 | return false; |
82 | 82 | } |
83 | 83 | return true; |