Completed
Push — master ( 546896...e1a197 )
by Anderson
02:36 queued 33s
created
src/Core/Route.php 1 patch
Braces   +106 added lines, -86 removed lines patch added patch discarded remove patch
@@ -140,11 +140,13 @@  discard block
 block discarded – undo
140 140
         $controller = $parsedController[1];
141 141
         $method     = $parsedController[2];
142 142
 
143
-        if(!is_string($path))
144
-            show_error('Route path must be a string ', 500, 'Route error: bad route path');
143
+        if(!is_string($path)) {
144
+                    show_error('Route path must be a string ', 500, 'Route error: bad route path');
145
+        }
145 146
 
146
-        if(!is_string($verb))
147
-            show_error('Route HTTP Verb must be a string', 500, 'Route error: bad verb type');
147
+        if(!is_string($verb)) {
148
+                    show_error('Route HTTP Verb must be a string', 500, 'Route error: bad verb type');
149
+        }
148 150
 
149 151
         $verb = strtoupper($verb);
150 152
 
@@ -170,8 +172,7 @@  discard block
 block discarded – undo
170 172
         if(isset($attr['as']))
171 173
         {
172 174
             $route['name'] = $attr['as'];
173
-        }
174
-        else
175
+        } else
175 176
         {
176 177
             $route['name'] = NULL;
177 178
         }
@@ -181,30 +182,36 @@  discard block
 block discarded – undo
181 182
         $route['prefix'] = NULL;
182 183
         $group_prefix = end(self::$prefix);
183 184
 
184
-        if($group_prefix)
185
-            $route['prefix'] = $group_prefix.'/';
185
+        if($group_prefix) {
186
+                    $route['prefix'] = $group_prefix.'/';
187
+        }
186 188
 
187
-        if(isset($attr['prefix']))
188
-            $route['prefix'] .= $attr['prefix'];
189
+        if(isset($attr['prefix'])) {
190
+                    $route['prefix'] .= $attr['prefix'];
191
+        }
189 192
 
190 193
         // Setting up the namespace
191 194
 
192 195
         $route['namespace'] = NULL;
193 196
         $group_namespace = end(self::$namespace);
194 197
 
195
-        if(!is_null($group_namespace))
196
-            $route['namespace'] = $group_namespace.'/';
197
-        if(isset($attr['namespace']))
198
-            $route['namespace'] .= $attr['namespace'];
198
+        if(!is_null($group_namespace)) {
199
+                    $route['namespace'] = $group_namespace.'/';
200
+        }
201
+        if(isset($attr['namespace'])) {
202
+                    $route['namespace'] .= $attr['namespace'];
203
+        }
199 204
 
200 205
         $route['prefix']    = trim($route['prefix'], '/');
201 206
         $route['namespace'] = trim($route['namespace'],'/');
202 207
 
203
-        if(empty($route['prefix']))
204
-            $route['prefix'] = NULL;
208
+        if(empty($route['prefix'])) {
209
+                    $route['prefix'] = NULL;
210
+        }
205 211
 
206
-        if(empty($route['namespace']))
207
-            $route['namespace'] = NULL;
212
+        if(empty($route['namespace'])) {
213
+                    $route['namespace'] = NULL;
214
+        }
208 215
 
209 216
         $route['middleware'] = array();
210 217
 
@@ -212,14 +219,14 @@  discard block
 block discarded – undo
212 219
         {
213 220
             if(is_array($attr['middleware']))
214 221
             {
215
-                foreach($attr['middleware'] as $middleware)
216
-                    $route['middleware'][] = $middleware; # Group
217
-            }
218
-            elseif( is_string($attr['middleware']))
222
+                foreach($attr['middleware'] as $middleware) {
223
+                                    $route['middleware'][] = $middleware;
224
+                }
225
+                # Group
226
+            } elseif( is_string($attr['middleware']))
219 227
             {
220 228
                 $route['middleware'][] = $attr['middleware']; # Group
221
-            }
222
-            else
229
+            } else
223 230
             {
224 231
                 show_error('Route middleware must be a string or an array',500,'Route error: bad middleware format');
225 232
             }
@@ -257,8 +264,7 @@  discard block
 block discarded – undo
257 264
         if(!$return)
258 265
         {
259 266
             self::$routes[] = (object) $route;
260
-        }
261
-        else
267
+        } else
262 268
         {
263 269
             return (object) $route;
264 270
         }
@@ -385,8 +391,9 @@  discard block
 block discarded – undo
385 391
      */
386 392
     public static function matches($verbs, $url, $attr, $hideOriginal = FALSE)
387 393
     {
388
-        if(!is_array($verbs))
389
-            show_error('Route::matches() first argument must be an array of valid HTTP Verbs', 500, 'Route error: bad Route::matches() verb list');
394
+        if(!is_array($verbs)) {
395
+                    show_error('Route::matches() first argument must be an array of valid HTTP Verbs', 500, 'Route error: bad Route::matches() verb list');
396
+        }
390 397
 
391 398
         foreach($verbs as $verb)
392 399
         {
@@ -423,19 +430,23 @@  discard block
 block discarded – undo
423 430
 
424 431
         $hideOriginal = FALSE;
425 432
 
426
-        if(isset($attr['namespace']))
427
-            $base_attr['namespace']  = $attr['namespace'];
433
+        if(isset($attr['namespace'])) {
434
+                    $base_attr['namespace']  = $attr['namespace'];
435
+        }
428 436
 
429
-        if(isset($attr['middleware']))
430
-            $base_attr['middleware'] = $attr['middleware'];
437
+        if(isset($attr['middleware'])) {
438
+                    $base_attr['middleware'] = $attr['middleware'];
439
+        }
431 440
 
432
-        if(isset($attr['hideOriginal']))
433
-            $hideOriginal = (bool) $attr['hideOriginal'];
441
+        if(isset($attr['hideOriginal'])) {
442
+                    $hideOriginal = (bool) $attr['hideOriginal'];
443
+        }
434 444
 
435 445
         $base_attr['prefix'] = strtolower($name);
436 446
 
437
-        if(isset($attr['prefix']))
438
-            $base_attr['prefix']  = $attr['prefix'];
447
+        if(isset($attr['prefix'])) {
448
+                    $base_attr['prefix']  = $attr['prefix'];
449
+        }
439 450
 
440 451
         $only = array();
441 452
 
@@ -446,8 +457,7 @@  discard block
 block discarded – undo
446 457
             if(is_array($attr['only']))
447 458
             {
448 459
                 $only  = $attr['only'];
449
-            }
450
-            else
460
+            } else
451 461
             {
452 462
                 $only[] = $attr['only'];
453 463
             }
@@ -523,8 +533,9 @@  discard block
 block discarded – undo
523 533
 
524 534
         $path = $route->path;
525 535
 
526
-        if(!is_null($prefix))
527
-            $path = $prefix.'/'.$path;
536
+        if(!is_null($prefix)) {
537
+                    $path = $prefix.'/'.$path;
538
+        }
528 539
 
529 540
         /*
530 541
         if(substr($path, 0, 1) == "/" && strlen($path) > 1)
@@ -533,8 +544,9 @@  discard block
 block discarded – undo
533 544
 
534 545
         $controller = $route->controller.'/'.$route->method;
535 546
 
536
-        if(!is_null($namespace))
537
-            $controller = $namespace.'/'.$controller;
547
+        if(!is_null($namespace)) {
548
+                    $controller = $namespace.'/'.$controller;
549
+        }
538 550
 
539 551
         $path       = trim($path,'/');
540 552
         $controller = trim($controller,'/');
@@ -572,11 +584,11 @@  discard block
 block discarded – undo
572 584
             {
573 585
                 $args['optional'][] = $arg;
574 586
                 $argConstraint = TRUE;
575
-            }
576
-            else
587
+            } else
577 588
             {
578
-                if($argConstraint)
579
-                    show_error('Optional route path argument not valid at this position', 500, 'Route error');
589
+                if($argConstraint) {
590
+                                    show_error('Optional route path argument not valid at this position', 500, 'Route error');
591
+                }
580 592
                 $args['required'][] = $arg;
581 593
             }
582 594
         }
@@ -625,12 +637,14 @@  discard block
 block discarded – undo
625 637
             $path = key($route);
626 638
             $_404 = $route[$path];
627 639
 
628
-            if(!isset($routes[$path]))
629
-                $routes[$path] = $_404;
640
+            if(!isset($routes[$path])) {
641
+                            $routes[$path] = $_404;
642
+            }
630 643
         }
631 644
 
632
-        if(is_null(self::$defaultController))
633
-            show_error('You must specify a home route: Route::home() as default controller!', 500, 'Route error: missing default controller');
645
+        if(is_null(self::$defaultController)) {
646
+                    show_error('You must specify a home route: Route::home() as default controller!', 500, 'Route error: missing default controller');
647
+        }
634 648
 
635 649
         $defaultController = self::$defaultController->compiled;
636 650
         $defaultController = $defaultController[key($defaultController)];
@@ -640,8 +654,7 @@  discard block
 block discarded – undo
640 654
         if(is_null(self::$_404page))
641 655
         {
642 656
             $routes['404_override'] = '';
643
-        }
644
-        else
657
+        } else
645 658
         {
646 659
             $routes['404_override'] = self::$_404page->controller;
647 660
         }
@@ -664,11 +677,13 @@  discard block
 block discarded – undo
664 677
      */
665 678
     public static function group($attr, $routes)
666 679
     {
667
-        if(!is_array($attr))
668
-            show_error('Group attribute must be a valid array');
680
+        if(!is_array($attr)) {
681
+                    show_error('Group attribute must be a valid array');
682
+        }
669 683
 
670
-        if(!isset($attr['prefix']))
671
-            show_error('You must specify an prefix!');
684
+        if(!isset($attr['prefix'])) {
685
+                    show_error('You must specify an prefix!');
686
+        }
672 687
 
673 688
         self::$prefix[] = $attr['prefix'];
674 689
 
@@ -680,8 +695,7 @@  discard block
 block discarded – undo
680 695
         if(isset($attr['hideOriginals']) && $attr['hideOriginals'] === TRUE)
681 696
         {
682 697
             self::$hideOriginals[] = TRUE;
683
-        }
684
-        else
698
+        } else
685 699
         {
686 700
             self::$hideOriginals[] = FALSE;
687 701
         }
@@ -692,15 +706,14 @@  discard block
 block discarded – undo
692 706
             {
693 707
                 if(is_array($attr['middleware']) && !empty($attr['middleware']))
694 708
                 {
695
-                    foreach($attr['middleware'] as $middleware)
696
-                        self::$groupMiddleware[] = [ $attr['prefix'] => $middleware ];
697
-                }
698
-                else
709
+                    foreach($attr['middleware'] as $middleware) {
710
+                                            self::$groupMiddleware[] = [ $attr['prefix'] => $middleware ];
711
+                    }
712
+                } else
699 713
                 {
700 714
                     self::$groupMiddleware[] = [ $attr['prefix'] => $attr['middleware'] ];
701 715
                 }
702
-            }
703
-            else
716
+            } else
704 717
             {
705 718
                 show_error('Group middleware not valid');
706 719
             }
@@ -734,14 +747,17 @@  discard block
 block discarded – undo
734 747
                 'as'   => $as
735 748
             ];
736 749
 
737
-        if(!is_null($attr) && !is_array($attr))
738
-            show_error('Default controller attributes must be an array',500,'Route error: bad attribute type');
750
+        if(!is_null($attr) && !is_array($attr)) {
751
+                    show_error('Default controller attributes must be an array',500,'Route error: bad attribute type');
752
+        }
739 753
 
740
-        if(!is_null($attr))
741
-            $routeAttr = array_merge($routeAttr,$attr);
754
+        if(!is_null($attr)) {
755
+                    $routeAttr = array_merge($routeAttr,$attr);
756
+        }
742 757
 
743
-        if(isset($attr['prefix']))
744
-            show_error('Default controller may not have a prefix!',500,'Route error: prefix not allowed');
758
+        if(isset($attr['prefix'])) {
759
+                    show_error('Default controller may not have a prefix!',500,'Route error: prefix not allowed');
760
+        }
745 761
 
746 762
         self::$defaultController = self::$routes[] = self::add('GET', '/', ['uses' => $controller, 'as' => $as],TRUE, TRUE);
747 763
     }
@@ -759,14 +775,14 @@  discard block
 block discarded – undo
759 775
         if(is_null($verb))
760 776
         {
761 777
             return self::$routes;
762
-        }
763
-        else
778
+        } else
764 779
         {
765 780
             $routes = [];
766 781
             foreach(self::$routes as $route)
767 782
             {
768
-                if($route->verb == $verb)
769
-                    $routes[] = $route;
783
+                if($route->verb == $verb) {
784
+                                    $routes[] = $route;
785
+                }
770 786
             }
771 787
             return $routes;
772 788
         }
@@ -875,21 +891,24 @@  discard block
 block discarded – undo
875 891
      */
876 892
     public static function getRouteByPath($path, $requestMethod = NULL)
877 893
     {
878
-        if(is_null($requestMethod))
879
-            $requestMethod = $_SERVER['REQUEST_METHOD'];
894
+        if(is_null($requestMethod)) {
895
+                    $requestMethod = $_SERVER['REQUEST_METHOD'];
896
+        }
880 897
 
881 898
         $routes = self::getRoutes($requestMethod);
882 899
 
883
-        if(empty($routes))
884
-            return FALSE;
900
+        if(empty($routes)) {
901
+                    return FALSE;
902
+        }
885 903
 
886 904
         $founded = FALSE;
887 905
         $matches = array();
888 906
 
889 907
         $path = trim($path);
890 908
 
891
-        if($path == '')
892
-            return self::$defaultController;
909
+        if($path == '') {
910
+                    return self::$defaultController;
911
+        }
893 912
 
894 913
         $wildcards =
895 914
             [
@@ -916,10 +935,10 @@  discard block
 block discarded – undo
916 935
 
917 936
                     if($mode == 'exact')
918 937
                     {
919
-                        if($findPath == $compiledPath)
920
-                            return $route;
921
-                    }
922
-                    else
938
+                        if($findPath == $compiledPath) {
939
+                                                    return $route;
940
+                        }
941
+                    } else
923 942
                     {
924 943
                         $e_findPath     = explode('/', $findPath);
925 944
                         $e_compiledPath = explode('/', $compiledPath);
@@ -933,8 +952,9 @@  discard block
 block discarded – undo
933 952
 
934 953
                                 $valid = (bool) preg_match('#^'.$reg.'$#', $e_findPath[$i]);
935 954
                             }
936
-                            if($valid)
937
-                                return $route;
955
+                            if($valid) {
956
+                                                            return $route;
957
+                            }
938 958
                         }
939 959
                     }
940 960
                 }
Please login to merge, or discard this patch.