@@ -163,7 +163,7 @@ discard block |
||
163 | 163 | } |
164 | 164 | |
165 | 165 | /** |
166 | - * @param $regex |
|
166 | + * @param string $regex |
|
167 | 167 | * @return bool |
168 | 168 | */ |
169 | 169 | private function routeMatch($regex) |
@@ -181,7 +181,7 @@ discard block |
||
181 | 181 | } |
182 | 182 | |
183 | 183 | /** |
184 | - * @return bool |
|
184 | + * @return null|boolean |
|
185 | 185 | */ |
186 | 186 | private function generateTarget() |
187 | 187 | { |
@@ -216,7 +216,7 @@ discard block |
||
216 | 216 | } |
217 | 217 | |
218 | 218 | /** |
219 | - * @param $key |
|
219 | + * @param string $key |
|
220 | 220 | */ |
221 | 221 | private function checkRequest($key) |
222 | 222 | { |
@@ -95,15 +95,15 @@ discard block |
||
95 | 95 | { |
96 | 96 | $this->request = []; |
97 | 97 | for ($i = 0; $i < $this->router->collection->countRoutes; ++$i) { |
98 | - $this->request['prefix'] = ($this->router->collection->getRoutes('prefix_' . $i) != '') ? $this->router->collection->getRoutes('prefix_' . $i) : ''; |
|
99 | - $this->request['subdomain'] = ($this->router->collection->getRoutes('subdomain_' . $i) != '') ? $this->router->collection->getRoutes('subdomain_' . $i) : ''; |
|
100 | - foreach ($this->router->collection->getRoutes('routes_' . $i) as $route => $params) { |
|
98 | + $this->request['prefix'] = ($this->router->collection->getRoutes('prefix_'.$i) != '') ? $this->router->collection->getRoutes('prefix_'.$i) : ''; |
|
99 | + $this->request['subdomain'] = ($this->router->collection->getRoutes('subdomain_'.$i) != '') ? $this->router->collection->getRoutes('subdomain_'.$i) : ''; |
|
100 | + foreach ($this->router->collection->getRoutes('routes_'.$i) as $route => $params) { |
|
101 | 101 | $this->request['params'] = $params; |
102 | 102 | $this->request['collection_index'] = $i; |
103 | 103 | if ($this->checkSubdomain($route)) { |
104 | 104 | $route = strstr($route, '/'); |
105 | - $this->request['route'] = preg_replace_callback('#:([\w]+)#', [$this, 'paramMatch'], '/' . trim(trim($this->request['prefix'], '/') . '/' . trim($route, '/'), '/')); |
|
106 | - if ($this->routeMatch('#^' . $this->request['route'] . '$#')) { |
|
105 | + $this->request['route'] = preg_replace_callback('#:([\w]+)#', [$this, 'paramMatch'], '/'.trim(trim($this->request['prefix'], '/').'/'.trim($route, '/'), '/')); |
|
106 | + if ($this->routeMatch('#^'.$this->request['route'].'$#')) { |
|
107 | 107 | $this->setCallback(); |
108 | 108 | if (!is_null($response = $this->generateTarget())) return $response; |
109 | 109 | } |
@@ -119,19 +119,19 @@ discard block |
||
119 | 119 | */ |
120 | 120 | private function checkSubdomain($route) |
121 | 121 | { |
122 | - $url = (isset($_SERVER['REQUEST_SCHEME']) ? $_SERVER['REQUEST_SCHEME'] : 'http') . '://' . ($host = (isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : $_SERVER['HTTP_HOST'])); |
|
122 | + $url = (isset($_SERVER['REQUEST_SCHEME']) ? $_SERVER['REQUEST_SCHEME'] : 'http').'://'.($host = (isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : $_SERVER['HTTP_HOST'])); |
|
123 | 123 | $host = explode(':', $host)[0]; |
124 | 124 | $domain = $this->router->collection->getDomain($url); |
125 | - if (!empty($this->request['subdomain']) && $route[0] == '/') $route = trim($this->request['subdomain'], '.') . '.' . $domain . $route; |
|
125 | + if (!empty($this->request['subdomain']) && $route[0] == '/') $route = trim($this->request['subdomain'], '.').'.'.$domain.$route; |
|
126 | 126 | if ($route[0] == '/') { |
127 | 127 | return ($host != $domain) ? false : true; |
128 | 128 | } elseif ($route[0] != '/' && $host != $domain) { |
129 | 129 | $route = substr($route, 0, strpos($route, "/")); |
130 | 130 | $route = str_replace('{host}', $domain, $route); |
131 | 131 | $route = preg_replace_callback('#{subdomain}#', [$this, 'subdomainMatch'], $route); |
132 | - if (preg_match('#^' . $route . '$#', $host, $this->request['called_subdomain'])) { |
|
132 | + if (preg_match('#^'.$route.'$#', $host, $this->request['called_subdomain'])) { |
|
133 | 133 | $this->request['called_subdomain'] = array_shift($this->request['called_subdomain']); |
134 | - $this->request['subdomain'] = str_replace('.' . $domain, '', $host); |
|
134 | + $this->request['subdomain'] = str_replace('.'.$domain, '', $host); |
|
135 | 135 | return true; |
136 | 136 | } |
137 | 137 | } |
@@ -144,7 +144,7 @@ discard block |
||
144 | 144 | private function subdomainMatch() |
145 | 145 | { |
146 | 146 | if (is_array($this->request['params']) && isset($this->request['params']['subdomain'])) { |
147 | - return '(' . $this->request['params']['subdomain'] . ')'; |
|
147 | + return '('.$this->request['params']['subdomain'].')'; |
|
148 | 148 | } |
149 | 149 | return '([^/]+)'; |
150 | 150 | } |
@@ -157,7 +157,7 @@ discard block |
||
157 | 157 | { |
158 | 158 | if (is_array($this->request['params']) && isset($this->request['params']['arguments'][$match[1]])) { |
159 | 159 | $this->request['params']['arguments'][$match[1]] = str_replace('(', '(?:', $this->request['params']['arguments'][$match[1]]); |
160 | - return '(' . $this->request['params']['arguments'][$match[1]] . ')'; |
|
160 | + return '('.$this->request['params']['arguments'][$match[1]].')'; |
|
161 | 161 | } |
162 | 162 | return '([^/]+)'; |
163 | 163 | } |
@@ -211,8 +211,8 @@ discard block |
||
211 | 211 | $this->checkRequest('prefix'); |
212 | 212 | $this->router->route->setDetail($this->request); |
213 | 213 | $this->router->route->setTarget($target); |
214 | - $this->router->route->addTarget('block', $this->router->collection->getRoutes('block_' . $index)); |
|
215 | - $this->router->route->addTarget('view_dir', $this->router->collection->getRoutes('view_dir_' . $index)); |
|
214 | + $this->router->route->addTarget('block', $this->router->collection->getRoutes('block_'.$index)); |
|
215 | + $this->router->route->addTarget('view_dir', $this->router->collection->getRoutes('view_dir_'.$index)); |
|
216 | 216 | } |
217 | 217 | |
218 | 218 | /** |
@@ -221,7 +221,7 @@ discard block |
||
221 | 221 | private function checkRequest($key) |
222 | 222 | { |
223 | 223 | if (strpos($this->request[$key], ':') !== false) { |
224 | - $this->request['@' . $key] = $this->request[$key]; |
|
224 | + $this->request['@'.$key] = $this->request[$key]; |
|
225 | 225 | $this->request[$key] = $this->request['parameters'][0]; |
226 | 226 | unset($this->request['parameters'][0]); |
227 | 227 | } |
@@ -329,9 +329,9 @@ discard block |
||
329 | 329 | $index = isset($this->request['collection_index']) ? $this->request['collection_index'] : 0; |
330 | 330 | $class = (class_exists($routes[0])) |
331 | 331 | ? $routes[0] |
332 | - : $this->router->collection->getRoutes()['ctrl_namespace_' . $index] . $routes[0]; |
|
332 | + : $this->router->collection->getRoutes()['ctrl_namespace_'.$index].$routes[0]; |
|
333 | 333 | if (!class_exists($class)) |
334 | - throw new \Exception('Class "' . $class . '." is not found'); |
|
334 | + throw new \Exception('Class "'.$class.'." is not found'); |
|
335 | 335 | if (method_exists($class, $routes[1])) { |
336 | 336 | return [ |
337 | 337 | 'dispatcher' => $this->dispatcher['isController'], |
@@ -341,7 +341,7 @@ discard block |
||
341 | 341 | ]; |
342 | 342 | } |
343 | 343 | if (!strpos($callback, '{method}') !== false) |
344 | - throw new \Exception('The required method "' . $routes[1] . '" is not found in "' . $class . '"'); |
|
344 | + throw new \Exception('The required method "'.$routes[1].'" is not found in "'.$class.'"'); |
|
345 | 345 | } |
346 | 346 | return false; |
347 | 347 | } |
@@ -357,13 +357,13 @@ discard block |
||
357 | 357 | $path = trim($callback, '/'); |
358 | 358 | $extension = substr(strrchr($path, "."), 1); |
359 | 359 | $index = isset($this->request['collection_index']) ? $this->request['collection_index'] : 0; |
360 | - $viewDir = $this->router->collection->getRoutes('view_dir_' . $index); |
|
360 | + $viewDir = $this->router->collection->getRoutes('view_dir_'.$index); |
|
361 | 361 | $target = null; |
362 | - if (in_array('.' . $extension, $this->router->getConfig()['templateExtension']) && (is_file($fullPath = $viewDir . $path) || is_file($fullPath = $path))) |
|
362 | + if (in_array('.'.$extension, $this->router->getConfig()['templateExtension']) && (is_file($fullPath = $viewDir.$path) || is_file($fullPath = $path))) |
|
363 | 363 | $target = $fullPath; |
364 | 364 | else { |
365 | 365 | foreach ($this->router->getConfig()['templateExtension'] as $ext) { |
366 | - if (is_file($fullPath = $viewDir . $path . $ext) || is_file($fullPath = $path . $ext)) { |
|
366 | + if (is_file($fullPath = $viewDir.$path.$ext) || is_file($fullPath = $path.$ext)) { |
|
367 | 367 | $target = $fullPath; |
368 | 368 | $extension = substr(strrchr($ext, "."), 1); |
369 | 369 | break; |
@@ -371,7 +371,7 @@ discard block |
||
371 | 371 | } |
372 | 372 | } |
373 | 373 | if (is_null($target)) |
374 | - throw new \Exception('Template file "' . $path . '" is not found in "' . $viewDir . '"'); |
|
374 | + throw new \Exception('Template file "'.$path.'" is not found in "'.$viewDir.'"'); |
|
375 | 375 | return [ |
376 | 376 | 'dispatcher' => $this->dispatcher['isTemplate'], |
377 | 377 | 'template' => $target, |
@@ -105,7 +105,9 @@ discard block |
||
105 | 105 | $this->request['route'] = preg_replace_callback('#:([\w]+)#', [$this, 'paramMatch'], '/' . trim(trim($this->request['prefix'], '/') . '/' . trim($route, '/'), '/')); |
106 | 106 | if ($this->routeMatch('#^' . $this->request['route'] . '$#')) { |
107 | 107 | $this->setCallback(); |
108 | - if (!is_null($response = $this->generateTarget())) return $response; |
|
108 | + if (!is_null($response = $this->generateTarget())) { |
|
109 | + return $response; |
|
110 | + } |
|
109 | 111 | } |
110 | 112 | } |
111 | 113 | } |
@@ -122,7 +124,9 @@ discard block |
||
122 | 124 | $url = (isset($_SERVER['REQUEST_SCHEME']) ? $_SERVER['REQUEST_SCHEME'] : 'http') . '://' . ($host = (isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : $_SERVER['HTTP_HOST'])); |
123 | 125 | $host = explode(':', $host)[0]; |
124 | 126 | $domain = $this->router->collection->getDomain($url); |
125 | - if (!empty($this->request['subdomain']) && $route[0] == '/') $route = trim($this->request['subdomain'], '.') . '.' . $domain . $route; |
|
127 | + if (!empty($this->request['subdomain']) && $route[0] == '/') { |
|
128 | + $route = trim($this->request['subdomain'], '.') . '.' . $domain . $route; |
|
129 | + } |
|
126 | 130 | if ($route[0] == '/') { |
127 | 131 | return ($host != $domain) ? false : true; |
128 | 132 | } elseif ($route[0] != '/' && $host != $domain) { |
@@ -170,8 +174,9 @@ discard block |
||
170 | 174 | { |
171 | 175 | if (substr($this->request['route'], -1) == '*') { |
172 | 176 | $pos = strpos($this->request['route'], '*'); |
173 | - if (substr($this->router->route->getUrl(), 0, $pos) == substr($this->request['route'], 0, $pos) && isset($this->request['params'])) |
|
174 | - return true; |
|
177 | + if (substr($this->router->route->getUrl(), 0, $pos) == substr($this->request['route'], 0, $pos) && isset($this->request['params'])) { |
|
178 | + return true; |
|
179 | + } |
|
175 | 180 | } |
176 | 181 | if (preg_match($regex, $this->router->route->getUrl(), $this->request['parameters'])) { |
177 | 182 | array_shift($this->request['parameters']); |
@@ -189,8 +194,9 @@ discard block |
||
189 | 194 | foreach ($this->resolver as $resolver) { |
190 | 195 | if (is_array($target = call_user_func_array([$this, $resolver], [$this->router->route->getCallback()]))) { |
191 | 196 | $this->setTarget($target); |
192 | - if ($this->router->middleware->globalMiddleware() === false || $this->router->middleware->blockMiddleware() === false || $this->router->middleware->classMiddleware() === false || $this->router->middleware->routeMiddleware() === false) |
|
193 | - return null; |
|
197 | + if ($this->router->middleware->globalMiddleware() === false || $this->router->middleware->blockMiddleware() === false || $this->router->middleware->classMiddleware() === false || $this->router->middleware->routeMiddleware() === false) { |
|
198 | + return null; |
|
199 | + } |
|
194 | 200 | $this->router->response->setStatusCode(202); |
195 | 201 | return true; |
196 | 202 | } |
@@ -233,14 +239,18 @@ discard block |
||
233 | 239 | private function setCallback() |
234 | 240 | { |
235 | 241 | if (isset($this->request['params'])) { |
236 | - if (is_callable($this->request['params'])) |
|
237 | - $this->router->route->setCallback($this->request['params']); |
|
238 | - else { |
|
242 | + if (is_callable($this->request['params'])) { |
|
243 | + $this->router->route->setCallback($this->request['params']); |
|
244 | + } else { |
|
239 | 245 | (is_array($this->request['params']) && isset($this->request['params']['use'])) |
240 | 246 | ? $this->router->route->setCallback($this->request['params']['use']) |
241 | 247 | : $this->router->route->setCallback($this->request['params']); |
242 | - if (isset($this->request['params']['name'])) $this->router->route->setName($this->request['params']['name']); |
|
243 | - if (isset($this->request['params']['method'])) $this->request['params']['method'] = is_array($this->request['params']['method']) ? $this->request['params']['method'] : [$this->request['params']['method']]; |
|
248 | + if (isset($this->request['params']['name'])) { |
|
249 | + $this->router->route->setName($this->request['params']['name']); |
|
250 | + } |
|
251 | + if (isset($this->request['params']['method'])) { |
|
252 | + $this->request['params']['method'] = is_array($this->request['params']['method']) ? $this->request['params']['method'] : [$this->request['params']['method']]; |
|
253 | + } |
|
244 | 254 | } |
245 | 255 | } |
246 | 256 | } |
@@ -250,9 +260,12 @@ discard block |
||
250 | 260 | */ |
251 | 261 | public function validMethod() |
252 | 262 | { |
253 | - if (is_callable($this->request['params'])) return true; |
|
254 | - if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') |
|
255 | - return (isset($this->request['params']['ajax']) && $this->request['params']['ajax'] === true) ? true : false; |
|
263 | + if (is_callable($this->request['params'])) { |
|
264 | + return true; |
|
265 | + } |
|
266 | + if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') { |
|
267 | + return (isset($this->request['params']['ajax']) && $this->request['params']['ajax'] === true) ? true : false; |
|
268 | + } |
|
256 | 269 | $method = (isset($this->request['params']['method'])) ? $this->request['params']['method'] : ['GET']; |
257 | 270 | return (in_array($this->router->route->getMethod(), $method)) ? true : false; |
258 | 271 | } |
@@ -318,20 +331,25 @@ discard block |
||
318 | 331 | { |
319 | 332 | if (is_string($callback) && strpos($callback, '@') !== false) { |
320 | 333 | $routes = explode('@', $callback); |
321 | - if (!isset($routes[1])) $routes[1] = 'index'; |
|
334 | + if (!isset($routes[1])) { |
|
335 | + $routes[1] = 'index'; |
|
336 | + } |
|
322 | 337 | if ($routes[1] == '{method}') { |
323 | 338 | $this->request['parameters'] = explode('/', str_replace(str_replace('*', '', $this->request['route']), '', $this->router->route->getUrl())); |
324 | 339 | $routes[1] = $this->request['parameters'][0]; |
325 | 340 | array_shift($this->request['parameters']); |
326 | - if (preg_match('/[A-Z]/', $routes[1])) return false; |
|
341 | + if (preg_match('/[A-Z]/', $routes[1])) { |
|
342 | + return false; |
|
343 | + } |
|
327 | 344 | $routes[1] = lcfirst(str_replace(' ', '', ucwords(str_replace('-', ' ', $routes[1])))); |
328 | 345 | } |
329 | 346 | $index = isset($this->request['collection_index']) ? $this->request['collection_index'] : 0; |
330 | 347 | $class = (class_exists($routes[0])) |
331 | 348 | ? $routes[0] |
332 | 349 | : $this->router->collection->getRoutes()['ctrl_namespace_' . $index] . $routes[0]; |
333 | - if (!class_exists($class)) |
|
334 | - throw new \Exception('Class "' . $class . '." is not found'); |
|
350 | + if (!class_exists($class)) { |
|
351 | + throw new \Exception('Class "' . $class . '." is not found'); |
|
352 | + } |
|
335 | 353 | if (method_exists($class, $routes[1])) { |
336 | 354 | return [ |
337 | 355 | 'dispatcher' => $this->dispatcher['isController'], |
@@ -340,8 +358,9 @@ discard block |
||
340 | 358 | 'action' => $routes[1] |
341 | 359 | ]; |
342 | 360 | } |
343 | - if (!strpos($callback, '{method}') !== false) |
|
344 | - throw new \Exception('The required method "' . $routes[1] . '" is not found in "' . $class . '"'); |
|
361 | + if (!strpos($callback, '{method}') !== false) { |
|
362 | + throw new \Exception('The required method "' . $routes[1] . '" is not found in "' . $class . '"'); |
|
363 | + } |
|
345 | 364 | } |
346 | 365 | return false; |
347 | 366 | } |
@@ -359,9 +378,9 @@ discard block |
||
359 | 378 | $index = isset($this->request['collection_index']) ? $this->request['collection_index'] : 0; |
360 | 379 | $viewDir = $this->router->collection->getRoutes('view_dir_' . $index); |
361 | 380 | $target = null; |
362 | - if (in_array('.' . $extension, $this->router->getConfig()['templateExtension']) && (is_file($fullPath = $viewDir . $path) || is_file($fullPath = $path))) |
|
363 | - $target = $fullPath; |
|
364 | - else { |
|
381 | + if (in_array('.' . $extension, $this->router->getConfig()['templateExtension']) && (is_file($fullPath = $viewDir . $path) || is_file($fullPath = $path))) { |
|
382 | + $target = $fullPath; |
|
383 | + } else { |
|
365 | 384 | foreach ($this->router->getConfig()['templateExtension'] as $ext) { |
366 | 385 | if (is_file($fullPath = $viewDir . $path . $ext) || is_file($fullPath = $path . $ext)) { |
367 | 386 | $target = $fullPath; |
@@ -370,8 +389,9 @@ discard block |
||
370 | 389 | } |
371 | 390 | } |
372 | 391 | } |
373 | - if (is_null($target)) |
|
374 | - throw new \Exception('Template file "' . $path . '" is not found in "' . $viewDir . '"'); |
|
392 | + if (is_null($target)) { |
|
393 | + throw new \Exception('Template file "' . $path . '" is not found in "' . $viewDir . '"'); |
|
394 | + } |
|
375 | 395 | return [ |
376 | 396 | 'dispatcher' => $this->dispatcher['isTemplate'], |
377 | 397 | 'template' => $target, |
@@ -59,7 +59,7 @@ discard block |
||
59 | 59 | $this->response->setStatusCode(404); |
60 | 60 | $this->route = is_null($route) ? new Route() : $route; |
61 | 61 | $this->middleware = new Middleware($this); |
62 | - $this->config['di'] = function ($class) { |
|
62 | + $this->config['di'] = function($class) { |
|
63 | 63 | return new $class; |
64 | 64 | }; |
65 | 65 | } |
@@ -116,7 +116,7 @@ discard block |
||
116 | 116 | { |
117 | 117 | if (is_null($url)) |
118 | 118 | $url = (isset($_GET['url'])) ? $_GET['url'] : substr(str_replace(str_replace('/index.php', '', $_SERVER['SCRIPT_NAME']), '', $_SERVER['REQUEST_URI']), 1); |
119 | - $this->route->setUrl('/' . trim(explode('?', $url)[0], '/')); |
|
119 | + $this->route->setUrl('/'.trim(explode('?', $url)[0], '/')); |
|
120 | 120 | } |
121 | 121 | |
122 | 122 | /** |
@@ -170,10 +170,10 @@ discard block |
||
170 | 170 | } |
171 | 171 | $this->response->setStatusCode($code); |
172 | 172 | } |
173 | - if (isset($this->collection->middleware['before_render'])){ |
|
174 | - foreach ($this->collection->middleware['before_render'] as $callback){ |
|
173 | + if (isset($this->collection->middleware['before_render'])) { |
|
174 | + foreach ($this->collection->middleware['before_render'] as $callback) { |
|
175 | 175 | $call = explode('@', $callback); |
176 | - if(isset($call[1])){ |
|
176 | + if (isset($call[1])) { |
|
177 | 177 | $classes = ['JetFire\Routing\Router' => $this]; |
178 | 178 | $args = ['router' => $this, 'route' => $this->route, 'response' => $this->response]; |
179 | 179 | $this->callMethod($call[0], $call[1], $args, $args, $classes); |
@@ -221,7 +221,7 @@ discard block |
||
221 | 221 | { |
222 | 222 | $reflector = new ReflectionClass($controller); |
223 | 223 | if (!$reflector->isInstantiable()) |
224 | - throw new \Exception('Controller [' . $controller . '] is not instantiable.'); |
|
224 | + throw new \Exception('Controller ['.$controller.'] is not instantiable.'); |
|
225 | 225 | $constructor = $reflector->getConstructor(); |
226 | 226 | if (is_null($constructor)) |
227 | 227 | return call_user_func_array($this->getConfig()['di'], [$controller]); |
@@ -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->getConfig()['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->getConfig()['di'], [$controller]); |
|
240 | + if (is_null($constructor)) { |
|
241 | + return call_user_func_array($this->getConfig()['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->getConfig()['di'], [$arg->getClass()->name])); |
236 | 251 | } |
237 | 252 | } |
@@ -51,17 +51,21 @@ discard block |
||
51 | 51 | |
52 | 52 | if ($count == count($this->route->getParameters()) || (empty($this->route->getParameters()) && $count == 0)) { |
53 | 53 | $dependencies = array_merge($dependencies, ($this->route->getParameters() == '') ? [] : $this->route->getParameters()); |
54 | - if ($this->response->getStatusCode() == 202) |
|
55 | - $this->response->setStatusCode(200); |
|
54 | + if ($this->response->getStatusCode() == 202) { |
|
55 | + $this->response->setStatusCode(200); |
|
56 | + } |
|
56 | 57 | if (is_array($content = $reflectionMethod->invokeArgs($this->getController(), $dependencies))) { |
57 | 58 | $this->route->addTarget('data', $content); |
58 | 59 | if (isset($this->route->getParams()['ajax']) && $this->route->getParams()['ajax'] === true) { |
59 | 60 | $this->response->setContent(json_encode($content)); |
60 | 61 | $this->response->setHeaders(['Content-Type' => 'application/json']); |
61 | 62 | } |
62 | - } elseif (!is_null($content)) $this->response->setContent($content); |
|
63 | - } else |
|
64 | - $this->response->setStatusCode(404); |
|
63 | + } elseif (!is_null($content)) { |
|
64 | + $this->response->setContent($content); |
|
65 | + } |
|
66 | + } else { |
|
67 | + $this->response->setStatusCode(404); |
|
68 | + } |
|
65 | 69 | } |
66 | 70 | |
67 | 71 | |
@@ -72,8 +76,9 @@ discard block |
||
72 | 76 | private function getController() |
73 | 77 | { |
74 | 78 | $reflector = new ReflectionClass($this->route->getTarget('controller')); |
75 | - if (!$reflector->isInstantiable()) |
|
76 | - throw new \Exception('Target [' . $this->route->getTarget('controller') . '] is not instantiable.'); |
|
79 | + if (!$reflector->isInstantiable()) { |
|
80 | + throw new \Exception('Target [' . $this->route->getTarget('controller') . '] is not instantiable.'); |
|
81 | + } |
|
77 | 82 | $constructor = $reflector->getConstructor(); |
78 | 83 | if (is_null($constructor)) { |
79 | 84 | $class = $this->route->getTarget('controller'); |