Completed
Push — master ( fab173...3e8504 )
by Sinnarasa
04:56
created
src/Routing/Router.php 3 patches
Unused Use Statements   -4 removed lines patch added patch discarded remove patch
@@ -2,10 +2,6 @@
 block discarded – undo
2 2
 
3 3
 namespace JetFire\Routing;
4 4
 
5
-use JetFire\Routing\Matcher\ArrayMatcher;
6
-use ReflectionClass;
7
-use ReflectionMethod;
8
-
9 5
 /**
10 6
  * Class Router
11 7
  * @package JetFire\Routing
Please login to merge, or discard this patch.
Spacing   +4 added lines, -4 removed lines patch added patch discarded remove patch
@@ -57,7 +57,7 @@  discard block
 block discarded – undo
57 57
         $this->collection = $collection;
58 58
         $this->response = is_null($response) ? new Response() : $response;
59 59
         $this->route = is_null($route) ? new Route() : $route;
60
-        $this->config['di'] = function ($class) {
60
+        $this->config['di'] = function($class) {
61 61
             return new $class;
62 62
         };
63 63
     }
@@ -123,10 +123,10 @@  discard block
 block discarded – undo
123 123
         if ($this->config['generateRoutesPath']) $this->collection->generateRoutesPath();
124 124
         if ($this->match() === true) {
125 125
             $this->callMiddleware('before');
126
-            if (!in_array(substr($this->response->getStatusCode(), 0, 1), [3,4,5])) {
126
+            if (!in_array(substr($this->response->getStatusCode(), 0, 1), [3, 4, 5])) {
127 127
                 $this->callTarget();
128 128
             }
129
-        }else{
129
+        } else {
130 130
             $this->response->setStatusCode(404);
131 131
         }
132 132
         $this->callMiddleware('after');
@@ -157,7 +157,7 @@  discard block
 block discarded – undo
157 157
     {
158 158
         if (is_null($url))
159 159
             $url = (isset($_GET['url'])) ? $_GET['url'] : substr(str_replace(str_replace('/index.php', '', $_SERVER['SCRIPT_NAME']), '', $_SERVER['REQUEST_URI']), 1);
160
-        $this->route->setUrl('/' . trim(explode('?', $url)[0], '/'));
160
+        $this->route->setUrl('/'.trim(explode('?', $url)[0], '/'));
161 161
     }
162 162
 
163 163
     /**
Please login to merge, or discard this patch.
Braces   +10 added lines, -5 removed lines patch added patch discarded remove patch
@@ -120,13 +120,15 @@  discard block
 block discarded – undo
120 120
     public function run()
121 121
     {
122 122
         $this->setUrl();
123
-        if ($this->config['generateRoutesPath']) $this->collection->generateRoutesPath();
123
+        if ($this->config['generateRoutesPath']) {
124
+            $this->collection->generateRoutesPath();
125
+        }
124 126
         if ($this->match() === true) {
125 127
             $this->callMiddleware('before');
126 128
             if (!in_array(substr($this->response->getStatusCode(), 0, 1), [3,4,5])) {
127 129
                 $this->callTarget();
128 130
             }
129
-        }else{
131
+        } else{
130 132
             $this->response->setStatusCode(404);
131 133
         }
132 134
         $this->callMiddleware('after');
@@ -155,8 +157,9 @@  discard block
 block discarded – undo
155 157
      */
156 158
     public function setUrl($url = null)
157 159
     {
158
-        if (is_null($url))
159
-            $url = (isset($_GET['url'])) ? $_GET['url'] : substr(str_replace(str_replace('/index.php', '', $_SERVER['SCRIPT_NAME']), '', $_SERVER['REQUEST_URI']), 1);
160
+        if (is_null($url)) {
161
+                    $url = (isset($_GET['url'])) ? $_GET['url'] : substr(str_replace(str_replace('/index.php', '', $_SERVER['SCRIPT_NAME']), '', $_SERVER['REQUEST_URI']), 1);
162
+        }
160 163
         $this->route->setUrl('/' . trim(explode('?', $url)[0], '/'));
161 164
     }
162 165
 
@@ -166,7 +169,9 @@  discard block
 block discarded – undo
166 169
     public function match()
167 170
     {
168 171
         foreach ($this->matcher as $key => $matcher) {
169
-            if (call_user_func([$this->matcher[$key], 'match'])) return true;
172
+            if (call_user_func([$this->matcher[$key], 'match'])) {
173
+                return true;
174
+            }
170 175
         }
171 176
         return false;
172 177
     }
Please login to merge, or discard this patch.
src/Routing/Matcher/ArrayMatcher.php 1 patch
Spacing   +24 added lines, -24 removed lines patch added patch discarded remove patch
@@ -98,15 +98,15 @@  discard block
 block discarded – undo
98 98
     {
99 99
         $this->request = [];
100 100
         for ($i = 0; $i < $this->router->collection->countRoutes; ++$i) {
101
-            $this->request['prefix'] = ($this->router->collection->getRoutes('prefix_' . $i) != '') ? $this->router->collection->getRoutes('prefix_' . $i) : '';
102
-            $this->request['subdomain'] = ($this->router->collection->getRoutes('subdomain_' . $i) != '') ? $this->router->collection->getRoutes('subdomain_' . $i) : '';
103
-            foreach ($this->router->collection->getRoutes('routes_' . $i) as $route => $params) {
101
+            $this->request['prefix'] = ($this->router->collection->getRoutes('prefix_'.$i) != '') ? $this->router->collection->getRoutes('prefix_'.$i) : '';
102
+            $this->request['subdomain'] = ($this->router->collection->getRoutes('subdomain_'.$i) != '') ? $this->router->collection->getRoutes('subdomain_'.$i) : '';
103
+            foreach ($this->router->collection->getRoutes('routes_'.$i) as $route => $params) {
104 104
                 $this->request['params'] = $params;
105 105
                 $this->request['collection_index'] = $i;
106 106
                 if ($this->checkSubdomain($route)) {
107 107
                     $route = strstr($route, '/');
108
-                    $this->request['route'] = preg_replace_callback('#:([\w]+)#', [$this, 'paramMatch'], '/' . trim(trim($this->request['prefix'], '/') . '/' . trim($route, '/'), '/'));
109
-                    if ($this->routeMatch('#^' . $this->request['route'] . '$#')) {
108
+                    $this->request['route'] = preg_replace_callback('#:([\w]+)#', [$this, 'paramMatch'], '/'.trim(trim($this->request['prefix'], '/').'/'.trim($route, '/'), '/'));
109
+                    if ($this->routeMatch('#^'.$this->request['route'].'$#')) {
110 110
                         $this->setCallback();
111 111
                         return $this->generateTarget();
112 112
                     }
@@ -122,19 +122,19 @@  discard block
 block discarded – undo
122 122
      */
123 123
     private function checkSubdomain($route)
124 124
     {
125
-        $url = (isset($_SERVER['REQUEST_SCHEME']) ? $_SERVER['REQUEST_SCHEME'] : 'http') . '://' . ($host = (isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : $_SERVER['HTTP_HOST']));
125
+        $url = (isset($_SERVER['REQUEST_SCHEME']) ? $_SERVER['REQUEST_SCHEME'] : 'http').'://'.($host = (isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : $_SERVER['HTTP_HOST']));
126 126
         $host = explode(':', $host)[0];
127 127
         $domain = $this->router->collection->getDomain($url);
128
-        if (!empty($this->request['subdomain']) && $route[0] == '/') $route = trim($this->request['subdomain'], '.') . '.' . $domain . $route;
128
+        if (!empty($this->request['subdomain']) && $route[0] == '/') $route = trim($this->request['subdomain'], '.').'.'.$domain.$route;
129 129
         if ($route[0] == '/') {
130 130
             return ($host != $domain) ? false : true;
131 131
         } elseif ($route[0] != '/' && $host != $domain) {
132 132
             $route = substr($route, 0, strpos($route, "/"));
133 133
             $route = str_replace('{host}', $domain, $route);
134 134
             $route = preg_replace_callback('#{subdomain}#', [$this, 'subdomainMatch'], $route);
135
-            if (preg_match('#^' . $route . '$#', $host, $this->request['called_subdomain'])) {
135
+            if (preg_match('#^'.$route.'$#', $host, $this->request['called_subdomain'])) {
136 136
                 $this->request['called_subdomain'] = array_shift($this->request['called_subdomain']);
137
-                $this->request['subdomain'] = str_replace('.' . $domain, '', $host);
137
+                $this->request['subdomain'] = str_replace('.'.$domain, '', $host);
138 138
                 return true;
139 139
             }
140 140
         }
@@ -147,7 +147,7 @@  discard block
 block discarded – undo
147 147
     private function subdomainMatch()
148 148
     {
149 149
         if (is_array($this->request['params']) && isset($this->request['params']['subdomain'])) {
150
-            return '(' . $this->request['params']['subdomain'] . ')';
150
+            return '('.$this->request['params']['subdomain'].')';
151 151
         }
152 152
         return '([^/]+)';
153 153
     }
@@ -160,7 +160,7 @@  discard block
 block discarded – undo
160 160
     {
161 161
         if (is_array($this->request['params']) && isset($this->request['params']['arguments'][$match[1]])) {
162 162
             $this->request['params']['arguments'][$match[1]] = str_replace('(', '(?:', $this->request['params']['arguments'][$match[1]]);
163
-            return '(' . $this->request['params']['arguments'][$match[1]] . ')';
163
+            return '('.$this->request['params']['arguments'][$match[1]].')';
164 164
         }
165 165
         return '([^/]+)';
166 166
     }
@@ -211,8 +211,8 @@  discard block
 block discarded – undo
211 211
         $this->checkRequest('prefix');
212 212
         $this->router->route->setDetail($this->request);
213 213
         $this->router->route->setTarget($target);
214
-        $this->router->route->addTarget('block', $this->router->collection->getRoutes('block_' . $index));
215
-        $this->router->route->addTarget('view_dir', $this->router->collection->getRoutes('view_dir_' . $index));
214
+        $this->router->route->addTarget('block', $this->router->collection->getRoutes('block_'.$index));
215
+        $this->router->route->addTarget('view_dir', $this->router->collection->getRoutes('view_dir_'.$index));
216 216
     }
217 217
 
218 218
     /**
@@ -223,8 +223,8 @@  discard block
 block discarded – undo
223 223
         if (strpos($this->request[$key], ':') !== false && isset($this->request['parameters'][0])) {
224 224
             $replacements = $this->request['parameters'];
225 225
             $keys = [];
226
-            $this->request['@' . $key] = $this->request[$key];
227
-            $this->request[$key] = preg_replace_callback('#:([\w]+)#', function ($matches) use (&$replacements, &$keys) {
226
+            $this->request['@'.$key] = $this->request[$key];
227
+            $this->request[$key] = preg_replace_callback('#:([\w]+)#', function($matches) use (&$replacements, &$keys) {
228 228
                 $keys[$matches[0]] = $replacements[0];
229 229
                 return array_shift($replacements);
230 230
             }, $this->request[$key]);
@@ -243,9 +243,9 @@  discard block
 block discarded – undo
243 243
                 $this->router->route->setCallback($this->request['params']);
244 244
             } else {
245 245
                 if (is_array($this->request['params']) && isset($this->request['params']['use'])) {
246
-                    if(is_array($this->request['params']['use']) && isset($this->request['params']['use'][$this->router->route->getMethod()])){
246
+                    if (is_array($this->request['params']['use']) && isset($this->request['params']['use'][$this->router->route->getMethod()])) {
247 247
                         $this->router->route->setCallback($this->request['params']['use'][$this->router->route->getMethod()]);
248
-                    }elseif(!is_array($this->request['params']['use'])){
248
+                    }elseif (!is_array($this->request['params']['use'])) {
249 249
                         $this->router->route->setCallback($this->request['params']['use']);
250 250
                     }
251 251
                 } else {
@@ -348,9 +348,9 @@  discard block
 block discarded – undo
348 348
             $index = isset($this->request['collection_index']) ? $this->request['collection_index'] : 0;
349 349
             $class = (class_exists($routes[0]))
350 350
                 ? $routes[0]
351
-                : $this->router->collection->getRoutes()['ctrl_namespace_' . $index] . $routes[0];
351
+                : $this->router->collection->getRoutes()['ctrl_namespace_'.$index].$routes[0];
352 352
             if (!class_exists($class)) {
353
-                throw new \Exception('Class "' . $class . '." is not found');
353
+                throw new \Exception('Class "'.$class.'." is not found');
354 354
             }
355 355
             if (method_exists($class, $routes[1])) {
356 356
                 return [
@@ -361,7 +361,7 @@  discard block
 block discarded – undo
361 361
                 ];
362 362
             }
363 363
             if (!strpos($callback, '{method}') !== false) {
364
-                throw new \Exception('The required method "' . $routes[1] . '" is not found in "' . $class . '"');
364
+                throw new \Exception('The required method "'.$routes[1].'" is not found in "'.$class.'"');
365 365
             }
366 366
         }
367 367
         return false;
@@ -378,13 +378,13 @@  discard block
 block discarded – undo
378 378
             $path = trim($callback, '/');
379 379
             $extension = substr(strrchr($path, "."), 1);
380 380
             $index = isset($this->request['collection_index']) ? $this->request['collection_index'] : 0;
381
-            $viewDir = $this->router->collection->getRoutes('view_dir_' . $index);
381
+            $viewDir = $this->router->collection->getRoutes('view_dir_'.$index);
382 382
             $target = null;
383
-            if (in_array('.' . $extension, $this->router->getConfig()['templateExtension']) && (is_file($fullPath = $viewDir . $path) || is_file($fullPath = $path))) {
383
+            if (in_array('.'.$extension, $this->router->getConfig()['templateExtension']) && (is_file($fullPath = $viewDir.$path) || is_file($fullPath = $path))) {
384 384
                 $target = $fullPath;
385 385
             } else {
386 386
                 foreach ($this->router->getConfig()['templateExtension'] as $ext) {
387
-                    if (is_file($fullPath = $viewDir . $path . $ext) || is_file($fullPath = $path . $ext)) {
387
+                    if (is_file($fullPath = $viewDir.$path.$ext) || is_file($fullPath = $path.$ext)) {
388 388
                         $target = $fullPath;
389 389
                         $extension = substr(strrchr($ext, "."), 1);
390 390
                         break;
@@ -392,7 +392,7 @@  discard block
 block discarded – undo
392 392
                 }
393 393
             }
394 394
             if (is_null($target)) {
395
-                throw new \Exception('Template file "' . $path . '" is not found in "' . $viewDir . '"');
395
+                throw new \Exception('Template file "'.$path.'" is not found in "'.$viewDir.'"');
396 396
             }
397 397
             return [
398 398
                 'dispatcher' => $this->dispatcher['isTemplate'],
Please login to merge, or discard this patch.