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