@@ -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 | |
@@ -64,7 +64,7 @@ |
||
| 64 | 64 | } |
| 65 | 65 | |
| 66 | 66 | /** |
| 67 | - * @param $regex |
|
| 67 | + * @param string $regex |
|
| 68 | 68 | * @return bool |
| 69 | 69 | */ |
| 70 | 70 | private function routeMatch($regex) |
@@ -118,8 +118,9 @@ discard block |
||
| 118 | 118 | { |
| 119 | 119 | if (substr($this->request['route'], -1) == '*') { |
| 120 | 120 | $pos = strpos($this->request['route'], '*'); |
| 121 | - if (substr($this->router->route->getUrl(), 0, $pos) == substr($this->request['route'], 0, $pos) && isset($this->request['params'])) |
|
| 122 | - return true; |
|
| 121 | + if (substr($this->router->route->getUrl(), 0, $pos) == substr($this->request['route'], 0, $pos) && isset($this->request['params'])) {
|
|
| 122 | + return true; |
|
| 123 | + } |
|
| 123 | 124 | } |
| 124 | 125 | if (preg_match($regex, $this->router->route->getUrl(), $this->request['parameters'])) { |
| 125 | 126 | array_shift($this->request['parameters']); |
@@ -134,14 +135,16 @@ discard block |
||
| 134 | 135 | private function generateTarget() |
| 135 | 136 | { |
| 136 | 137 | if($this->validMethod()) { |
| 137 | - foreach($this->matcher as $match) |
|
| 138 | - if(call_user_func_array([$this,$match],[$this->router->route->getCallback()])) break; |
|
| 138 | + foreach($this->matcher as $match) {
|
|
| 139 | + if(call_user_func_array([$this,$match],[$this->router->route->getCallback()])) break; |
|
| 140 | + } |
|
| 139 | 141 | $index = isset($this->request['collection_index']) ? $this->request['collection_index'] : 0; |
| 140 | 142 | $this->router->route->addTarget('block',$this->router->collection->getRoutes('block_'.$index)); |
| 141 | 143 | $this->router->route->addTarget('view_dir',$this->router->collection->getRoutes('view_dir_'.$index)); |
| 142 | 144 | $this->router->response->setStatusCode(202); |
| 143 | - }else |
|
| 144 | - $this->router->response->setStatusCode(405); |
|
| 145 | + } else {
|
|
| 146 | + $this->router->response->setStatusCode(405); |
|
| 147 | + } |
|
| 145 | 148 | return $this->router->route->hasTarget(); |
| 146 | 149 | } |
| 147 | 150 | |
@@ -150,14 +153,18 @@ discard block |
||
| 150 | 153 | */ |
| 151 | 154 | private function setRoute(){ |
| 152 | 155 | if (isset($this->request['params'])) { |
| 153 | - if(is_callable($this->request['params'])) |
|
| 154 | - $this->router->route->setCallback($this->request['params']); |
|
| 155 | - else { |
|
| 156 | + if(is_callable($this->request['params'])) {
|
|
| 157 | + $this->router->route->setCallback($this->request['params']); |
|
| 158 | + } else { |
|
| 156 | 159 | (is_array($this->request['params']) && isset($this->request['params']['use'])) |
| 157 | 160 | ? $this->router->route->setCallback($this->request['params']['use']) |
| 158 | 161 | : $this->router->route->setCallback($this->request['params']); |
| 159 | - if (isset($this->request['params']['name'])) $this->router->route->setName($this->request['params']['name']); |
|
| 160 | - if (isset($this->request['params']['method'])) $this->request['params']['method'] = is_array($this->request['params']['method']) ? $this->request['params']['method'] : [$this->request['params']['method']]; |
|
| 162 | + if (isset($this->request['params']['name'])) {
|
|
| 163 | + $this->router->route->setName($this->request['params']['name']); |
|
| 164 | + } |
|
| 165 | + if (isset($this->request['params']['method'])) {
|
|
| 166 | + $this->request['params']['method'] = is_array($this->request['params']['method']) ? $this->request['params']['method'] : [$this->request['params']['method']]; |
|
| 167 | + } |
|
| 161 | 168 | } |
| 162 | 169 | } |
| 163 | 170 | $this->router->route->setDetail($this->request); |
@@ -168,9 +175,12 @@ discard block |
||
| 168 | 175 | */ |
| 169 | 176 | public function validMethod() |
| 170 | 177 | { |
| 171 | - if(is_callable($this->request['params']))return true; |
|
| 172 | - if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') |
|
| 173 | - return (isset($this->request['params']['ajax']) && $this->request['params']['ajax'] === true) ? true : false; |
|
| 178 | + if(is_callable($this->request['params'])) {
|
|
| 179 | + return true; |
|
| 180 | + } |
|
| 181 | + if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
|
|
| 182 | + return (isset($this->request['params']['ajax']) && $this->request['params']['ajax'] === true) ? true : false; |
|
| 183 | + } |
|
| 174 | 184 | $method = (isset($this->router->route->getDetail()['params']['method'])) ? $this->router->route->getDetail()['params']['method'] : ['GET']; |
| 175 | 185 | return (in_array($this->router->route->getMethod(), $method)) ? true : false; |
| 176 | 186 | } |
@@ -201,13 +211,16 @@ discard block |
||
| 201 | 211 | { |
| 202 | 212 | if (is_string($callback) && strpos($callback, '@') !== false) { |
| 203 | 213 | $routes = explode('@', $callback); |
| 204 | - if (!isset($routes[1])) $routes[1] = 'index'; |
|
| 214 | + if (!isset($routes[1])) {
|
|
| 215 | + $routes[1] = 'index'; |
|
| 216 | + } |
|
| 205 | 217 | $index = isset($this->request['collection_index']) ? $this->request['collection_index'] : 0; |
| 206 | 218 | $class = (class_exists($routes[0])) |
| 207 | 219 | ? $routes[0] |
| 208 | 220 | : $this->router->collection->getRoutes()['ctrl_namespace_'.$index].$routes[0]; |
| 209 | - if (!class_exists($class)) |
|
| 210 | - throw new \Exception('Class "' . $class . '." is not found'); |
|
| 221 | + if (!class_exists($class)) {
|
|
| 222 | + throw new \Exception('Class "' . $class . '." is not found');
|
|
| 223 | + } |
|
| 211 | 224 | if (method_exists($class, $routes[1])) { |
| 212 | 225 | $this->router->route->setTarget([ |
| 213 | 226 | 'dispatcher' => $this->dispatcher['matchController'], |
@@ -235,9 +248,9 @@ discard block |
||
| 235 | 248 | $index = isset($this->request['collection_index']) ? $this->request['collection_index'] : 0; |
| 236 | 249 | $viewDir = $this->router->collection->getRoutes('view_dir_' . $index); |
| 237 | 250 | $target = null; |
| 238 | - if (in_array('.' . $extension, $this->router->getConfig()['templateExtension']) && (is_file($fullPath = $viewDir . $path) || is_file($fullPath = $path))) |
|
| 239 | - $target = $fullPath; |
|
| 240 | - else { |
|
| 251 | + if (in_array('.' . $extension, $this->router->getConfig()['templateExtension']) && (is_file($fullPath = $viewDir . $path) || is_file($fullPath = $path))) {
|
|
| 252 | + $target = $fullPath; |
|
| 253 | + } else { |
|
| 241 | 254 | foreach ($this->router->getConfig()['templateExtension'] as $ext) { |
| 242 | 255 | if (is_file($fullPath = $viewDir . $path . $ext) || is_file($fullPath = $path . $ext)) { |
| 243 | 256 | $target = $fullPath; |
@@ -246,8 +259,9 @@ discard block |
||
| 246 | 259 | } |
| 247 | 260 | } |
| 248 | 261 | } |
| 249 | - if(is_null($target)) |
|
| 250 | - throw new \Exception('Template file "' . $path . '" is not found in "' . $viewDir . '"'); |
|
| 262 | + if(is_null($target)) {
|
|
| 263 | + throw new \Exception('Template file "' . $path . '" is not found in "' . $viewDir . '"');
|
|
| 264 | + } |
|
| 251 | 265 | $this->router->route->setTarget([ |
| 252 | 266 | 'dispatcher' => $this->dispatcher['matchTemplate'], |
| 253 | 267 | 'template' => $target, |
@@ -42,12 +42,14 @@ discard block |
||
| 42 | 42 | { |
| 43 | 43 | $reflectionMethod = new ReflectionMethod($this->route->getTarget('controller'), $this->route->getTarget('action')); |
| 44 | 44 | $dependencies = []; |
| 45 | - foreach ($reflectionMethod->getParameters() as $arg) |
|
| 46 | - if (!is_null($arg->getClass())) |
|
| 47 | - $dependencies[] = call_user_func_array($this->route->getTarget('di'),[$arg->getClass()->name]); |
|
| 45 | + foreach ($reflectionMethod->getParameters() as $arg) {
|
|
| 46 | + if (!is_null($arg->getClass())) |
|
| 47 | + $dependencies[] = call_user_func_array($this->route->getTarget('di'),[$arg->getClass()->name]);
|
|
| 48 | + } |
|
| 48 | 49 | $dependencies = array_merge($dependencies,($this->route->getParameters() == '') ? [] : $this->route->getParameters()); |
| 49 | - if ($this->response->getStatusCode() == 202) |
|
| 50 | - $this->response->setStatusCode(200); |
|
| 50 | + if ($this->response->getStatusCode() == 202) {
|
|
| 51 | + $this->response->setStatusCode(200); |
|
| 52 | + } |
|
| 51 | 53 | return $this->response->setContent($reflectionMethod->invokeArgs($this->getController(), $dependencies)); |
| 52 | 54 | } |
| 53 | 55 | |
@@ -59,8 +61,9 @@ discard block |
||
| 59 | 61 | private function getController() |
| 60 | 62 | { |
| 61 | 63 | $reflector = new ReflectionClass($this->route->getTarget('controller')); |
| 62 | - if (!$reflector->isInstantiable()) |
|
| 63 | - throw new \Exception('Target [' . $this->route->getTarget('controller') . '] is not instantiable.'); |
|
| 64 | + if (!$reflector->isInstantiable()) {
|
|
| 65 | + throw new \Exception('Target [' . $this->route->getTarget('controller') . '] is not instantiable.');
|
|
| 66 | + } |
|
| 64 | 67 | $constructor = $reflector->getConstructor(); |
| 65 | 68 | if (is_null($constructor)) { |
| 66 | 69 | $class = $this->route->getTarget('controller'); |
@@ -47,11 +47,12 @@ |
||
| 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 | 57 | require($this->route->getTarget('template')); |
| 57 | 58 | $this->response->setContent(ob_get_clean()); |
@@ -86,7 +86,9 @@ discard block |
||
| 86 | 86 | public function run() |
| 87 | 87 | { |
| 88 | 88 | $this->setUrl(); |
| 89 | - if ($this->config['generateRoutesPath']) $this->collection->generateRoutesPath(); |
|
| 89 | + if ($this->config['generateRoutesPath']) {
|
|
| 90 | + $this->collection->generateRoutesPath(); |
|
| 91 | + } |
|
| 90 | 92 | if ($this->match()) { |
| 91 | 93 | $this->handle(); |
| 92 | 94 | $this->callTarget(); |
@@ -99,8 +101,9 @@ discard block |
||
| 99 | 101 | */ |
| 100 | 102 | public function setUrl($url = null) |
| 101 | 103 | { |
| 102 | - if (is_null($url)) |
|
| 103 | - $url = (isset($_GET['url'])) ? $_GET['url'] : substr(str_replace(str_replace('/index.php', '', $_SERVER['SCRIPT_NAME']), '', $_SERVER['REQUEST_URI']), 1); |
|
| 104 | + if (is_null($url)) {
|
|
| 105 | + $url = (isset($_GET['url'])) ? $_GET['url'] : substr(str_replace(str_replace('/index.php', '', $_SERVER['SCRIPT_NAME']), '', $_SERVER['REQUEST_URI']), 1);
|
|
| 106 | + } |
|
| 104 | 107 | $this->route->setUrl('/' . trim(explode('?', $url)[0], '/')); |
| 105 | 108 | } |
| 106 | 109 | |
@@ -111,8 +114,9 @@ discard block |
||
| 111 | 114 | { |
| 112 | 115 | foreach ($this->config['matcher'] as $matcher) { |
| 113 | 116 | $this->config['matcherInstance'][$matcher] = new $matcher($this); |
| 114 | - if (call_user_func([$this->config['matcherInstance'][$matcher], 'match'])) |
|
| 115 | - return true; |
|
| 117 | + if (call_user_func([$this->config['matcherInstance'][$matcher], 'match'])) {
|
|
| 118 | + return true; |
|
| 119 | + } |
|
| 116 | 120 | } |
| 117 | 121 | return false; |
| 118 | 122 | } |
@@ -157,12 +161,15 @@ discard block |
||
| 157 | 161 | { |
| 158 | 162 | if (isset($this->route->getDetail()['response_templates']) && isset($this->route->getDetail()['response_templates'][$code = $this->response->getStatusCode()])) { |
| 159 | 163 | $this->route->setCallback($this->route->getDetail()['response_templates'][$code]); |
| 160 | - if(!isset($this->config['matcherInstance'])) |
|
| 161 | - foreach ($this->config['matcher'] as $matcher) |
|
| 162 | - $this->config['matcherInstance'][$matcher] = new $matcher($this); |
|
| 164 | + if(!isset($this->config['matcherInstance'])) {
|
|
| 165 | + foreach ($this->config['matcher'] as $matcher) |
|
| 166 | + $this->config['matcherInstance'][$matcher] = new $matcher($this); |
|
| 167 | + } |
|
| 163 | 168 | foreach($this->config['matcherInstance'] as $instance) { |
| 164 | - foreach (call_user_func([$instance, 'getMatcher']) as $match) |
|
| 165 | - if (call_user_func_array([$instance, $match],[$this->route->getCallback()])){ $this->callTarget(); break; } |
|
| 169 | + foreach (call_user_func([$instance, 'getMatcher']) as $match) {
|
|
| 170 | + if (call_user_func_array([$instance, $match],[$this->route->getCallback()])){ $this->callTarget();
|
|
| 171 | + } |
|
| 172 | + break; } |
|
| 166 | 173 | } |
| 167 | 174 | $this->response->setStatusCode($code); |
| 168 | 175 | } |
@@ -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->routes['view_dir_' . $this->countRoutes] = (isset($options['view_dir']) && !empty($options['view_dir'])) ? rtrim($options['view_dir'], '/') . '/' : ''; |
@@ -62,8 +70,9 @@ discard block |
||
| 62 | 70 | */ |
| 63 | 71 | public function getRoutes($key = null) |
| 64 | 72 | { |
| 65 | - if(!is_null($key)) |
|
| 66 | - return isset($this->routes[$key])?$this->routes[$key]:''; |
|
| 73 | + if(!is_null($key)) {
|
|
| 74 | + return isset($this->routes[$key])?$this->routes[$key]:''; |
|
| 75 | + } |
|
| 67 | 76 | return $this->routes; |
| 68 | 77 | } |
| 69 | 78 | |
@@ -74,12 +83,18 @@ discard block |
||
| 74 | 83 | { |
| 75 | 84 | if (is_array($args)) { |
| 76 | 85 | $nbrArgs = count($args); |
| 77 | - for ($i = 0; $i < $nbrArgs; ++$i) |
|
| 78 | - $this->routes['prefix_' . $i] = '/' . trim($args[$i], '/'); |
|
| 79 | - } elseif (is_string($args)) |
|
| 80 | - for ($i = 0; $i < $this->countRoutes; ++$i) |
|
| 81 | - $this->routes['prefix_' . $i] = '/' . trim($args, '/'); |
|
| 82 | - if($this->countRoutes == 0)$this->countRoutes++; |
|
| 86 | + for ($i = 0; $i < $nbrArgs; ++$i) {
|
|
| 87 | + $this->routes['prefix_' . $i] = '/' . trim($args[$i], '/'); |
|
| 88 | + } |
|
| 89 | + } elseif (is_string($args)) {
|
|
| 90 | + for ($i = 0; |
|
| 91 | + } |
|
| 92 | + $i < $this->countRoutes; ++$i) {
|
|
| 93 | + $this->routes['prefix_' . $i] = '/' . trim($args, '/'); |
|
| 94 | + } |
|
| 95 | + if($this->countRoutes == 0) {
|
|
| 96 | + $this->countRoutes++; |
|
| 97 | + } |
|
| 83 | 98 | } |
| 84 | 99 | |
| 85 | 100 | /** |
@@ -94,10 +109,14 @@ discard block |
||
| 94 | 109 | $this->routes['view_dir_' . $i] = (isset($args[$i]['view_dir']) && !empty($args[$i]['view_dir'])) ? rtrim($args[$i]['view_dir'], '/') . '/' : ''; |
| 95 | 110 | $this->routes['ctrl_namespace_' . $i] = (isset($args[$i]['ctrl_namespace']) && !empty($args[$i]['ctrl_namespace'])) ? trim($args[$i]['ctrl_namespace'], '\\') . '\\' : ''; |
| 96 | 111 | $this->routes['prefix_' . $i] = (isset($args[$i]['prefix']) && !empty($args[$i]['prefix'])) ? '/'.trim($args[$i]['prefix'], '/') : ''; |
| 97 | - if(!isset($this->routes['routes_' . $i]))$this->routes['routes_' . $i] = []; |
|
| 112 | + if(!isset($this->routes['routes_' . $i])) {
|
|
| 113 | + $this->routes['routes_' . $i] = []; |
|
| 114 | + } |
|
| 98 | 115 | } |
| 99 | 116 | } |
| 100 | - if($this->countRoutes == 0)$this->countRoutes++; |
|
| 117 | + if($this->countRoutes == 0) {
|
|
| 118 | + $this->countRoutes++; |
|
| 119 | + } |
|
| 101 | 120 | } |
| 102 | 121 | |
| 103 | 122 | /** |
@@ -106,12 +125,16 @@ discard block |
||
| 106 | 125 | */ |
| 107 | 126 | public function setMiddleware($middleware) |
| 108 | 127 | { |
| 109 | - if (is_string($middleware)) $middleware = rtrim($middleware, '/'); |
|
| 110 | - if (is_array($middleware)) |
|
| 111 | - $this->middleware = $middleware; |
|
| 112 | - elseif (is_file($middleware) && is_array($mid = include $middleware)) |
|
| 113 | - $this->middleware = $mid; |
|
| 114 | - else throw new \InvalidArgumentException('Accepted argument for setMiddleware are array and array file'); |
|
| 128 | + if (is_string($middleware)) {
|
|
| 129 | + $middleware = rtrim($middleware, '/'); |
|
| 130 | + } |
|
| 131 | + if (is_array($middleware)) {
|
|
| 132 | + $this->middleware = $middleware; |
|
| 133 | + } elseif (is_file($middleware) && is_array($mid = include $middleware)) {
|
|
| 134 | + $this->middleware = $mid; |
|
| 135 | + } else {
|
|
| 136 | + throw new \InvalidArgumentException('Accepted argument for setMiddleware are array and array file');
|
|
| 137 | + } |
|
| 115 | 138 | } |
| 116 | 139 | |
| 117 | 140 | /** |
@@ -123,14 +146,15 @@ discard block |
||
| 123 | 146 | $count = 0; |
| 124 | 147 | for ($i = 0; $i < $this->countRoutes; ++$i) { |
| 125 | 148 | $prefix = (isset($this->routes['prefix_' . $i])) ? $this->routes['prefix_' . $i] : ''; |
| 126 | - if (isset($this->routes['routes_' . $i])) |
|
| 127 | - foreach ($this->routes['routes_' . $i] as $route => $dependencies) { |
|
| 149 | + if (isset($this->routes['routes_' . $i])) {
|
|
| 150 | + foreach ($this->routes['routes_' . $i] as $route => $dependencies) { |
|
| 128 | 151 | if (is_array($dependencies) && isset($dependencies['use'])) |
| 129 | - $use = (is_callable($dependencies['use'])) ? 'closure-' . $count : trim($dependencies['use'], '/'); |
|
| 130 | - elseif(!is_array($dependencies)) |
|
| 131 | - $use = (is_callable($dependencies)) ? 'closure-' . $count : trim($dependencies, '/'); |
|
| 132 | - else |
|
| 133 | - $use = $route; |
|
| 152 | + $use = (is_callable($dependencies['use'])) ? 'closure-' . $count : trim($dependencies['use'], '/'); |
|
| 153 | + } elseif(!is_array($dependencies)) {
|
|
| 154 | + $use = (is_callable($dependencies)) ? 'closure-' . $count : trim($dependencies, '/'); |
|
| 155 | + } else {
|
|
| 156 | + $use = $route; |
|
| 157 | + } |
|
| 134 | 158 | (!is_callable($dependencies) && isset($dependencies['name'])) ? $this->routesByName[$use . '#' . $dependencies['name']] = $root . $prefix . $route : $this->routesByName[$use] = $root . $prefix . $route; |
| 135 | 159 | $count++; |
| 136 | 160 | } |
@@ -147,9 +171,14 @@ discard block |
||
| 147 | 171 | { |
| 148 | 172 | foreach ($this->routesByName as $key => $route) { |
| 149 | 173 | $param = explode('#', $key); |
| 150 | - foreach ($params as $key2 => $value) $route = str_replace(':' . $key2, $value, $route); |
|
| 151 | - if ($param[0] == trim($name, '/')) return $route; |
|
| 152 | - else if (isset($param[1]) && $param[1] == $name) return $route; |
|
| 174 | + foreach ($params as $key2 => $value) {
|
|
| 175 | + $route = str_replace(':' . $key2, $value, $route);
|
|
| 176 | + } |
|
| 177 | + if ($param[0] == trim($name, '/')) {
|
|
| 178 | + return $route; |
|
| 179 | + } else if (isset($param[1]) && $param[1] == $name) {
|
|
| 180 | + return $route; |
|
| 181 | + } |
|
| 153 | 182 | } |
| 154 | 183 | return null; |
| 155 | 184 | } |
@@ -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)) $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)) $this->callHandler($class); |
|
| 35 | + } |
|
| 35 | 36 | } |
| 36 | 37 | |
| 37 | 38 | /** |
@@ -39,9 +40,10 @@ discard block |
||
| 39 | 40 | */ |
| 40 | 41 | public function blockMiddleware() |
| 41 | 42 | { |
| 42 | - if (isset($this->router->collection->middleware['block_middleware'])) |
|
| 43 | - if (isset($this->router->collection->middleware['block_middleware'][$this->router->route->getTarget('block')]) && class_exists($this->router->collection->middleware['block_middleware'][$this->router->route->getTarget('block')])) { |
|
| 44 | - $class = $this->router->collection->middleware['block_middleware'][$this->router->route->getTarget('block')]; |
|
| 43 | + if (isset($this->router->collection->middleware['block_middleware'])) {
|
|
| 44 | + if (isset($this->router->collection->middleware['block_middleware'][$this->router->route->getTarget('block')]) && class_exists($this->router->collection->middleware['block_middleware'][$this->router->route->getTarget('block')])) { |
|
| 45 | + $class = $this->router->collection->middleware['block_middleware'][$this->router->route->getTarget('block')];
|
|
| 46 | + } |
|
| 45 | 47 | $this->callHandler($class); |
| 46 | 48 | } |
| 47 | 49 | } |
@@ -65,9 +67,10 @@ discard block |
||
| 65 | 67 | */ |
| 66 | 68 | public function routeMiddleware() |
| 67 | 69 | { |
| 68 | - if (isset($this->router->collection->middleware['route_middleware'])) |
|
| 69 | - if (isset($this->router->route->getPath()['middleware']) && class_exists($this->router->collection->middleware['route_middleware'][$this->router->route->getPath()['middleware']])) { |
|
| 70 | - $class = $this->router->collection->middleware['route_middleware'][$this->router->route->getPath()['middleware']]; |
|
| 70 | + if (isset($this->router->collection->middleware['route_middleware'])) {
|
|
| 71 | + if (isset($this->router->route->getPath()['middleware']) && class_exists($this->router->collection->middleware['route_middleware'][$this->router->route->getPath()['middleware']])) { |
|
| 72 | + $class = $this->router->collection->middleware['route_middleware'][$this->router->route->getPath()['middleware']]; |
|
| 73 | + } |
|
| 71 | 74 | $this->callHandler($class); |
| 72 | 75 | } |
| 73 | 76 | } |
@@ -81,9 +84,10 @@ discard block |
||
| 81 | 84 | if (method_exists($instance, 'handle')) { |
| 82 | 85 | $reflectionMethod = new ReflectionMethod($instance, 'handle'); |
| 83 | 86 | $dependencies = []; |
| 84 | - foreach ($reflectionMethod->getParameters() as $arg) |
|
| 85 | - if (!is_null($arg->getClass())) |
|
| 86 | - $dependencies[] = $this->getClass($arg->getClass()->name); |
|
| 87 | + foreach ($reflectionMethod->getParameters() as $arg) {
|
|
| 88 | + if (!is_null($arg->getClass())) |
|
| 89 | + $dependencies[] = $this->getClass($arg->getClass()->name); |
|
| 90 | + } |
|
| 87 | 91 | $dependencies = array_merge($dependencies, [$this->router->route]); |
| 88 | 92 | $reflectionMethod->invokeArgs($instance, $dependencies); |
| 89 | 93 | } |