Completed
Push — master ( 9056d3...546896 )
by Anderson
01:59
created
src/Middleware/Request.php 1 patch
Braces   +17 added lines, -18 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
 
@@ -92,22 +94,21 @@  discard block
 block discarded – undo
92 94
     {
93 95
         if(!$this->route)
94 96
         {
95
-            if(is_null(Route::get404()))
96
-                show_404();
97
+            if(is_null(Route::get404())) {
98
+                            show_404();
99
+            }
97 100
 
98 101
             if(Route::get404()->controller != get_class($this->CI))
99 102
             {
100 103
                 if(ENVIRONMENT != 'production')
101 104
                 {
102 105
                     show_error('The request method '.$this->requestMethod.' is not allowed to view the resource', 403, 'Forbidden method');
103
-                }
104
-                else
106
+                } else
105 107
                 {
106 108
                     redirect(Route::get404()->path);
107 109
                 }
108 110
             }
109
-        }
110
-        else
111
+        } else
111 112
         {
112 113
             if(method_exists($this->CI,$this->route->method))
113 114
             {
@@ -117,14 +118,12 @@  discard block
 block discarded – undo
117 118
 
118 119
                 $this->CI->output->_display();
119 120
                 exit(0);
120
-            }
121
-            else
121
+            } else
122 122
             {
123 123
                 if(ENVIRONMENT != 'production')
124 124
                 {
125 125
                     show_error('The method '.$this->route->controller.'::'.$this->route->method.'() does not exists', 500, 'Method not found');
126
-                }
127
-                else
126
+                } else
128 127
                 {
129 128
                     redirect(Route::get404()->path);
130 129
                 }
Please login to merge, or discard this patch.
src/Core/Route.php 1 patch
Braces   +108 added lines, -80 removed lines patch added patch discarded remove patch
@@ -148,11 +148,13 @@  discard block
 block discarded – undo
148 148
         $controller = $parsedController[1];
149 149
         $method     = $parsedController[2];
150 150
 
151
-        if(!is_string($path))
152
-            show_error('Route path must be a string ', 500, 'Route error: bad route path');
151
+        if(!is_string($path)) {
152
+                    show_error('Route path must be a string ', 500, 'Route error: bad route path');
153
+        }
153 154
 
154
-        if(!is_string($verb))
155
-            show_error('Route HTTP Verb must be a string', 500, 'Route error: bad verb type');
155
+        if(!is_string($verb)) {
156
+                    show_error('Route HTTP Verb must be a string', 500, 'Route error: bad verb type');
157
+        }
156 158
 
157 159
         $verb = strtoupper($verb);
158 160
 
@@ -181,38 +183,47 @@  discard block
 block discarded – undo
181 183
         if(isset($attr['as']))
182 184
         {
183 185
             $route['name'] = $attr['as'];
184
-        }
185
-        else
186
+        } else
186 187
         {
187 188
             $route['name'] = NULL;
188 189
         }
189 190
 
190 191
         $route['prefix'] = NULL;
191 192
 
192
-        if(!is_null(self::$prefix))
193
-            $route['prefix'] = self::$prefix;   # Group
194
-        if(isset($attr['prefix']))
195
-            $route['prefix'] = $attr['prefix']; # Specific (will overwrite group prefix)
193
+        if(!is_null(self::$prefix)) {
194
+                    $route['prefix'] = self::$prefix;
195
+        }
196
+        # Group
197
+        if(isset($attr['prefix'])) {
198
+                    $route['prefix'] = $attr['prefix'];
199
+        }
200
+        # Specific (will overwrite group prefix)
196 201
 
197 202
         $route['namespace'] = NULL;
198 203
 
199
-        if(!is_null(self::$namespace))
200
-            $route['namespace'] = self::$namespace;   # Group
201
-        if(isset($attr['namespace']))
202
-            $route['namespace'] = $attr['namespace']; # Specific (will overwrite group namespace)
204
+        if(!is_null(self::$namespace)) {
205
+                    $route['namespace'] = self::$namespace;
206
+        }
207
+        # Group
208
+        if(isset($attr['namespace'])) {
209
+                    $route['namespace'] = $attr['namespace'];
210
+        }
211
+        # Specific (will overwrite group namespace)
203 212
 
204 213
         # Removing trailing slashes
205 214
         if(!is_null($route['prefix']))
206 215
         {
207 216
             $route['prefix'] = trim($route['prefix'],'/');
208
-            if($route['prefix'] == '')
209
-                $route['prefix'] = NULL;
217
+            if($route['prefix'] == '') {
218
+                            $route['prefix'] = NULL;
219
+            }
210 220
         }
211 221
         if(!is_null($route['namespace']))
212 222
         {
213 223
             $route['namespace'] = trim($route['namespace'],'/');
214
-            if($route['namespace'] == '')
215
-                $route['namespace'] = NULL;
224
+            if($route['namespace'] == '') {
225
+                            $route['namespace'] = NULL;
226
+            }
216 227
         }
217 228
 
218 229
         $route['middleware'] = array();
@@ -221,14 +232,14 @@  discard block
 block discarded – undo
221 232
         {
222 233
             if(is_array($attr['middleware']))
223 234
             {
224
-                foreach($attr['middleware'] as $middleware)
225
-                    $route['middleware'][] = $middleware; # Group
226
-            }
227
-            elseif( is_string($attr['middleware']))
235
+                foreach($attr['middleware'] as $middleware) {
236
+                                    $route['middleware'][] = $middleware;
237
+                }
238
+                # Group
239
+            } elseif( is_string($attr['middleware']))
228 240
             {
229 241
                 $route['middleware'][] = $attr['middleware']; # Group
230
-            }
231
-            else
242
+            } else
232 243
             {
233 244
                 show_error('Route middleware must be a string or an array',500,'Route error: bad middleware format');
234 245
             }
@@ -263,8 +274,7 @@  discard block
 block discarded – undo
263 274
         if(!$return)
264 275
         {
265 276
             self::$routes[] = (object) $route;
266
-        }
267
-        else
277
+        } else
268 278
         {
269 279
             return (object) $route;
270 280
         }
@@ -391,8 +401,9 @@  discard block
 block discarded – undo
391 401
      */
392 402
     public static function matches($verbs, $url, $attr, $hideOriginal = FALSE)
393 403
     {
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');
404
+        if(!is_array($verbs)) {
405
+                    show_error('Route::matches() first argument must be an array of valid HTTP Verbs', 500, 'Route error: bad Route::matches() verb list');
406
+        }
396 407
 
397 408
         foreach($verbs as $verb)
398 409
         {
@@ -429,17 +440,21 @@  discard block
 block discarded – undo
429 440
 
430 441
         $hideOriginal = FALSE;
431 442
 
432
-        if(isset($attr['namespace']))
433
-            $base_attr['namespace']  = $attr['namespace'];
443
+        if(isset($attr['namespace'])) {
444
+                    $base_attr['namespace']  = $attr['namespace'];
445
+        }
434 446
 
435
-        if(isset($attr['middleware']))
436
-            $base_attr['middleware'] = $attr['middleware'];
447
+        if(isset($attr['middleware'])) {
448
+                    $base_attr['middleware'] = $attr['middleware'];
449
+        }
437 450
 
438
-        if(isset($attr['hideOriginal']))
439
-            $hideOriginal = (bool) $attr['hideOriginal'];
451
+        if(isset($attr['hideOriginal'])) {
452
+                    $hideOriginal = (bool) $attr['hideOriginal'];
453
+        }
440 454
 
441
-        if(isset($attr['prefix']))
442
-            $base_attr['prefix']  = $attr['prefix'];
455
+        if(isset($attr['prefix'])) {
456
+                    $base_attr['prefix']  = $attr['prefix'];
457
+        }
443 458
 
444 459
         $only = array();
445 460
 
@@ -448,8 +463,7 @@  discard block
 block discarded – undo
448 463
             if(is_array($attr['only']))
449 464
             {
450 465
                 $only  = strtolower($attr['only']);
451
-            }
452
-            else
466
+            } else
453 467
             {
454 468
                 $only[] = strtolower($attr['only']);
455 469
             }
@@ -525,16 +539,19 @@  discard block
 block discarded – undo
525 539
 
526 540
         $path = $route->path;
527 541
 
528
-        if(!is_null($prefix))
529
-            $path = $prefix.'/'.$path;
542
+        if(!is_null($prefix)) {
543
+                    $path = $prefix.'/'.$path;
544
+        }
530 545
 
531
-        if(substr($path, 0, 1) == "/" && strlen($path) > 1)
532
-            $path = substr($path,1);
546
+        if(substr($path, 0, 1) == "/" && strlen($path) > 1) {
547
+                    $path = substr($path,1);
548
+        }
533 549
 
534 550
         $controller = $route->controller.'/'.$route->method;
535 551
 
536
-        if(!is_null($namespace))
537
-            $controller = $namespace.'/'.$controller;
552
+        if(!is_null($namespace)) {
553
+                    $controller = $namespace.'/'.$controller;
554
+        }
538 555
 
539 556
         $replaces =
540 557
             [
@@ -600,12 +617,14 @@  discard block
 block discarded – undo
600 617
             $path = key($route);
601 618
             $_404 = $route[$path];
602 619
 
603
-            if(!isset($routes[$path]))
604
-                $routes[$path] = $_404;
620
+            if(!isset($routes[$path])) {
621
+                            $routes[$path] = $_404;
622
+            }
605 623
         }
606 624
 
607
-        if(is_null(self::$defaultController))
608
-            show_error('You must specify a home route: Route::home() as default controller!', 500, 'Route error: missing default controller');
625
+        if(is_null(self::$defaultController)) {
626
+                    show_error('You must specify a home route: Route::home() as default controller!', 500, 'Route error: missing default controller');
627
+        }
609 628
 
610 629
         $defaultController = self::$defaultController->compiled;
611 630
         $defaultController = $defaultController[key($defaultController)];
@@ -615,8 +634,7 @@  discard block
 block discarded – undo
615 634
         if(is_null(self::$_404page))
616 635
         {
617 636
             $routes['404_override'] = '';
618
-        }
619
-        else
637
+        } else
620 638
         {
621 639
             $routes['404_override'] = self::$_404page->controller;
622 640
         }
@@ -639,11 +657,13 @@  discard block
 block discarded – undo
639 657
      */
640 658
     public static function group($attr, $routes)
641 659
     {
642
-        if(!is_array($attr))
643
-            show_error('Group attribute must be a valid array');
660
+        if(!is_array($attr)) {
661
+                    show_error('Group attribute must be a valid array');
662
+        }
644 663
 
645
-        if(!isset($attr['prefix']))
646
-            show_error('You must specify an prefix!');
664
+        if(!isset($attr['prefix'])) {
665
+                    show_error('You must specify an prefix!');
666
+        }
647 667
 
648 668
         self::$prefix = $attr['prefix'];
649 669
 
@@ -655,8 +675,7 @@  discard block
 block discarded – undo
655 675
         if(isset($attr['hideOriginals']) && $attr['hideOriginals'] === TRUE)
656 676
         {
657 677
             self::$hideOriginals = TRUE;
658
-        }
659
-        else
678
+        } else
660 679
         {
661 680
             self::$hideOriginals = FALSE;
662 681
         }
@@ -668,15 +687,14 @@  discard block
 block discarded – undo
668 687
                 //self::$middleware = $attr['middleware'];
669 688
                 if(is_array($attr['middleware']) && !empty($attr['middleware']))
670 689
                 {
671
-                    foreach($attr['middleware'] as $middleware)
672
-                        self::$groupMiddleware[] = [ $attr['prefix'] => $middleware ];
673
-                }
674
-                else
690
+                    foreach($attr['middleware'] as $middleware) {
691
+                                            self::$groupMiddleware[] = [ $attr['prefix'] => $middleware ];
692
+                    }
693
+                } else
675 694
                 {
676 695
                     self::$groupMiddleware[] = [ $attr['prefix'] => $attr['middleware'] ];
677 696
                 }
678
-            }
679
-            else
697
+            } else
680 698
             {
681 699
                 show_error('Group middleware not valid');
682 700
             }
@@ -691,8 +709,10 @@  discard block
 block discarded – undo
691 709
             $controller = $uri[0];
692 710
             $method = isset($uri[1]) ? $uri[1] : NULL;
693 711
 
694
-            if(self::$hideOriginals === TRUE)
695
-                self::$hiddenRoutes[] = [$controller => function(){ show_404(); }];
712
+            if(self::$hideOriginals === TRUE) {
713
+                            self::$hiddenRoutes[] = [$controller => function(){ show_404();
714
+            }
715
+            }];
696 716
 
697 717
             $route_attr['uses'] = $controller.( !is_null($method) ? '/'.$method : '');
698 718
             self::get('/', $route_attr);
@@ -727,14 +747,17 @@  discard block
 block discarded – undo
727 747
                 'as'   => $as
728 748
             ];
729 749
 
730
-        if(!is_null($attr) && !is_array($attr))
731
-            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
+        }
732 753
 
733
-        if(!is_null($attr))
734
-            $routeAttr = array_merge($routeAttr,$attr);
754
+        if(!is_null($attr)) {
755
+                    $routeAttr = array_merge($routeAttr,$attr);
756
+        }
735 757
 
736
-        if(isset($attr['prefix']))
737
-            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
+        }
738 761
 
739 762
         self::$defaultController = self::$routes[] = self::add('GET', '/', ['uses' => $controller, 'as' => $as],TRUE, TRUE);
740 763
     }
@@ -824,8 +847,9 @@  discard block
 block discarded – undo
824 847
     {
825 848
         foreach(self::$routes as $route)
826 849
         {
827
-            if($route->name == $search)
828
-                return base_url(self::compileRoute($route)->path);
850
+            if($route->name == $search) {
851
+                            return base_url(self::compileRoute($route)->path);
852
+            }
829 853
         }
830 854
 
831 855
         show_error('The route "'.$search.'" is not defined', 500, 'Route error');
@@ -850,14 +874,17 @@  discard block
 block discarded – undo
850 874
             $routes[$route->verb][] = $route;
851 875
         }
852 876
 
853
-        if(empty($routes))
854
-            return FALSE;
877
+        if(empty($routes)) {
878
+                    return FALSE;
879
+        }
855 880
 
856
-        if(is_null($requestMethod))
857
-            $requestMethod = $_SERVER['REQUEST_METHOD'];
881
+        if(is_null($requestMethod)) {
882
+                    $requestMethod = $_SERVER['REQUEST_METHOD'];
883
+        }
858 884
 
859
-        if(!isset($routes[$requestMethod]))
860
-            return FALSE;
885
+        if(!isset($routes[$requestMethod])) {
886
+                    return FALSE;
887
+        }
861 888
 
862 889
         $routes = $routes[$requestMethod];
863 890
 
@@ -877,8 +904,9 @@  discard block
 block discarded – undo
877 904
             {
878 905
                 $compiled = $route->compiled;
879 906
 
880
-                if($findPath == key($compiled))
881
-                    return $route;
907
+                if($findPath == key($compiled)) {
908
+                                    return $route;
909
+                }
882 910
             }
883 911
         }
884 912
 
Please login to merge, or discard this patch.