@@ -98,15 +98,15 @@ discard block |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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 | } |
@@ -171,7 +171,7 @@ discard block |
||
| 171 | 171 | */ |
| 172 | 172 | private function routeMatch($regex) |
| 173 | 173 | { |
| 174 | - $regex = (substr($this->request['route'], -1) == '*') ? '#^' . $this->request['route'] . '#' : $regex; |
|
| 174 | + $regex = (substr($this->request['route'], -1) == '*') ? '#^'.$this->request['route'].'#' : $regex; |
|
| 175 | 175 | if (preg_match($regex, $this->router->route->getUrl(), $this->request['parameters'])) { |
| 176 | 176 | array_shift($this->request['parameters']); |
| 177 | 177 | return true; |
@@ -206,9 +206,9 @@ discard block |
||
| 206 | 206 | $this->checkRequest('prefix'); |
| 207 | 207 | $this->router->route->setDetail($this->request); |
| 208 | 208 | $this->router->route->setTarget($target); |
| 209 | - $this->router->route->addTarget('block', $this->router->collection->getRoutes('block_' . $index)); |
|
| 210 | - $this->router->route->addTarget('view_dir', $this->router->collection->getRoutes('view_dir_' . $index)); |
|
| 211 | - $this->router->route->addTarget('params', $this->router->collection->getRoutes('params_' . $index)); |
|
| 209 | + $this->router->route->addTarget('block', $this->router->collection->getRoutes('block_'.$index)); |
|
| 210 | + $this->router->route->addTarget('view_dir', $this->router->collection->getRoutes('view_dir_'.$index)); |
|
| 211 | + $this->router->route->addTarget('params', $this->router->collection->getRoutes('params_'.$index)); |
|
| 212 | 212 | } |
| 213 | 213 | |
| 214 | 214 | /** |
@@ -219,8 +219,8 @@ discard block |
||
| 219 | 219 | if (strpos($this->request[$key], ':') !== false && isset($this->request['parameters'][0])) { |
| 220 | 220 | $replacements = $this->request['parameters']; |
| 221 | 221 | $keys = []; |
| 222 | - $this->request['@' . $key] = $this->request[$key]; |
|
| 223 | - $this->request[$key] = preg_replace_callback('#:([\w]+)#', function ($matches) use (&$replacements, &$keys) { |
|
| 222 | + $this->request['@'.$key] = $this->request[$key]; |
|
| 223 | + $this->request[$key] = preg_replace_callback('#:([\w]+)#', function($matches) use (&$replacements, &$keys) { |
|
| 224 | 224 | $keys[$matches[0]] = $replacements[0]; |
| 225 | 225 | return array_shift($replacements); |
| 226 | 226 | }, $this->request[$key]); |
@@ -335,7 +335,7 @@ discard block |
||
| 335 | 335 | $routes = explode('@', $callback); |
| 336 | 336 | if (!isset($routes[1])) $routes[1] = 'index'; |
| 337 | 337 | if ($routes[1] == '{method}') { |
| 338 | - $params = explode('/', preg_replace('#' . str_replace('*', '', $this->request['route']) . '#', '', $this->router->route->getUrl())); |
|
| 338 | + $params = explode('/', preg_replace('#'.str_replace('*', '', $this->request['route']).'#', '', $this->router->route->getUrl())); |
|
| 339 | 339 | $routes[1] = empty($params[0]) ? 'index' : $params[0]; |
| 340 | 340 | $this->request['@method'] = $routes[1]; |
| 341 | 341 | array_shift($params); |
@@ -346,9 +346,9 @@ discard block |
||
| 346 | 346 | $index = isset($this->request['collection_index']) ? $this->request['collection_index'] : 0; |
| 347 | 347 | $class = (class_exists($routes[0])) |
| 348 | 348 | ? $routes[0] |
| 349 | - : $this->router->collection->getRoutes()['ctrl_namespace_' . $index] . $routes[0]; |
|
| 349 | + : $this->router->collection->getRoutes()['ctrl_namespace_'.$index].$routes[0]; |
|
| 350 | 350 | if (!class_exists($class)) { |
| 351 | - throw new \Exception('Class "' . $class . '." is not found'); |
|
| 351 | + throw new \Exception('Class "'.$class.'." is not found'); |
|
| 352 | 352 | } |
| 353 | 353 | if (method_exists($class, $routes[1])) { |
| 354 | 354 | return [ |
@@ -359,7 +359,7 @@ discard block |
||
| 359 | 359 | ]; |
| 360 | 360 | } |
| 361 | 361 | if (!strpos($callback, '{method}') !== false) { |
| 362 | - throw new \Exception('The required method "' . $routes[1] . '" is not found in "' . $class . '"'); |
|
| 362 | + throw new \Exception('The required method "'.$routes[1].'" is not found in "'.$class.'"'); |
|
| 363 | 363 | } |
| 364 | 364 | } |
| 365 | 365 | return false; |
@@ -377,13 +377,13 @@ discard block |
||
| 377 | 377 | $path = str_replace('{template}', $replace, trim($callback, '/')); |
| 378 | 378 | $extension = substr(strrchr($path, "."), 1); |
| 379 | 379 | $index = isset($this->request['collection_index']) ? $this->request['collection_index'] : 0; |
| 380 | - $viewDir = $this->router->collection->getRoutes('view_dir_' . $index); |
|
| 380 | + $viewDir = $this->router->collection->getRoutes('view_dir_'.$index); |
|
| 381 | 381 | $target = null; |
| 382 | - if (in_array('.' . $extension, $this->router->getConfig()['templateExtension']) && (is_file($fullPath = $viewDir . $path) || is_file($fullPath = $path))) { |
|
| 382 | + if (in_array('.'.$extension, $this->router->getConfig()['templateExtension']) && (is_file($fullPath = $viewDir.$path) || is_file($fullPath = $path))) { |
|
| 383 | 383 | $target = $fullPath; |
| 384 | 384 | } else { |
| 385 | 385 | foreach ($this->router->getConfig()['templateExtension'] as $ext) { |
| 386 | - if (is_file($fullPath = $viewDir . $path . $ext) || is_file($fullPath = $path . $ext)) { |
|
| 386 | + if (is_file($fullPath = $viewDir.$path.$ext) || is_file($fullPath = $path.$ext)) { |
|
| 387 | 387 | $target = $fullPath; |
| 388 | 388 | $extension = substr(strrchr($ext, "."), 1); |
| 389 | 389 | break; |
@@ -391,7 +391,7 @@ discard block |
||
| 391 | 391 | } |
| 392 | 392 | } |
| 393 | 393 | if (is_null($target)) { |
| 394 | - throw new \Exception('Template file "' . $path . '" is not found in "' . $viewDir . '"'); |
|
| 394 | + throw new \Exception('Template file "'.$path.'" is not found in "'.$viewDir.'"'); |
|
| 395 | 395 | } |
| 396 | 396 | return [ |
| 397 | 397 | 'dispatcher' => $this->dispatcher['isTemplate'], |
@@ -125,7 +125,9 @@ discard block |
||
| 125 | 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] == '/') {
|
|
| 129 | + $route = trim($this->request['subdomain'], '.') . '.' . $domain . $route; |
|
| 130 | + } |
|
| 129 | 131 | if ($route[0] == '/') { |
| 130 | 132 | return ($host != $domain) ? false : true; |
| 131 | 133 | } elseif ($route[0] != '/' && $host != $domain) { |
@@ -333,14 +335,18 @@ discard block |
||
| 333 | 335 | { |
| 334 | 336 | if (is_string($callback) && strpos($callback, '@') !== false) { |
| 335 | 337 | $routes = explode('@', $callback); |
| 336 | - if (!isset($routes[1])) $routes[1] = 'index'; |
|
| 338 | + if (!isset($routes[1])) {
|
|
| 339 | + $routes[1] = 'index'; |
|
| 340 | + } |
|
| 337 | 341 | if ($routes[1] == '{method}') { |
| 338 | 342 | $params = explode('/', preg_replace('#' . str_replace('*', '', $this->request['route']) . '#', '', $this->router->route->getUrl())); |
| 339 | 343 | $routes[1] = empty($params[0]) ? 'index' : $params[0]; |
| 340 | 344 | $this->request['@method'] = $routes[1]; |
| 341 | 345 | array_shift($params); |
| 342 | 346 | array_merge($this->request['parameters'], $params); |
| 343 | - if (preg_match('/[A-Z]/', $routes[1])) return false; |
|
| 347 | + if (preg_match('/[A-Z]/', $routes[1])) {
|
|
| 348 | + return false; |
|
| 349 | + } |
|
| 344 | 350 | $routes[1] = lcfirst(str_replace(' ', '', ucwords(str_replace('-', ' ', $routes[1])))); |
| 345 | 351 | } |
| 346 | 352 | $index = isset($this->request['collection_index']) ? $this->request['collection_index'] : 0; |
@@ -43,12 +43,12 @@ discard block |
||
| 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 |
||
| 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 |
||
| 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,13 +100,13 @@ discard block |
||
| 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'; |
|
| 109 | - $this->routes['params_' . $i] = (isset($args['params'])) ? $args['params'] : []; |
|
| 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 | + $this->routes['params_'.$i] = (isset($args['params'])) ? $args['params'] : []; |
|
| 110 | 110 | } |
| 111 | 111 | |
| 112 | 112 | /** |
@@ -119,11 +119,11 @@ discard block |
||
| 119 | 119 | $protocol = isset($_SERVER['REQUEST_SCHEME']) ? $_SERVER['REQUEST_SCHEME'] : 'http'; |
| 120 | 120 | $domain = isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : null; |
| 121 | 121 | |
| 122 | - if(!is_null($root)){ |
|
| 122 | + if (!is_null($root)) { |
|
| 123 | 123 | $protocol = explode('://', $root); |
| 124 | 124 | $protocol = $protocol[0]; |
| 125 | - }else{ |
|
| 126 | - $root = $protocol . '://' . $domain . ((!empty($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] != 80) ? ':' . $_SERVER['SERVER_PORT'] : '') . str_replace('/' . $script_file, '', $_SERVER['SCRIPT_NAME']); |
|
| 125 | + } else { |
|
| 126 | + $root = $protocol.'://'.$domain.((!empty($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] != 80) ? ':'.$_SERVER['SERVER_PORT'] : '').str_replace('/'.$script_file, '', $_SERVER['SCRIPT_NAME']); |
|
| 127 | 127 | } |
| 128 | 128 | |
| 129 | 129 | $new_domain = $this->getDomain($root); |
@@ -133,29 +133,29 @@ discard block |
||
| 133 | 133 | |
| 134 | 134 | $count = 0; |
| 135 | 135 | for ($i = 0; $i < $this->countRoutes; ++$i) { |
| 136 | - $prefix = (isset($this->routes['prefix_' . $i])) ? $this->routes['prefix_' . $i] : ''; |
|
| 137 | - $subdomain = (isset($this->routes['subdomain_' . $i])) ? $this->routes['subdomain_' . $i] : ''; |
|
| 138 | - $block_protocol = (isset($this->routes['protocol_' . $i])) ? $this->routes['protocol_' . $i] : 'http'; |
|
| 139 | - $url = (!empty($subdomain)) ? str_replace($protocol . '://', $block_protocol . '://' . $subdomain . '.', $root) : $root; |
|
| 140 | - if (isset($this->routes['routes_' . $i])) |
|
| 141 | - foreach ($this->routes['routes_' . $i] as $route => $dependencies) { |
|
| 136 | + $prefix = (isset($this->routes['prefix_'.$i])) ? $this->routes['prefix_'.$i] : ''; |
|
| 137 | + $subdomain = (isset($this->routes['subdomain_'.$i])) ? $this->routes['subdomain_'.$i] : ''; |
|
| 138 | + $block_protocol = (isset($this->routes['protocol_'.$i])) ? $this->routes['protocol_'.$i] : 'http'; |
|
| 139 | + $url = (!empty($subdomain)) ? str_replace($protocol.'://', $block_protocol.'://'.$subdomain.'.', $root) : $root; |
|
| 140 | + if (isset($this->routes['routes_'.$i])) |
|
| 141 | + foreach ($this->routes['routes_'.$i] as $route => $dependencies) { |
|
| 142 | 142 | if (is_array($dependencies) && isset($dependencies['use']) && !is_array($dependencies['use'])) { |
| 143 | - $use = (is_callable($dependencies['use'])) ? 'closure-' . $count : trim($dependencies['use'], '/'); |
|
| 143 | + $use = (is_callable($dependencies['use'])) ? 'closure-'.$count : trim($dependencies['use'], '/'); |
|
| 144 | 144 | } elseif (!is_array($dependencies)) { |
| 145 | - $use = (is_callable($dependencies)) ? 'closure-' . $count : trim($dependencies, '/'); |
|
| 145 | + $use = (is_callable($dependencies)) ? 'closure-'.$count : trim($dependencies, '/'); |
|
| 146 | 146 | } else { |
| 147 | 147 | $use = $route; |
| 148 | 148 | } |
| 149 | 149 | if (isset($route[0]) && $route[0] == '/') { |
| 150 | - $full_url = rtrim($url, '/') . '/' . trim($prefix, '/') . (empty($prefix) ? '' : '/') . trim($route, '/'); |
|
| 150 | + $full_url = rtrim($url, '/').'/'.trim($prefix, '/').(empty($prefix) ? '' : '/').trim($route, '/'); |
|
| 151 | 151 | (!is_callable($dependencies) && isset($dependencies['name'])) |
| 152 | - ? $this->routesByName[$use . '#' . $dependencies['name']] = $full_url |
|
| 152 | + ? $this->routesByName[$use.'#'.$dependencies['name']] = $full_url |
|
| 153 | 153 | : $this->routesByName[$use] = $full_url; |
| 154 | 154 | } else { |
| 155 | - $full_url = $block_protocol . '://' . str_replace('{host}', $new_domain, $route); |
|
| 155 | + $full_url = $block_protocol.'://'.str_replace('{host}', $new_domain, $route); |
|
| 156 | 156 | (!is_callable($dependencies) && isset($dependencies['name'])) |
| 157 | - ? $this->routesByName[$use . '#' . $dependencies['name']] = $full_url . $prefix |
|
| 158 | - : $this->routesByName[$use] = $full_url . $prefix; |
|
| 157 | + ? $this->routesByName[$use.'#'.$dependencies['name']] = $full_url.$prefix |
|
| 158 | + : $this->routesByName[$use] = $full_url.$prefix; |
|
| 159 | 159 | } |
| 160 | 160 | $count++; |
| 161 | 161 | } |
@@ -186,9 +186,9 @@ discard block |
||
| 186 | 186 | { |
| 187 | 187 | foreach ($this->routesByName as $key => $route) { |
| 188 | 188 | $param = explode('#', $key); |
| 189 | - if(isset($params['{subdomain}'])) $route = str_replace('*', $params['{subdomain}'], $route); |
|
| 190 | - if(isset($params['{method}'])) $route = str_replace('*', $params['{method}'], $route); |
|
| 191 | - foreach ($params as $key2 => $value) $route = str_replace(':' . $key2, $value, $route); |
|
| 189 | + if (isset($params['{subdomain}'])) $route = str_replace('*', $params['{subdomain}'], $route); |
|
| 190 | + if (isset($params['{method}'])) $route = str_replace('*', $params['{method}'], $route); |
|
| 191 | + foreach ($params as $key2 => $value) $route = str_replace(':'.$key2, $value, $route); |
|
| 192 | 192 | if ($param[0] == trim($name, '/')) return $route; |
| 193 | 193 | else if (isset($param[1]) && $param[1] == $name) return $route; |
| 194 | 194 | } |
@@ -33,7 +33,9 @@ discard block |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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 | /** |
@@ -122,7 +141,7 @@ discard block |
||
| 122 | 141 | if(!is_null($root)){ |
| 123 | 142 | $protocol = explode('://', $root); |
| 124 | 143 | $protocol = $protocol[0]; |
| 125 | - }else{ |
|
| 144 | + } else{ |
|
| 126 | 145 | $root = $protocol . '://' . $domain . ((!empty($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] != 80) ? ':' . $_SERVER['SERVER_PORT'] : '') . str_replace('/' . $script_file, '', $_SERVER['SCRIPT_NAME']); |
| 127 | 146 | } |
| 128 | 147 | |
@@ -137,10 +156,11 @@ discard block |
||
| 137 | 156 | $subdomain = (isset($this->routes['subdomain_' . $i])) ? $this->routes['subdomain_' . $i] : ''; |
| 138 | 157 | $block_protocol = (isset($this->routes['protocol_' . $i])) ? $this->routes['protocol_' . $i] : 'http'; |
| 139 | 158 | $url = (!empty($subdomain)) ? str_replace($protocol . '://', $block_protocol . '://' . $subdomain . '.', $root) : $root; |
| 140 | - if (isset($this->routes['routes_' . $i])) |
|
| 141 | - foreach ($this->routes['routes_' . $i] as $route => $dependencies) { |
|
| 159 | + if (isset($this->routes['routes_' . $i])) {
|
|
| 160 | + foreach ($this->routes['routes_' . $i] as $route => $dependencies) { |
|
| 142 | 161 | if (is_array($dependencies) && isset($dependencies['use']) && !is_array($dependencies['use'])) { |
| 143 | - $use = (is_callable($dependencies['use'])) ? 'closure-' . $count : trim($dependencies['use'], '/'); |
|
| 162 | + $use = (is_callable($dependencies['use'])) ? 'closure-' . $count : trim($dependencies['use'], '/'); |
|
| 163 | + } |
|
| 144 | 164 | } elseif (!is_array($dependencies)) { |
| 145 | 165 | $use = (is_callable($dependencies)) ? 'closure-' . $count : trim($dependencies, '/'); |
| 146 | 166 | } else { |
@@ -186,11 +206,20 @@ discard block |
||
| 186 | 206 | { |
| 187 | 207 | foreach ($this->routesByName as $key => $route) { |
| 188 | 208 | $param = explode('#', $key); |
| 189 | - if(isset($params['{subdomain}'])) $route = str_replace('*', $params['{subdomain}'], $route); |
|
| 190 | - if(isset($params['{method}'])) $route = str_replace('*', $params['{method}'], $route); |
|
| 191 | - foreach ($params as $key2 => $value) $route = str_replace(':' . $key2, $value, $route); |
|
| 192 | - if ($param[0] == trim($name, '/')) return $route; |
|
| 193 | - 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 | + } |
|
| 194 | 223 | } |
| 195 | 224 | return null; |
| 196 | 225 | } |