Passed
Push — master ( fee020...50f7b0 )
by Sinnarasa
01:52
created
src/Routing/Matcher/ArrayMatcher.php 1 patch
Spacing   +28 added lines, -28 removed lines patch added patch discarded remove patch
@@ -97,15 +97,15 @@  discard block
 block discarded – undo
97 97
     {
98 98
         $this->request = [];
99 99
         for ($i = 0; $i < $this->router->collection->countRoutes; ++$i) {
100
-            $this->request['prefix'] = ($this->router->collection->getRoutes('prefix_' . $i) != '') ? $this->router->collection->getRoutes('prefix_' . $i) : '';
101
-            $this->request['subdomain'] = ($this->router->collection->getRoutes('subdomain_' . $i) != '') ? $this->router->collection->getRoutes('subdomain_' . $i) : '';
102
-            foreach ($this->router->collection->getRoutes('routes_' . $i) as $route => $params) {
100
+            $this->request['prefix'] = ($this->router->collection->getRoutes('prefix_'.$i) != '') ? $this->router->collection->getRoutes('prefix_'.$i) : '';
101
+            $this->request['subdomain'] = ($this->router->collection->getRoutes('subdomain_'.$i) != '') ? $this->router->collection->getRoutes('subdomain_'.$i) : '';
102
+            foreach ($this->router->collection->getRoutes('routes_'.$i) as $route => $params) {
103 103
                 $this->request['params'] = $params;
104 104
                 $this->request['collection_index'] = $i;
105 105
                 if ($this->checkSubdomain($route)) {
106 106
                     $route = strstr($route, '/');
107
-                    $this->request['route'] = preg_replace_callback('#:([\w]+)#', [$this, 'paramMatch'], '/' . trim(trim($this->request['prefix'], '/') . '/' . trim($route, '/'), '/'));
108
-                    if ($this->routeMatch('#^' . $this->request['route'] . '$#')) {
107
+                    $this->request['route'] = preg_replace_callback('#:([\w]+)#', [$this, 'paramMatch'], '/'.trim(trim($this->request['prefix'], '/').'/'.trim($route, '/'), '/'));
108
+                    if ($this->routeMatch('#^'.$this->request['route'].'$#')) {
109 109
                         $this->setCallback();
110 110
                         return $this->generateTarget();
111 111
                     }
@@ -121,19 +121,19 @@  discard block
 block discarded – undo
121 121
      */
122 122
     private function checkSubdomain($route)
123 123
     {
124
-        $url = (isset($_SERVER['REQUEST_SCHEME']) ? $_SERVER['REQUEST_SCHEME'] : 'http') . '://' . ($host = (isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : $_SERVER['HTTP_HOST']));
124
+        $url = (isset($_SERVER['REQUEST_SCHEME']) ? $_SERVER['REQUEST_SCHEME'] : 'http').'://'.($host = (isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : $_SERVER['HTTP_HOST']));
125 125
         $host = explode(':', $host)[0];
126 126
         $domain = $this->router->collection->getDomain($url);
127
-        if (!empty($this->request['subdomain']) && $route[0] == '/') $route = trim($this->request['subdomain'], '.') . '.' . $domain . $route;
127
+        if (!empty($this->request['subdomain']) && $route[0] == '/') $route = trim($this->request['subdomain'], '.').'.'.$domain.$route;
128 128
         if ($route[0] == '/') {
129 129
             return ($host != $domain) ? false : true;
130 130
         } elseif ($route[0] != '/' && $host != $domain) {
131 131
             $route = substr($route, 0, strpos($route, "/"));
132 132
             $route = str_replace('{host}', $domain, $route);
133 133
             $route = preg_replace_callback('#{subdomain}#', [$this, 'subdomainMatch'], $route);
134
-            if (preg_match('#^' . $route . '$#', $host, $this->request['called_subdomain'])) {
134
+            if (preg_match('#^'.$route.'$#', $host, $this->request['called_subdomain'])) {
135 135
                 $this->request['called_subdomain'] = array_shift($this->request['called_subdomain']);
136
-                $this->request['subdomain'] = str_replace('.' . $domain, '', $host);
136
+                $this->request['subdomain'] = str_replace('.'.$domain, '', $host);
137 137
                 return true;
138 138
             }
139 139
         }
@@ -146,7 +146,7 @@  discard block
 block discarded – undo
146 146
     private function subdomainMatch()
147 147
     {
148 148
         if (is_array($this->request['params']) && isset($this->request['params']['subdomain'])) {
149
-            return '(' . $this->request['params']['subdomain'] . ')';
149
+            return '('.$this->request['params']['subdomain'].')';
150 150
         }
151 151
         return '([^/]+)';
152 152
     }
@@ -159,11 +159,11 @@  discard block
 block discarded – undo
159 159
     {
160 160
         if (is_array($this->request['params']) && isset($this->request['params']['arguments'][$match[1]])) {
161 161
             $this->request['params']['arguments'][$match[1]] = str_replace('(', '(?:', $this->request['params']['arguments'][$match[1]]);
162
-            return '(' . $this->request['params']['arguments'][$match[1]] . ')';
162
+            return '('.$this->request['params']['arguments'][$match[1]].')';
163 163
         }
164
-        if(isset($this->router->collection->getRoutes('params_' . $this->request['collection_index'])['arguments'][$match[1]])){
165
-            $this->request['params']['arguments'][$match[1]] = str_replace('(', '(?:', $this->router->collection->getRoutes('params_' . $this->request['collection_index'])['arguments'][$match[1]]);
166
-            return '(' . $this->request['params']['arguments'][$match[1]] . ')';
164
+        if (isset($this->router->collection->getRoutes('params_'.$this->request['collection_index'])['arguments'][$match[1]])) {
165
+            $this->request['params']['arguments'][$match[1]] = str_replace('(', '(?:', $this->router->collection->getRoutes('params_'.$this->request['collection_index'])['arguments'][$match[1]]);
166
+            return '('.$this->request['params']['arguments'][$match[1]].')';
167 167
         }
168 168
         return '([^/]+)';
169 169
     }
@@ -174,7 +174,7 @@  discard block
 block discarded – undo
174 174
      */
175 175
     private function routeMatch($regex)
176 176
     {
177
-        $regex = (substr($this->request['route'], -1) == '*') ? '#^' . $this->request['route'] . '#' : $regex;
177
+        $regex = (substr($this->request['route'], -1) == '*') ? '#^'.$this->request['route'].'#' : $regex;
178 178
         if (preg_match($regex, $this->router->route->getUrl(), $this->request['parameters'])) {
179 179
             array_shift($this->request['parameters']);
180 180
             return true;
@@ -209,9 +209,9 @@  discard block
 block discarded – undo
209 209
         $this->checkRequest('prefix');
210 210
         $this->router->route->setDetail($this->request);
211 211
         $this->router->route->setTarget($target);
212
-        $this->router->route->addTarget('block', $this->router->collection->getRoutes('block_' . $index));
213
-        $this->router->route->addTarget('view_dir', $this->router->collection->getRoutes('view_dir_' . $index));
214
-        $this->router->route->addTarget('params', $this->router->collection->getRoutes('params_' . $index));
212
+        $this->router->route->addTarget('block', $this->router->collection->getRoutes('block_'.$index));
213
+        $this->router->route->addTarget('view_dir', $this->router->collection->getRoutes('view_dir_'.$index));
214
+        $this->router->route->addTarget('params', $this->router->collection->getRoutes('params_'.$index));
215 215
     }
216 216
 
217 217
     /**
@@ -222,8 +222,8 @@  discard block
 block discarded – undo
222 222
         if (strpos($this->request[$key], ':') !== false && isset($this->request['parameters'][0])) {
223 223
             $replacements = $this->request['parameters'];
224 224
             $keys = [];
225
-            $this->request['@' . $key] = $this->request[$key];
226
-            $this->request[$key] = preg_replace_callback('#:([\w?]+)#', function ($matches) use (&$replacements, &$keys) {
225
+            $this->request['@'.$key] = $this->request[$key];
226
+            $this->request[$key] = preg_replace_callback('#:([\w?]+)#', function($matches) use (&$replacements, &$keys) {
227 227
                 $route_key = preg_replace("/[^A-Za-z0-9\\-_:]/", '', $matches[0]);
228 228
                 $keys[$route_key] = isset($replacements[0]) ? $replacements[0] : null;
229 229
                 return is_null($keys[$route_key]) ? '' : array_shift($replacements);
@@ -339,7 +339,7 @@  discard block
 block discarded – undo
339 339
             $routes = explode('@', $callback);
340 340
             if (!isset($routes[1])) $routes[1] = 'index';
341 341
             if ($routes[1] == '{method}') {
342
-                $params = explode('/', trim(preg_replace('#' . rtrim(str_replace('*', '', $this->request['route']), '/') . '#', '', $this->router->route->getUrl()), '/'));
342
+                $params = explode('/', trim(preg_replace('#'.rtrim(str_replace('*', '', $this->request['route']), '/').'#', '', $this->router->route->getUrl()), '/'));
343 343
                 $routes[1] = empty($params[0]) ? 'index' : $params[0];
344 344
                 $this->request['@method'] = $routes[1];
345 345
                 array_shift($params);
@@ -350,7 +350,7 @@  discard block
 block discarded – undo
350 350
             $index = isset($this->request['collection_index']) ? $this->request['collection_index'] : 0;
351 351
             $class = (class_exists($routes[0]))
352 352
                 ? $routes[0]
353
-                : $this->router->collection->getRoutes()['ctrl_namespace_' . $index] . $routes[0];
353
+                : $this->router->collection->getRoutes()['ctrl_namespace_'.$index].$routes[0];
354 354
             if (method_exists($class, $routes[1])) {
355 355
                 return [
356 356
                     'dispatcher' => $this->dispatcher['isController'],
@@ -360,7 +360,7 @@  discard block
 block discarded – undo
360 360
                 ];
361 361
             }
362 362
             if (!strpos($callback, '{method}') !== false) {
363
-                throw new \Exception('The required method "' . $routes[1] . '" is not found in "' . $class . '"');
363
+                throw new \Exception('The required method "'.$routes[1].'" is not found in "'.$class.'"');
364 364
             }
365 365
         }
366 366
         return false;
@@ -378,17 +378,17 @@  discard block
 block discarded – undo
378 378
             $path = str_replace('{template}', $replace, trim($callback, '/'));
379 379
             $extension = substr(strrchr($path, "."), 1);
380 380
             $index = isset($this->request['collection_index']) ? $this->request['collection_index'] : 0;
381
-            $viewDir = is_array($viewDir = $this->router->collection->getRoutes('view_dir_' . $index)) ? $viewDir : [$viewDir];
381
+            $viewDir = is_array($viewDir = $this->router->collection->getRoutes('view_dir_'.$index)) ? $viewDir : [$viewDir];
382 382
             $target = null;
383
-            if (in_array('.' . $extension, $this->router->getConfig()['templateExtension'])){
383
+            if (in_array('.'.$extension, $this->router->getConfig()['templateExtension'])) {
384 384
                 foreach ($viewDir as $dir) {
385
-                    if (is_file($fullPath = rtrim($dir, '/') . '/' . $path) || is_file($fullPath = $path)) $target = $fullPath;
385
+                    if (is_file($fullPath = rtrim($dir, '/').'/'.$path) || is_file($fullPath = $path)) $target = $fullPath;
386 386
                 }
387 387
             }
388
-            if(is_null($target)){
388
+            if (is_null($target)) {
389 389
                 foreach ($viewDir as $dir) {
390 390
                     foreach ($this->router->getConfig()['templateExtension'] as $ext) {
391
-                        if (is_file($fullPath = rtrim($dir, '/') . '/' . $path . $ext) || is_file($fullPath = $path . $ext)) {
391
+                        if (is_file($fullPath = rtrim($dir, '/').'/'.$path.$ext) || is_file($fullPath = $path.$ext)) {
392 392
                             $target = $fullPath;
393 393
                             $extension = substr(strrchr($ext, "."), 1);
394 394
                             break;
Please login to merge, or discard this patch.