Completed
Push — master ( a7b148...9b6038 )
by Sinnarasa
02:32
created
src/Routing/RouteCollection.php 1 patch
Braces   +58 added lines, -29 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_file($middleware) && is_array($mid = include $middleware))
109
-            $this->middleware = $mid;
110
-        elseif (is_array($middleware))
111
-            $this->middleware = $middleware;
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_file($middleware) && is_array($mid = include $middleware)) {
130
+                    $this->middleware = $mid;
131
+        } elseif (is_array($middleware)) {
132
+                    $this->middleware = $middleware;
133
+        } else {
134
+            throw new \InvalidArgumentException('Accepted argument for setMiddleware are array and array file');
135
+        }
113 136
     }
114 137
 
115 138
     /**
@@ -121,12 +144,13 @@  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
-                    else
129
-                        $use = (is_callable($dependencies)) ? 'closure-' . $count : trim($dependencies, '/');
150
+                        $use = (is_callable($dependencies['use'])) ? 'closure-' . $count : trim($dependencies['use'], '/');
151
+            } else {
152
+                                            $use = (is_callable($dependencies)) ? 'closure-' . $count : trim($dependencies, '/');
153
+                    }
130 154
                     (!is_callable($dependencies) && isset($dependencies['name'])) ? $this->routesByName[$use . '#' . $dependencies['name']] = $root . $prefix . $route : $this->routesByName[$use] = $root . $prefix . $route;
131 155
                     $count++;
132 156
                 }
@@ -143,9 +167,14 @@  discard block
 block discarded – undo
143 167
     {
144 168
         foreach ($this->routesByName as $key => $route) {
145 169
             $param = explode('#', $key);
146
-            foreach ($params as $key2 => $value) $route = str_replace(':' . $key2, $value, $route);
147
-            if ($param[0] == trim($name, '/')) return $route;
148
-            else if (isset($param[1]) && $param[1] == $name) return $route;
170
+            foreach ($params as $key2 => $value) {
171
+                $route = str_replace(':' . $key2, $value, $route);
172
+            }
173
+            if ($param[0] == trim($name, '/')) {
174
+                return $route;
175
+            } else if (isset($param[1]) && $param[1] == $name) {
176
+                return $route;
177
+            }
149 178
         }
150 179
         return null;
151 180
     }
Please login to merge, or discard this patch.