@@ -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 | } |
@@ -83,8 +83,9 @@ discard block |
||
| 83 | 83 | */ |
| 84 | 84 | public function setMatcher($matcher) |
| 85 | 85 | { |
| 86 | - if (is_object($matcher)) |
|
| 87 | - $matcher = [$matcher]; |
|
| 86 | + if (is_object($matcher)) {
|
|
| 87 | + $matcher = [$matcher]; |
|
| 88 | + } |
|
| 88 | 89 | $this->matcher = $matcher; |
| 89 | 90 | } |
| 90 | 91 | |
@@ -102,8 +103,12 @@ discard block |
||
| 102 | 103 | public function run() |
| 103 | 104 | { |
| 104 | 105 | $this->setUrl(); |
| 105 | - if ($this->config['generateRoutesPath']) $this->collection->generateRoutesPath(); |
|
| 106 | - if ($this->match()) $this->callTarget(); |
|
| 106 | + if ($this->config['generateRoutesPath']) {
|
|
| 107 | + $this->collection->generateRoutesPath(); |
|
| 108 | + } |
|
| 109 | + if ($this->match()) {
|
|
| 110 | + $this->callTarget(); |
|
| 111 | + } |
|
| 107 | 112 | $this->callResponse(); |
| 108 | 113 | } |
| 109 | 114 | |
@@ -112,8 +117,9 @@ discard block |
||
| 112 | 117 | */ |
| 113 | 118 | public function setUrl($url = null) |
| 114 | 119 | { |
| 115 | - if (is_null($url)) |
|
| 116 | - $url = (isset($_GET['url'])) ? $_GET['url'] : substr(str_replace(str_replace('/index.php', '', $_SERVER['SCRIPT_NAME']), '', $_SERVER['REQUEST_URI']), 1); |
|
| 120 | + if (is_null($url)) {
|
|
| 121 | + $url = (isset($_GET['url'])) ? $_GET['url'] : substr(str_replace(str_replace('/index.php', '', $_SERVER['SCRIPT_NAME']), '', $_SERVER['REQUEST_URI']), 1);
|
|
| 122 | + } |
|
| 117 | 123 | $this->route->setUrl('/' . trim(explode('?', $url)[0], '/')); |
| 118 | 124 | } |
| 119 | 125 | |
@@ -123,7 +129,9 @@ discard block |
||
| 123 | 129 | public function match() |
| 124 | 130 | { |
| 125 | 131 | foreach ($this->matcher as $key => $matcher) { |
| 126 | - if (call_user_func([$this->matcher[$key], 'match'])) return true; |
|
| 132 | + if (call_user_func([$this->matcher[$key], 'match'])) {
|
|
| 133 | + return true; |
|
| 134 | + } |
|
| 127 | 135 | } |
| 128 | 136 | return false; |
| 129 | 137 | } |
@@ -158,11 +166,16 @@ discard block |
||
| 158 | 166 | if (isset($this->route->getDetail()['response_templates']) && isset($this->route->getDetail()['response_templates'][$code = $this->response->getStatusCode()])) { |
| 159 | 167 | $this->route->setCallback($this->route->getDetail()['response_templates'][$code]); |
| 160 | 168 | $matcher = null; |
| 161 | - foreach ($this->matcher as $instance) if ($instance instanceof ArrayMatcher) $matcher = $instance; |
|
| 162 | - if (is_null($matcher)) $matcher = new ArrayMatcher($this); |
|
| 163 | - foreach (call_user_func([$matcher, 'getResolver']) as $match) |
|
| 164 | - if (is_array($target = call_user_func_array([$matcher, $match], [$this->route->getCallback()]))) { |
|
| 165 | - call_user_func_array([$matcher, 'setTarget'], [$target]); |
|
| 169 | + foreach ($this->matcher as $instance) {
|
|
| 170 | + if ($instance instanceof ArrayMatcher) $matcher = $instance; |
|
| 171 | + } |
|
| 172 | + if (is_null($matcher)) {
|
|
| 173 | + $matcher = new ArrayMatcher($this); |
|
| 174 | + } |
|
| 175 | + foreach (call_user_func([$matcher, 'getResolver']) as $match) {
|
|
| 176 | + if (is_array($target = call_user_func_array([$matcher, $match], [$this->route->getCallback()]))) { |
|
| 177 | + call_user_func_array([$matcher, 'setTarget'], [$target]); |
|
| 178 | + } |
|
| 166 | 179 | $this->callTarget(); |
| 167 | 180 | break; |
| 168 | 181 | } |
@@ -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 | } |
@@ -50,17 +50,21 @@ discard block |
||
| 50 | 50 | } |
| 51 | 51 | if ($count == count($this->route->getParameters()) || ($this->route->getParameters() == '' && $count == 0)) { |
| 52 | 52 | $dependencies = array_merge($dependencies, ($this->route->getParameters() == '') ? [] : $this->route->getParameters()); |
| 53 | - if ($this->response->getStatusCode() == 202) |
|
| 54 | - $this->response->setStatusCode(200); |
|
| 53 | + if ($this->response->getStatusCode() == 202) {
|
|
| 54 | + $this->response->setStatusCode(200); |
|
| 55 | + } |
|
| 55 | 56 | if (is_array($content = $reflectionMethod->invokeArgs($this->getController(), $dependencies))) { |
| 56 | 57 | $this->route->addTarget('data', $content); |
| 57 | 58 | if (isset($this->route->getParams()['ajax']) && $this->route->getParams()['ajax'] === true) { |
| 58 | 59 | $this->response->setContent(json_encode($content)); |
| 59 | 60 | $this->response->setHeaders(['Content-Type' => 'application/json']); |
| 60 | 61 | } |
| 61 | - } elseif (!is_null($content)) $this->response->setContent($content); |
|
| 62 | - } else |
|
| 63 | - $this->response->setStatusCode(404); |
|
| 62 | + } elseif (!is_null($content)) {
|
|
| 63 | + $this->response->setContent($content); |
|
| 64 | + } |
|
| 65 | + } else {
|
|
| 66 | + $this->response->setStatusCode(404); |
|
| 67 | + } |
|
| 64 | 68 | } |
| 65 | 69 | |
| 66 | 70 | |
@@ -71,8 +75,9 @@ discard block |
||
| 71 | 75 | private function getController() |
| 72 | 76 | { |
| 73 | 77 | $reflector = new ReflectionClass($this->route->getTarget('controller')); |
| 74 | - if (!$reflector->isInstantiable()) |
|
| 75 | - throw new \Exception('Target [' . $this->route->getTarget('controller') . '] is not instantiable.'); |
|
| 78 | + if (!$reflector->isInstantiable()) {
|
|
| 79 | + throw new \Exception('Target [' . $this->route->getTarget('controller') . '] is not instantiable.');
|
|
| 80 | + } |
|
| 76 | 81 | $constructor = $reflector->getConstructor(); |
| 77 | 82 | if (is_null($constructor)) { |
| 78 | 83 | $class = $this->route->getTarget('controller'); |
@@ -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, |