@@ -166,8 +166,9 @@ |
||
| 166 | 166 | */ |
| 167 | 167 | public function sendHeaders() |
| 168 | 168 | { |
| 169 | - foreach($this->headers as $key => $content) |
|
| 170 | - header($key.' : '.$content); |
|
| 169 | + foreach($this->headers as $key => $content) {
|
|
| 170 | + header($key.' : '.$content); |
|
| 171 | + } |
|
| 171 | 172 | http_response_code($this->getStatusCode()); |
| 172 | 173 | return $this; |
| 173 | 174 | } |
@@ -52,10 +52,18 @@ discard block |
||
| 52 | 52 | */ |
| 53 | 53 | public function set($args = []) |
| 54 | 54 | { |
| 55 | - if (isset($args['name'])) $this->name = $args['name']; |
|
| 56 | - if (isset($args['callback'])) $this->callback = $args['callback']; |
|
| 57 | - if (isset($args['target'])) $this->target = $args['target']; |
|
| 58 | - if (isset($args['detail'])) $this->detail = $args['detail']; |
|
| 55 | + if (isset($args['name'])) {
|
|
| 56 | + $this->name = $args['name']; |
|
| 57 | + } |
|
| 58 | + if (isset($args['callback'])) {
|
|
| 59 | + $this->callback = $args['callback']; |
|
| 60 | + } |
|
| 61 | + if (isset($args['target'])) {
|
|
| 62 | + $this->target = $args['target']; |
|
| 63 | + } |
|
| 64 | + if (isset($args['detail'])) {
|
|
| 65 | + $this->detail = $args['detail']; |
|
| 66 | + } |
|
| 59 | 67 | } |
| 60 | 68 | |
| 61 | 69 | /** |
@@ -145,8 +153,9 @@ discard block |
||
| 145 | 153 | */ |
| 146 | 154 | public function getTarget($key = null) |
| 147 | 155 | { |
| 148 | - if (!is_null($key)) |
|
| 149 | - return isset($this->target[$key]) ? $this->target[$key] : ''; |
|
| 156 | + if (!is_null($key)) {
|
|
| 157 | + return isset($this->target[$key]) ? $this->target[$key] : ''; |
|
| 158 | + } |
|
| 150 | 159 | return empty($this->target) ? '' : $this->target; |
| 151 | 160 | } |
| 152 | 161 | |
@@ -165,8 +174,9 @@ discard block |
||
| 165 | 174 | */ |
| 166 | 175 | public function hasTarget($key = null) |
| 167 | 176 | { |
| 168 | - if (!is_null($key)) |
|
| 169 | - return isset($this->target[$key]) ? true : false; |
|
| 177 | + if (!is_null($key)) {
|
|
| 178 | + return isset($this->target[$key]) ? true : false; |
|
| 179 | + } |
|
| 170 | 180 | return empty($this->target) ? false : true; |
| 171 | 181 | } |
| 172 | 182 | |
@@ -166,7 +166,7 @@ discard block |
||
| 166 | 166 | } |
| 167 | 167 | |
| 168 | 168 | /** |
| 169 | - * @param $regex |
|
| 169 | + * @param string $regex |
|
| 170 | 170 | * @return bool |
| 171 | 171 | */ |
| 172 | 172 | private function routeMatch($regex) |
@@ -216,7 +216,7 @@ discard block |
||
| 216 | 216 | } |
| 217 | 217 | |
| 218 | 218 | /** |
| 219 | - * @param $key |
|
| 219 | + * @param string $key |
|
| 220 | 220 | */ |
| 221 | 221 | private function checkRequest($key) |
| 222 | 222 | { |
@@ -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('/', trim(preg_replace('#' . rtrim(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 | $this->request['parameters'] = 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; |
@@ -378,7 +384,9 @@ discard block |
||
| 378 | 384 | $target = null; |
| 379 | 385 | if (in_array('.' . $extension, $this->router->getConfig()['templateExtension'])){ |
| 380 | 386 | foreach ($viewDir as $dir) { |
| 381 | - if (is_file($fullPath = rtrim($dir, '/') . '/' . $path) || is_file($fullPath = $path)) $target = $fullPath; |
|
| 387 | + if (is_file($fullPath = rtrim($dir, '/') . '/' . $path) || is_file($fullPath = $path)) {
|
|
| 388 | + $target = $fullPath; |
|
| 389 | + } |
|
| 382 | 390 | } |
| 383 | 391 | } |
| 384 | 392 | if(is_null($target)){ |
@@ -44,8 +44,12 @@ |
||
| 44 | 44 | $this->router->response->setContent(call_user_func_array($this->router->route->getTarget('callback')[$this->router->route->getTarget('extension')], [$this->router->route])); |
| 45 | 45 | } else { |
| 46 | 46 | ob_start(); |
| 47 | - if (isset($this->router->route->getTarget()['data'])) extract($this->router->route->getTarget('data')); |
|
| 48 | - if (isset($this->router->route->getParams()['data'])) extract($this->router->route->getParams()['data']); |
|
| 47 | + if (isset($this->router->route->getTarget()['data'])) {
|
|
| 48 | + extract($this->router->route->getTarget('data'));
|
|
| 49 | + } |
|
| 50 | + if (isset($this->router->route->getParams()['data'])) {
|
|
| 51 | + extract($this->router->route->getParams()['data']); |
|
| 52 | + } |
|
| 49 | 53 | require($this->router->route->getTarget('template')); |
| 50 | 54 | $this->router->response->setContent(ob_get_clean()); |
| 51 | 55 | } |
@@ -120,13 +120,15 @@ discard block |
||
| 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 |
||
| 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 |
||
| 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 | } |
@@ -135,7 +135,7 @@ |
||
| 135 | 135 | |
| 136 | 136 | /** |
| 137 | 137 | * @description call the middleware before and after the target |
| 138 | - * @param $action |
|
| 138 | + * @param string $action |
|
| 139 | 139 | */ |
| 140 | 140 | public function callMiddleware($action) |
| 141 | 141 | { |
@@ -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 | $options['routes'] = is_array($routes) ? $routes : []; |
| 52 | 60 | $this->setRoutes($options, $this->countRoutes); |
@@ -71,12 +79,18 @@ discard block |
||
| 71 | 79 | { |
| 72 | 80 | if (is_array($args)) { |
| 73 | 81 | $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++; |
|
| 82 | + for ($i = 0; $i < $nbrArgs; ++$i) {
|
|
| 83 | + $this->routes['prefix_' . $i] = '/' . trim($args[$i], '/'); |
|
| 84 | + } |
|
| 85 | + } elseif (is_string($args)) {
|
|
| 86 | + for ($i = 0; |
|
| 87 | + } |
|
| 88 | + $i < $this->countRoutes; ++$i) {
|
|
| 89 | + $this->routes['prefix_' . $i] = '/' . trim($args, '/'); |
|
| 90 | + } |
|
| 91 | + if ($this->countRoutes == 0) {
|
|
| 92 | + $this->countRoutes++; |
|
| 93 | + } |
|
| 80 | 94 | } |
| 81 | 95 | |
| 82 | 96 | /** |
@@ -88,10 +102,14 @@ discard block |
||
| 88 | 102 | for ($i = 0; $i < $nbrArgs; ++$i) { |
| 89 | 103 | if (is_array($args[$i])) { |
| 90 | 104 | $this->setRoutes($args[$i], $i); |
| 91 | - if (!isset($this->routes['routes_' . $i])) $this->routes['routes_' . $i] = []; |
|
| 105 | + if (!isset($this->routes['routes_' . $i])) {
|
|
| 106 | + $this->routes['routes_' . $i] = []; |
|
| 107 | + } |
|
| 92 | 108 | } |
| 93 | 109 | } |
| 94 | - if ($this->countRoutes == 0) $this->countRoutes++; |
|
| 110 | + if ($this->countRoutes == 0) {
|
|
| 111 | + $this->countRoutes++; |
|
| 112 | + } |
|
| 95 | 113 | } |
| 96 | 114 | |
| 97 | 115 | /** |
@@ -123,7 +141,7 @@ discard block |
||
| 123 | 141 | if(!is_null($root)){ |
| 124 | 142 | $protocol = explode('://', $root); |
| 125 | 143 | $protocol = $protocol[0]; |
| 126 | - }else{ |
|
| 144 | + } else{ |
|
| 127 | 145 | $root = $protocol . '://' . $domain . ((!empty($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] != 80) ? ':' . $_SERVER['SERVER_PORT'] : '') . str_replace('/' . $script_file, '', $_SERVER['SCRIPT_NAME']); |
| 128 | 146 | } |
| 129 | 147 | |
@@ -138,10 +156,11 @@ discard block |
||
| 138 | 156 | $subdomain = (isset($this->routes['subdomain_' . $i])) ? $this->routes['subdomain_' . $i] : ''; |
| 139 | 157 | $block_protocol = (isset($this->routes['protocol_' . $i])) ? $this->routes['protocol_' . $i] : 'http'; |
| 140 | 158 | $url = (!empty($subdomain)) ? str_replace($protocol . '://', $block_protocol . '://' . $subdomain . '.', $root) : $root; |
| 141 | - if (isset($this->routes['routes_' . $i])) |
|
| 142 | - foreach ($this->routes['routes_' . $i] as $route => $dependencies) { |
|
| 159 | + if (isset($this->routes['routes_' . $i])) {
|
|
| 160 | + foreach ($this->routes['routes_' . $i] as $route => $dependencies) { |
|
| 143 | 161 | if (is_array($dependencies) && isset($dependencies['use']) && !is_array($dependencies['use'])) { |
| 144 | - $use = (is_callable($dependencies['use'])) ? 'closure-' . $count : trim($dependencies['use'], '/'); |
|
| 162 | + $use = (is_callable($dependencies['use'])) ? 'closure-' . $count : trim($dependencies['use'], '/'); |
|
| 163 | + } |
|
| 145 | 164 | } elseif (!is_array($dependencies)) { |
| 146 | 165 | $use = (is_callable($dependencies)) ? 'closure-' . $count : trim($dependencies, '/'); |
| 147 | 166 | } else { |
@@ -187,11 +206,20 @@ discard block |
||
| 187 | 206 | { |
| 188 | 207 | foreach ($this->routesByName as $key => $route) { |
| 189 | 208 | $param = explode('#', $key); |
| 190 | - if(isset($params['{subdomain}'])) $route = str_replace('*', $params['{subdomain}'], $route); |
|
| 191 | - if(isset($params['{method}'])) $route = str_replace('*', $params['{method}'], $route); |
|
| 192 | - foreach ($params as $key2 => $value) $route = str_replace(':' . $key2, $value, $route); |
|
| 193 | - if ($param[0] == trim($name, '/')) return $route; |
|
| 194 | - 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 | + } |
|
| 195 | 223 | } |
| 196 | 224 | return null; |
| 197 | 225 | } |