Completed
Push — master ( 855079...1a7984 )
by Sinnarasa
02:36
created
src/Routing/RouteCollection.php 2 patches
Spacing   +11 added lines, -11 removed lines patch added patch discarded remove patch
@@ -42,11 +42,11 @@  discard block
 block discarded – undo
42 42
      */
43 43
     public function addRoutes($routes = null, $options = [])
44 44
     {
45
-        if(!is_null($routes) && !is_array($routes)) {
45
+        if (!is_null($routes) && !is_array($routes)) {
46 46
             if (strpos($routes, '.php') === false) $routes = trim($routes, '/') . '/';
47 47
             if (is_file($routes . '/routes.php') && is_array($routesFile = include $routes . '/routes.php')) $routes = $routesFile;
48 48
             elseif (is_file($routes) && is_array($routesFile = include $routes)) $routes = $routesFile;
49
-            else throw new \InvalidArgumentException('Argument for "' . get_called_class() . '" constructor is not recognized. Expected argument array or file containing array but "'.$routes.'" given');
49
+            else throw new \InvalidArgumentException('Argument for "' . get_called_class() . '" constructor is not recognized. Expected argument array or file containing array but "' . $routes . '" given');
50 50
         }
51 51
         $this->routes['routes_' . $this->countRoutes] = is_array($routes) ? $routes : [];
52 52
         $this->routes['path_' . $this->countRoutes] = (isset($options['path']) && !empty($options['path'])) ? rtrim($options['path'], '/') . '/' : '';
@@ -61,8 +61,8 @@  discard block
 block discarded – undo
61 61
      */
62 62
     public function getRoutes($key = null)
63 63
     {
64
-        if(!is_null($key))
65
-            return isset($this->routes[$key])?$this->routes[$key]:'';
64
+        if (!is_null($key))
65
+            return isset($this->routes[$key]) ? $this->routes[$key] : '';
66 66
         return $this->routes;
67 67
     }
68 68
 
@@ -78,7 +78,7 @@  discard block
 block discarded – undo
78 78
         } elseif (is_string($args))
79 79
             for ($i = 0; $i < $this->countRoutes; ++$i)
80 80
                 $this->routes['prefix_' . $i] = '/' . trim($args, '/');
81
-        if($this->countRoutes == 0)$this->countRoutes++;
81
+        if ($this->countRoutes == 0)$this->countRoutes++;
82 82
     }
83 83
 
84 84
     /**
@@ -88,14 +88,14 @@  discard block
 block discarded – undo
88 88
     {
89 89
         $nbrArgs = count($args);
90 90
         for ($i = 0; $i < $nbrArgs; ++$i) {
91
-            if(is_array($args[$i])){
91
+            if (is_array($args[$i])) {
92 92
                 $this->routes['path_' . $i] = (isset($args[$i]['path']) && !empty($args[$i]['path'])) ? rtrim($args[$i]['path'], '/') . '/' : '';
93 93
                 $this->routes['namespace_' . $i] = (isset($args[$i]['namespace']) && !empty($args[$i]['namespace'])) ? trim($args[$i]['namespace'], '\\') . '\\' : '';
94
-                $this->routes['prefix_' . $i] = (isset($args[$i]['prefix']) && !empty($args[$i]['prefix'])) ? '/'.trim($args[$i]['prefix'], '/') : '';
95
-                if(!isset($this->routes['routes_' . $i]))$this->routes['routes_' . $i] = [];
94
+                $this->routes['prefix_' . $i] = (isset($args[$i]['prefix']) && !empty($args[$i]['prefix'])) ? '/' . trim($args[$i]['prefix'], '/') : '';
95
+                if (!isset($this->routes['routes_' . $i]))$this->routes['routes_' . $i] = [];
96 96
             }
97 97
         }
98
-        if($this->countRoutes == 0)$this->countRoutes++;
98
+        if ($this->countRoutes == 0)$this->countRoutes++;
99 99
     }
100 100
 
101 101
     /**
@@ -105,7 +105,7 @@  discard block
 block discarded – undo
105 105
     public function setMiddleware($middleware)
106 106
     {
107 107
         if (is_string($middleware)) $middleware = rtrim($middleware, '/');
108
-        if(is_array($middleware))
108
+        if (is_array($middleware))
109 109
             $this->middleware = $middleware;
110 110
         elseif (is_file($middleware) && is_array($mid = include $middleware))
111 111
             $this->middleware = $mid;
@@ -125,7 +125,7 @@  discard block
 block discarded – undo
125 125
                 foreach ($this->routes['routes_' . $i] as $route => $dependencies) {
126 126
                     if (is_array($dependencies) && isset($dependencies['use']))
127 127
                         $use = (is_callable($dependencies['use'])) ? 'closure-' . $count : trim($dependencies['use'], '/');
128
-                    elseif(!is_array($dependencies))
128
+                    elseif (!is_array($dependencies))
129 129
                         $use = (is_callable($dependencies)) ? 'closure-' . $count : trim($dependencies, '/');
130 130
                     else
131 131
                         $use = $route;
Please login to merge, or discard this patch.
Braces   +60 added lines, -31 removed lines patch added patch discarded remove patch
@@ -33,7 +33,9 @@  discard block
 block discarded – undo
33 33
      */
34 34
     public function __construct($routes = null, $options = [])
35 35
     {
36
-        if (!is_null($routes) || !empty($options)) $this->addRoutes($routes, $options);
36
+        if (!is_null($routes) || !empty($options)) {
37
+            $this->addRoutes($routes, $options);
38
+        }
37 39
     }
38 40
 
39 41
     /**
@@ -43,10 +45,16 @@  discard block
 block discarded – undo
43 45
     public function addRoutes($routes = null, $options = [])
44 46
     {
45 47
         if(!is_null($routes) && !is_array($routes)) {
46
-            if (strpos($routes, '.php') === false) $routes = trim($routes, '/') . '/';
47
-            if (is_file($routes . '/routes.php') && is_array($routesFile = include $routes . '/routes.php')) $routes = $routesFile;
48
-            elseif (is_file($routes) && is_array($routesFile = include $routes)) $routes = $routesFile;
49
-            else throw new \InvalidArgumentException('Argument for "' . get_called_class() . '" constructor is not recognized. Expected argument array or file containing array but "'.$routes.'" given');
48
+            if (strpos($routes, '.php') === false) {
49
+                $routes = trim($routes, '/') . '/';
50
+            }
51
+            if (is_file($routes . '/routes.php') && is_array($routesFile = include $routes . '/routes.php')) {
52
+                $routes = $routesFile;
53
+            } elseif (is_file($routes) && is_array($routesFile = include $routes)) {
54
+                $routes = $routesFile;
55
+            } else {
56
+                throw new \InvalidArgumentException('Argument for "' . get_called_class() . '" constructor is not recognized. Expected argument array or file containing array but "'.$routes.'" given');
57
+            }
50 58
         }
51 59
         $this->routes['routes_' . $this->countRoutes] = is_array($routes) ? $routes : [];
52 60
         $this->routes['path_' . $this->countRoutes] = (isset($options['path']) && !empty($options['path'])) ? rtrim($options['path'], '/') . '/' : '';
@@ -61,8 +69,9 @@  discard block
 block discarded – undo
61 69
      */
62 70
     public function getRoutes($key = null)
63 71
     {
64
-        if(!is_null($key))
65
-            return isset($this->routes[$key])?$this->routes[$key]:'';
72
+        if(!is_null($key)) {
73
+                    return isset($this->routes[$key])?$this->routes[$key]:'';
74
+        }
66 75
         return $this->routes;
67 76
     }
68 77
 
@@ -73,12 +82,18 @@  discard block
 block discarded – undo
73 82
     {
74 83
         if (is_array($args)) {
75 84
             $nbrArgs = count($args);
76
-            for ($i = 0; $i < $nbrArgs; ++$i)
77
-                $this->routes['prefix_' . $i] = '/' . trim($args[$i], '/');
78
-        } elseif (is_string($args))
79
-            for ($i = 0; $i < $this->countRoutes; ++$i)
80
-                $this->routes['prefix_' . $i] = '/' . trim($args, '/');
81
-        if($this->countRoutes == 0)$this->countRoutes++;
85
+            for ($i = 0; $i < $nbrArgs; ++$i) {
86
+                            $this->routes['prefix_' . $i] = '/' . trim($args[$i], '/');
87
+            }
88
+        } elseif (is_string($args)) {
89
+                    for ($i = 0;
90
+        }
91
+        $i < $this->countRoutes; ++$i) {
92
+                            $this->routes['prefix_' . $i] = '/' . trim($args, '/');
93
+            }
94
+        if($this->countRoutes == 0) {
95
+            $this->countRoutes++;
96
+        }
82 97
     }
83 98
 
84 99
     /**
@@ -92,10 +107,14 @@  discard block
 block discarded – undo
92 107
                 $this->routes['path_' . $i] = (isset($args[$i]['path']) && !empty($args[$i]['path'])) ? rtrim($args[$i]['path'], '/') . '/' : '';
93 108
                 $this->routes['namespace_' . $i] = (isset($args[$i]['namespace']) && !empty($args[$i]['namespace'])) ? trim($args[$i]['namespace'], '\\') . '\\' : '';
94 109
                 $this->routes['prefix_' . $i] = (isset($args[$i]['prefix']) && !empty($args[$i]['prefix'])) ? '/'.trim($args[$i]['prefix'], '/') : '';
95
-                if(!isset($this->routes['routes_' . $i]))$this->routes['routes_' . $i] = [];
110
+                if(!isset($this->routes['routes_' . $i])) {
111
+                    $this->routes['routes_' . $i] = [];
112
+                }
96 113
             }
97 114
         }
98
-        if($this->countRoutes == 0)$this->countRoutes++;
115
+        if($this->countRoutes == 0) {
116
+            $this->countRoutes++;
117
+        }
99 118
     }
100 119
 
101 120
     /**
@@ -104,12 +123,16 @@  discard block
 block discarded – undo
104 123
      */
105 124
     public function setMiddleware($middleware)
106 125
     {
107
-        if (is_string($middleware)) $middleware = rtrim($middleware, '/');
108
-        if(is_array($middleware))
109
-            $this->middleware = $middleware;
110
-        elseif (is_file($middleware) && is_array($mid = include $middleware))
111
-            $this->middleware = $mid;
112
-        else throw new \InvalidArgumentException('Accepted argument for setMiddleware are array and array file');
126
+        if (is_string($middleware)) {
127
+            $middleware = rtrim($middleware, '/');
128
+        }
129
+        if(is_array($middleware)) {
130
+                    $this->middleware = $middleware;
131
+        } elseif (is_file($middleware) && is_array($mid = include $middleware)) {
132
+                    $this->middleware = $mid;
133
+        } else {
134
+            throw new \InvalidArgumentException('Accepted argument for setMiddleware are array and array file');
135
+        }
113 136
     }
114 137
 
115 138
     /**
@@ -121,14 +144,15 @@  discard block
 block discarded – undo
121 144
         $count = 0;
122 145
         for ($i = 0; $i < $this->countRoutes; ++$i) {
123 146
             $prefix = (isset($this->routes['prefix_' . $i])) ? $this->routes['prefix_' . $i] : '';
124
-            if (isset($this->routes['routes_' . $i]))
125
-                foreach ($this->routes['routes_' . $i] as $route => $dependencies) {
147
+            if (isset($this->routes['routes_' . $i])) {
148
+                            foreach ($this->routes['routes_' . $i] as $route => $dependencies) {
126 149
                     if (is_array($dependencies) && isset($dependencies['use']))
127
-                        $use = (is_callable($dependencies['use'])) ? 'closure-' . $count : trim($dependencies['use'], '/');
128
-                    elseif(!is_array($dependencies))
129
-                        $use = (is_callable($dependencies)) ? 'closure-' . $count : trim($dependencies, '/');
130
-                    else
131
-                        $use = $route;
150
+                        $use = (is_callable($dependencies['use'])) ? 'closure-' . $count : trim($dependencies['use'], '/');
151
+            } elseif(!is_array($dependencies)) {
152
+                                            $use = (is_callable($dependencies)) ? 'closure-' . $count : trim($dependencies, '/');
153
+                    } else {
154
+                                            $use = $route;
155
+                    }
132 156
                     (!is_callable($dependencies) && isset($dependencies['name'])) ? $this->routesByName[$use . '#' . $dependencies['name']] = $root . $prefix . $route : $this->routesByName[$use] = $root . $prefix . $route;
133 157
                     $count++;
134 158
                 }
@@ -145,9 +169,14 @@  discard block
 block discarded – undo
145 169
     {
146 170
         foreach ($this->routesByName as $key => $route) {
147 171
             $param = explode('#', $key);
148
-            foreach ($params as $key2 => $value) $route = str_replace(':' . $key2, $value, $route);
149
-            if ($param[0] == trim($name, '/')) return $route;
150
-            else if (isset($param[1]) && $param[1] == $name) return $route;
172
+            foreach ($params as $key2 => $value) {
173
+                $route = str_replace(':' . $key2, $value, $route);
174
+            }
175
+            if ($param[0] == trim($name, '/')) {
176
+                return $route;
177
+            } else if (isset($param[1]) && $param[1] == $name) {
178
+                return $route;
179
+            }
151 180
         }
152 181
         return null;
153 182
     }
Please login to merge, or discard this patch.