Completed
Push — master ( cd2d6e...bff802 )
by Sinnarasa
04:31
created
src/Routing/RouteCollection.php 1 patch
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,31 +135,31 @@  discard block
 block discarded – undo
135 135
         $protocol = (isset($_SERVER['REQUEST_SCHEME']) ? $_SERVER['REQUEST_SCHEME'] : 'http');
136 136
         $domain = $_SERVER['SERVER_NAME'];
137 137
         $root = (is_null($root))
138
-            ? $protocol . '://' . $domain . ((!empty($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] != 80 && $_SERVER['SERVER_PORT'] != 8080) ? ':' . $_SERVER['SERVER_PORT'] : '') . str_replace('/' . $script_file, '', $_SERVER['SCRIPT_NAME'])
138
+            ? $protocol.'://'.$domain.((!empty($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] != 80 && $_SERVER['SERVER_PORT'] != 8080) ? ':'.$_SERVER['SERVER_PORT'] : '').str_replace('/'.$script_file, '', $_SERVER['SCRIPT_NAME'])
139 139
             : $root;
140 140
         if (strpos($domain, ($new_domain = $this->getDomain($root))) !== false)
141 141
             $root = str_replace($domain, $new_domain, $root);
142 142
         $count = 0;
143 143
         for ($i = 0; $i < $this->countRoutes; ++$i) {
144
-            $prefix = (isset($this->routes['prefix_' . $i])) ? $this->routes['prefix_' . $i] : '';
145
-            $subdomain = (isset($this->routes['subdomain_' . $i])) ? $this->routes['subdomain_' . $i] : '';
146
-            $url = (!empty($subdomain)) ? str_replace($protocol.'://',$protocol.'://'.$subdomain.'.' ,$root) : $root;
147
-            if (isset($this->routes['routes_' . $i]))
148
-                foreach ($this->routes['routes_' . $i] as $route => $dependencies) {
144
+            $prefix = (isset($this->routes['prefix_'.$i])) ? $this->routes['prefix_'.$i] : '';
145
+            $subdomain = (isset($this->routes['subdomain_'.$i])) ? $this->routes['subdomain_'.$i] : '';
146
+            $url = (!empty($subdomain)) ? str_replace($protocol.'://', $protocol.'://'.$subdomain.'.', $root) : $root;
147
+            if (isset($this->routes['routes_'.$i]))
148
+                foreach ($this->routes['routes_'.$i] as $route => $dependencies) {
149 149
                     if (is_array($dependencies) && isset($dependencies['use']))
150
-                        $use = (is_callable($dependencies['use'])) ? 'closure-' . $count : trim($dependencies['use'], '/');
150
+                        $use = (is_callable($dependencies['use'])) ? 'closure-'.$count : trim($dependencies['use'], '/');
151 151
                     elseif (!is_array($dependencies))
152
-                        $use = (is_callable($dependencies)) ? 'closure-' . $count : trim($dependencies, '/');
152
+                        $use = (is_callable($dependencies)) ? 'closure-'.$count : trim($dependencies, '/');
153 153
                     else
154 154
                         $use = $route;
155 155
                     if (isset($route[0]) && $route[0] == '/') {
156 156
                         (!is_callable($dependencies) && isset($dependencies['name']))
157
-                            ? $this->routesByName[$use . '#' . $dependencies['name']] = $url . $prefix . $route
158
-                            : $this->routesByName[$use] = $url . $prefix . $route;
157
+                            ? $this->routesByName[$use.'#'.$dependencies['name']] = $url.$prefix.$route
158
+                            : $this->routesByName[$use] = $url.$prefix.$route;
159 159
                     } else {
160 160
                         (!is_callable($dependencies) && isset($dependencies['name']))
161
-                            ? $this->routesByName[$use . '#' . $dependencies['name']] = $protocol . '://' . str_replace('{host}', $new_domain, $route) . $prefix
162
-                            : $this->routesByName[$use] = $protocol . '://' . str_replace('{host}', $new_domain, $route) . $prefix;
161
+                            ? $this->routesByName[$use.'#'.$dependencies['name']] = $protocol.'://'.str_replace('{host}', $new_domain, $route).$prefix
162
+                            : $this->routesByName[$use] = $protocol.'://'.str_replace('{host}', $new_domain, $route).$prefix;
163 163
                     }
164 164
                     $count++;
165 165
                 }
@@ -192,7 +192,7 @@  discard block
 block discarded – undo
192 192
         foreach ($this->routesByName as $key => $route) {
193 193
             $param = explode('#', $key);
194 194
             $route = str_replace('{subdomain}', $subdomain, $route);
195
-            foreach ($params as $key2 => $value) $route = str_replace(':' . $key2, $value, $route);
195
+            foreach ($params as $key2 => $value) $route = str_replace(':'.$key2, $value, $route);
196 196
             if ($param[0] == trim($name, '/')) return $route;
197 197
             else if (isset($param[1]) && $param[1] == $name) return $route;
198 198
         }
Please login to merge, or discard this patch.
src/Routing/Dispatcher/ClosureDispatcher.php 1 patch
Braces   +5 added lines, -2 removed lines patch added patch discarded remove patch
@@ -43,8 +43,11 @@
 block discarded – undo
43 43
             $this->response->setHeaders(['Content-Type' => 'text/html']);
44 44
         }
45 45
         $params = ($this->route->getParameters() == '') ? [] : $this->route->getParameters();
46
-        if (is_array($content = call_user_func_array($this->route->getTarget('closure'), $params))) $this->route->addTarget('data', $content);
47
-        elseif (!is_null($content)) $this->response->setContent($content);
46
+        if (is_array($content = call_user_func_array($this->route->getTarget('closure'), $params))) {
47
+            $this->route->addTarget('data', $content);
48
+        } elseif (!is_null($content)) {
49
+            $this->response->setContent($content);
50
+        }
48 51
     }
49 52
 
50 53
 }
Please login to merge, or discard this patch.
src/Routing/Dispatcher/ControllerDispatcher.php 2 patches
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -72,7 +72,7 @@
 block discarded – undo
72 72
     {
73 73
         $reflector = new ReflectionClass($this->route->getTarget('controller'));
74 74
         if (!$reflector->isInstantiable())
75
-            throw new \Exception('Target [' . $this->route->getTarget('controller') . '] is not instantiable.');
75
+            throw new \Exception('Target ['.$this->route->getTarget('controller').'] is not instantiable.');
76 76
         $constructor = $reflector->getConstructor();
77 77
         if (is_null($constructor)) {
78 78
             $class = $this->route->getTarget('controller');
Please login to merge, or discard this patch.
Braces   +12 added lines, -7 removed lines patch added patch discarded remove patch
@@ -50,17 +50,21 @@  discard block
 block discarded – undo
50 50
         }
51 51
         if ($count == count($this->route->getParameters()) || ($this->route->getParameters() == '' && $count == 0)) {
52 52
             $dependencies = array_merge($dependencies, ($this->route->getParameters() == '') ? [] : $this->route->getParameters());
53
-            if ($this->response->getStatusCode() == 202)
54
-                $this->response->setStatusCode(200);
53
+            if ($this->response->getStatusCode() == 202) {
54
+                            $this->response->setStatusCode(200);
55
+            }
55 56
             if (is_array($content = $reflectionMethod->invokeArgs($this->getController(), $dependencies))) {
56 57
                 $this->route->addTarget('data', $content);
57 58
                 if (isset($this->route->getParams()['ajax']) && $this->route->getParams()['ajax'] === true) {
58 59
                     $this->response->setContent(json_encode($content));
59 60
                     $this->response->setHeaders(['Content-Type' => 'application/json']);
60 61
                 }
61
-            } elseif (!is_null($content)) $this->response->setContent($content);
62
-        } else
63
-            $this->response->setStatusCode(404);
62
+            } elseif (!is_null($content)) {
63
+                $this->response->setContent($content);
64
+            }
65
+        } else {
66
+                    $this->response->setStatusCode(404);
67
+        }
64 68
     }
65 69
 
66 70
 
@@ -71,8 +75,9 @@  discard block
 block discarded – undo
71 75
     private function getController()
72 76
     {
73 77
         $reflector = new ReflectionClass($this->route->getTarget('controller'));
74
-        if (!$reflector->isInstantiable())
75
-            throw new \Exception('Target [' . $this->route->getTarget('controller') . '] is not instantiable.');
78
+        if (!$reflector->isInstantiable()) {
79
+                    throw new \Exception('Target [' . $this->route->getTarget('controller') . '] is not instantiable.');
80
+        }
76 81
         $constructor = $reflector->getConstructor();
77 82
         if (is_null($constructor)) {
78 83
             $class = $this->route->getTarget('controller');
Please login to merge, or discard this patch.