@@ -570,12 +570,12 @@ discard block |
||
| 570 | 570 | |
| 571 | 571 | foreach ($replaces as $regex => $replace) |
| 572 | 572 | { |
| 573 | - if($customRegex) |
|
| 573 | + if ($customRegex) |
|
| 574 | 574 | continue; |
| 575 | 575 | |
| 576 | 576 | $matches = []; |
| 577 | 577 | |
| 578 | - if(preg_match('/^\{(.*)\}$/', $segment)) |
|
| 578 | + if (preg_match('/^\{(.*)\}$/', $segment)) |
|
| 579 | 579 | { |
| 580 | 580 | $foundedArgs[$key] = $segment; |
| 581 | 581 | } |
@@ -583,7 +583,7 @@ discard block |
||
| 583 | 583 | $c = 0; |
| 584 | 584 | $segment = preg_replace('/'.$regex.'/', $replace, $segment, 1, $c); |
| 585 | 585 | |
| 586 | - if( $regex == array_keys($replaces)[0] && $c > 0) |
|
| 586 | + if ($regex == array_keys($replaces)[0] && $c > 0) |
|
| 587 | 587 | $customRegex = TRUE; |
| 588 | 588 | } |
| 589 | 589 | } |
@@ -1018,7 +1018,7 @@ discard block |
||
| 1018 | 1018 | { |
| 1019 | 1019 | for ($i = 0; $i < count($e_findPath); $i++) |
| 1020 | 1020 | { |
| 1021 | - if(in_array($i, $skip_seg)) |
|
| 1021 | + if (in_array($i, $skip_seg)) |
|
| 1022 | 1022 | continue; |
| 1023 | 1023 | |
| 1024 | 1024 | if ($valid) |
@@ -146,11 +146,13 @@ discard block |
||
| 146 | 146 | $controller = $parsedController[1]; |
| 147 | 147 | $method = $parsedController[2]; |
| 148 | 148 | |
| 149 | - if (!is_string($path)) |
|
| 150 | - show_error('Route path must be a string ', 500, 'Route error: bad route path'); |
|
| 149 | + if (!is_string($path)) { |
|
| 150 | + show_error('Route path must be a string ', 500, 'Route error: bad route path'); |
|
| 151 | + } |
|
| 151 | 152 | |
| 152 | - if (!is_string($verb)) |
|
| 153 | - show_error('Route HTTP Verb must be a string', 500, 'Route error: bad verb type'); |
|
| 153 | + if (!is_string($verb)) { |
|
| 154 | + show_error('Route HTTP Verb must be a string', 500, 'Route error: bad verb type'); |
|
| 155 | + } |
|
| 154 | 156 | |
| 155 | 157 | $verb = strtoupper($verb); |
| 156 | 158 | |
@@ -186,30 +188,36 @@ discard block |
||
| 186 | 188 | $route['prefix'] = NULL; |
| 187 | 189 | $group_prefix = implode('/', self::$prefix); |
| 188 | 190 | |
| 189 | - if ($group_prefix) |
|
| 190 | - $route['prefix'] = $group_prefix.'/'; |
|
| 191 | + if ($group_prefix) { |
|
| 192 | + $route['prefix'] = $group_prefix.'/'; |
|
| 193 | + } |
|
| 191 | 194 | |
| 192 | - if (isset($attr['prefix'])) |
|
| 193 | - $route['prefix'] .= $attr['prefix']; |
|
| 195 | + if (isset($attr['prefix'])) { |
|
| 196 | + $route['prefix'] .= $attr['prefix']; |
|
| 197 | + } |
|
| 194 | 198 | |
| 195 | 199 | // Setting up the namespace |
| 196 | 200 | |
| 197 | 201 | $route['namespace'] = NULL; |
| 198 | 202 | $group_namespace = implode('/', self::$namespace); |
| 199 | 203 | |
| 200 | - if (!is_null($group_namespace)) |
|
| 201 | - $route['namespace'] = $group_namespace.'/'; |
|
| 202 | - if (isset($attr['namespace'])) |
|
| 203 | - $route['namespace'] .= $attr['namespace']; |
|
| 204 | + if (!is_null($group_namespace)) { |
|
| 205 | + $route['namespace'] = $group_namespace.'/'; |
|
| 206 | + } |
|
| 207 | + if (isset($attr['namespace'])) { |
|
| 208 | + $route['namespace'] .= $attr['namespace']; |
|
| 209 | + } |
|
| 204 | 210 | |
| 205 | 211 | $route['prefix'] = trim($route['prefix'], '/'); |
| 206 | 212 | $route['namespace'] = trim($route['namespace'], '/'); |
| 207 | 213 | |
| 208 | - if (empty($route['prefix'])) |
|
| 209 | - $route['prefix'] = NULL; |
|
| 214 | + if (empty($route['prefix'])) { |
|
| 215 | + $route['prefix'] = NULL; |
|
| 216 | + } |
|
| 210 | 217 | |
| 211 | - if (empty($route['namespace'])) |
|
| 212 | - $route['namespace'] = NULL; |
|
| 218 | + if (empty($route['namespace'])) { |
|
| 219 | + $route['namespace'] = NULL; |
|
| 220 | + } |
|
| 213 | 221 | |
| 214 | 222 | // Route middleware |
| 215 | 223 | $route['middleware'] = []; |
@@ -219,14 +227,14 @@ discard block |
||
| 219 | 227 | { |
| 220 | 228 | if (is_array($attr['middleware'])) |
| 221 | 229 | { |
| 222 | - foreach ($attr['middleware'] as $middleware) |
|
| 223 | - $route['middleware'][] = $middleware; # Group |
|
| 224 | - } |
|
| 225 | - elseif (is_string($attr['middleware'])) |
|
| 230 | + foreach ($attr['middleware'] as $middleware) { |
|
| 231 | + $route['middleware'][] = $middleware; |
|
| 232 | + } |
|
| 233 | + # Group |
|
| 234 | + } elseif (is_string($attr['middleware'])) |
|
| 226 | 235 | { |
| 227 | 236 | $route['middleware'][] = $attr['middleware']; # Group |
| 228 | - } |
|
| 229 | - else |
|
| 237 | + } else |
|
| 230 | 238 | { |
| 231 | 239 | show_error('Route middleware must be a string or an array', 500, 'Route error: bad middleware format'); |
| 232 | 240 | } |
@@ -398,8 +406,9 @@ discard block |
||
| 398 | 406 | */ |
| 399 | 407 | public static function matches($verbs, $url, $attr, $hideOriginal = FALSE) |
| 400 | 408 | { |
| 401 | - if (!is_array($verbs)) |
|
| 402 | - show_error('Route::matches() first argument must be an array of valid HTTP Verbs', 500, 'Route error: bad Route::matches() verb list'); |
|
| 409 | + if (!is_array($verbs)) { |
|
| 410 | + show_error('Route::matches() first argument must be an array of valid HTTP Verbs', 500, 'Route error: bad Route::matches() verb list'); |
|
| 411 | + } |
|
| 403 | 412 | |
| 404 | 413 | foreach ($verbs as $verb) |
| 405 | 414 | { |
@@ -427,19 +436,23 @@ discard block |
||
| 427 | 436 | |
| 428 | 437 | $hideOriginal = FALSE; |
| 429 | 438 | |
| 430 | - if (isset($attr['namespace'])) |
|
| 431 | - $base_attr['namespace'] = $attr['namespace']; |
|
| 439 | + if (isset($attr['namespace'])) { |
|
| 440 | + $base_attr['namespace'] = $attr['namespace']; |
|
| 441 | + } |
|
| 432 | 442 | |
| 433 | - if (isset($attr['middleware'])) |
|
| 434 | - $base_attr['middleware'] = $attr['middleware']; |
|
| 443 | + if (isset($attr['middleware'])) { |
|
| 444 | + $base_attr['middleware'] = $attr['middleware']; |
|
| 445 | + } |
|
| 435 | 446 | |
| 436 | - if (isset($attr['hideOriginal'])) |
|
| 437 | - $hideOriginal = (bool) $attr['hideOriginal']; |
|
| 447 | + if (isset($attr['hideOriginal'])) { |
|
| 448 | + $hideOriginal = (bool) $attr['hideOriginal']; |
|
| 449 | + } |
|
| 438 | 450 | |
| 439 | 451 | $base_attr['prefix'] = strtolower($name); |
| 440 | 452 | |
| 441 | - if (isset($attr['prefix'])) |
|
| 442 | - $base_attr['prefix'] = $attr['prefix']; |
|
| 453 | + if (isset($attr['prefix'])) { |
|
| 454 | + $base_attr['prefix'] = $attr['prefix']; |
|
| 455 | + } |
|
| 443 | 456 | |
| 444 | 457 | $only = []; |
| 445 | 458 | |
@@ -450,8 +463,7 @@ discard block |
||
| 450 | 463 | if (is_array($attr['only'])) |
| 451 | 464 | { |
| 452 | 465 | $only = $attr['only']; |
| 453 | - } |
|
| 454 | - else |
|
| 466 | + } else |
|
| 455 | 467 | { |
| 456 | 468 | $only[] = $attr['only']; |
| 457 | 469 | } |
@@ -528,13 +540,15 @@ discard block |
||
| 528 | 540 | |
| 529 | 541 | $path = $route->path; |
| 530 | 542 | |
| 531 | - if (!is_null($prefix)) |
|
| 532 | - $path = $prefix.'/'.$path; |
|
| 543 | + if (!is_null($prefix)) { |
|
| 544 | + $path = $prefix.'/'.$path; |
|
| 545 | + } |
|
| 533 | 546 | |
| 534 | 547 | $controller = $route->controller.'/'.$route->method; |
| 535 | 548 | |
| 536 | - if (!is_null($namespace)) |
|
| 537 | - $controller = $namespace.'/'.$controller; |
|
| 549 | + if (!is_null($namespace)) { |
|
| 550 | + $controller = $namespace.'/'.$controller; |
|
| 551 | + } |
|
| 538 | 552 | |
| 539 | 553 | $path = trim($path, '/'); |
| 540 | 554 | $controller = trim($controller, '/'); |
@@ -570,8 +584,9 @@ discard block |
||
| 570 | 584 | |
| 571 | 585 | foreach ($replaces as $regex => $replace) |
| 572 | 586 | { |
| 573 | - if($customRegex) |
|
| 574 | - continue; |
|
| 587 | + if($customRegex) { |
|
| 588 | + continue; |
|
| 589 | + } |
|
| 575 | 590 | |
| 576 | 591 | $matches = []; |
| 577 | 592 | |
@@ -583,8 +598,9 @@ discard block |
||
| 583 | 598 | $c = 0; |
| 584 | 599 | $segment = preg_replace('/'.$regex.'/', $replace, $segment, 1, $c); |
| 585 | 600 | |
| 586 | - if( $regex == array_keys($replaces)[0] && $c > 0) |
|
| 587 | - $customRegex = TRUE; |
|
| 601 | + if( $regex == array_keys($replaces)[0] && $c > 0) { |
|
| 602 | + $customRegex = TRUE; |
|
| 603 | + } |
|
| 588 | 604 | } |
| 589 | 605 | } |
| 590 | 606 | |
@@ -602,11 +618,11 @@ discard block |
||
| 602 | 618 | { |
| 603 | 619 | $args['optional'][] = $arg; |
| 604 | 620 | $argConstraint = TRUE; |
| 605 | - } |
|
| 606 | - else |
|
| 621 | + } else |
|
| 607 | 622 | { |
| 608 | - if ($argConstraint) |
|
| 609 | - show_error('Optional route path argument not valid at this position', 500, 'Route error'); |
|
| 623 | + if ($argConstraint) { |
|
| 624 | + show_error('Optional route path argument not valid at this position', 500, 'Route error'); |
|
| 625 | + } |
|
| 610 | 626 | $args['required'][] = $arg; |
| 611 | 627 | } |
| 612 | 628 | } |
@@ -718,12 +734,14 @@ discard block |
||
| 718 | 734 | $path = key($route); |
| 719 | 735 | $_404 = $route[$path]; |
| 720 | 736 | |
| 721 | - if (!isset($routes[$path])) |
|
| 722 | - $routes[$path] = $_404; |
|
| 737 | + if (!isset($routes[$path])) { |
|
| 738 | + $routes[$path] = $_404; |
|
| 739 | + } |
|
| 723 | 740 | } |
| 724 | 741 | |
| 725 | - if (is_null(self::$defaultController)) |
|
| 726 | - show_error('You must specify a home route: Route::home() as default controller!', 500, 'Route error: missing default controller'); |
|
| 742 | + if (is_null(self::$defaultController)) { |
|
| 743 | + show_error('You must specify a home route: Route::home() as default controller!', 500, 'Route error: missing default controller'); |
|
| 744 | + } |
|
| 727 | 745 | |
| 728 | 746 | $defaultController = self::$defaultController->compiled; |
| 729 | 747 | $defaultController = $defaultController[key($defaultController)]; |
@@ -757,11 +775,13 @@ discard block |
||
| 757 | 775 | */ |
| 758 | 776 | public static function group($attr, $routes) |
| 759 | 777 | { |
| 760 | - if (!is_array($attr)) |
|
| 761 | - show_error('Group attribute must be a valid array'); |
|
| 778 | + if (!is_array($attr)) { |
|
| 779 | + show_error('Group attribute must be a valid array'); |
|
| 780 | + } |
|
| 762 | 781 | |
| 763 | - if (!isset($attr['prefix'])) |
|
| 764 | - show_error('You must specify an prefix!'); |
|
| 782 | + if (!isset($attr['prefix'])) { |
|
| 783 | + show_error('You must specify an prefix!'); |
|
| 784 | + } |
|
| 765 | 785 | |
| 766 | 786 | self::$prefix[] = $attr['prefix']; |
| 767 | 787 | |
@@ -784,15 +804,14 @@ discard block |
||
| 784 | 804 | { |
| 785 | 805 | if (is_array($attr['middleware']) && !empty($attr['middleware'])) |
| 786 | 806 | { |
| 787 | - foreach ($attr['middleware'] as $middleware) |
|
| 788 | - self::$middleware[] = $middleware; |
|
| 789 | - } |
|
| 790 | - else |
|
| 807 | + foreach ($attr['middleware'] as $middleware) { |
|
| 808 | + self::$middleware[] = $middleware; |
|
| 809 | + } |
|
| 810 | + } else |
|
| 791 | 811 | { |
| 792 | 812 | self::$middleware[] = $attr['middleware']; |
| 793 | 813 | } |
| 794 | - } |
|
| 795 | - else |
|
| 814 | + } else |
|
| 796 | 815 | { |
| 797 | 816 | show_error('Group middleware must be an array o a string', 500, 'Route error'); |
| 798 | 817 | } |
@@ -824,14 +843,17 @@ discard block |
||
| 824 | 843 | 'as' => $as |
| 825 | 844 | ]; |
| 826 | 845 | |
| 827 | - if (!is_null($attr) && !is_array($attr)) |
|
| 828 | - show_error('Default controller attributes must be an array', 500, 'Route error: bad attribute type'); |
|
| 846 | + if (!is_null($attr) && !is_array($attr)) { |
|
| 847 | + show_error('Default controller attributes must be an array', 500, 'Route error: bad attribute type'); |
|
| 848 | + } |
|
| 829 | 849 | |
| 830 | - if (!is_null($attr)) |
|
| 831 | - $routeAttr = array_merge($routeAttr, $attr); |
|
| 850 | + if (!is_null($attr)) { |
|
| 851 | + $routeAttr = array_merge($routeAttr, $attr); |
|
| 852 | + } |
|
| 832 | 853 | |
| 833 | - if (isset($attr['prefix'])) |
|
| 834 | - show_error('Default controller may not have a prefix!', 500, 'Route error: prefix not allowed'); |
|
| 854 | + if (isset($attr['prefix'])) { |
|
| 855 | + show_error('Default controller may not have a prefix!', 500, 'Route error: prefix not allowed'); |
|
| 856 | + } |
|
| 835 | 857 | |
| 836 | 858 | self::$defaultController = self::$routes[] = self::add('GET', '/', ['uses' => $controller, 'as' => $as], TRUE, TRUE); |
| 837 | 859 | } |
@@ -850,14 +872,14 @@ discard block |
||
| 850 | 872 | if (is_null($verb)) |
| 851 | 873 | { |
| 852 | 874 | return self::$routes; |
| 853 | - } |
|
| 854 | - else |
|
| 875 | + } else |
|
| 855 | 876 | { |
| 856 | 877 | $routes = []; |
| 857 | 878 | foreach (self::$routes as $route) |
| 858 | 879 | { |
| 859 | - if ($route->verb == $verb) |
|
| 860 | - $routes[] = $route; |
|
| 880 | + if ($route->verb == $verb) { |
|
| 881 | + $routes[] = $route; |
|
| 882 | + } |
|
| 861 | 883 | } |
| 862 | 884 | return $routes; |
| 863 | 885 | } |
@@ -954,18 +976,21 @@ discard block |
||
| 954 | 976 | */ |
| 955 | 977 | public static function getRouteByPath($path, $requestMethod = NULL) |
| 956 | 978 | { |
| 957 | - if (is_null($requestMethod)) |
|
| 958 | - $requestMethod = $_SERVER['REQUEST_METHOD']; |
|
| 979 | + if (is_null($requestMethod)) { |
|
| 980 | + $requestMethod = $_SERVER['REQUEST_METHOD']; |
|
| 981 | + } |
|
| 959 | 982 | |
| 960 | 983 | $routes = self::getRoutes($requestMethod); |
| 961 | 984 | |
| 962 | - if (empty($routes)) |
|
| 963 | - return FALSE; |
|
| 985 | + if (empty($routes)) { |
|
| 986 | + return FALSE; |
|
| 987 | + } |
|
| 964 | 988 | |
| 965 | 989 | $path = trim($path); |
| 966 | 990 | |
| 967 | - if ($path == '') |
|
| 968 | - return self::$defaultController; |
|
| 991 | + if ($path == '') { |
|
| 992 | + return self::$defaultController; |
|
| 993 | + } |
|
| 969 | 994 | |
| 970 | 995 | $wildcards = |
| 971 | 996 | [ |
@@ -991,10 +1016,10 @@ discard block |
||
| 991 | 1016 | |
| 992 | 1017 | if ($mode == 'exact') |
| 993 | 1018 | { |
| 994 | - if ($findPath == $compiledPath) |
|
| 995 | - return $route; |
|
| 996 | - } |
|
| 997 | - else |
|
| 1019 | + if ($findPath == $compiledPath) { |
|
| 1020 | + return $route; |
|
| 1021 | + } |
|
| 1022 | + } else |
|
| 998 | 1023 | { |
| 999 | 1024 | $e_findPath = explode('/', $findPath); |
| 1000 | 1025 | $e_compiledPath = explode('/', $compiledPath); |
@@ -1010,24 +1035,28 @@ discard block |
||
| 1010 | 1035 | $reg = preg_replace($wildcards, $replaces, $e_compiledPath[$i], -1, $count); |
| 1011 | 1036 | $valid = (bool) preg_match('#^'.$reg.'$#', $e_findPath[$i]); |
| 1012 | 1037 | |
| 1013 | - if ($valid && $count > 0) |
|
| 1014 | - $skip_seg[] = $i; |
|
| 1038 | + if ($valid && $count > 0) { |
|
| 1039 | + $skip_seg[] = $i; |
|
| 1040 | + } |
|
| 1015 | 1041 | } |
| 1016 | 1042 | |
| 1017 | 1043 | if ($valid) |
| 1018 | 1044 | { |
| 1019 | 1045 | for ($i = 0; $i < count($e_findPath); $i++) |
| 1020 | 1046 | { |
| 1021 | - if(in_array($i, $skip_seg)) |
|
| 1022 | - continue; |
|
| 1047 | + if(in_array($i, $skip_seg)) { |
|
| 1048 | + continue; |
|
| 1049 | + } |
|
| 1023 | 1050 | |
| 1024 | - if ($valid) |
|
| 1025 | - $valid = $e_findPath[$i] == $e_compiledPath[$i]; |
|
| 1051 | + if ($valid) { |
|
| 1052 | + $valid = $e_findPath[$i] == $e_compiledPath[$i]; |
|
| 1053 | + } |
|
| 1026 | 1054 | } |
| 1027 | 1055 | } |
| 1028 | 1056 | |
| 1029 | - if ($valid) |
|
| 1030 | - return $route; |
|
| 1057 | + if ($valid) { |
|
| 1058 | + return $route; |
|
| 1059 | + } |
|
| 1031 | 1060 | } |
| 1032 | 1061 | } |
| 1033 | 1062 | } |
@@ -1061,8 +1090,9 @@ discard block |
||
| 1061 | 1090 | |
| 1062 | 1091 | for ($s = 0; $s < count($r_seg); $s++) |
| 1063 | 1092 | { |
| 1064 | - if (!isset($p_seg[$s])) |
|
| 1065 | - continue; |
|
| 1093 | + if (!isset($p_seg[$s])) { |
|
| 1094 | + continue; |
|
| 1095 | + } |
|
| 1066 | 1096 | |
| 1067 | 1097 | if ($r_seg[$s] != $p_seg[$s]) |
| 1068 | 1098 | { |