Completed
Push — master ( c2c9db...effdc3 )
by Sinnarasa
04:18 queued 13s
created
src/Routing/RouteCollection.php 2 patches
Spacing   +25 added lines, -25 removed lines patch added patch discarded remove patch
@@ -47,12 +47,12 @@  discard block
 block discarded – undo
47 47
     public function addRoutes($routes = null, $options = [])
48 48
     {
49 49
         if (!is_null($routes) && !is_array($routes)) {
50
-            if (strpos($routes, '.php') === false) $routes = trim($routes, '/') . '/';
51
-            if (is_file($routes . '/routes.php') && is_array($routesFile = include $routes . '/routes.php')) $routes = $routesFile;
50
+            if (strpos($routes, '.php') === false) $routes = trim($routes, '/').'/';
51
+            if (is_file($routes.'/routes.php') && is_array($routesFile = include $routes.'/routes.php')) $routes = $routesFile;
52 52
             elseif (is_file($routes) && is_array($routesFile = include $routes)) $routes = $routesFile;
53
-            else throw new \InvalidArgumentException('Argument for "' . get_called_class() . '" constructor is not recognized. Expected argument array or file containing array but "' . $routes . '" given');
53
+            else throw new \InvalidArgumentException('Argument for "'.get_called_class().'" constructor is not recognized. Expected argument array or file containing array but "'.$routes.'" given');
54 54
         }
55
-        $this->routes['routes_' . $this->countRoutes] = is_array($routes) ? $routes : [];
55
+        $this->routes['routes_'.$this->countRoutes] = is_array($routes) ? $routes : [];
56 56
         $this->setRoutes($options, $this->countRoutes);
57 57
         $this->countRoutes++;
58 58
     }
@@ -76,10 +76,10 @@  discard block
 block discarded – undo
76 76
         if (is_array($args)) {
77 77
             $nbrArgs = count($args);
78 78
             for ($i = 0; $i < $nbrArgs; ++$i)
79
-                $this->routes['prefix_' . $i] = '/' . trim($args[$i], '/');
79
+                $this->routes['prefix_'.$i] = '/'.trim($args[$i], '/');
80 80
         } elseif (is_string($args))
81 81
             for ($i = 0; $i < $this->countRoutes; ++$i)
82
-                $this->routes['prefix_' . $i] = '/' . trim($args, '/');
82
+                $this->routes['prefix_'.$i] = '/'.trim($args, '/');
83 83
         if ($this->countRoutes == 0) $this->countRoutes++;
84 84
     }
85 85
 
@@ -92,7 +92,7 @@  discard block
 block discarded – undo
92 92
         for ($i = 0; $i < $nbrArgs; ++$i) {
93 93
             if (is_array($args[$i])) {
94 94
                 $this->setRoutes($args[$i], $i);
95
-                if (!isset($this->routes['routes_' . $i])) $this->routes['routes_' . $i] = [];
95
+                if (!isset($this->routes['routes_'.$i])) $this->routes['routes_'.$i] = [];
96 96
             }
97 97
         }
98 98
         if ($this->countRoutes == 0) $this->countRoutes++;
@@ -104,11 +104,11 @@  discard block
 block discarded – undo
104 104
      */
105 105
     private function setRoutes($args = [], $i)
106 106
     {
107
-        $this->routes['block_' . $i] = (isset($args['block']) && !empty($args['block'])) ? rtrim($args['block'], '/') . '/' : '';
108
-        $this->routes['view_dir_' . $i] = (isset($args['view_dir']) && !empty($args['view_dir'])) ? rtrim($args['view_dir'], '/') . '/' : '';
109
-        $this->routes['ctrl_namespace_' . $i] = (isset($args['ctrl_namespace']) && !empty($args['ctrl_namespace'])) ? trim($args['ctrl_namespace'], '\\') . '\\' : '';
110
-        $this->routes['prefix_' . $i] = (isset($args['prefix']) && !empty($args['prefix'])) ? '/' . trim($args['prefix'], '/') : '';
111
-        $this->routes['subdomain_' . $i] = (isset($args['subdomain'])) ? $args['subdomain'] : '';
107
+        $this->routes['block_'.$i] = (isset($args['block']) && !empty($args['block'])) ? rtrim($args['block'], '/').'/' : '';
108
+        $this->routes['view_dir_'.$i] = (isset($args['view_dir']) && !empty($args['view_dir'])) ? rtrim($args['view_dir'], '/').'/' : '';
109
+        $this->routes['ctrl_namespace_'.$i] = (isset($args['ctrl_namespace']) && !empty($args['ctrl_namespace'])) ? trim($args['ctrl_namespace'], '\\').'\\' : '';
110
+        $this->routes['prefix_'.$i] = (isset($args['prefix']) && !empty($args['prefix'])) ? '/'.trim($args['prefix'], '/') : '';
111
+        $this->routes['subdomain_'.$i] = (isset($args['subdomain'])) ? $args['subdomain'] : '';
112 112
     }
113 113
 
114 114
     /**
@@ -135,32 +135,32 @@  discard block
 block discarded – undo
135 135
         $protocol = isset($_SERVER['REQUEST_SCHEME']) ? $_SERVER['REQUEST_SCHEME'] : 'http';
136 136
         $domain = isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : null;
137 137
         $root = (is_null($root))
138
-            ? $protocol . '://' . $domain . ((!empty($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] != 80) ? ':' . $_SERVER['SERVER_PORT'] : '') . str_replace('/' . $script_file, '', $_SERVER['SCRIPT_NAME'])
138
+            ? $protocol.'://'.$domain.((!empty($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] != 80) ? ':'.$_SERVER['SERVER_PORT'] : '').str_replace('/'.$script_file, '', $_SERVER['SCRIPT_NAME'])
139 139
             : $root;
140 140
         $new_domain = $this->getDomain($root);
141 141
         if (!is_null($domain) && strpos($domain, $new_domain) !== false)
142 142
             $root = str_replace($domain, $new_domain, $root);
143 143
         $count = 0;
144 144
         for ($i = 0; $i < $this->countRoutes; ++$i) {
145
-            $prefix = (isset($this->routes['prefix_' . $i])) ? $this->routes['prefix_' . $i] : '';
146
-            $subdomain = (isset($this->routes['subdomain_' . $i])) ? $this->routes['subdomain_' . $i] : '';
147
-            $url = (!empty($subdomain)) ? str_replace($protocol.'://',$protocol.'://'.$subdomain.'.' ,$root) : $root;
148
-            if (isset($this->routes['routes_' . $i]))
149
-                foreach ($this->routes['routes_' . $i] as $route => $dependencies) {
145
+            $prefix = (isset($this->routes['prefix_'.$i])) ? $this->routes['prefix_'.$i] : '';
146
+            $subdomain = (isset($this->routes['subdomain_'.$i])) ? $this->routes['subdomain_'.$i] : '';
147
+            $url = (!empty($subdomain)) ? str_replace($protocol.'://', $protocol.'://'.$subdomain.'.', $root) : $root;
148
+            if (isset($this->routes['routes_'.$i]))
149
+                foreach ($this->routes['routes_'.$i] as $route => $dependencies) {
150 150
                     if (is_array($dependencies) && isset($dependencies['use']))
151
-                        $use = (is_callable($dependencies['use'])) ? 'closure-' . $count : trim($dependencies['use'], '/');
151
+                        $use = (is_callable($dependencies['use'])) ? 'closure-'.$count : trim($dependencies['use'], '/');
152 152
                     elseif (!is_array($dependencies))
153
-                        $use = (is_callable($dependencies)) ? 'closure-' . $count : trim($dependencies, '/');
153
+                        $use = (is_callable($dependencies)) ? 'closure-'.$count : trim($dependencies, '/');
154 154
                     else
155 155
                         $use = $route;
156 156
                     if (isset($route[0]) && $route[0] == '/') {
157 157
                         (!is_callable($dependencies) && isset($dependencies['name']))
158
-                            ? $this->routesByName[$use . '#' . $dependencies['name']] = $url . $prefix . $route
159
-                            : $this->routesByName[$use] = $url . $prefix . $route;
158
+                            ? $this->routesByName[$use.'#'.$dependencies['name']] = $url.$prefix.$route
159
+                            : $this->routesByName[$use] = $url.$prefix.$route;
160 160
                     } else {
161 161
                         (!is_callable($dependencies) && isset($dependencies['name']))
162
-                            ? $this->routesByName[$use . '#' . $dependencies['name']] = $protocol . '://' . str_replace('{host}', $new_domain, $route) . $prefix
163
-                            : $this->routesByName[$use] = $protocol . '://' . str_replace('{host}', $new_domain, $route) . $prefix;
162
+                            ? $this->routesByName[$use.'#'.$dependencies['name']] = $protocol.'://'.str_replace('{host}', $new_domain, $route).$prefix
163
+                            : $this->routesByName[$use] = $protocol.'://'.str_replace('{host}', $new_domain, $route).$prefix;
164 164
                     }
165 165
                     $count++;
166 166
                 }
@@ -193,7 +193,7 @@  discard block
 block discarded – undo
193 193
         foreach ($this->routesByName as $key => $route) {
194 194
             $param = explode('#', $key);
195 195
             $route = str_replace('{subdomain}', $subdomain, $route);
196
-            foreach ($params as $key2 => $value) $route = str_replace(':' . $key2, $value, $route);
196
+            foreach ($params as $key2 => $value) $route = str_replace(':'.$key2, $value, $route);
197 197
             if ($param[0] == trim($name, '/')) return $route;
198 198
             else if (isset($param[1]) && $param[1] == $name) return $route;
199 199
         }
Please login to merge, or discard this patch.
Braces   +63 added lines, -33 removed lines patch added patch discarded remove patch
@@ -37,7 +37,9 @@  discard block
 block discarded – undo
37 37
      */
38 38
     public function __construct($routes = null, $options = [])
39 39
     {
40
-        if (!is_null($routes) || !empty($options)) $this->addRoutes($routes, $options);
40
+        if (!is_null($routes) || !empty($options)) {
41
+            $this->addRoutes($routes, $options);
42
+        }
41 43
     }
42 44
 
43 45
     /**
@@ -47,10 +49,16 @@  discard block
 block discarded – undo
47 49
     public function addRoutes($routes = null, $options = [])
48 50
     {
49 51
         if (!is_null($routes) && !is_array($routes)) {
50
-            if (strpos($routes, '.php') === false) $routes = trim($routes, '/') . '/';
51
-            if (is_file($routes . '/routes.php') && is_array($routesFile = include $routes . '/routes.php')) $routes = $routesFile;
52
-            elseif (is_file($routes) && is_array($routesFile = include $routes)) $routes = $routesFile;
53
-            else throw new \InvalidArgumentException('Argument for "' . get_called_class() . '" constructor is not recognized. Expected argument array or file containing array but "' . $routes . '" given');
52
+            if (strpos($routes, '.php') === false) {
53
+                $routes = trim($routes, '/') . '/';
54
+            }
55
+            if (is_file($routes . '/routes.php') && is_array($routesFile = include $routes . '/routes.php')) {
56
+                $routes = $routesFile;
57
+            } elseif (is_file($routes) && is_array($routesFile = include $routes)) {
58
+                $routes = $routesFile;
59
+            } else {
60
+                throw new \InvalidArgumentException('Argument for "' . get_called_class() . '" constructor is not recognized. Expected argument array or file containing array but "' . $routes . '" given');
61
+            }
54 62
         }
55 63
         $this->routes['routes_' . $this->countRoutes] = is_array($routes) ? $routes : [];
56 64
         $this->setRoutes($options, $this->countRoutes);
@@ -63,8 +71,9 @@  discard block
 block discarded – undo
63 71
      */
64 72
     public function getRoutes($key = null)
65 73
     {
66
-        if (!is_null($key))
67
-            return isset($this->routes[$key]) ? $this->routes[$key] : '';
74
+        if (!is_null($key)) {
75
+                    return isset($this->routes[$key]) ? $this->routes[$key] : '';
76
+        }
68 77
         return $this->routes;
69 78
     }
70 79
 
@@ -75,12 +84,18 @@  discard block
 block discarded – undo
75 84
     {
76 85
         if (is_array($args)) {
77 86
             $nbrArgs = count($args);
78
-            for ($i = 0; $i < $nbrArgs; ++$i)
79
-                $this->routes['prefix_' . $i] = '/' . trim($args[$i], '/');
80
-        } elseif (is_string($args))
81
-            for ($i = 0; $i < $this->countRoutes; ++$i)
82
-                $this->routes['prefix_' . $i] = '/' . trim($args, '/');
83
-        if ($this->countRoutes == 0) $this->countRoutes++;
87
+            for ($i = 0; $i < $nbrArgs; ++$i) {
88
+                            $this->routes['prefix_' . $i] = '/' . trim($args[$i], '/');
89
+            }
90
+        } elseif (is_string($args)) {
91
+                    for ($i = 0;
92
+        }
93
+        $i < $this->countRoutes; ++$i) {
94
+                            $this->routes['prefix_' . $i] = '/' . trim($args, '/');
95
+            }
96
+        if ($this->countRoutes == 0) {
97
+            $this->countRoutes++;
98
+        }
84 99
     }
85 100
 
86 101
     /**
@@ -92,10 +107,14 @@  discard block
 block discarded – undo
92 107
         for ($i = 0; $i < $nbrArgs; ++$i) {
93 108
             if (is_array($args[$i])) {
94 109
                 $this->setRoutes($args[$i], $i);
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
     /**
@@ -117,12 +136,16 @@  discard block
 block discarded – undo
117 136
      */
118 137
     public function setMiddleware($middleware)
119 138
     {
120
-        if (is_string($middleware)) $middleware = rtrim($middleware, '/');
121
-        if (is_array($middleware))
122
-            $this->middleware = $middleware;
123
-        elseif (is_file($middleware) && is_array($mid = include $middleware))
124
-            $this->middleware = $mid;
125
-        else throw new \InvalidArgumentException('Accepted argument for setMiddleware are array and array file');
139
+        if (is_string($middleware)) {
140
+            $middleware = rtrim($middleware, '/');
141
+        }
142
+        if (is_array($middleware)) {
143
+                    $this->middleware = $middleware;
144
+        } elseif (is_file($middleware) && is_array($mid = include $middleware)) {
145
+                    $this->middleware = $mid;
146
+        } else {
147
+            throw new \InvalidArgumentException('Accepted argument for setMiddleware are array and array file');
148
+        }
126 149
     }
127 150
 
128 151
     /**
@@ -138,21 +161,23 @@  discard block
 block discarded – undo
138 161
             ? $protocol . '://' . $domain . ((!empty($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] != 80) ? ':' . $_SERVER['SERVER_PORT'] : '') . str_replace('/' . $script_file, '', $_SERVER['SCRIPT_NAME'])
139 162
             : $root;
140 163
         $new_domain = $this->getDomain($root);
141
-        if (!is_null($domain) && strpos($domain, $new_domain) !== false)
142
-            $root = str_replace($domain, $new_domain, $root);
164
+        if (!is_null($domain) && strpos($domain, $new_domain) !== false) {
165
+                    $root = str_replace($domain, $new_domain, $root);
166
+        }
143 167
         $count = 0;
144 168
         for ($i = 0; $i < $this->countRoutes; ++$i) {
145 169
             $prefix = (isset($this->routes['prefix_' . $i])) ? $this->routes['prefix_' . $i] : '';
146 170
             $subdomain = (isset($this->routes['subdomain_' . $i])) ? $this->routes['subdomain_' . $i] : '';
147 171
             $url = (!empty($subdomain)) ? str_replace($protocol.'://',$protocol.'://'.$subdomain.'.' ,$root) : $root;
148
-            if (isset($this->routes['routes_' . $i]))
149
-                foreach ($this->routes['routes_' . $i] as $route => $dependencies) {
172
+            if (isset($this->routes['routes_' . $i])) {
173
+                            foreach ($this->routes['routes_' . $i] as $route => $dependencies) {
150 174
                     if (is_array($dependencies) && isset($dependencies['use']))
151
-                        $use = (is_callable($dependencies['use'])) ? 'closure-' . $count : trim($dependencies['use'], '/');
152
-                    elseif (!is_array($dependencies))
153
-                        $use = (is_callable($dependencies)) ? 'closure-' . $count : trim($dependencies, '/');
154
-                    else
155
-                        $use = $route;
175
+                        $use = (is_callable($dependencies['use'])) ? 'closure-' . $count : trim($dependencies['use'], '/');
176
+            } elseif (!is_array($dependencies)) {
177
+                                            $use = (is_callable($dependencies)) ? 'closure-' . $count : trim($dependencies, '/');
178
+                    } else {
179
+                                            $use = $route;
180
+                    }
156 181
                     if (isset($route[0]) && $route[0] == '/') {
157 182
                         (!is_callable($dependencies) && isset($dependencies['name']))
158 183
                             ? $this->routesByName[$use . '#' . $dependencies['name']] = $url . $prefix . $route
@@ -193,9 +218,14 @@  discard block
 block discarded – undo
193 218
         foreach ($this->routesByName as $key => $route) {
194 219
             $param = explode('#', $key);
195 220
             $route = str_replace('{subdomain}', $subdomain, $route);
196
-            foreach ($params as $key2 => $value) $route = str_replace(':' . $key2, $value, $route);
197
-            if ($param[0] == trim($name, '/')) return $route;
198
-            else if (isset($param[1]) && $param[1] == $name) return $route;
221
+            foreach ($params as $key2 => $value) {
222
+                $route = str_replace(':' . $key2, $value, $route);
223
+            }
224
+            if ($param[0] == trim($name, '/')) {
225
+                return $route;
226
+            } else if (isset($param[1]) && $param[1] == $name) {
227
+                return $route;
228
+            }
199 229
         }
200 230
         return null;
201 231
     }
Please login to merge, or discard this patch.