@@ -2,12 +2,12 @@ |
||
2 | 2 | |
3 | 3 | namespace HnrAzevedo\Router; |
4 | 4 | |
5 | -trait DefinitionsTrait{ |
|
5 | +trait DefinitionsTrait { |
|
6 | 6 | private static $instance = null; |
7 | 7 | |
8 | 8 | public static function match(string $protocols, string $uri, $walking): Router |
9 | 9 | { |
10 | - foreach(explode('|',$protocols) as $protocol){ |
|
10 | + foreach (explode('|', $protocols) as $protocol) { |
|
11 | 11 | self::getInstance()->add($uri, $walking, $protocol); |
12 | 12 | } |
13 | 13 | return self::$instance; |
@@ -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, DefinitionsTrait; |
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; |
@@ -36,12 +36,12 @@ discard block |
||
36 | 36 | return self::$instance; |
37 | 37 | } |
38 | 38 | |
39 | - public function set($url ,$walking , string $protocol): Router |
|
39 | + public function set($url, $walking, string $protocol): Router |
|
40 | 40 | { |
41 | - $url = (substr($url,0,1) !=='/' and strlen($url) > 0) ? "/{$url}" : $url; |
|
41 | + $url = (substr($url, 0, 1) !== '/' and strlen($url) > 0) ? "/{$url}" : $url; |
|
42 | 42 | |
43 | - foreach($this->routers as $key => $value){ |
|
44 | - if( md5($this->prefix . $value['url'] . $value['protocol'] ) === md5( $url . $protocol ) ){ |
|
43 | + foreach ($this->routers as $key => $value) { |
|
44 | + if (md5($this->prefix.$value['url'].$value['protocol']) === md5($url.$protocol)) { |
|
45 | 45 | throw new Exception("There is already a route with the url {$url} and with the {$protocol} protocol configured."); |
46 | 46 | } |
47 | 47 | } |
@@ -62,9 +62,9 @@ discard block |
||
62 | 62 | return self::getInstance(); |
63 | 63 | } |
64 | 64 | |
65 | - public static function group(string $prefix,$callback): Router |
|
65 | + public static function group(string $prefix, $callback): Router |
|
66 | 66 | { |
67 | - self::getInstance()->prefix = (substr($prefix,0,1) !== '/') ? "/{$prefix}" : $prefix; |
|
67 | + self::getInstance()->prefix = (substr($prefix, 0, 1) !== '/') ? "/{$prefix}" : $prefix; |
|
68 | 68 | self::getInstance()->group = sha1(date('d/m/Y h:m:i')); |
69 | 69 | $callback(); |
70 | 70 | self::getInstance()->group = null; |
@@ -75,7 +75,7 @@ discard block |
||
75 | 75 | |
76 | 76 | public static function where(): Router |
77 | 77 | { |
78 | - if(self::getInstance()->lastReturn){ |
|
78 | + if (self::getInstance()->lastReturn) { |
|
79 | 79 | throw new Exception("It is not possible to define parameter tests for groups of routes."); |
80 | 80 | } |
81 | 81 | |
@@ -86,14 +86,14 @@ discard block |
||
86 | 86 | |
87 | 87 | public static function name(string $name): Router |
88 | 88 | { |
89 | - if(self::getInstance()->lastReturn){ |
|
89 | + if (self::getInstance()->lastReturn) { |
|
90 | 90 | throw new Exception("There is no reason to call a {$name} route group."); |
91 | 91 | } |
92 | 92 | |
93 | 93 | $currentRoute = end(self::getInstance()->routers); |
94 | 94 | |
95 | - foreach(self::getInstance()->routers as $key => $value){ |
|
96 | - if(array_key_exists($name, self::getInstance()->routers)){ |
|
95 | + foreach (self::getInstance()->routers as $key => $value) { |
|
96 | + if (array_key_exists($name, self::getInstance()->routers)) { |
|
97 | 97 | throw new Exception("There is already a route with the name {$name} configured."); |
98 | 98 | } |
99 | 99 | } |
@@ -109,15 +109,15 @@ discard block |
||
109 | 109 | |
110 | 110 | private function byName(?string $routName) |
111 | 111 | { |
112 | - if(!is_null($routName)){ |
|
112 | + if (!is_null($routName)) { |
|
113 | 113 | $currentProtocol = $this->getProtocol(); |
114 | 114 | |
115 | 115 | $this->checkName($routName); |
116 | 116 | |
117 | 117 | $route = $this->routers[$routName]; |
118 | 118 | |
119 | - if(!$this->checkProtocol($route['protocol'], $currentProtocol)){ |
|
120 | - throw new Exception('Page not found.',404); |
|
119 | + if (!$this->checkProtocol($route['protocol'], $currentProtocol)) { |
|
120 | + throw new Exception('Page not found.', 404); |
|
121 | 121 | } |
122 | 122 | |
123 | 123 | $this->checkFiltering($route); |
@@ -135,23 +135,23 @@ discard block |
||
135 | 135 | |
136 | 136 | $currentProtocol = $instance->getInstance()->getProtocol(); |
137 | 137 | |
138 | - foreach(array_reverse($instance->getInstance()->routers) as $r => $route){ |
|
138 | + foreach (array_reverse($instance->getInstance()->routers) as $r => $route) { |
|
139 | 139 | |
140 | 140 | $instance->getInstance()->currentRoute = $route; |
141 | 141 | |
142 | - if(!$instance->getInstance()->checkProtocol($route['protocol'], $currentProtocol)){ |
|
142 | + if (!$instance->getInstance()->checkProtocol($route['protocol'], $currentProtocol)) { |
|
143 | 143 | continue; |
144 | 144 | } |
145 | 145 | |
146 | 146 | $instance->getInstance()->hasProtocol($route, $currentProtocol); |
147 | 147 | |
148 | - $routeLoop = $instance->getInstance()->explodeRoute( (substr($route['url'],strlen($route['url'])-1,1) === '/') , $route['url']); |
|
148 | + $routeLoop = $instance->getInstance()->explodeRoute((substr($route['url'], strlen($route['url'])-1, 1) === '/'), $route['url']); |
|
149 | 149 | |
150 | 150 | $_SERVER['REQUEST_URI'] = (array_key_exists('REQUEST_URI', $_SERVER)) ? $_SERVER['REQUEST_URI'] : ''; |
151 | 151 | |
152 | - $routeRequest = $instance->getInstance()->explodeRoute((substr($_SERVER['REQUEST_URI'],strlen($_SERVER['REQUEST_URI'])-1,1) === '/') , $_SERVER['REQUEST_URI']); |
|
152 | + $routeRequest = $instance->getInstance()->explodeRoute((substr($_SERVER['REQUEST_URI'], strlen($_SERVER['REQUEST_URI'])-1, 1) === '/'), $_SERVER['REQUEST_URI']); |
|
153 | 153 | |
154 | - if(!$instance->getInstance()->checkToHiking($route, $routeRequest, $routeLoop)){ |
|
154 | + if (!$instance->getInstance()->checkToHiking($route, $routeRequest, $routeLoop)) { |
|
155 | 155 | continue; |
156 | 156 | } |
157 | 157 | |
@@ -164,18 +164,18 @@ discard block |
||
164 | 164 | |
165 | 165 | $instance->getInstance()->currentRoute = null; |
166 | 166 | |
167 | - throw new Exception('Page not found.',404); |
|
167 | + throw new Exception('Page not found.', 404); |
|
168 | 168 | } |
169 | 169 | |
170 | 170 | public static function filter($filters): Router |
171 | 171 | { |
172 | - if(self::getInstance()->lastReturn !== null){ |
|
172 | + if (self::getInstance()->lastReturn !== null) { |
|
173 | 173 | $currentGroup = end(self::getInstance()->routers)['group']; |
174 | 174 | |
175 | 175 | foreach (self::getInstance()->routers as $key => $value) { |
176 | 176 | |
177 | - if($value['group'] === $currentGroup){ |
|
178 | - $currentRoute = self::getInstance()->addFilter(self::getInstance()->routers[$key],$filters); |
|
177 | + if ($value['group'] === $currentGroup) { |
|
178 | + $currentRoute = self::getInstance()->addFilter(self::getInstance()->routers[$key], $filters); |
|
179 | 179 | self::getInstance()->routers[$key] = $currentRoute; |
180 | 180 | } |
181 | 181 | |
@@ -186,24 +186,24 @@ discard block |
||
186 | 186 | return self::getInstance(); |
187 | 187 | } |
188 | 188 | |
189 | - self::getInstance()->routers[count(self::getInstance()->routers)-1] = self::getInstance()->addFilter(end(self::getInstance()->routers),$filters); |
|
189 | + self::getInstance()->routers[count(self::getInstance()->routers)-1] = self::getInstance()->addFilter(end(self::getInstance()->routers), $filters); |
|
190 | 190 | return self::getInstance(); |
191 | 191 | } |
192 | 192 | |
193 | 193 | public static function addFilter(array $route, $filter): array |
194 | 194 | { |
195 | - if(is_null($route['filters'])){ |
|
195 | + if (is_null($route['filters'])) { |
|
196 | 196 | $route['filters'] = $filter; |
197 | 197 | return $route; |
198 | 198 | } |
199 | 199 | |
200 | 200 | $filters = (is_array($filter)) ? $filter : [0 => $filter]; |
201 | 201 | |
202 | - if(is_array($route['filters'])){ |
|
202 | + if (is_array($route['filters'])) { |
|
203 | 203 | foreach ($route['filters'] as $key => $value) { |
204 | 204 | $filters[] = $value; |
205 | 205 | } |
206 | - }else{ |
|
206 | + }else { |
|
207 | 207 | $filters[] = $route['filters']; |
208 | 208 | } |
209 | 209 |