Passed
Branch master (388b38)
by Henri
06:53
created
src/Helper.php 1 patch
Spacing   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -2,16 +2,16 @@
 block discarded – undo
2 2
 
3 3
 namespace HnrAzevedo\Router;
4 4
 
5
-trait Helper{
5
+trait Helper {
6 6
     
7 7
     protected function getProtocol(): string
8 8
     {
9
-        if((isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')){
9
+        if ((isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')) {
10 10
             return 'ajax';
11 11
         }
12 12
         
13 13
         /* ONLY FOR DEBUG CONDITION */
14
-        if(!array_key_exists('REQUEST_METHOD',$_SERVER)){
14
+        if (!array_key_exists('REQUEST_METHOD', $_SERVER)) {
15 15
             return 'get';
16 16
         }
17 17
 
Please login to merge, or discard this patch.
src/Router.php 3 patches
Indentation   +53 added lines, -53 removed lines patch added patch discarded remove patch
@@ -79,23 +79,23 @@  discard block
 block discarded – undo
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
     }
@@ -137,33 +137,33 @@  discard block
 block discarded – undo
137 137
 
138 138
     public function dispatch(?string $route_name = null): bool
139 139
     {
140
-		$currentProtocol = $this->getProtocol();
140
+        $currentProtocol = $this->getProtocol();
141 141
         $context = [];
142 142
 
143
-		if(!empty($route_name)){
143
+        if(!empty($route_name)){
144 144
 
145
-			if(!array_key_exists($route_name,$this->routers)){
145
+            if(!array_key_exists($route_name,$this->routers)){
146 146
                 throw new Exception('Page not found.'.$route_name,404);
147 147
             }
148 148
 
149
-			$route = $this->routers[$route_name];
149
+            $route = $this->routers[$route_name];
150 150
 
151
-			if($route['protocol']!==$currentProtocol){
151
+            if($route['protocol']!==$currentProtocol){
152 152
                 throw new Exception('Page not found.'.$route_name,404);
153 153
             }
154 154
 
155
-			if(!empty($route['filters'])){
156
-	            if(is_array($route['filters'])){
157
-	                foreach($route['filters'] as $filter){
158
-	                    $this->filter->filtering($filter);
159
-	                }
160
-	            }else{
161
-	                $this->filter->filtering($route['filters']);
162
-	            }
163
-	        }
155
+            if(!empty($route['filters'])){
156
+                if(is_array($route['filters'])){
157
+                    foreach($route['filters'] as $filter){
158
+                        $this->filter->filtering($filter);
159
+                    }
160
+                }else{
161
+                    $this->filter->filtering($route['filters']);
162
+                }
163
+            }
164 164
 
165 165
             $this->Controller($route['role']);
166
-			return true;
166
+            return true;
167 167
         }
168 168
 
169 169
         foreach(array_reverse($this->routers) as $r => $route){
@@ -173,13 +173,13 @@  discard block
 block discarded – undo
173 173
                         continue;
174 174
                     }
175 175
                 }
176
-	        }else{
177
-				if($route['protocol'] !== $currentProtocol){
176
+            }else{
177
+                if($route['protocol'] !== $currentProtocol){
178 178
                     continue;
179 179
                 }
180
-			}
180
+            }
181 181
 
182
-	        $route_loop = explode(
182
+            $route_loop = explode(
183 183
                 '/',
184 184
                 (substr($route['url'],strlen($route['url'])-1,1) === '/') 
185 185
                     ? substr($route['url'], 0, -1) 
@@ -188,45 +188,45 @@  discard block
 block discarded – undo
188 188
 
189 189
             /* ONLY FOR DEBUG CONDITION */
190 190
             $route_request = $route_loop;
191
-	        /*$route_request = explode(
191
+            /*$route_request = explode(
192 192
                 '/',
193 193
                 (substr($_SERVER['REQUEST_URI'],strlen($_SERVER['REQUEST_URI'])-1,1) === '/') 
194 194
                 ? substr($_SERVER['REQUEST_URI'], 0, -1) 
195 195
                 : $_SERVER['REQUEST_URI'] 
196 196
             );*/
197 197
 
198
-	        if(count($route_loop) !== count($route_request)){
198
+            if(count($route_loop) !== count($route_request)){
199 199
                 continue;
200 200
             }
201 201
 
202
-	        for($rr = 0; $rr < count($route_loop); $rr++){
203
-	            $param = (substr($route_loop[$rr],0,1)==='{');
202
+            for($rr = 0; $rr < count($route_loop); $rr++){
203
+                $param = (substr($route_loop[$rr],0,1)==='{');
204 204
 
205
-	            if($param){
205
+                if($param){
206 206
                     $param_name = substr($route_loop[$rr],1,strlen($route_loop[$rr])-2);
207
-	                $data[$param_name] = $route_request[$rr];
208
-	            }
207
+                    $data[$param_name] = $route_request[$rr];
208
+                }
209 209
 
210
-	            if(!$param and $route_loop[$rr] !== $route_request[$rr]){
210
+                if(!$param and $route_loop[$rr] !== $route_request[$rr]){
211 211
                     continue 2;
212 212
                 }
213
-	        }
214
-
215
-	        if(!empty($route['filters'])){
216
-	            if(is_array($route['filters'])){
217
-	                foreach($route['filters'] as $filter){
218
-	                    $this->filter->filtering($filter);
219
-	                }
220
-	            }else{
221
-	                $this->filter->filtering($route['filters']);
222
-	            }
223
-	        }
213
+            }
214
+
215
+            if(!empty($route['filters'])){
216
+                if(is_array($route['filters'])){
217
+                    foreach($route['filters'] as $filter){
218
+                        $this->filter->filtering($filter);
219
+                    }
220
+                }else{
221
+                    $this->filter->filtering($route['filters']);
222
+                }
223
+            }
224 224
 
225 225
             $this->Controller($route['role']);
226
-	        return true;
227
-	    }
226
+            return true;
227
+        }
228 228
 
229
-	    throw new Exception('Page not found.',404);
229
+        throw new Exception('Page not found.',404);
230 230
     }
231 231
 
232 232
     public static function filter($filters): Router
@@ -314,7 +314,7 @@  discard block
 block discarded – undo
314 314
     }    
315 315
 
316 316
     public function ControllerForm($controller, string $method, array $values){
317
-		if(Validator::execute($values)){
317
+        if(Validator::execute($values)){
318 318
             if(!array_key_exists('role',$this->getData()['POST'])){
319 319
                 throw new Exception('O servidor não conseguiu identificar a finalidade deste formulário.');
320 320
             }
Please login to merge, or discard this patch.
Spacing   +60 added lines, -60 removed lines patch added patch discarded remove patch
@@ -6,7 +6,7 @@  discard block
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
         }
@@ -140,24 +140,24 @@  discard block
 block discarded – undo
140 140
 		$currentProtocol = $this->getProtocol();
141 141
         $context = [];
142 142
 
143
-		if(!empty($route_name)){
143
+		if (!empty($route_name)) {
144 144
 
145
-			if(!array_key_exists($route_name,$this->routers)){
146
-                throw new Exception('Page not found.'.$route_name,404);
145
+			if (!array_key_exists($route_name, $this->routers)) {
146
+                throw new Exception('Page not found.'.$route_name, 404);
147 147
             }
148 148
 
149 149
 			$route = $this->routers[$route_name];
150 150
 
151
-			if($route['protocol']!==$currentProtocol){
152
-                throw new Exception('Page not found.'.$route_name,404);
151
+			if ($route['protocol'] !== $currentProtocol) {
152
+                throw new Exception('Page not found.'.$route_name, 404);
153 153
             }
154 154
 
155
-			if(!empty($route['filters'])){
156
-	            if(is_array($route['filters'])){
157
-	                foreach($route['filters'] as $filter){
155
+			if (!empty($route['filters'])) {
156
+	            if (is_array($route['filters'])) {
157
+	                foreach ($route['filters'] as $filter) {
158 158
 	                    $this->filter->filtering($filter);
159 159
 	                }
160
-	            }else{
160
+	            }else {
161 161
 	                $this->filter->filtering($route['filters']);
162 162
 	            }
163 163
 	        }
@@ -166,22 +166,22 @@  discard block
 block discarded – undo
166 166
 			return true;
167 167
         }
168 168
 
169
-        foreach(array_reverse($this->routers) as $r => $route){
170
-            if(is_array($route['protocol'])){
171
-                foreach($route['protocol'] as $protocol){
172
-                    if($protocol !== $currentProtocol){
169
+        foreach (array_reverse($this->routers) as $r => $route) {
170
+            if (is_array($route['protocol'])) {
171
+                foreach ($route['protocol'] as $protocol) {
172
+                    if ($protocol !== $currentProtocol) {
173 173
                         continue;
174 174
                     }
175 175
                 }
176
-	        }else{
177
-				if($route['protocol'] !== $currentProtocol){
176
+	        }else {
177
+				if ($route['protocol'] !== $currentProtocol) {
178 178
                     continue;
179 179
                 }
180 180
 			}
181 181
 
182 182
 	        $route_loop = explode(
183 183
                 '/',
184
-                (substr($route['url'],strlen($route['url'])-1,1) === '/') 
184
+                (substr($route['url'], strlen($route['url'])-1, 1) === '/') 
185 185
                     ? substr($route['url'], 0, -1) 
186 186
                     : $route['url'] 
187 187
             );
@@ -195,29 +195,29 @@  discard block
 block discarded – undo
195 195
                 : $_SERVER['REQUEST_URI'] 
196 196
             );*/
197 197
 
198
-	        if(count($route_loop) !== count($route_request)){
198
+	        if (count($route_loop) !== count($route_request)) {
199 199
                 continue;
200 200
             }
201 201
 
202
-	        for($rr = 0; $rr < count($route_loop); $rr++){
203
-	            $param = (substr($route_loop[$rr],0,1)==='{');
202
+	        for ($rr = 0; $rr < count($route_loop); $rr++) {
203
+	            $param = (substr($route_loop[$rr], 0, 1) === '{');
204 204
 
205
-	            if($param){
206
-                    $param_name = substr($route_loop[$rr],1,strlen($route_loop[$rr])-2);
205
+	            if ($param) {
206
+                    $param_name = substr($route_loop[$rr], 1, strlen($route_loop[$rr])-2);
207 207
 	                $data[$param_name] = $route_request[$rr];
208 208
 	            }
209 209
 
210
-	            if(!$param and $route_loop[$rr] !== $route_request[$rr]){
210
+	            if (!$param and $route_loop[$rr] !== $route_request[$rr]) {
211 211
                     continue 2;
212 212
                 }
213 213
 	        }
214 214
 
215
-	        if(!empty($route['filters'])){
216
-	            if(is_array($route['filters'])){
217
-	                foreach($route['filters'] as $filter){
215
+	        if (!empty($route['filters'])) {
216
+	            if (is_array($route['filters'])) {
217
+	                foreach ($route['filters'] as $filter) {
218 218
 	                    $this->filter->filtering($filter);
219 219
 	                }
220
-	            }else{
220
+	            }else {
221 221
 	                $this->filter->filtering($route['filters']);
222 222
 	            }
223 223
 	        }
@@ -226,23 +226,23 @@  discard block
 block discarded – undo
226 226
 	        return true;
227 227
 	    }
228 228
 
229
-	    throw new Exception('Page not found.',404);
229
+	    throw new Exception('Page not found.', 404);
230 230
     }
231 231
 
232 232
     public static function filter($filters): Router
233 233
     {
234
-        if(self::getInstance()->lastReturn !== null){
234
+        if (self::getInstance()->lastReturn !== null) {
235 235
             $currentGroup = end(self::getInstance()->routers)['group'];
236 236
 
237 237
             foreach (self::getInstance()->routers as $key => $value) {
238
-                if($value['group'] === $currentGroup){
239
-                    $currentRoute = self::getInstance()->addFilter(self::getInstance()->routers[$key],$filters);
238
+                if ($value['group'] === $currentGroup) {
239
+                    $currentRoute = self::getInstance()->addFilter(self::getInstance()->routers[$key], $filters);
240 240
                     self::getInstance()->routers[$key] = $currentRoute;
241 241
                 }
242 242
             }
243 243
             
244
-        }else{
245
-            $currentRoute = self::getInstance()->addFilter(end(self::getInstance()->routers),$filters);
244
+        }else {
245
+            $currentRoute = self::getInstance()->addFilter(end(self::getInstance()->routers), $filters);
246 246
             self::getInstance()->routers[count(self::getInstance()->routers)-1] = $currentRoute;
247 247
         }
248 248
 
@@ -251,18 +251,18 @@  discard block
 block discarded – undo
251 251
 
252 252
     public static function addFilter(array $route, $filter): array
253 253
     {
254
-        if(is_null($route['filters'])){
254
+        if (is_null($route['filters'])) {
255 255
             $route['filters'] = $filter;
256 256
             return $route;
257 257
         }
258 258
 
259 259
         $filters = (is_array($filter)) ? $filter : [0 => $filter];
260 260
 
261
-        if(is_array($route['filters'])){
261
+        if (is_array($route['filters'])) {
262 262
             foreach ($route['filters'] as $key => $value) {
263 263
                 $filters[] = $value;
264 264
             }
265
-        }else{
265
+        }else {
266 266
             $filters[] = $route['filters'];
267 267
         }
268 268
 
@@ -283,39 +283,39 @@  discard block
 block discarded – undo
283 283
         $data = $this->getData();
284 284
 
285 285
         foreach ($data['GET'] as $name => $value) {
286
-            $controll = str_replace('{'.$name.'}',$value,$controll);
286
+            $controll = str_replace('{'.$name.'}', $value, $controll);
287 287
         }
288 288
 
289
-        $d = explode(':',$controll);
289
+        $d = explode(':', $controll);
290 290
 
291
-        if(count($d) != 2){
291
+        if (count($d) != 2) {
292 292
             throw new Exception("Controller {$controller} badly set.");
293 293
         }
294 294
 
295
-        if(!class_exists('Controllers\\' . ucfirst($d[0]))){
295
+        if (!class_exists('Controllers\\'.ucfirst($d[0]))) {
296 296
             throw new Exception("No controller {$d[0]} found.");
297 297
         }
298 298
 
299
-        if(!method_exists('Controllers\\' . ucfirst($d[0]), $d[1])){
299
+        if (!method_exists('Controllers\\'.ucfirst($d[0]), $d[1])) {
300 300
             throw new Exception("No method {$d[1]} found for {$d[0]}.");
301 301
         }
302 302
 
303
-        $controller = 'Controllers\\' . ucfirst($d[0]);
303
+        $controller = 'Controllers\\'.ucfirst($d[0]);
304 304
         $controller = new $controller();
305 305
         $method = $d[1];
306 306
 
307
-        $isForm = ( $this->getProtocol() == 'form');
307
+        $isForm = ($this->getProtocol() == 'form');
308 308
 
309
-        if($isform){
309
+        if ($isform) {
310 310
             $this->ControllerForm($controller, $method, $data['POST']);
311 311
         }else {
312 312
             $controller->$method($data);
313 313
         }
314 314
     }    
315 315
 
316
-    public function ControllerForm($controller, string $method, array $values){
317
-		if(Validator::execute($values)){
318
-            if(!array_key_exists('role',$this->getData()['POST'])){
316
+    public function ControllerForm($controller, string $method, array $values) {
317
+		if (Validator::execute($values)) {
318
+            if (!array_key_exists('role', $this->getData()['POST'])) {
319 319
                 throw new Exception('O servidor não conseguiu identificar a finalidade deste formulário.');
320 320
             }
321 321
 
Please login to merge, or discard this patch.
Braces   +6 added lines, -6 removed lines patch added patch discarded remove patch
@@ -157,7 +157,7 @@  discard block
 block discarded – undo
157 157
 	                foreach($route['filters'] as $filter){
158 158
 	                    $this->filter->filtering($filter);
159 159
 	                }
160
-	            }else{
160
+	            } else{
161 161
 	                $this->filter->filtering($route['filters']);
162 162
 	            }
163 163
 	        }
@@ -173,7 +173,7 @@  discard block
 block discarded – undo
173 173
                         continue;
174 174
                     }
175 175
                 }
176
-	        }else{
176
+	        } else{
177 177
 				if($route['protocol'] !== $currentProtocol){
178 178
                     continue;
179 179
                 }
@@ -217,7 +217,7 @@  discard block
 block discarded – undo
217 217
 	                foreach($route['filters'] as $filter){
218 218
 	                    $this->filter->filtering($filter);
219 219
 	                }
220
-	            }else{
220
+	            } else{
221 221
 	                $this->filter->filtering($route['filters']);
222 222
 	            }
223 223
 	        }
@@ -241,7 +241,7 @@  discard block
 block discarded – undo
241 241
                 }
242 242
             }
243 243
             
244
-        }else{
244
+        } else{
245 245
             $currentRoute = self::getInstance()->addFilter(end(self::getInstance()->routers),$filters);
246 246
             self::getInstance()->routers[count(self::getInstance()->routers)-1] = $currentRoute;
247 247
         }
@@ -262,7 +262,7 @@  discard block
 block discarded – undo
262 262
             foreach ($route['filters'] as $key => $value) {
263 263
                 $filters[] = $value;
264 264
             }
265
-        }else{
265
+        } else{
266 266
             $filters[] = $route['filters'];
267 267
         }
268 268
 
@@ -308,7 +308,7 @@  discard block
 block discarded – undo
308 308
 
309 309
         if($isform){
310 310
             $this->ControllerForm($controller, $method, $data['POST']);
311
-        }else {
311
+        } else {
312 312
             $controller->$method($data);
313 313
         }
314 314
     }    
Please login to merge, or discard this patch.
examples/index.php 2 patches
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -7,11 +7,11 @@
 block discarded – undo
7 7
 
8 8
 /* NOTE: in case of error an exception is thrown */
9 9
 
10
-try{
10
+try {
11 11
     
12 12
     Router::create()->dispatch();
13 13
 
14
-}catch(Exception $er){
14
+}catch (Exception $er) {
15 15
 
16 16
     die($er->getCode().'  -  '.$er->getMessage());
17 17
 
Please login to merge, or discard this patch.
Braces   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -11,7 +11,7 @@
 block discarded – undo
11 11
     
12 12
     Router::create()->dispatch();
13 13
 
14
-}catch(Exception $er){
14
+} catch(Exception $er){
15 15
 
16 16
     die($er->getCode().'  -  '.$er->getMessage());
17 17
 
Please login to merge, or discard this patch.
examples/Routes/default.php 1 patch
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -2,4 +2,4 @@
 block discarded – undo
2 2
 
3 3
 use HnrAzevedo\Router\Router;
4 4
 
5
-Router::get('/','Application:index');
6 5
\ No newline at end of file
6
+Router::get('/', 'Application:index');
7 7
\ No newline at end of file
Please login to merge, or discard this patch.