Completed
Push — master ( c88b81...fee020 )
by Sinnarasa
01:46
created
src/Routing/RouteCollection.php 2 patches
Spacing   +33 added lines, -33 removed lines patch added patch discarded remove patch
@@ -43,10 +43,10 @@  discard block
 block discarded – undo
43 43
     public function addRoutes($routes = null, $options = [])
44 44
     {
45 45
         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;
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 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
         $options['routes'] = is_array($routes) ? $routes : [];
52 52
         $this->setRoutes($options, $this->countRoutes);
@@ -72,10 +72,10 @@  discard block
 block discarded – undo
72 72
         if (is_array($args)) {
73 73
             $nbrArgs = count($args);
74 74
             for ($i = 0; $i < $nbrArgs; ++$i)
75
-                $this->routes['prefix_' . $i] = '/' . trim($args[$i], '/');
75
+                $this->routes['prefix_'.$i] = '/'.trim($args[$i], '/');
76 76
         } elseif (is_string($args))
77 77
             for ($i = 0; $i < $this->countRoutes; ++$i)
78
-                $this->routes['prefix_' . $i] = '/' . trim($args, '/');
78
+                $this->routes['prefix_'.$i] = '/'.trim($args, '/');
79 79
         if ($this->countRoutes == 0) $this->countRoutes++;
80 80
     }
81 81
 
@@ -88,7 +88,7 @@  discard block
 block discarded – undo
88 88
         for ($i = 0; $i < $nbrArgs; ++$i) {
89 89
             if (is_array($args[$i])) {
90 90
                 $this->setRoutes($args[$i], $i);
91
-                if (!isset($this->routes['routes_' . $i])) $this->routes['routes_' . $i] = [];
91
+                if (!isset($this->routes['routes_'.$i])) $this->routes['routes_'.$i] = [];
92 92
             }
93 93
         }
94 94
         if ($this->countRoutes == 0) $this->countRoutes++;
@@ -100,14 +100,14 @@  discard block
 block discarded – undo
100 100
      */
101 101
     public function setRoutes($args = [], $i)
102 102
     {
103
-        $this->routes['routes_' . $i] = $args['routes'];
104
-        $this->routes['block_' . $i] = (isset($args['block']) && !empty($args['block'])) ? rtrim($args['block'], '/') . '/' : '';
105
-        $this->routes['view_dir_' . $i] = (isset($args['view_dir']) && !empty($args['view_dir'])) ? $args['view_dir'] : [];
106
-        $this->routes['ctrl_namespace_' . $i] = (isset($args['ctrl_namespace']) && !empty($args['ctrl_namespace'])) ? trim($args['ctrl_namespace'], '\\') . '\\' : '';
107
-        $this->routes['prefix_' . $i] = (isset($args['prefix']) && !empty($args['prefix'])) ? '/' . trim($args['prefix'], '/') : '';
108
-        $this->routes['subdomain_' . $i] = (isset($args['subdomain'])) ? $args['subdomain'] : '';
109
-        $this->routes['protocol_' . $i] = (isset($args['protocol'])) ? $args['protocol'] : 'http';
110
-        $this->routes['params_' . $i] = (isset($args['params'])) ? $args['params'] : [];
103
+        $this->routes['routes_'.$i] = $args['routes'];
104
+        $this->routes['block_'.$i] = (isset($args['block']) && !empty($args['block'])) ? rtrim($args['block'], '/').'/' : '';
105
+        $this->routes['view_dir_'.$i] = (isset($args['view_dir']) && !empty($args['view_dir'])) ? $args['view_dir'] : [];
106
+        $this->routes['ctrl_namespace_'.$i] = (isset($args['ctrl_namespace']) && !empty($args['ctrl_namespace'])) ? trim($args['ctrl_namespace'], '\\').'\\' : '';
107
+        $this->routes['prefix_'.$i] = (isset($args['prefix']) && !empty($args['prefix'])) ? '/'.trim($args['prefix'], '/') : '';
108
+        $this->routes['subdomain_'.$i] = (isset($args['subdomain'])) ? $args['subdomain'] : '';
109
+        $this->routes['protocol_'.$i] = (isset($args['protocol'])) ? $args['protocol'] : 'http';
110
+        $this->routes['params_'.$i] = (isset($args['params'])) ? $args['params'] : [];
111 111
     }
112 112
 
113 113
     /**
@@ -120,11 +120,11 @@  discard block
 block discarded – undo
120 120
         $protocol = isset($_SERVER['REQUEST_SCHEME']) ? $_SERVER['REQUEST_SCHEME'] : 'http';
121 121
         $domain = isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : null;
122 122
 
123
-        if(!is_null($root)){
123
+        if (!is_null($root)) {
124 124
             $protocol = explode('://', $root);
125 125
             $protocol = $protocol[0];
126
-        }else{
127
-            $root = $protocol . '://' . $domain . ((!empty($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] != 80) ? ':' . $_SERVER['SERVER_PORT'] : '') . str_replace('/' . $script_file, '', $_SERVER['SCRIPT_NAME']);
126
+        } else {
127
+            $root = $protocol.'://'.$domain.((!empty($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] != 80) ? ':'.$_SERVER['SERVER_PORT'] : '').str_replace('/'.$script_file, '', $_SERVER['SCRIPT_NAME']);
128 128
         }
129 129
 
130 130
         $new_domain = $this->getDomain($root);
@@ -134,29 +134,29 @@  discard block
 block discarded – undo
134 134
 
135 135
         $count = 0;
136 136
         for ($i = 0; $i < $this->countRoutes; ++$i) {
137
-            $prefix = (isset($this->routes['prefix_' . $i])) ? $this->routes['prefix_' . $i] : '';
138
-            $subdomain = (isset($this->routes['subdomain_' . $i])) ? $this->routes['subdomain_' . $i] : '';
139
-            $block_protocol = (isset($this->routes['protocol_' . $i])) ? $this->routes['protocol_' . $i] : 'http';
140
-            $url = (!empty($subdomain)) ? str_replace($protocol . '://', $block_protocol . '://' . $subdomain . '.', $root) : $root;
141
-            if (isset($this->routes['routes_' . $i]))
142
-                foreach ($this->routes['routes_' . $i] as $route => $dependencies) {
137
+            $prefix = (isset($this->routes['prefix_'.$i])) ? $this->routes['prefix_'.$i] : '';
138
+            $subdomain = (isset($this->routes['subdomain_'.$i])) ? $this->routes['subdomain_'.$i] : '';
139
+            $block_protocol = (isset($this->routes['protocol_'.$i])) ? $this->routes['protocol_'.$i] : 'http';
140
+            $url = (!empty($subdomain)) ? str_replace($protocol.'://', $block_protocol.'://'.$subdomain.'.', $root) : $root;
141
+            if (isset($this->routes['routes_'.$i]))
142
+                foreach ($this->routes['routes_'.$i] as $route => $dependencies) {
143 143
                     if (is_array($dependencies) && isset($dependencies['use']) && !is_array($dependencies['use'])) {
144
-                        $use = (is_callable($dependencies['use'])) ? 'closure-' . $count : trim($dependencies['use'], '/');
144
+                        $use = (is_callable($dependencies['use'])) ? 'closure-'.$count : trim($dependencies['use'], '/');
145 145
                     } elseif (!is_array($dependencies)) {
146
-                        $use = (is_callable($dependencies)) ? 'closure-' . $count : trim($dependencies, '/');
146
+                        $use = (is_callable($dependencies)) ? 'closure-'.$count : trim($dependencies, '/');
147 147
                     } else {
148 148
                         $use = $route;
149 149
                     }
150 150
                     if (isset($route[0]) && $route[0] == '/') {
151
-                        $full_url = rtrim($url, '/') . '/' . trim($prefix, '/') . (empty($prefix) ? '' : '/') . trim($route, '/');
151
+                        $full_url = rtrim($url, '/').'/'.trim($prefix, '/').(empty($prefix) ? '' : '/').trim($route, '/');
152 152
                         (!is_callable($dependencies) && isset($dependencies['name']))
153
-                            ? $this->routesByName[$use . '#' . $dependencies['name']] = $full_url
153
+                            ? $this->routesByName[$use.'#'.$dependencies['name']] = $full_url
154 154
                             : $this->routesByName[$use] = $full_url;
155 155
                     } else {
156
-                        $full_url = $block_protocol . '://' . str_replace('{host}', $new_domain, $route);
156
+                        $full_url = $block_protocol.'://'.str_replace('{host}', $new_domain, $route);
157 157
                         (!is_callable($dependencies) && isset($dependencies['name']))
158
-                            ? $this->routesByName[$use . '#' . $dependencies['name']] = $full_url . $prefix
159
-                            : $this->routesByName[$use] = $full_url . $prefix;
158
+                            ? $this->routesByName[$use.'#'.$dependencies['name']] = $full_url.$prefix
159
+                            : $this->routesByName[$use] = $full_url.$prefix;
160 160
                     }
161 161
                     $count++;
162 162
                 }
@@ -187,9 +187,9 @@  discard block
 block discarded – undo
187 187
     {
188 188
         foreach ($this->routesByName as $key => $route) {
189 189
             $param = explode('#', $key);
190
-            if(isset($params['{subdomain}'])) $route = str_replace('*', $params['{subdomain}'], $route);
191
-            if(isset($params['{method}'])) $route = str_replace('*', $params['{method}'], $route);
192
-            foreach ($params as $key2 => $value) $route = str_replace(':' . $key2, $value, $route);
190
+            if (isset($params['{subdomain}'])) $route = str_replace('*', $params['{subdomain}'], $route);
191
+            if (isset($params['{method}'])) $route = str_replace('*', $params['{method}'], $route);
192
+            foreach ($params as $key2 => $value) $route = str_replace(':'.$key2, $value, $route);
193 193
             if ($param[0] == trim($name, '/')) return $route;
194 194
             else if (isset($param[1]) && $param[1] == $name) return $route;
195 195
         }
Please login to merge, or discard this patch.
Braces   +50 added lines, -22 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
         $options['routes'] = is_array($routes) ? $routes : [];
52 60
         $this->setRoutes($options, $this->countRoutes);
@@ -71,12 +79,18 @@  discard block
 block discarded – undo
71 79
     {
72 80
         if (is_array($args)) {
73 81
             $nbrArgs = count($args);
74
-            for ($i = 0; $i < $nbrArgs; ++$i)
75
-                $this->routes['prefix_' . $i] = '/' . trim($args[$i], '/');
76
-        } elseif (is_string($args))
77
-            for ($i = 0; $i < $this->countRoutes; ++$i)
78
-                $this->routes['prefix_' . $i] = '/' . trim($args, '/');
79
-        if ($this->countRoutes == 0) $this->countRoutes++;
82
+            for ($i = 0; $i < $nbrArgs; ++$i) {
83
+                            $this->routes['prefix_' . $i] = '/' . trim($args[$i], '/');
84
+            }
85
+        } elseif (is_string($args)) {
86
+                    for ($i = 0;
87
+        }
88
+        $i < $this->countRoutes; ++$i) {
89
+                            $this->routes['prefix_' . $i] = '/' . trim($args, '/');
90
+            }
91
+        if ($this->countRoutes == 0) {
92
+            $this->countRoutes++;
93
+        }
80 94
     }
81 95
 
82 96
     /**
@@ -88,10 +102,14 @@  discard block
 block discarded – undo
88 102
         for ($i = 0; $i < $nbrArgs; ++$i) {
89 103
             if (is_array($args[$i])) {
90 104
                 $this->setRoutes($args[$i], $i);
91
-                if (!isset($this->routes['routes_' . $i])) $this->routes['routes_' . $i] = [];
105
+                if (!isset($this->routes['routes_' . $i])) {
106
+                    $this->routes['routes_' . $i] = [];
107
+                }
92 108
             }
93 109
         }
94
-        if ($this->countRoutes == 0) $this->countRoutes++;
110
+        if ($this->countRoutes == 0) {
111
+            $this->countRoutes++;
112
+        }
95 113
     }
96 114
 
97 115
     /**
@@ -123,7 +141,7 @@  discard block
 block discarded – undo
123 141
         if(!is_null($root)){
124 142
             $protocol = explode('://', $root);
125 143
             $protocol = $protocol[0];
126
-        }else{
144
+        } else{
127 145
             $root = $protocol . '://' . $domain . ((!empty($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] != 80) ? ':' . $_SERVER['SERVER_PORT'] : '') . str_replace('/' . $script_file, '', $_SERVER['SCRIPT_NAME']);
128 146
         }
129 147
 
@@ -138,10 +156,11 @@  discard block
 block discarded – undo
138 156
             $subdomain = (isset($this->routes['subdomain_' . $i])) ? $this->routes['subdomain_' . $i] : '';
139 157
             $block_protocol = (isset($this->routes['protocol_' . $i])) ? $this->routes['protocol_' . $i] : 'http';
140 158
             $url = (!empty($subdomain)) ? str_replace($protocol . '://', $block_protocol . '://' . $subdomain . '.', $root) : $root;
141
-            if (isset($this->routes['routes_' . $i]))
142
-                foreach ($this->routes['routes_' . $i] as $route => $dependencies) {
159
+            if (isset($this->routes['routes_' . $i])) {
160
+                            foreach ($this->routes['routes_' . $i] as $route => $dependencies) {
143 161
                     if (is_array($dependencies) && isset($dependencies['use']) && !is_array($dependencies['use'])) {
144
-                        $use = (is_callable($dependencies['use'])) ? 'closure-' . $count : trim($dependencies['use'], '/');
162
+                        $use = (is_callable($dependencies['use'])) ? 'closure-' . $count : trim($dependencies['use'], '/');
163
+            }
145 164
                     } elseif (!is_array($dependencies)) {
146 165
                         $use = (is_callable($dependencies)) ? 'closure-' . $count : trim($dependencies, '/');
147 166
                     } else {
@@ -187,11 +206,20 @@  discard block
 block discarded – undo
187 206
     {
188 207
         foreach ($this->routesByName as $key => $route) {
189 208
             $param = explode('#', $key);
190
-            if(isset($params['{subdomain}'])) $route = str_replace('*', $params['{subdomain}'], $route);
191
-            if(isset($params['{method}'])) $route = str_replace('*', $params['{method}'], $route);
192
-            foreach ($params as $key2 => $value) $route = str_replace(':' . $key2, $value, $route);
193
-            if ($param[0] == trim($name, '/')) return $route;
194
-            else if (isset($param[1]) && $param[1] == $name) return $route;
209
+            if(isset($params['{subdomain}'])) {
210
+                $route = str_replace('*', $params['{subdomain}'], $route);
211
+            }
212
+            if(isset($params['{method}'])) {
213
+                $route = str_replace('*', $params['{method}'], $route);
214
+            }
215
+            foreach ($params as $key2 => $value) {
216
+                $route = str_replace(':' . $key2, $value, $route);
217
+            }
218
+            if ($param[0] == trim($name, '/')) {
219
+                return $route;
220
+            } else if (isset($param[1]) && $param[1] == $name) {
221
+                return $route;
222
+            }
195 223
         }
196 224
         return null;
197 225
     }
Please login to merge, or discard this patch.