@@ -4,17 +4,17 @@ discard block |
||
4 | 4 | |
5 | 5 | use HnrAzevedo\Validator\Validator; |
6 | 6 | |
7 | -trait Helper{ |
|
7 | +trait Helper { |
|
8 | 8 | use CheckTrait, ControllerTrait; |
9 | 9 | |
10 | 10 | protected function getProtocol(): string |
11 | 11 | { |
12 | - if((isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')){ |
|
12 | + if ((isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')) { |
|
13 | 13 | return 'ajax'; |
14 | 14 | } |
15 | 15 | |
16 | 16 | /* ONLY FOR DEBUG CONDITION */ |
17 | - if(!array_key_exists('REQUEST_METHOD',$_SERVER)){ |
|
17 | + if (!array_key_exists('REQUEST_METHOD', $_SERVER)) { |
|
18 | 18 | return 'get'; |
19 | 19 | } |
20 | 20 | |
@@ -33,14 +33,14 @@ discard block |
||
33 | 33 | protected function import(string $path) |
34 | 34 | { |
35 | 35 | foreach (scandir($path) as $routeFile) { |
36 | - if(pathinfo($path.DIRECTORY_SEPARATOR.$routeFile, PATHINFO_EXTENSION) === 'php'){ |
|
37 | - require_once($path. DIRECTORY_SEPARATOR .$routeFile); |
|
36 | + if (pathinfo($path.DIRECTORY_SEPARATOR.$routeFile, PATHINFO_EXTENSION) === 'php') { |
|
37 | + require_once($path.DIRECTORY_SEPARATOR.$routeFile); |
|
38 | 38 | } |
39 | 39 | } |
40 | 40 | } |
41 | 41 | |
42 | - protected function ControllerForm($controller, string $method, array $values){ |
|
43 | - if(Validator::execute($values)){ |
|
42 | + protected function ControllerForm($controller, string $method, array $values) { |
|
43 | + if (Validator::execute($values)) { |
|
44 | 44 | |
45 | 45 | $this->check_role(); |
46 | 46 | |
@@ -55,7 +55,7 @@ discard block |
||
55 | 55 | $data = $this->getData(); |
56 | 56 | |
57 | 57 | foreach ($data['GET'] as $name => $value) { |
58 | - $controll = str_replace('{'.$name.'}',$value,$controll); |
|
58 | + $controll = str_replace('{'.$name.'}', $value, $controll); |
|
59 | 59 | } |
60 | 60 | |
61 | 61 | $this->check_controllsettable($controll); |
@@ -64,11 +64,11 @@ discard block |
||
64 | 64 | |
65 | 65 | $this->check_controllmethod($controll); |
66 | 66 | |
67 | - $controller = 'Controllers\\' . ucfirst(explode(':',$controll)[0]); |
|
67 | + $controller = 'Controllers\\'.ucfirst(explode(':', $controll)[0]); |
|
68 | 68 | $controller = new $controller(); |
69 | - $method = explode(':',$controll)[1]; |
|
69 | + $method = explode(':', $controll)[1]; |
|
70 | 70 | |
71 | - if( ( $this->getProtocol() == 'form') ){ |
|
71 | + if (($this->getProtocol() == 'form')) { |
|
72 | 72 | $this->ControllerForm($controller, $method, $data['POST']); |
73 | 73 | }else { |
74 | 74 | $controller->$method($data); |
@@ -77,7 +77,7 @@ discard block |
||
77 | 77 | |
78 | 78 | protected function explodeRoute(bool $bar, string $url): array |
79 | 79 | { |
80 | - return explode( '/', $bar ? substr($url, 0, -1) : $url ); |
|
80 | + return explode('/', $bar ? substr($url, 0, -1) : $url); |
|
81 | 81 | } |
82 | 82 | |
83 | 83 | } |
84 | 84 | \ No newline at end of file |
@@ -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; |
9 | 9 | |
10 | 10 | private static $instance = null; |
@@ -59,12 +59,12 @@ discard block |
||
59 | 59 | return self::getInstance()->set($uri, $controll, $protocol); |
60 | 60 | } |
61 | 61 | |
62 | - public function set($url , $role , $protocol = null): Router |
|
62 | + public function set($url, $role, $protocol = null): Router |
|
63 | 63 | { |
64 | - $url = (substr($url,0,1) !=='/' and strlen($url) > 0) ? "/{$url}" : $url; |
|
64 | + $url = (substr($url, 0, 1) !== '/' and strlen($url) > 0) ? "/{$url}" : $url; |
|
65 | 65 | |
66 | - foreach($this->routers as $key => $value){ |
|
67 | - if( md5($this->prefix . $value['url'] . $value['protocol'] ) === md5( $url . $protocol ) ){ |
|
66 | + foreach ($this->routers as $key => $value) { |
|
67 | + if (md5($this->prefix.$value['url'].$value['protocol']) === md5($url.$protocol)) { |
|
68 | 68 | throw new Exception("There is already a route with the url {$url} and with the {$protocol} protocol configured."); |
69 | 69 | } |
70 | 70 | } |
@@ -82,9 +82,9 @@ discard block |
||
82 | 82 | return self::getInstance(); |
83 | 83 | } |
84 | 84 | |
85 | - public static function group(string $prefix,$callback): Router |
|
85 | + public static function group(string $prefix, $callback): Router |
|
86 | 86 | { |
87 | - self::getInstance()->prefix = (substr($prefix,0,1) !== '/') ? "/{$prefix}" : $prefix; |
|
87 | + self::getInstance()->prefix = (substr($prefix, 0, 1) !== '/') ? "/{$prefix}" : $prefix; |
|
88 | 88 | self::getInstance()->group = sha1(date('d/m/Y h:m:i')); |
89 | 89 | $callback(); |
90 | 90 | self::getInstance()->group = null; |
@@ -96,14 +96,14 @@ discard block |
||
96 | 96 | public static function name(string $name): Router |
97 | 97 | { |
98 | 98 | |
99 | - if(self::getInstance()->lastReturn){ |
|
99 | + if (self::getInstance()->lastReturn) { |
|
100 | 100 | throw new Exception("There is no reason to call a {$name} route group."); |
101 | 101 | } |
102 | 102 | |
103 | 103 | $currentRoute = end(self::getInstance()->routers); |
104 | 104 | |
105 | - foreach(self::getInstance()->routers as $key => $value){ |
|
106 | - if(array_key_exists($name, self::getInstance()->routers)){ |
|
105 | + foreach (self::getInstance()->routers as $key => $value) { |
|
106 | + if (array_key_exists($name, self::getInstance()->routers)) { |
|
107 | 107 | throw new Exception("There is already a route with the name {$name} configured."); |
108 | 108 | } |
109 | 109 | } |
@@ -119,7 +119,7 @@ discard block |
||
119 | 119 | |
120 | 120 | private function byName(?string $route_name) |
121 | 121 | { |
122 | - if(!is_null($route_name)){ |
|
122 | + if (!is_null($route_name)) { |
|
123 | 123 | $currentProtocol = $this->getProtocol(); |
124 | 124 | |
125 | 125 | $this->check_name($route_name); |
@@ -142,15 +142,15 @@ discard block |
||
142 | 142 | |
143 | 143 | $currentProtocol = $this->getProtocol(); |
144 | 144 | |
145 | - foreach(array_reverse($this->routers) as $r => $route){ |
|
145 | + foreach (array_reverse($this->routers) as $r => $route) { |
|
146 | 146 | |
147 | 147 | $this->hasProtocol($route, $currentProtocol); |
148 | 148 | |
149 | - $route_loop = $this->explodeRoute( (substr($route['url'],strlen($route['url'])-1,1) === '/') , $route['url']); |
|
149 | + $route_loop = $this->explodeRoute((substr($route['url'], strlen($route['url'])-1, 1) === '/'), $route['url']); |
|
150 | 150 | |
151 | - $route_request = $this->explodeRoute((substr($_SERVER['REQUEST_URI'],strlen($_SERVER['REQUEST_URI'])-1,1) === '/') , $_SERVER['REQUEST_URI']); |
|
151 | + $route_request = $this->explodeRoute((substr($_SERVER['REQUEST_URI'], strlen($_SERVER['REQUEST_URI'])-1, 1) === '/'), $_SERVER['REQUEST_URI']); |
|
152 | 152 | |
153 | - if($this->check_numparams($route_loop, $route_request) || !$this->check_parameters($route_loop, $route_request)){ |
|
153 | + if ($this->check_numparams($route_loop, $route_request) || !$this->check_parameters($route_loop, $route_request)) { |
|
154 | 154 | continue; |
155 | 155 | } |
156 | 156 | |
@@ -160,18 +160,18 @@ discard block |
||
160 | 160 | return true; |
161 | 161 | } |
162 | 162 | |
163 | - throw new Exception('Page not found.',404); |
|
163 | + throw new Exception('Page not found.', 404); |
|
164 | 164 | } |
165 | 165 | |
166 | 166 | public static function filter($filters): Router |
167 | 167 | { |
168 | - if(self::getInstance()->lastReturn !== null){ |
|
168 | + if (self::getInstance()->lastReturn !== null) { |
|
169 | 169 | $currentGroup = end(self::getInstance()->routers)['group']; |
170 | 170 | |
171 | 171 | foreach (self::getInstance()->routers as $key => $value) { |
172 | 172 | |
173 | - if($value['group'] === $currentGroup){ |
|
174 | - $currentRoute = self::getInstance()->addFilter(self::getInstance()->routers[$key],$filters); |
|
173 | + if ($value['group'] === $currentGroup) { |
|
174 | + $currentRoute = self::getInstance()->addFilter(self::getInstance()->routers[$key], $filters); |
|
175 | 175 | self::getInstance()->routers[$key] = $currentRoute; |
176 | 176 | } |
177 | 177 | |
@@ -180,24 +180,24 @@ discard block |
||
180 | 180 | return self::getInstance(); |
181 | 181 | } |
182 | 182 | |
183 | - self::getInstance()->routers[count(self::getInstance()->routers)-1] = self::getInstance()->addFilter(end(self::getInstance()->routers),$filters); |
|
183 | + self::getInstance()->routers[count(self::getInstance()->routers)-1] = self::getInstance()->addFilter(end(self::getInstance()->routers), $filters); |
|
184 | 184 | return self::getInstance(); |
185 | 185 | } |
186 | 186 | |
187 | 187 | public static function addFilter(array $route, $filter): array |
188 | 188 | { |
189 | - if(is_null($route['filters'])){ |
|
189 | + if (is_null($route['filters'])) { |
|
190 | 190 | $route['filters'] = $filter; |
191 | 191 | return $route; |
192 | 192 | } |
193 | 193 | |
194 | 194 | $filters = (is_array($filter)) ? $filter : [0 => $filter]; |
195 | 195 | |
196 | - if(is_array($route['filters'])){ |
|
196 | + if (is_array($route['filters'])) { |
|
197 | 197 | foreach ($route['filters'] as $key => $value) { |
198 | 198 | $filters[] = $value; |
199 | 199 | } |
200 | - }else{ |
|
200 | + }else { |
|
201 | 201 | $filters[] = $route['filters']; |
202 | 202 | } |
203 | 203 |