@@ -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 |
@@ -101,7 +101,9 @@ discard block |
||
101 | 101 | { |
102 | 102 | if (isset($this->middleware[$action][$action]['global_middleware'])) { |
103 | 103 | foreach ($this->middleware[$action]['global_middleware'] as $class) { |
104 | - if (class_exists($class)) return $this->callHandler($class); |
|
104 | + if (class_exists($class)) { |
|
105 | + return $this->callHandler($class); |
|
106 | + } |
|
105 | 107 | } |
106 | 108 | } |
107 | 109 | return true; |
@@ -120,7 +122,9 @@ discard block |
||
120 | 122 | if (is_array($blocks)) { |
121 | 123 | foreach ($blocks as $block) { |
122 | 124 | if (class_exists($block)) { |
123 | - if ($this->callHandler($block) === false) return false; |
|
125 | + if ($this->callHandler($block) === false) { |
|
126 | + return false; |
|
127 | + } |
|
124 | 128 | } |
125 | 129 | } |
126 | 130 | } elseif (is_string($blocks) && class_exists($blocks)) { |
@@ -144,7 +148,9 @@ discard block |
||
144 | 148 | $classes = $this->middleware[$action]['class_middleware'][$ctrl]; |
145 | 149 | if (is_array($classes)) { |
146 | 150 | foreach ($classes as $class) { |
147 | - if ($this->callHandler($class) === false) return false; |
|
151 | + if ($this->callHandler($class) === false) { |
|
152 | + return false; |
|
153 | + } |
|
148 | 154 | } |
149 | 155 | } elseif (is_string($classes)) { |
150 | 156 | return $this->callHandler($classes); |
@@ -166,7 +172,9 @@ discard block |
||
166 | 172 | $classes = $this->middleware[$action]['route_middleware'][$this->router->route->getPath()['middleware']]; |
167 | 173 | if (is_array($classes)) { |
168 | 174 | foreach ($classes as $class) { |
169 | - if ($this->callHandler($class) === false) return false; |
|
175 | + if ($this->callHandler($class) === false) { |
|
176 | + return false; |
|
177 | + } |
|
170 | 178 | } |
171 | 179 | } elseif (is_string($classes)) { |
172 | 180 | return $this->callHandler($classes); |
@@ -33,7 +33,9 @@ discard block |
||
33 | 33 | */ |
34 | 34 | public function __construct($routes = null, $options = []) |
35 | 35 | { |
36 | - if (!is_null($routes) || !empty($options)) $this->addRoutes($routes, $options); |
|
36 | + if (!is_null($routes) || !empty($options)) { |
|
37 | + $this->addRoutes($routes, $options); |
|
38 | + } |
|
37 | 39 | } |
38 | 40 | |
39 | 41 | /** |
@@ -43,10 +45,16 @@ discard block |
||
43 | 45 | public function addRoutes($routes = null, $options = []) |
44 | 46 | { |
45 | 47 | if (!is_null($routes) && !is_array($routes)) { |
46 | - if (strpos($routes, '.php') === false) $routes = trim($routes, '/') . '/'; |
|
47 | - if (is_file($routes . '/routes.php') && is_array($routesFile = include $routes . '/routes.php')) $routes = $routesFile; |
|
48 | - elseif (is_file($routes) && is_array($routesFile = include $routes)) $routes = $routesFile; |
|
49 | - else throw new \InvalidArgumentException('Argument for "' . get_called_class() . '" constructor is not recognized. Expected argument array or file containing array but "' . $routes . '" given'); |
|
48 | + if (strpos($routes, '.php') === false) { |
|
49 | + $routes = trim($routes, '/') . '/'; |
|
50 | + } |
|
51 | + if (is_file($routes . '/routes.php') && is_array($routesFile = include $routes . '/routes.php')) { |
|
52 | + $routes = $routesFile; |
|
53 | + } elseif (is_file($routes) && is_array($routesFile = include $routes)) { |
|
54 | + $routes = $routesFile; |
|
55 | + } else { |
|
56 | + throw new \InvalidArgumentException('Argument for "' . get_called_class() . '" constructor is not recognized. Expected argument array or file containing array but "' . $routes . '" given'); |
|
57 | + } |
|
50 | 58 | } |
51 | 59 | $this->routes['routes_' . $this->countRoutes] = is_array($routes) ? $routes : []; |
52 | 60 | $this->setRoutes($options, $this->countRoutes); |
@@ -59,8 +67,9 @@ discard block |
||
59 | 67 | */ |
60 | 68 | public function getRoutes($key = null) |
61 | 69 | { |
62 | - if (!is_null($key)) |
|
63 | - return isset($this->routes[$key]) ? $this->routes[$key] : ''; |
|
70 | + if (!is_null($key)) { |
|
71 | + return isset($this->routes[$key]) ? $this->routes[$key] : ''; |
|
72 | + } |
|
64 | 73 | return $this->routes; |
65 | 74 | } |
66 | 75 | |
@@ -71,12 +80,18 @@ discard block |
||
71 | 80 | { |
72 | 81 | if (is_array($args)) { |
73 | 82 | $nbrArgs = count($args); |
74 | - for ($i = 0; $i < $nbrArgs; ++$i) |
|
75 | - $this->routes['prefix_' . $i] = '/' . trim($args[$i], '/'); |
|
76 | - } elseif (is_string($args)) |
|
77 | - for ($i = 0; $i < $this->countRoutes; ++$i) |
|
78 | - $this->routes['prefix_' . $i] = '/' . trim($args, '/'); |
|
79 | - if ($this->countRoutes == 0) $this->countRoutes++; |
|
83 | + for ($i = 0; $i < $nbrArgs; ++$i) { |
|
84 | + $this->routes['prefix_' . $i] = '/' . trim($args[$i], '/'); |
|
85 | + } |
|
86 | + } elseif (is_string($args)) { |
|
87 | + for ($i = 0; |
|
88 | + } |
|
89 | + $i < $this->countRoutes; ++$i) { |
|
90 | + $this->routes['prefix_' . $i] = '/' . trim($args, '/'); |
|
91 | + } |
|
92 | + if ($this->countRoutes == 0) { |
|
93 | + $this->countRoutes++; |
|
94 | + } |
|
80 | 95 | } |
81 | 96 | |
82 | 97 | /** |
@@ -88,10 +103,14 @@ discard block |
||
88 | 103 | for ($i = 0; $i < $nbrArgs; ++$i) { |
89 | 104 | if (is_array($args[$i])) { |
90 | 105 | $this->setRoutes($args[$i], $i); |
91 | - if (!isset($this->routes['routes_' . $i])) $this->routes['routes_' . $i] = []; |
|
106 | + if (!isset($this->routes['routes_' . $i])) { |
|
107 | + $this->routes['routes_' . $i] = []; |
|
108 | + } |
|
92 | 109 | } |
93 | 110 | } |
94 | - if ($this->countRoutes == 0) $this->countRoutes++; |
|
111 | + if ($this->countRoutes == 0) { |
|
112 | + $this->countRoutes++; |
|
113 | + } |
|
95 | 114 | } |
96 | 115 | |
97 | 116 | /** |
@@ -128,10 +147,11 @@ discard block |
||
128 | 147 | $prefix = (isset($this->routes['prefix_' . $i])) ? $this->routes['prefix_' . $i] : ''; |
129 | 148 | $subdomain = (isset($this->routes['subdomain_' . $i])) ? $this->routes['subdomain_' . $i] : ''; |
130 | 149 | $url = (!empty($subdomain)) ? str_replace($protocol.'://',$protocol.'://'.$subdomain.'.' ,$root) : $root; |
131 | - if (isset($this->routes['routes_' . $i])) |
|
132 | - foreach ($this->routes['routes_' . $i] as $route => $dependencies) { |
|
150 | + if (isset($this->routes['routes_' . $i])) { |
|
151 | + foreach ($this->routes['routes_' . $i] as $route => $dependencies) { |
|
133 | 152 | if (is_array($dependencies) && isset($dependencies['use']) && !is_array($dependencies['use'])) { |
134 | - $use = (is_callable($dependencies['use'])) ? 'closure-' . $count : trim($dependencies['use'], '/'); |
|
153 | + $use = (is_callable($dependencies['use'])) ? 'closure-' . $count : trim($dependencies['use'], '/'); |
|
154 | + } |
|
135 | 155 | } elseif (!is_array($dependencies)) { |
136 | 156 | $use = (is_callable($dependencies)) ? 'closure-' . $count : trim($dependencies, '/'); |
137 | 157 | } else { |
@@ -177,9 +197,14 @@ discard block |
||
177 | 197 | foreach ($this->routesByName as $key => $route) { |
178 | 198 | $param = explode('#', $key); |
179 | 199 | $route = str_replace('{subdomain}', $subdomain, $route); |
180 | - foreach ($params as $key2 => $value) $route = str_replace(':' . $key2, $value, $route); |
|
181 | - if ($param[0] == trim($name, '/')) return $route; |
|
182 | - else if (isset($param[1]) && $param[1] == $name) return $route; |
|
200 | + foreach ($params as $key2 => $value) { |
|
201 | + $route = str_replace(':' . $key2, $value, $route); |
|
202 | + } |
|
203 | + if ($param[0] == trim($name, '/')) { |
|
204 | + return $route; |
|
205 | + } else if (isset($param[1]) && $param[1] == $name) { |
|
206 | + return $route; |
|
207 | + } |
|
183 | 208 | } |
184 | 209 | return null; |
185 | 210 | } |
@@ -49,10 +49,11 @@ |
||
49 | 49 | |
50 | 50 | foreach ($reflectionMethod->getParameters() as $arg) { |
51 | 51 | if (!is_null($arg->getClass())) { |
52 | - if (isset($classInstance[$arg->getClass()->name])) |
|
53 | - $dependencies[] = $classInstance[$arg->getClass()->name]; |
|
54 | - else |
|
55 | - $dependencies[] = call_user_func_array($this->router->route->getTarget('di'), [$arg->getClass()->name]); |
|
52 | + if (isset($classInstance[$arg->getClass()->name])) { |
|
53 | + $dependencies[] = $classInstance[$arg->getClass()->name]; |
|
54 | + } else { |
|
55 | + $dependencies[] = call_user_func_array($this->router->route->getTarget('di'), [$arg->getClass()->name]); |
|
56 | + } |
|
56 | 57 | } else { |
57 | 58 | $count++; |
58 | 59 | } |
@@ -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 | } |
@@ -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) { |
@@ -239,7 +241,7 @@ discard block |
||
239 | 241 | if (is_array($this->request['params']) && isset($this->request['params']['use'])) { |
240 | 242 | if(is_array($this->request['params']['use']) && isset($this->request['params']['use'][$this->router->route->getMethod()])){ |
241 | 243 | $this->router->route->setCallback($this->request['params']['use'][$this->router->route->getMethod()]); |
242 | - }elseif(!is_array($this->request['params']['use'])){ |
|
244 | + } elseif(!is_array($this->request['params']['use'])){ |
|
243 | 245 | $this->router->route->setCallback($this->request['params']['use']); |
244 | 246 | } |
245 | 247 | } else { |
@@ -331,12 +333,16 @@ discard block |
||
331 | 333 | { |
332 | 334 | if (is_string($callback) && strpos($callback, '@') !== false) { |
333 | 335 | $routes = explode('@', $callback); |
334 | - if (!isset($routes[1])) $routes[1] = 'index'; |
|
336 | + if (!isset($routes[1])) { |
|
337 | + $routes[1] = 'index'; |
|
338 | + } |
|
335 | 339 | if ($routes[1] == '{method}') { |
336 | 340 | $this->request['parameters'] = explode('/', str_replace(str_replace('*', '', $this->request['route']), '', $this->router->route->getUrl())); |
337 | 341 | $routes[1] = $this->request['parameters'][0]; |
338 | 342 | array_shift($this->request['parameters']); |
339 | - if (preg_match('/[A-Z]/', $routes[1])) return false; |
|
343 | + if (preg_match('/[A-Z]/', $routes[1])) { |
|
344 | + return false; |
|
345 | + } |
|
340 | 346 | $routes[1] = lcfirst(str_replace(' ', '', ucwords(str_replace('-', ' ', $routes[1])))); |
341 | 347 | } |
342 | 348 | $index = isset($this->request['collection_index']) ? $this->request['collection_index'] : 0; |
@@ -120,14 +120,16 @@ 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 | 131 | $this->callMiddleware('after'); |
130 | - }else{ |
|
132 | + } else{ |
|
131 | 133 | $this->response->setStatusCode(404); |
132 | 134 | } |
133 | 135 | return $this->callResponse(); |
@@ -158,8 +160,9 @@ discard block |
||
158 | 160 | */ |
159 | 161 | public function setUrl($url = null) |
160 | 162 | { |
161 | - if (is_null($url)) |
|
162 | - $url = (isset($_GET['url'])) ? $_GET['url'] : substr(str_replace(str_replace('/index.php', '', $_SERVER['SCRIPT_NAME']), '', $_SERVER['REQUEST_URI']), 1); |
|
163 | + if (is_null($url)) { |
|
164 | + $url = (isset($_GET['url'])) ? $_GET['url'] : substr(str_replace(str_replace('/index.php', '', $_SERVER['SCRIPT_NAME']), '', $_SERVER['REQUEST_URI']), 1); |
|
165 | + } |
|
163 | 166 | $this->route->setUrl('/' . trim(explode('?', $url)[0], '/')); |
164 | 167 | } |
165 | 168 | |
@@ -169,7 +172,9 @@ discard block |
||
169 | 172 | public function match() |
170 | 173 | { |
171 | 174 | foreach ($this->matcher as $key => $matcher) { |
172 | - if (call_user_func([$this->matcher[$key], 'match'])) return true; |
|
175 | + if (call_user_func([$this->matcher[$key], 'match'])) { |
|
176 | + return true; |
|
177 | + } |
|
173 | 178 | } |
174 | 179 | return false; |
175 | 180 | } |