Completed
Push — master ( 645ffa...19a002 )
by Anderson
02:01
created
src/Core/Middleware.php 1 patch
Braces   +18 added lines, -12 removed lines patch added patch discarded remove patch
@@ -96,8 +96,9 @@  discard block
 block discarded – undo
96 96
         {
97 97
             foreach ($currentRoute->middleware as $middleware)
98 98
             {
99
-                if (!in_array($middleware, $_run))
100
-                    $_run[] = $middleware;
99
+                if (!in_array($middleware, $_run)) {
100
+                                    $_run[] = $middleware;
101
+                }
101 102
             }
102 103
         }
103 104
 
@@ -125,31 +126,36 @@  discard block
 block discarded – undo
125 126
 
126 127
         $middlewareName = ucfirst($middlewareName).'_middleware';
127 128
 
128
-        if (!file_exists($middlewareDir))
129
-            show_error('Unable to find (or read) the middleware folder: "'.$middlewareDir.'"');
129
+        if (!file_exists($middlewareDir)) {
130
+                    show_error('Unable to find (or read) the middleware folder: "'.$middlewareDir.'"');
131
+        }
130 132
 
131 133
         $runMiddleware = $middlewareDir.$middlewareName.'.php';
132 134
 
133
-        if (!file_exists($runMiddleware))
134
-            show_error('Unable to find (or read) the middleware "'.$runMiddleware.'"');
135
+        if (!file_exists($runMiddleware)) {
136
+                    show_error('Unable to find (or read) the middleware "'.$runMiddleware.'"');
137
+        }
135 138
 
136 139
         require $runMiddleware;
137 140
 
138
-        if (!class_exists($middlewareName))
139
-            show_error('Class "'.$middlewareName.'" not found');
141
+        if (!class_exists($middlewareName)) {
142
+                    show_error('Class "'.$middlewareName.'" not found');
143
+        }
140 144
 
141 145
         $middleware = new $middlewareName();
142 146
 
143 147
         // Call the current controller __beforeMiddleware() method, if exists:
144
-        if (method_exists(self::$instance, '_beforeMiddleware'))
145
-            self::$instance->_beforeMiddleware();
148
+        if (method_exists(self::$instance, '_beforeMiddleware')) {
149
+                    self::$instance->_beforeMiddleware();
150
+        }
146 151
 
147 152
         // Run the middleware
148 153
         $middleware->run();
149 154
 
150 155
         // Call the current controller _afterMiddleware() method, if exists:
151
-        if (method_exists(self::$instance, '_afterMiddleware'))
152
-            self::$instance->_afterMiddleware();
156
+        if (method_exists(self::$instance, '_afterMiddleware')) {
157
+                    self::$instance->_afterMiddleware();
158
+        }
153 159
 
154 160
     }
155 161
 }
Please login to merge, or discard this patch.
src/Core/Route.php 1 patch
Braces   +111 added lines, -84 removed lines patch added patch discarded remove patch
@@ -146,11 +146,13 @@  discard block
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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, '/');
@@ -589,11 +603,11 @@  discard block
 block discarded – undo
589 603
             {
590 604
                 $args['optional'][] = $arg;
591 605
                 $argConstraint = TRUE;
592
-            }
593
-            else
606
+            } else
594 607
             {
595
-                if ($argConstraint)
596
-                    show_error('Optional route path argument not valid at this position', 500, 'Route error');
608
+                if ($argConstraint) {
609
+                                    show_error('Optional route path argument not valid at this position', 500, 'Route error');
610
+                }
597 611
                 $args['required'][] = $arg;
598 612
             }
599 613
         }
@@ -703,12 +717,14 @@  discard block
 block discarded – undo
703 717
             $path = key($route);
704 718
             $_404 = $route[$path];
705 719
 
706
-            if (!isset($routes[$path]))
707
-                $routes[$path] = $_404;
720
+            if (!isset($routes[$path])) {
721
+                            $routes[$path] = $_404;
722
+            }
708 723
         }
709 724
 
710
-        if (is_null(self::$defaultController))
711
-            show_error('You must specify a home route: Route::home() as default controller!', 500, 'Route error: missing default controller');
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');
727
+        }
712 728
 
713 729
         $defaultController = self::$defaultController->compiled;
714 730
         $defaultController = $defaultController[key($defaultController)];
@@ -742,11 +758,13 @@  discard block
 block discarded – undo
742 758
      */
743 759
     public static function group($attr, $routes)
744 760
     {
745
-        if (!is_array($attr))
746
-            show_error('Group attribute must be a valid array');
761
+        if (!is_array($attr)) {
762
+                    show_error('Group attribute must be a valid array');
763
+        }
747 764
 
748
-        if (!isset($attr['prefix']))
749
-            show_error('You must specify an prefix!');
765
+        if (!isset($attr['prefix'])) {
766
+                    show_error('You must specify an prefix!');
767
+        }
750 768
 
751 769
         self::$prefix[] = $attr['prefix'];
752 770
 
@@ -769,15 +787,14 @@  discard block
 block discarded – undo
769 787
             {
770 788
                 if (is_array($attr['middleware']) && !empty($attr['middleware']))
771 789
                 {
772
-                    foreach ($attr['middleware'] as $middleware)
773
-                        self::$middleware[] = $middleware;
774
-                }
775
-                else
790
+                    foreach ($attr['middleware'] as $middleware) {
791
+                                            self::$middleware[] = $middleware;
792
+                    }
793
+                } else
776 794
                 {
777 795
                     self::$middleware[] = $attr['middleware'];
778 796
                 }
779
-            }
780
-            else
797
+            } else
781 798
             {
782 799
                 show_error('Group middleware must be an array o a string', 500, 'Route error');
783 800
             }
@@ -809,14 +826,17 @@  discard block
 block discarded – undo
809 826
                 'as'   => $as
810 827
             ];
811 828
 
812
-        if (!is_null($attr) && !is_array($attr))
813
-            show_error('Default controller attributes must be an array', 500, 'Route error: bad attribute type');
829
+        if (!is_null($attr) && !is_array($attr)) {
830
+                    show_error('Default controller attributes must be an array', 500, 'Route error: bad attribute type');
831
+        }
814 832
 
815
-        if (!is_null($attr))
816
-            $routeAttr = array_merge($routeAttr, $attr);
833
+        if (!is_null($attr)) {
834
+                    $routeAttr = array_merge($routeAttr, $attr);
835
+        }
817 836
 
818
-        if (isset($attr['prefix']))
819
-            show_error('Default controller may not have a prefix!', 500, 'Route error: prefix not allowed');
837
+        if (isset($attr['prefix'])) {
838
+                    show_error('Default controller may not have a prefix!', 500, 'Route error: prefix not allowed');
839
+        }
820 840
 
821 841
         self::$defaultController = self::$routes[] = self::add('GET', '/', ['uses' => $controller, 'as' => $as], TRUE, TRUE);
822 842
     }
@@ -835,14 +855,14 @@  discard block
 block discarded – undo
835 855
         if (is_null($verb))
836 856
         {
837 857
             return self::$routes;
838
-        }
839
-        else
858
+        } else
840 859
         {
841 860
             $routes = [];
842 861
             foreach (self::$routes as $route)
843 862
             {
844
-                if ($route->verb == $verb)
845
-                    $routes[] = $route;
863
+                if ($route->verb == $verb) {
864
+                                    $routes[] = $route;
865
+                }
846 866
             }
847 867
             return $routes;
848 868
         }
@@ -939,18 +959,21 @@  discard block
 block discarded – undo
939 959
      */
940 960
     public static function getRouteByPath($path, $requestMethod = NULL)
941 961
     {
942
-        if (is_null($requestMethod))
943
-            $requestMethod = $_SERVER['REQUEST_METHOD'];
962
+        if (is_null($requestMethod)) {
963
+                    $requestMethod = $_SERVER['REQUEST_METHOD'];
964
+        }
944 965
 
945 966
         $routes = self::getRoutes($requestMethod);
946 967
 
947
-        if (empty($routes))
948
-            return FALSE;
968
+        if (empty($routes)) {
969
+                    return FALSE;
970
+        }
949 971
 
950 972
         $path = trim($path);
951 973
 
952
-        if ($path == '')
953
-            return self::$defaultController;
974
+        if ($path == '') {
975
+                    return self::$defaultController;
976
+        }
954 977
 
955 978
         $wildcards =
956 979
             [
@@ -976,10 +999,10 @@  discard block
 block discarded – undo
976 999
 
977 1000
                     if ($mode == 'exact')
978 1001
                     {
979
-                        if ($findPath == $compiledPath)
980
-                            return $route;
981
-                    }
982
-                    else
1002
+                        if ($findPath == $compiledPath) {
1003
+                                                    return $route;
1004
+                        }
1005
+                    } else
983 1006
                     {
984 1007
                         $e_findPath     = explode('/', $findPath);
985 1008
                         $e_compiledPath = explode('/', $compiledPath);
@@ -997,18 +1020,21 @@  discard block
 block discarded – undo
997 1020
 
998 1021
                                 $valid = (bool) preg_match('#^'.$reg.'$#', $e_findPath[$i]);
999 1022
 
1000
-                                if ($valid && is_null($seachUntil))
1001
-                                    $seachUntil = $i;
1023
+                                if ($valid && is_null($seachUntil)) {
1024
+                                                                    $seachUntil = $i;
1025
+                                }
1002 1026
                             }
1003 1027
 
1004 1028
                             if ($valid)
1005 1029
                             {
1006
-                                for ($i = 0; $i < $seachUntil; $i++)
1007
-                                    $valid = $e_findPath[$i] == $e_compiledPath[$i];
1030
+                                for ($i = 0; $i < $seachUntil; $i++) {
1031
+                                                                    $valid = $e_findPath[$i] == $e_compiledPath[$i];
1032
+                                }
1008 1033
                             }
1009 1034
 
1010
-                            if ($valid)
1011
-                                return $route;
1035
+                            if ($valid) {
1036
+                                                            return $route;
1037
+                            }
1012 1038
                         }
1013 1039
                     }
1014 1040
                 }
@@ -1042,8 +1068,9 @@  discard block
 block discarded – undo
1042 1068
 
1043 1069
         for ($s = 0; $s < count($r_seg); $s++)
1044 1070
         {
1045
-            if (!isset($p_seg[$s]))
1046
-                continue;
1071
+            if (!isset($p_seg[$s])) {
1072
+                            continue;
1073
+            }
1047 1074
 
1048 1075
             if ($r_seg[$s] != $p_seg[$s])
1049 1076
             {
Please login to merge, or discard this patch.
src/Middleware/Request.php 2 patches
Spacing   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -117,16 +117,16 @@
 block discarded – undo
117 117
 
118 118
                 // Redirect to 404 if not enough parameters provided
119 119
 
120
-                if(count($path_args) < count($route_args['required']))
120
+                if (count($path_args) < count($route_args['required']))
121 121
                     redirect(Route::get404()->path);
122 122
 
123
-                if(count($path_args) == 0)
123
+                if (count($path_args) == 0)
124 124
                 {
125 125
                     $this->CI->{$this->route->method}();
126 126
                 }
127 127
                 else
128 128
                 {
129
-                    call_user_func_array( [$this->CI, $this->route->method], array_values($path_args) );
129
+                    call_user_func_array([$this->CI, $this->route->method], array_values($path_args));
130 130
                 }
131 131
 
132 132
 
Please login to merge, or discard this patch.
Braces   +16 added lines, -16 removed lines patch added patch discarded remove patch
@@ -64,20 +64,22 @@  discard block
 block discarded – undo
64 64
 
65 65
         // FIXME: Solve ambiguity here! POST with _method="GET" makes no sense
66 66
 
67
-        if (isset($_POST['_method']) && in_array(strtoupper($_POST['_method']), $validMethods, TRUE))
68
-            $formMethod = strtoupper($_POST['_method']);
67
+        if (isset($_POST['_method']) && in_array(strtoupper($_POST['_method']), $validMethods, TRUE)) {
68
+                    $formMethod = strtoupper($_POST['_method']);
69
+        }
69 70
 
70 71
         if (is_null($formMethod))
71 72
         {
72 73
             $this->requestMethod = $requestMethod;
73
-        }
74
-        else
74
+        } else
75 75
         {
76
-            if ($requestMethod == 'POST')
77
-                $this->requestMethod = $formMethod;
76
+            if ($requestMethod == 'POST') {
77
+                            $this->requestMethod = $formMethod;
78
+            }
78 79
 
79
-            if (!$this->CI->input->is_ajax_request() && $this->requestMethod == 'HEAD')
80
-                $this->requestMethod = 'POST';
80
+            if (!$this->CI->input->is_ajax_request() && $this->requestMethod == 'HEAD') {
81
+                            $this->requestMethod = 'POST';
82
+            }
81 83
         }
82 84
     }
83 85
 
@@ -117,14 +119,14 @@  discard block
 block discarded – undo
117 119
 
118 120
                 // Redirect to 404 if not enough parameters provided
119 121
 
120
-                if(count($path_args) < count($route_args['required']))
121
-                    redirect(Route::get404()->path);
122
+                if(count($path_args) < count($route_args['required'])) {
123
+                                    redirect(Route::get404()->path);
124
+                }
122 125
 
123 126
                 if(count($path_args) == 0)
124 127
                 {
125 128
                     $this->CI->{$this->route->method}();
126
-                }
127
-                else
129
+                } else
128 130
                 {
129 131
                     call_user_func_array( [$this->CI, $this->route->method], array_values($path_args) );
130 132
                 }
@@ -134,14 +136,12 @@  discard block
 block discarded – undo
134 136
 
135 137
                 $this->CI->output->_display();
136 138
                 exit(0);
137
-            }
138
-            else
139
+            } else
139 140
             {
140 141
                 if (ENVIRONMENT != 'production')
141 142
                 {
142 143
                     show_error('The method '.$this->route->controller.'::'.$this->route->method.'() does not exists', 500, 'Method not found');
143
-                }
144
-                else
144
+                } else
145 145
                 {
146 146
                     //redirect(Route::get404()->path);
147 147
                     Route::trigger404();
Please login to merge, or discard this patch.