Completed
Branch master (9056d3)
by Anderson
01:57
created
src/Middleware/Request.php 1 patch
Braces   +13 added lines, -14 removed lines patch added patch discarded remove patch
@@ -65,20 +65,22 @@  discard block
 block discarded – undo
65 65
 
66 66
         // FIXME: Solve ambiguity here! POST with _method="GET" makes no sense
67 67
 
68
-        if(isset($_POST['_method']) && in_array(strtoupper($_POST['_method']), $validMethods , TRUE))
69
-            $formMethod = strtoupper($_POST['_method']);
68
+        if(isset($_POST['_method']) && in_array(strtoupper($_POST['_method']), $validMethods , TRUE)) {
69
+                    $formMethod = strtoupper($_POST['_method']);
70
+        }
70 71
 
71 72
         if(is_null($formMethod))
72 73
         {
73 74
             $this->requestMethod = $requestMethod;
74
-        }
75
-        else
75
+        } else
76 76
         {
77
-            if($requestMethod == 'POST')
78
-                $this->requestMethod = $formMethod;
77
+            if($requestMethod == 'POST') {
78
+                            $this->requestMethod = $formMethod;
79
+            }
79 80
 
80
-            if(!$this->CI->input->is_ajax_request() && $this->requestMethod == 'HEAD')
81
-                $this->requestMethod = 'POST';
81
+            if(!$this->CI->input->is_ajax_request() && $this->requestMethod == 'HEAD') {
82
+                            $this->requestMethod = 'POST';
83
+            }
82 84
         }
83 85
     }
84 86
 
@@ -96,8 +98,7 @@  discard block
 block discarded – undo
96 98
             if(ENVIRONMENT != 'production')
97 99
             {
98 100
                 show_error('The request method '.$this->requestMethod.' is not allowed to view the resource', 403, 'Forbidden method');
99
-            }
100
-            else
101
+            } else
101 102
             {
102 103
                 show_404();
103 104
             }
@@ -111,14 +112,12 @@  discard block
 block discarded – undo
111 112
 
112 113
             $this->CI->output->_display();
113 114
             exit(0);
114
-        }
115
-        else
115
+        } else
116 116
         {
117 117
             if(ENVIRONMENT != 'production')
118 118
             {
119 119
                 show_error('The method '.$this->route->controller.'::'.$this->route->method.'() does not exists', 500, 'Method not found');
120
-            }
121
-            else
120
+            } else
122 121
             {
123 122
                 show_404();
124 123
             }
Please login to merge, or discard this patch.
src/Core/Middleware.php 1 patch
Braces   +21 added lines, -14 removed lines patch added patch discarded remove patch
@@ -98,8 +98,9 @@  discard block
 block discarded – undo
98 98
         {
99 99
             foreach($currentRoute->middleware as $middleware)
100 100
             {
101
-                if(!in_array($middleware,$_run))
102
-                    $_run[] = $middleware;
101
+                if(!in_array($middleware,$_run)) {
102
+                                    $_run[] = $middleware;
103
+                }
103 104
             }
104 105
         }
105 106
 
@@ -115,8 +116,9 @@  discard block
 block discarded – undo
115 116
 
116 117
                 if( $search === $path )
117 118
                 {
118
-                    if(!in_array($middleware,$_run))
119
-                         $_run[] = $middleware;
119
+                    if(!in_array($middleware,$_run)) {
120
+                                             $_run[] = $middleware;
121
+                    }
120 122
                 }
121 123
             }
122 124
         }
@@ -145,31 +147,36 @@  discard block
 block discarded – undo
145 147
 
146 148
         $middlewareName = ucfirst($middlewareName).'_middleware';
147 149
 
148
-        if(!file_exists($middlewareDir))
149
-            show_error('Unable to find (or read) the middleware folder: "'.$middlewareDir.'"');
150
+        if(!file_exists($middlewareDir)) {
151
+                    show_error('Unable to find (or read) the middleware folder: "'.$middlewareDir.'"');
152
+        }
150 153
 
151 154
         $runMiddleware =  $middlewareDir.$middlewareName.'.php';
152 155
 
153
-        if(!file_exists($runMiddleware))
154
-            show_error('Unable to find (or read) the middleware "'.$runMiddleware.'"');
156
+        if(!file_exists($runMiddleware)) {
157
+                    show_error('Unable to find (or read) the middleware "'.$runMiddleware.'"');
158
+        }
155 159
 
156 160
         require $runMiddleware;
157 161
 
158
-        if(!class_exists($middlewareName))
159
-            show_error('Class "'.$middlewareName.'" not found');
162
+        if(!class_exists($middlewareName)) {
163
+                    show_error('Class "'.$middlewareName.'" not found');
164
+        }
160 165
 
161 166
         $middleware = new $middlewareName();
162 167
 
163 168
         // Call the current controller __beforeMiddleware() method, if exists:
164
-        if(method_exists(self::$instance, '_beforeMiddleware'))
165
-            self::$instance->_beforeMiddleware();
169
+        if(method_exists(self::$instance, '_beforeMiddleware')) {
170
+                    self::$instance->_beforeMiddleware();
171
+        }
166 172
 
167 173
         // Run the middleware
168 174
         $middleware->run();
169 175
 
170 176
         // Call the current controller _afterMiddleware() method, if exists:
171
-        if(method_exists(self::$instance, '_afterMiddleware'))
172
-            self::$instance->_afterMiddleware();
177
+        if(method_exists(self::$instance, '_afterMiddleware')) {
178
+                    self::$instance->_afterMiddleware();
179
+        }
173 180
 
174 181
     }
175 182
 }
Please login to merge, or discard this patch.
src/Core/Route.php 1 patch
Braces   +107 added lines, -78 removed lines patch added patch discarded remove patch
@@ -149,11 +149,13 @@  discard block
 block discarded – undo
149 149
         $controller = $parsedController[1];
150 150
         $method     = $parsedController[2];
151 151
 
152
-        if(!is_string($path))
153
-            show_error('Route path must be a string ', 500, 'Route error: bad route path');
152
+        if(!is_string($path)) {
153
+                    show_error('Route path must be a string ', 500, 'Route error: bad route path');
154
+        }
154 155
 
155
-        if(!is_string($verb))
156
-            show_error('Route HTTP Verb must be a string', 500, 'Route error: bad verb type');
156
+        if(!is_string($verb)) {
157
+                    show_error('Route HTTP Verb must be a string', 500, 'Route error: bad verb type');
158
+        }
157 159
 
158 160
         $verb = strtoupper($verb);
159 161
 
@@ -182,38 +184,47 @@  discard block
 block discarded – undo
182 184
         if(isset($attr['as']))
183 185
         {
184 186
             $route['name'] = $attr['as'];
185
-        }
186
-        else
187
+        } else
187 188
         {
188 189
             $route['name'] = NULL;
189 190
         }
190 191
 
191 192
         $route['prefix'] = NULL;
192 193
 
193
-        if(!is_null(self::$prefix))
194
-            $route['prefix'] = self::$prefix;   # Group
195
-        if(isset($attr['prefix']))
196
-            $route['prefix'] = $attr['prefix']; # Specific (will overwrite group prefix)
194
+        if(!is_null(self::$prefix)) {
195
+                    $route['prefix'] = self::$prefix;
196
+        }
197
+        # Group
198
+        if(isset($attr['prefix'])) {
199
+                    $route['prefix'] = $attr['prefix'];
200
+        }
201
+        # Specific (will overwrite group prefix)
197 202
 
198 203
         $route['namespace'] = NULL;
199 204
 
200
-        if(!is_null(self::$namespace))
201
-            $route['namespace'] = self::$namespace;   # Group
202
-        if(isset($attr['namespace']))
203
-            $route['namespace'] = $attr['namespace']; # Specific (will overwrite group namespace)
205
+        if(!is_null(self::$namespace)) {
206
+                    $route['namespace'] = self::$namespace;
207
+        }
208
+        # Group
209
+        if(isset($attr['namespace'])) {
210
+                    $route['namespace'] = $attr['namespace'];
211
+        }
212
+        # Specific (will overwrite group namespace)
204 213
 
205 214
         # Removing trailing slashes
206 215
         if(!is_null($route['prefix']))
207 216
         {
208 217
             $route['prefix'] = trim($route['prefix'],'/');
209
-            if($route['prefix'] == '')
210
-                $route['prefix'] = NULL;
218
+            if($route['prefix'] == '') {
219
+                            $route['prefix'] = NULL;
220
+            }
211 221
         }
212 222
         if(!is_null($route['namespace']))
213 223
         {
214 224
             $route['namespace'] = trim($route['namespace'],'/');
215
-            if($route['namespace'] == '')
216
-                $route['namespace'] = NULL;
225
+            if($route['namespace'] == '') {
226
+                            $route['namespace'] = NULL;
227
+            }
217 228
         }
218 229
 
219 230
         $route['middleware'] = array();
@@ -222,14 +233,14 @@  discard block
 block discarded – undo
222 233
         {
223 234
             if(is_array($attr['middleware']))
224 235
             {
225
-                foreach($attr['middleware'] as $middleware)
226
-                    $route['middleware'][] = $middleware; # Group
227
-            }
228
-            elseif( is_string($attr['middleware']))
236
+                foreach($attr['middleware'] as $middleware) {
237
+                                    $route['middleware'][] = $middleware;
238
+                }
239
+                # Group
240
+            } elseif( is_string($attr['middleware']))
229 241
             {
230 242
                 $route['middleware'][] = $attr['middleware']; # Group
231
-            }
232
-            else
243
+            } else
233 244
             {
234 245
                 show_error('Route middleware must be a string or an array',500,'Route error: bad middleware format');
235 246
             }
@@ -264,8 +275,7 @@  discard block
 block discarded – undo
264 275
         if(!$return)
265 276
         {
266 277
             self::$routes[] = (object) $route;
267
-        }
268
-        else
278
+        } else
269 279
         {
270 280
             return (object) $route;
271 281
         }
@@ -392,8 +402,9 @@  discard block
 block discarded – undo
392 402
      */
393 403
     public static function matches($verbs, $url, $attr, $hideOriginal = FALSE)
394 404
     {
395
-        if(!is_array($verbs))
396
-            show_error('Route::matches() first argument must be an array of valid HTTP Verbs', 500, 'Route error: bad Route::matches() verb list');
405
+        if(!is_array($verbs)) {
406
+                    show_error('Route::matches() first argument must be an array of valid HTTP Verbs', 500, 'Route error: bad Route::matches() verb list');
407
+        }
397 408
 
398 409
         foreach($verbs as $verb)
399 410
         {
@@ -430,17 +441,21 @@  discard block
 block discarded – undo
430 441
 
431 442
         $hideOriginal = FALSE;
432 443
 
433
-        if(isset($attr['namespace']))
434
-            $base_attr['namespace']  = $attr['namespace'];
444
+        if(isset($attr['namespace'])) {
445
+                    $base_attr['namespace']  = $attr['namespace'];
446
+        }
435 447
 
436
-        if(isset($attr['middleware']))
437
-            $base_attr['middleware'] = $attr['middleware'];
448
+        if(isset($attr['middleware'])) {
449
+                    $base_attr['middleware'] = $attr['middleware'];
450
+        }
438 451
 
439
-        if(isset($attr['hideOriginal']))
440
-            $hideOriginal = (bool) $attr['hideOriginal'];
452
+        if(isset($attr['hideOriginal'])) {
453
+                    $hideOriginal = (bool) $attr['hideOriginal'];
454
+        }
441 455
 
442
-        if(isset($attr['prefix']))
443
-            $base_attr['prefix']  = $attr['prefix'];
456
+        if(isset($attr['prefix'])) {
457
+                    $base_attr['prefix']  = $attr['prefix'];
458
+        }
444 459
 
445 460
         $only = array();
446 461
 
@@ -449,8 +464,7 @@  discard block
 block discarded – undo
449 464
             if(is_array($attr['only']))
450 465
             {
451 466
                 $only  = strtolower($attr['only']);
452
-            }
453
-            else
467
+            } else
454 468
             {
455 469
                 $only[] = strtolower($attr['only']);
456 470
             }
@@ -526,16 +540,19 @@  discard block
 block discarded – undo
526 540
 
527 541
         $path = $route->path;
528 542
 
529
-        if(!is_null($prefix))
530
-            $path = $prefix.'/'.$path;
543
+        if(!is_null($prefix)) {
544
+                    $path = $prefix.'/'.$path;
545
+        }
531 546
 
532
-        if(substr($path, 0, 1) == "/" && strlen($path) > 1)
533
-            $path = substr($path,1);
547
+        if(substr($path, 0, 1) == "/" && strlen($path) > 1) {
548
+                    $path = substr($path,1);
549
+        }
534 550
 
535 551
         $controller = $route->controller.'/'.$route->method;
536 552
 
537
-        if(!is_null($namespace))
538
-            $controller = $namespace.'/'.$controller;
553
+        if(!is_null($namespace)) {
554
+                    $controller = $namespace.'/'.$controller;
555
+        }
539 556
 
540 557
         $replaces =
541 558
             [
@@ -601,12 +618,14 @@  discard block
 block discarded – undo
601 618
             $path = key($route);
602 619
             $_404 = $route[$path];
603 620
 
604
-            if(!isset($routes[$path]))
605
-                $routes[$path] = $_404;
621
+            if(!isset($routes[$path])) {
622
+                            $routes[$path] = $_404;
623
+            }
606 624
         }
607 625
 
608
-        if(is_null(self::$defaultController))
609
-            show_error('You must specify a home route: Route::home() as default controller!', 500, 'Route error: missing default controller');
626
+        if(is_null(self::$defaultController)) {
627
+                    show_error('You must specify a home route: Route::home() as default controller!', 500, 'Route error: missing default controller');
628
+        }
610 629
 
611 630
         $defaultController = self::$defaultController->compiled;
612 631
         $defaultController = $defaultController[key($defaultController)];
@@ -632,11 +651,13 @@  discard block
 block discarded – undo
632 651
      */
633 652
     public static function group($attr, $routes)
634 653
     {
635
-        if(!is_array($attr))
636
-            show_error('Group attribute must be a valid array');
654
+        if(!is_array($attr)) {
655
+                    show_error('Group attribute must be a valid array');
656
+        }
637 657
 
638
-        if(!isset($attr['prefix']))
639
-            show_error('You must specify an prefix!');
658
+        if(!isset($attr['prefix'])) {
659
+                    show_error('You must specify an prefix!');
660
+        }
640 661
 
641 662
         self::$prefix = $attr['prefix'];
642 663
 
@@ -648,8 +669,7 @@  discard block
 block discarded – undo
648 669
         if(isset($attr['hideOriginals']) && $attr['hideOriginals'] === TRUE)
649 670
         {
650 671
             self::$hideOriginals = TRUE;
651
-        }
652
-        else
672
+        } else
653 673
         {
654 674
             self::$hideOriginals = FALSE;
655 675
         }
@@ -661,15 +681,14 @@  discard block
 block discarded – undo
661 681
                 //self::$middleware = $attr['middleware'];
662 682
                 if(is_array($attr['middleware']) && !empty($attr['middleware']))
663 683
                 {
664
-                    foreach($attr['middleware'] as $middleware)
665
-                        self::$groupMiddleware[] = [ $attr['prefix'] => $middleware ];
666
-                }
667
-                else
684
+                    foreach($attr['middleware'] as $middleware) {
685
+                                            self::$groupMiddleware[] = [ $attr['prefix'] => $middleware ];
686
+                    }
687
+                } else
668 688
                 {
669 689
                     self::$groupMiddleware[] = [ $attr['prefix'] => $attr['middleware'] ];
670 690
                 }
671
-            }
672
-            else
691
+            } else
673 692
             {
674 693
                 show_error('Group middleware not valid');
675 694
             }
@@ -684,8 +703,10 @@  discard block
 block discarded – undo
684 703
             $controller = $uri[0];
685 704
             $method = isset($uri[1]) ? $uri[1] : NULL;
686 705
 
687
-            if(self::$hideOriginals === TRUE)
688
-                self::$hiddenRoutes[] = [$controller => function(){ show_404(); }];
706
+            if(self::$hideOriginals === TRUE) {
707
+                            self::$hiddenRoutes[] = [$controller => function(){ show_404();
708
+            }
709
+            }];
689 710
 
690 711
             $route_attr['uses'] = $controller.( !is_null($method) ? '/'.$method : '');
691 712
             self::get('/', $route_attr);
@@ -720,14 +741,17 @@  discard block
 block discarded – undo
720 741
                 'as'   => $as
721 742
             ];
722 743
 
723
-        if(!is_null($attr) && !is_array($attr))
724
-            show_error('Default controller attributes must be an array',500,'Route error: bad attribute type');
744
+        if(!is_null($attr) && !is_array($attr)) {
745
+                    show_error('Default controller attributes must be an array',500,'Route error: bad attribute type');
746
+        }
725 747
 
726
-        if(!is_null($attr))
727
-            $routeAttr = array_merge($routeAttr,$attr);
748
+        if(!is_null($attr)) {
749
+                    $routeAttr = array_merge($routeAttr,$attr);
750
+        }
728 751
 
729
-        if(isset($attr['prefix']))
730
-            show_error('Default controller may not have a prefix!',500,'Route error: prefix not allowed');
752
+        if(isset($attr['prefix'])) {
753
+                    show_error('Default controller may not have a prefix!',500,'Route error: prefix not allowed');
754
+        }
731 755
 
732 756
         self::$defaultController = self::$routes[] = self::add('GET', '/', ['uses' => $controller, 'as' => $as],TRUE, TRUE);
733 757
     }
@@ -817,8 +841,9 @@  discard block
 block discarded – undo
817 841
     {
818 842
         foreach(self::$routes as $route)
819 843
         {
820
-            if($route->name == $search)
821
-                return base_url(self::compileRoute($route)->path);
844
+            if($route->name == $search) {
845
+                            return base_url(self::compileRoute($route)->path);
846
+            }
822 847
         }
823 848
 
824 849
         show_error('The route "'.$search.'" is not defined', 500, 'Route error');
@@ -841,14 +866,17 @@  discard block
 block discarded – undo
841 866
             $routes[$route->verb][] = $route;
842 867
         }
843 868
 
844
-        if(empty($routes))
845
-            return FALSE;
869
+        if(empty($routes)) {
870
+                    return FALSE;
871
+        }
846 872
 
847
-        if(is_null($requestMethod))
848
-            $requestMethod = $_SERVER['REQUEST_METHOD'];
873
+        if(is_null($requestMethod)) {
874
+                    $requestMethod = $_SERVER['REQUEST_METHOD'];
875
+        }
849 876
 
850
-        if(!isset($routes[$requestMethod]))
851
-            return FALSE;
877
+        if(!isset($routes[$requestMethod])) {
878
+                    return FALSE;
879
+        }
852 880
 
853 881
         $routes = $routes[$requestMethod];
854 882
 
@@ -868,8 +896,9 @@  discard block
 block discarded – undo
868 896
             {
869 897
                 $compiled = $route->compiled;
870 898
 
871
-                if($findPath == key($compiled))
872
-                    return $route;
899
+                if($findPath == key($compiled)) {
900
+                                    return $route;
901
+                }
873 902
             }
874 903
         }
875 904
 
Please login to merge, or discard this patch.