@@ -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 | |
@@ -90,8 +90,9 @@ |
||
| 90 | 90 | foreach($this->resolver as $resolver){ |
| 91 | 91 | if(is_array($target = call_user_func([$this,$resolver]))) { |
| 92 | 92 | $this->setTarget($target); |
| 93 | - if($this->router->middleware->globalMiddleware() === false || $this->router->middleware->blockMiddleware() === false || $this->router->middleware->classMiddleware() === false || $this->router->middleware->routeMiddleware() === false) |
|
| 94 | - return null; |
|
| 93 | + if($this->router->middleware->globalMiddleware() === false || $this->router->middleware->blockMiddleware() === false || $this->router->middleware->classMiddleware() === false || $this->router->middleware->routeMiddleware() === false) {
|
|
| 94 | + return null; |
|
| 95 | + } |
|
| 95 | 96 | $this->router->response->setStatusCode(202); |
| 96 | 97 | return true; |
| 97 | 98 | } |
@@ -29,9 +29,10 @@ discard block |
||
| 29 | 29 | */ |
| 30 | 30 | public function globalMiddleware() |
| 31 | 31 | { |
| 32 | - if (isset($this->router->collection->middleware['global_middleware'])) |
|
| 33 | - foreach ($this->router->collection->middleware['global_middleware'] as $class) |
|
| 34 | - if (class_exists($class)) return $this->callHandler($class); |
|
| 32 | + if (isset($this->router->collection->middleware['global_middleware'])) {
|
|
| 33 | + foreach ($this->router->collection->middleware['global_middleware'] as $class) |
|
| 34 | + if (class_exists($class)) return $this->callHandler($class); |
|
| 35 | + } |
|
| 35 | 36 | return null; |
| 36 | 37 | } |
| 37 | 38 | |
@@ -40,16 +41,18 @@ discard block |
||
| 40 | 41 | */ |
| 41 | 42 | public function blockMiddleware() |
| 42 | 43 | { |
| 43 | - if (isset($this->router->collection->middleware['block_middleware'])) |
|
| 44 | - if (isset($this->router->collection->middleware['block_middleware'][$this->router->route->getTarget('block')])) { |
|
| 45 | - $blocks = $this->router->collection->middleware['block_middleware'][$this->router->route->getTarget('block')]; |
|
| 44 | + if (isset($this->router->collection->middleware['block_middleware'])) {
|
|
| 45 | + if (isset($this->router->collection->middleware['block_middleware'][$this->router->route->getTarget('block')])) { |
|
| 46 | + $blocks = $this->router->collection->middleware['block_middleware'][$this->router->route->getTarget('block')];
|
|
| 47 | + } |
|
| 46 | 48 | if (is_array($blocks)) { |
| 47 | - foreach ($blocks as $block) |
|
| 48 | - if (class_exists($block)) |
|
| 49 | - if($this->callHandler($block) === false) return false; |
|
| 50 | - } |
|
| 51 | - elseif (is_string($blocks) && class_exists($blocks)) |
|
| 52 | - return $this->callHandler($blocks); |
|
| 49 | + foreach ($blocks as $block) {
|
|
| 50 | + if (class_exists($block)) |
|
| 51 | + if($this->callHandler($block) === false) return false; |
|
| 52 | + } |
|
| 53 | + } elseif (is_string($blocks) && class_exists($blocks)) {
|
|
| 54 | + return $this->callHandler($blocks); |
|
| 55 | + } |
|
| 53 | 56 | } |
| 54 | 57 | return null; |
| 55 | 58 | } |
@@ -64,10 +67,12 @@ discard block |
||
| 64 | 67 | if (isset($this->router->collection->middleware['class_middleware'][$ctrl]) && class_exists($this->router->route->getTarget('controller'))) { |
| 65 | 68 | $classes = $this->router->collection->middleware['class_middleware'][$ctrl]; |
| 66 | 69 | if(is_array($classes)){ |
| 67 | - foreach ($classes as $class) |
|
| 68 | - if($this->callHandler($class) === false)return false; |
|
| 69 | - }elseif(is_string($classes)) |
|
| 70 | - return $this->callHandler($classes); |
|
| 70 | + foreach ($classes as $class) {
|
|
| 71 | + if($this->callHandler($class) === false)return false; |
|
| 72 | + } |
|
| 73 | + } elseif(is_string($classes)) {
|
|
| 74 | + return $this->callHandler($classes); |
|
| 75 | + } |
|
| 71 | 76 | } |
| 72 | 77 | } |
| 73 | 78 | return null; |
@@ -78,14 +83,17 @@ discard block |
||
| 78 | 83 | */ |
| 79 | 84 | public function routeMiddleware() |
| 80 | 85 | { |
| 81 | - if (isset($this->router->collection->middleware['route_middleware'])) |
|
| 82 | - if (isset($this->router->route->getPath()['middleware']) && class_exists($this->router->collection->middleware['route_middleware'][$this->router->route->getPath()['middleware']])) { |
|
| 83 | - $classes = $this->router->collection->middleware['route_middleware'][$this->router->route->getPath()['middleware']]; |
|
| 86 | + if (isset($this->router->collection->middleware['route_middleware'])) {
|
|
| 87 | + if (isset($this->router->route->getPath()['middleware']) && class_exists($this->router->collection->middleware['route_middleware'][$this->router->route->getPath()['middleware']])) { |
|
| 88 | + $classes = $this->router->collection->middleware['route_middleware'][$this->router->route->getPath()['middleware']]; |
|
| 89 | + } |
|
| 84 | 90 | if(is_array($classes)){ |
| 85 | - foreach ($classes as $class) |
|
| 86 | - if($this->callHandler($class) === false)return false; |
|
| 87 | - }elseif(is_string($classes)) |
|
| 88 | - return $this->callHandler($classes); |
|
| 91 | + foreach ($classes as $class) {
|
|
| 92 | + if($this->callHandler($class) === false)return false; |
|
| 93 | + } |
|
| 94 | + } elseif(is_string($classes)) {
|
|
| 95 | + return $this->callHandler($classes); |
|
| 96 | + } |
|
| 89 | 97 | } |
| 90 | 98 | return null; |
| 91 | 99 | } |
@@ -99,9 +107,10 @@ discard block |
||
| 99 | 107 | if (method_exists($instance, 'handle')) { |
| 100 | 108 | $reflectionMethod = new ReflectionMethod($instance, 'handle'); |
| 101 | 109 | $dependencies = []; |
| 102 | - foreach ($reflectionMethod->getParameters() as $arg) |
|
| 103 | - if (!is_null($arg->getClass())) |
|
| 104 | - $dependencies[] = $this->getClass($arg->getClass()->name); |
|
| 110 | + foreach ($reflectionMethod->getParameters() as $arg) {
|
|
| 111 | + if (!is_null($arg->getClass())) |
|
| 112 | + $dependencies[] = $this->getClass($arg->getClass()->name); |
|
| 113 | + } |
|
| 105 | 114 | $dependencies = array_merge($dependencies, [$this->router->route]); |
| 106 | 115 | return $reflectionMethod->invokeArgs($instance, $dependencies); |
| 107 | 116 | } |
@@ -47,14 +47,19 @@ |
||
| 47 | 47 | */ |
| 48 | 48 | public function call() |
| 49 | 49 | { |
| 50 | - if ($this->response->getStatusCode() == 202) |
|
| 51 | - $this->setContentType($this->route->getTarget('extension')); |
|
| 52 | - if (isset($this->route->getTarget('callback')[$this->route->getTarget('extension')])) |
|
| 53 | - $this->response->setContent(call_user_func_array($this->route->getTarget('callback')[$this->route->getTarget('extension')], [$this->route])); |
|
| 54 | - else { |
|
| 50 | + if ($this->response->getStatusCode() == 202) {
|
|
| 51 | + $this->setContentType($this->route->getTarget('extension'));
|
|
| 52 | + } |
|
| 53 | + if (isset($this->route->getTarget('callback')[$this->route->getTarget('extension')])) {
|
|
| 54 | + $this->response->setContent(call_user_func_array($this->route->getTarget('callback')[$this->route->getTarget('extension')], [$this->route]));
|
|
| 55 | + } else { |
|
| 55 | 56 | ob_start(); |
| 56 | - if (isset($this->route->getTarget()['data'])) extract($this->route->getTarget('data')); |
|
| 57 | - if (isset($this->route->getParams()['data'])) extract($this->route->getParams()['data']); |
|
| 57 | + if (isset($this->route->getTarget()['data'])) {
|
|
| 58 | + extract($this->route->getTarget('data'));
|
|
| 59 | + } |
|
| 60 | + if (isset($this->route->getParams()['data'])) {
|
|
| 61 | + extract($this->route->getParams()['data']); |
|
| 62 | + } |
|
| 58 | 63 | require($this->route->getTarget('template')); |
| 59 | 64 | $this->response->setContent(ob_get_clean()); |
| 60 | 65 | } |
@@ -43,8 +43,11 @@ |
||
| 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 | } |
@@ -102,7 +102,9 @@ discard block |
||
| 102 | 102 | $this->request['route'] = preg_replace_callback('#:([\w]+)#', [$this, 'paramMatch'], '/' . trim(trim($this->request['prefix'], '/') . '/' . trim($route, '/'), '/')); |
| 103 | 103 | if ($this->routeMatch('#^' . $this->request['route'] . '$#')) { |
| 104 | 104 | $this->setCallback(); |
| 105 | - if(!is_null($response = $this->generateTarget())) return $response; |
|
| 105 | + if(!is_null($response = $this->generateTarget())) {
|
|
| 106 | + return $response; |
|
| 107 | + } |
|
| 106 | 108 | } |
| 107 | 109 | } |
| 108 | 110 | } |
@@ -118,10 +120,12 @@ discard block |
||
| 118 | 120 | $url = (isset($_SERVER['REQUEST_SCHEME']) ? $_SERVER['REQUEST_SCHEME'] : 'http') . '://' . ($host = (isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : $_SERVER['HTTP_HOST'])); |
| 119 | 121 | $host = explode(':',$host)[0]; |
| 120 | 122 | $domain = $this->router->collection->getDomain($url); |
| 121 | - if(!empty($this->request['subdomain']) && $route[0] == '/') $route = trim($this->request['subdomain'],'.').'.'.$domain.$route; |
|
| 123 | + if(!empty($this->request['subdomain']) && $route[0] == '/') {
|
|
| 124 | + $route = trim($this->request['subdomain'],'.').'.'.$domain.$route; |
|
| 125 | + } |
|
| 122 | 126 | if($route[0] == '/'){ |
| 123 | 127 | return ($host != $domain) ? false : true; |
| 124 | - }elseif($route[0] != '/' && $host != $domain){ |
|
| 128 | + } elseif($route[0] != '/' && $host != $domain){ |
|
| 125 | 129 | $route = substr($route, 0, strpos($route, "/")); |
| 126 | 130 | $route = str_replace('{host}', $domain, $route); |
| 127 | 131 | $route = preg_replace_callback('#{subdomain}#', [$this, 'subdomainMatch'], $route); |
@@ -166,8 +170,9 @@ discard block |
||
| 166 | 170 | { |
| 167 | 171 | if (substr($this->request['route'], -1) == '*') { |
| 168 | 172 | $pos = strpos($this->request['route'], '*'); |
| 169 | - if (substr($this->router->route->getUrl(), 0, $pos) == substr($this->request['route'], 0, $pos) && isset($this->request['params'])) |
|
| 170 | - return true; |
|
| 173 | + if (substr($this->router->route->getUrl(), 0, $pos) == substr($this->request['route'], 0, $pos) && isset($this->request['params'])) {
|
|
| 174 | + return true; |
|
| 175 | + } |
|
| 171 | 176 | } |
| 172 | 177 | if (preg_match($regex, $this->router->route->getUrl(), $this->request['parameters'])) { |
| 173 | 178 | array_shift($this->request['parameters']); |
@@ -185,8 +190,9 @@ discard block |
||
| 185 | 190 | foreach ($this->resolver as $resolver) { |
| 186 | 191 | if (is_array($target = call_user_func_array([$this, $resolver], [$this->router->route->getCallback()]))) { |
| 187 | 192 | $this->setTarget($target); |
| 188 | - if ($this->router->middleware->globalMiddleware() === false || $this->router->middleware->blockMiddleware() === false || $this->router->middleware->classMiddleware() === false || $this->router->middleware->routeMiddleware() === false) |
|
| 189 | - return null; |
|
| 193 | + if ($this->router->middleware->globalMiddleware() === false || $this->router->middleware->blockMiddleware() === false || $this->router->middleware->classMiddleware() === false || $this->router->middleware->routeMiddleware() === false) {
|
|
| 194 | + return null; |
|
| 195 | + } |
|
| 190 | 196 | $this->router->response->setStatusCode(202); |
| 191 | 197 | return true; |
| 192 | 198 | } |
@@ -213,14 +219,18 @@ discard block |
||
| 213 | 219 | */ |
| 214 | 220 | private function setCallback(){ |
| 215 | 221 | if (isset($this->request['params'])) { |
| 216 | - if(is_callable($this->request['params'])) |
|
| 217 | - $this->router->route->setCallback($this->request['params']); |
|
| 218 | - else { |
|
| 222 | + if(is_callable($this->request['params'])) {
|
|
| 223 | + $this->router->route->setCallback($this->request['params']); |
|
| 224 | + } else { |
|
| 219 | 225 | (is_array($this->request['params']) && isset($this->request['params']['use'])) |
| 220 | 226 | ? $this->router->route->setCallback($this->request['params']['use']) |
| 221 | 227 | : $this->router->route->setCallback($this->request['params']); |
| 222 | - if (isset($this->request['params']['name'])) $this->router->route->setName($this->request['params']['name']); |
|
| 223 | - if (isset($this->request['params']['method'])) $this->request['params']['method'] = is_array($this->request['params']['method']) ? $this->request['params']['method'] : [$this->request['params']['method']]; |
|
| 228 | + if (isset($this->request['params']['name'])) {
|
|
| 229 | + $this->router->route->setName($this->request['params']['name']); |
|
| 230 | + } |
|
| 231 | + if (isset($this->request['params']['method'])) {
|
|
| 232 | + $this->request['params']['method'] = is_array($this->request['params']['method']) ? $this->request['params']['method'] : [$this->request['params']['method']]; |
|
| 233 | + } |
|
| 224 | 234 | } |
| 225 | 235 | } |
| 226 | 236 | } |
@@ -230,9 +240,12 @@ discard block |
||
| 230 | 240 | */ |
| 231 | 241 | public function validMethod() |
| 232 | 242 | { |
| 233 | - if(is_callable($this->request['params']))return true; |
|
| 234 | - if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') |
|
| 235 | - return (isset($this->request['params']['ajax']) && $this->request['params']['ajax'] === true) ? true : false; |
|
| 243 | + if(is_callable($this->request['params'])) {
|
|
| 244 | + return true; |
|
| 245 | + } |
|
| 246 | + if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
|
|
| 247 | + return (isset($this->request['params']['ajax']) && $this->request['params']['ajax'] === true) ? true : false; |
|
| 248 | + } |
|
| 236 | 249 | $method = (isset($this->request['params']['method'])) ? $this->request['params']['method'] : ['GET']; |
| 237 | 250 | return (in_array($this->router->route->getMethod(), $method)) ? true : false; |
| 238 | 251 | } |
@@ -296,20 +309,25 @@ discard block |
||
| 296 | 309 | { |
| 297 | 310 | if (is_string($callback) && strpos($callback, '@') !== false) { |
| 298 | 311 | $routes = explode('@', $callback); |
| 299 | - if (!isset($routes[1])) $routes[1] = 'index'; |
|
| 312 | + if (!isset($routes[1])) {
|
|
| 313 | + $routes[1] = 'index'; |
|
| 314 | + } |
|
| 300 | 315 | if ($routes[1] == '{method}'){ |
| 301 | 316 | $this->request['parameters'] = explode('/',str_replace(str_replace('*','',$this->request['route']),'',$this->router->route->getUrl())); |
| 302 | 317 | $routes[1] = $this->request['parameters'][0]; |
| 303 | 318 | array_shift($this->request['parameters']); |
| 304 | - if(preg_match('/[A-Z]/', $routes[1])) return false; |
|
| 319 | + if(preg_match('/[A-Z]/', $routes[1])) {
|
|
| 320 | + return false; |
|
| 321 | + } |
|
| 305 | 322 | $routes[1] = lcfirst(str_replace(' ','',ucwords(str_replace('-',' ',$routes[1])))); |
| 306 | 323 | } |
| 307 | 324 | $index = isset($this->request['collection_index']) ? $this->request['collection_index'] : 0; |
| 308 | 325 | $class = (class_exists($routes[0])) |
| 309 | 326 | ? $routes[0] |
| 310 | 327 | : $this->router->collection->getRoutes()['ctrl_namespace_' . $index] . $routes[0]; |
| 311 | - if (!class_exists($class)) |
|
| 312 | - throw new \Exception('Class "' . $class . '." is not found'); |
|
| 328 | + if (!class_exists($class)) {
|
|
| 329 | + throw new \Exception('Class "' . $class . '." is not found');
|
|
| 330 | + } |
|
| 313 | 331 | if (method_exists($class, $routes[1])) { |
| 314 | 332 | return [ |
| 315 | 333 | 'dispatcher' => $this->dispatcher['isController'], |
@@ -318,8 +336,9 @@ discard block |
||
| 318 | 336 | 'action' => $routes[1] |
| 319 | 337 | ]; |
| 320 | 338 | } |
| 321 | - if (!strpos($callback, '{method}') !== false) |
|
| 322 | - throw new \Exception('The required method "' . $routes[1] . '" is not found in "' . $class . '"'); |
|
| 339 | + if (!strpos($callback, '{method}') !== false) {
|
|
| 340 | + throw new \Exception('The required method "' . $routes[1] . '" is not found in "' . $class . '"');
|
|
| 341 | + } |
|
| 323 | 342 | } |
| 324 | 343 | return false; |
| 325 | 344 | } |
@@ -337,9 +356,9 @@ discard block |
||
| 337 | 356 | $index = isset($this->request['collection_index']) ? $this->request['collection_index'] : 0; |
| 338 | 357 | $viewDir = $this->router->collection->getRoutes('view_dir_' . $index); |
| 339 | 358 | $target = null; |
| 340 | - if (in_array('.' . $extension, $this->router->getConfig()['templateExtension']) && (is_file($fullPath = $viewDir . $path) || is_file($fullPath = $path))) |
|
| 341 | - $target = $fullPath; |
|
| 342 | - else { |
|
| 359 | + if (in_array('.' . $extension, $this->router->getConfig()['templateExtension']) && (is_file($fullPath = $viewDir . $path) || is_file($fullPath = $path))) {
|
|
| 360 | + $target = $fullPath; |
|
| 361 | + } else { |
|
| 343 | 362 | foreach ($this->router->getConfig()['templateExtension'] as $ext) { |
| 344 | 363 | if (is_file($fullPath = $viewDir . $path . $ext) || is_file($fullPath = $path . $ext)) { |
| 345 | 364 | $target = $fullPath; |
@@ -348,8 +367,9 @@ discard block |
||
| 348 | 367 | } |
| 349 | 368 | } |
| 350 | 369 | } |
| 351 | - if(is_null($target)) |
|
| 352 | - throw new \Exception('Template file "' . $path . '" is not found in "' . $viewDir . '"'); |
|
| 370 | + if(is_null($target)) {
|
|
| 371 | + throw new \Exception('Template file "' . $path . '" is not found in "' . $viewDir . '"');
|
|
| 372 | + } |
|
| 353 | 373 | return [ |
| 354 | 374 | 'dispatcher' => $this->dispatcher['isTemplate'], |
| 355 | 375 | 'template' => $target, |
@@ -37,7 +37,9 @@ discard block |
||
| 37 | 37 | */ |
| 38 | 38 | public function __construct($routes = null, $options = []) |
| 39 | 39 | { |
| 40 | - if (!is_null($routes) || !empty($options)) $this->addRoutes($routes, $options); |
|
| 40 | + if (!is_null($routes) || !empty($options)) {
|
|
| 41 | + $this->addRoutes($routes, $options); |
|
| 42 | + } |
|
| 41 | 43 | } |
| 42 | 44 | |
| 43 | 45 | /** |
@@ -47,10 +49,16 @@ discard block |
||
| 47 | 49 | public function addRoutes($routes = null, $options = []) |
| 48 | 50 | { |
| 49 | 51 | 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; |
|
| 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'); |
|
| 52 | + if (strpos($routes, '.php') === false) {
|
|
| 53 | + $routes = trim($routes, '/') . '/'; |
|
| 54 | + } |
|
| 55 | + if (is_file($routes . '/routes.php') && is_array($routesFile = include $routes . '/routes.php')) {
|
|
| 56 | + $routes = $routesFile; |
|
| 57 | + } elseif (is_file($routes) && is_array($routesFile = include $routes)) {
|
|
| 58 | + $routes = $routesFile; |
|
| 59 | + } else {
|
|
| 60 | + throw new \InvalidArgumentException('Argument for "' . get_called_class() . '" constructor is not recognized. Expected argument array or file containing array but "' . $routes . '" given');
|
|
| 61 | + } |
|
| 54 | 62 | } |
| 55 | 63 | $this->routes['routes_' . $this->countRoutes] = is_array($routes) ? $routes : []; |
| 56 | 64 | $this->setRoutes($options, $this->countRoutes); |
@@ -63,8 +71,9 @@ discard block |
||
| 63 | 71 | */ |
| 64 | 72 | public function getRoutes($key = null) |
| 65 | 73 | { |
| 66 | - if (!is_null($key)) |
|
| 67 | - return isset($this->routes[$key]) ? $this->routes[$key] : ''; |
|
| 74 | + if (!is_null($key)) {
|
|
| 75 | + return isset($this->routes[$key]) ? $this->routes[$key] : ''; |
|
| 76 | + } |
|
| 68 | 77 | return $this->routes; |
| 69 | 78 | } |
| 70 | 79 | |
@@ -75,12 +84,18 @@ discard block |
||
| 75 | 84 | { |
| 76 | 85 | if (is_array($args)) { |
| 77 | 86 | $nbrArgs = count($args); |
| 78 | - for ($i = 0; $i < $nbrArgs; ++$i) |
|
| 79 | - $this->routes['prefix_' . $i] = '/' . trim($args[$i], '/'); |
|
| 80 | - } elseif (is_string($args)) |
|
| 81 | - for ($i = 0; $i < $this->countRoutes; ++$i) |
|
| 82 | - $this->routes['prefix_' . $i] = '/' . trim($args, '/'); |
|
| 83 | - if ($this->countRoutes == 0) $this->countRoutes++; |
|
| 87 | + for ($i = 0; $i < $nbrArgs; ++$i) {
|
|
| 88 | + $this->routes['prefix_' . $i] = '/' . trim($args[$i], '/'); |
|
| 89 | + } |
|
| 90 | + } elseif (is_string($args)) {
|
|
| 91 | + for ($i = 0; |
|
| 92 | + } |
|
| 93 | + $i < $this->countRoutes; ++$i) {
|
|
| 94 | + $this->routes['prefix_' . $i] = '/' . trim($args, '/'); |
|
| 95 | + } |
|
| 96 | + if ($this->countRoutes == 0) {
|
|
| 97 | + $this->countRoutes++; |
|
| 98 | + } |
|
| 84 | 99 | } |
| 85 | 100 | |
| 86 | 101 | /** |
@@ -92,10 +107,14 @@ discard block |
||
| 92 | 107 | for ($i = 0; $i < $nbrArgs; ++$i) { |
| 93 | 108 | if (is_array($args[$i])) { |
| 94 | 109 | $this->setRoutes($args[$i], $i); |
| 95 | - if (!isset($this->routes['routes_' . $i])) $this->routes['routes_' . $i] = []; |
|
| 110 | + if (!isset($this->routes['routes_' . $i])) {
|
|
| 111 | + $this->routes['routes_' . $i] = []; |
|
| 112 | + } |
|
| 96 | 113 | } |
| 97 | 114 | } |
| 98 | - if ($this->countRoutes == 0) $this->countRoutes++; |
|
| 115 | + if ($this->countRoutes == 0) {
|
|
| 116 | + $this->countRoutes++; |
|
| 117 | + } |
|
| 99 | 118 | } |
| 100 | 119 | |
| 101 | 120 | /** |
@@ -117,12 +136,16 @@ discard block |
||
| 117 | 136 | */ |
| 118 | 137 | public function setMiddleware($middleware) |
| 119 | 138 | { |
| 120 | - if (is_string($middleware)) $middleware = rtrim($middleware, '/'); |
|
| 121 | - if (is_array($middleware)) |
|
| 122 | - $this->middleware = $middleware; |
|
| 123 | - elseif (is_file($middleware) && is_array($mid = include $middleware)) |
|
| 124 | - $this->middleware = $mid; |
|
| 125 | - else throw new \InvalidArgumentException('Accepted argument for setMiddleware are array and array file'); |
|
| 139 | + if (is_string($middleware)) {
|
|
| 140 | + $middleware = rtrim($middleware, '/'); |
|
| 141 | + } |
|
| 142 | + if (is_array($middleware)) {
|
|
| 143 | + $this->middleware = $middleware; |
|
| 144 | + } elseif (is_file($middleware) && is_array($mid = include $middleware)) {
|
|
| 145 | + $this->middleware = $mid; |
|
| 146 | + } else {
|
|
| 147 | + throw new \InvalidArgumentException('Accepted argument for setMiddleware are array and array file');
|
|
| 148 | + } |
|
| 126 | 149 | } |
| 127 | 150 | |
| 128 | 151 | /** |
@@ -138,21 +161,23 @@ discard block |
||
| 138 | 161 | ? $protocol . '://' . $domain . ((!empty($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] != 80) ? ':' . $_SERVER['SERVER_PORT'] : '') . str_replace('/' . $script_file, '', $_SERVER['SCRIPT_NAME']) |
| 139 | 162 | : $root; |
| 140 | 163 | $new_domain = $this->getDomain($root); |
| 141 | - if (!is_null($domain) && strpos($domain, $new_domain) !== false) |
|
| 142 | - $root = str_replace($domain, $new_domain, $root); |
|
| 164 | + if (!is_null($domain) && strpos($domain, $new_domain) !== false) {
|
|
| 165 | + $root = str_replace($domain, $new_domain, $root); |
|
| 166 | + } |
|
| 143 | 167 | $count = 0; |
| 144 | 168 | for ($i = 0; $i < $this->countRoutes; ++$i) { |
| 145 | 169 | $prefix = (isset($this->routes['prefix_' . $i])) ? $this->routes['prefix_' . $i] : ''; |
| 146 | 170 | $subdomain = (isset($this->routes['subdomain_' . $i])) ? $this->routes['subdomain_' . $i] : ''; |
| 147 | 171 | $url = (!empty($subdomain)) ? str_replace($protocol.'://',$protocol.'://'.$subdomain.'.' ,$root) : $root; |
| 148 | - if (isset($this->routes['routes_' . $i])) |
|
| 149 | - foreach ($this->routes['routes_' . $i] as $route => $dependencies) { |
|
| 172 | + if (isset($this->routes['routes_' . $i])) {
|
|
| 173 | + foreach ($this->routes['routes_' . $i] as $route => $dependencies) { |
|
| 150 | 174 | if (is_array($dependencies) && isset($dependencies['use'])) |
| 151 | - $use = (is_callable($dependencies['use'])) ? 'closure-' . $count : trim($dependencies['use'], '/'); |
|
| 152 | - elseif (!is_array($dependencies)) |
|
| 153 | - $use = (is_callable($dependencies)) ? 'closure-' . $count : trim($dependencies, '/'); |
|
| 154 | - else |
|
| 155 | - $use = $route; |
|
| 175 | + $use = (is_callable($dependencies['use'])) ? 'closure-' . $count : trim($dependencies['use'], '/'); |
|
| 176 | + } elseif (!is_array($dependencies)) {
|
|
| 177 | + $use = (is_callable($dependencies)) ? 'closure-' . $count : trim($dependencies, '/'); |
|
| 178 | + } else {
|
|
| 179 | + $use = $route; |
|
| 180 | + } |
|
| 156 | 181 | if (isset($route[0]) && $route[0] == '/') { |
| 157 | 182 | (!is_callable($dependencies) && isset($dependencies['name'])) |
| 158 | 183 | ? $this->routesByName[$use . '#' . $dependencies['name']] = $url . $prefix . $route |
@@ -193,9 +218,14 @@ discard block |
||
| 193 | 218 | foreach ($this->routesByName as $key => $route) { |
| 194 | 219 | $param = explode('#', $key); |
| 195 | 220 | $route = str_replace('{subdomain}', $subdomain, $route); |
| 196 | - foreach ($params as $key2 => $value) $route = str_replace(':' . $key2, $value, $route); |
|
| 197 | - if ($param[0] == trim($name, '/')) return $route; |
|
| 198 | - else if (isset($param[1]) && $param[1] == $name) return $route; |
|
| 221 | + foreach ($params as $key2 => $value) {
|
|
| 222 | + $route = str_replace(':' . $key2, $value, $route);
|
|
| 223 | + } |
|
| 224 | + if ($param[0] == trim($name, '/')) {
|
|
| 225 | + return $route; |
|
| 226 | + } else if (isset($param[1]) && $param[1] == $name) {
|
|
| 227 | + return $route; |
|
| 228 | + } |
|
| 199 | 229 | } |
| 200 | 230 | return null; |
| 201 | 231 | } |
@@ -85,8 +85,9 @@ discard block |
||
| 85 | 85 | */ |
| 86 | 86 | public function setMatcher($matcher) |
| 87 | 87 | { |
| 88 | - if (is_object($matcher)) |
|
| 89 | - $matcher = [$matcher]; |
|
| 88 | + if (is_object($matcher)) {
|
|
| 89 | + $matcher = [$matcher]; |
|
| 90 | + } |
|
| 90 | 91 | $this->matcher = $matcher; |
| 91 | 92 | } |
| 92 | 93 | |
@@ -104,8 +105,12 @@ discard block |
||
| 104 | 105 | public function run() |
| 105 | 106 | { |
| 106 | 107 | $this->setUrl(); |
| 107 | - if ($this->config['generateRoutesPath']) $this->collection->generateRoutesPath(); |
|
| 108 | - if ($this->match()) $this->callTarget(); |
|
| 108 | + if ($this->config['generateRoutesPath']) {
|
|
| 109 | + $this->collection->generateRoutesPath(); |
|
| 110 | + } |
|
| 111 | + if ($this->match()) {
|
|
| 112 | + $this->callTarget(); |
|
| 113 | + } |
|
| 109 | 114 | $this->callResponse(); |
| 110 | 115 | } |
| 111 | 116 | |
@@ -114,8 +119,9 @@ discard block |
||
| 114 | 119 | */ |
| 115 | 120 | public function setUrl($url = null) |
| 116 | 121 | { |
| 117 | - if (is_null($url)) |
|
| 118 | - $url = (isset($_GET['url'])) ? $_GET['url'] : substr(str_replace(str_replace('/index.php', '', $_SERVER['SCRIPT_NAME']), '', $_SERVER['REQUEST_URI']), 1); |
|
| 122 | + if (is_null($url)) {
|
|
| 123 | + $url = (isset($_GET['url'])) ? $_GET['url'] : substr(str_replace(str_replace('/index.php', '', $_SERVER['SCRIPT_NAME']), '', $_SERVER['REQUEST_URI']), 1);
|
|
| 124 | + } |
|
| 119 | 125 | $this->route->setUrl('/' . trim(explode('?', $url)[0], '/')); |
| 120 | 126 | } |
| 121 | 127 | |
@@ -125,7 +131,9 @@ discard block |
||
| 125 | 131 | public function match() |
| 126 | 132 | { |
| 127 | 133 | foreach ($this->matcher as $key => $matcher) { |
| 128 | - if (call_user_func([$this->matcher[$key], 'match'])) return true; |
|
| 134 | + if (call_user_func([$this->matcher[$key], 'match'])) {
|
|
| 135 | + return true; |
|
| 136 | + } |
|
| 129 | 137 | } |
| 130 | 138 | return false; |
| 131 | 139 | } |
@@ -160,11 +168,16 @@ discard block |
||
| 160 | 168 | if (isset($this->route->getDetail()['response_templates']) && isset($this->route->getDetail()['response_templates'][$code = $this->response->getStatusCode()])) { |
| 161 | 169 | $this->route->setCallback($this->route->getDetail()['response_templates'][$code]); |
| 162 | 170 | $matcher = null; |
| 163 | - foreach ($this->matcher as $instance) if ($instance instanceof ArrayMatcher) $matcher = $instance; |
|
| 164 | - if (is_null($matcher)) $matcher = new ArrayMatcher($this); |
|
| 165 | - foreach (call_user_func([$matcher, 'getResolver']) as $match) |
|
| 166 | - if (is_array($target = call_user_func_array([$matcher, $match], [$this->route->getCallback()]))) { |
|
| 167 | - call_user_func_array([$matcher, 'setTarget'], [$target]); |
|
| 171 | + foreach ($this->matcher as $instance) {
|
|
| 172 | + if ($instance instanceof ArrayMatcher) $matcher = $instance; |
|
| 173 | + } |
|
| 174 | + if (is_null($matcher)) {
|
|
| 175 | + $matcher = new ArrayMatcher($this); |
|
| 176 | + } |
|
| 177 | + foreach (call_user_func([$matcher, 'getResolver']) as $match) {
|
|
| 178 | + if (is_array($target = call_user_func_array([$matcher, $match], [$this->route->getCallback()]))) { |
|
| 179 | + call_user_func_array([$matcher, 'setTarget'], [$target]); |
|
| 180 | + } |
|
| 168 | 181 | $this->callTarget(); |
| 169 | 182 | break; |
| 170 | 183 | } |
@@ -198,9 +211,9 @@ discard block |
||
| 198 | 211 | $reflectionMethod = new ReflectionMethod($controller, $method); |
| 199 | 212 | $dependencies = []; |
| 200 | 213 | foreach ($reflectionMethod->getParameters() as $arg) { |
| 201 | - if (isset($methodArgs[$arg->name])) |
|
| 202 | - array_push($dependencies, $methodArgs[$arg->name]); |
|
| 203 | - else if (!is_null($arg->getClass())) { |
|
| 214 | + if (isset($methodArgs[$arg->name])) {
|
|
| 215 | + array_push($dependencies, $methodArgs[$arg->name]); |
|
| 216 | + } else if (!is_null($arg->getClass())) { |
|
| 204 | 217 | array_push($dependencies, call_user_func_array($this->route->getTarget('di'), [$arg->getClass()->name])); |
| 205 | 218 | } |
| 206 | 219 | } |
@@ -220,18 +233,20 @@ discard block |
||
| 220 | 233 | public function callClass($controller, $ctrlArgs = [], $classInstance = []) |
| 221 | 234 | { |
| 222 | 235 | $reflector = new ReflectionClass($controller); |
| 223 | - if (!$reflector->isInstantiable()) |
|
| 224 | - throw new \Exception('Controller [' . $controller . '] is not instantiable.'); |
|
| 236 | + if (!$reflector->isInstantiable()) {
|
|
| 237 | + throw new \Exception('Controller [' . $controller . '] is not instantiable.');
|
|
| 238 | + } |
|
| 225 | 239 | $constructor = $reflector->getConstructor(); |
| 226 | - if (is_null($constructor)) |
|
| 227 | - return call_user_func_array($this->route->getTarget('di'), [$controller]); |
|
| 240 | + if (is_null($constructor)) {
|
|
| 241 | + return call_user_func_array($this->route->getTarget('di'), [$controller]);
|
|
| 242 | + } |
|
| 228 | 243 | $dependencies = []; |
| 229 | 244 | foreach ($constructor->getParameters() as $arg) { |
| 230 | - if (isset($ctrlArgs[$arg->name])) |
|
| 231 | - array_push($dependencies, $ctrlArgs[$arg->name]); |
|
| 232 | - else if (isset($classInstance[$arg->getClass()->name])) |
|
| 233 | - array_push($dependencies, $classInstance[$arg->getClass()->name]); |
|
| 234 | - else if (!is_null($arg->getClass())) { |
|
| 245 | + if (isset($ctrlArgs[$arg->name])) {
|
|
| 246 | + array_push($dependencies, $ctrlArgs[$arg->name]); |
|
| 247 | + } else if (isset($classInstance[$arg->getClass()->name])) {
|
|
| 248 | + array_push($dependencies, $classInstance[$arg->getClass()->name]); |
|
| 249 | + } else if (!is_null($arg->getClass())) { |
|
| 235 | 250 | array_push($dependencies, call_user_func_array($this->route->getTarget('di'), [$arg->getClass()->name])); |
| 236 | 251 | } |
| 237 | 252 | } |