@@ -57,7 +57,7 @@ discard block |
||
| 57 | 57 | $this->response->setStatusCode(404); |
| 58 | 58 | $this->route = is_null($route) ? new Route() : $route; |
| 59 | 59 | $this->middleware = new Middleware($this); |
| 60 | - $this->config['di'] = function ($class) { |
|
| 60 | + $this->config['di'] = function($class) { |
|
| 61 | 61 | return new $class; |
| 62 | 62 | }; |
| 63 | 63 | } |
@@ -114,7 +114,7 @@ discard block |
||
| 114 | 114 | { |
| 115 | 115 | if (is_null($url)) |
| 116 | 116 | $url = (isset($_GET['url'])) ? $_GET['url'] : substr(str_replace(str_replace('/index.php', '', $_SERVER['SCRIPT_NAME']), '', $_SERVER['REQUEST_URI']), 1); |
| 117 | - $this->route->setUrl('/' . trim(explode('?', $url)[0], '/')); |
|
| 117 | + $this->route->setUrl('/'.trim(explode('?', $url)[0], '/')); |
|
| 118 | 118 | } |
| 119 | 119 | |
| 120 | 120 | /** |
@@ -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 | } |
@@ -27,7 +27,7 @@ discard block |
||
| 27 | 27 | /** |
| 28 | 28 | * @param Route $route |
| 29 | 29 | */ |
| 30 | - public function __construct(Route $route,ResponseInterface $response) |
|
| 30 | + public function __construct(Route $route, ResponseInterface $response) |
|
| 31 | 31 | { |
| 32 | 32 | $this->route = $route; |
| 33 | 33 | $this->response = $response; |
@@ -47,18 +47,18 @@ discard block |
||
| 47 | 47 | ? $count++ |
| 48 | 48 | : $dependencies[] = call_user_func_array($this->route->getTarget('di'), [$arg->getClass()->name]); |
| 49 | 49 | } |
| 50 | - if($count == count($this->route->getParameters()) || ($this->route->getParameters() == '' && $count == 0)) { |
|
| 50 | + if ($count == count($this->route->getParameters()) || ($this->route->getParameters() == '' && $count == 0)) { |
|
| 51 | 51 | $dependencies = array_merge($dependencies, ($this->route->getParameters() == '') ? [] : $this->route->getParameters()); |
| 52 | 52 | if ($this->response->getStatusCode() == 202) |
| 53 | 53 | $this->response->setStatusCode(200); |
| 54 | - if (is_array($content = $reflectionMethod->invokeArgs($this->getController(), $dependencies))){ |
|
| 54 | + if (is_array($content = $reflectionMethod->invokeArgs($this->getController(), $dependencies))) { |
|
| 55 | 55 | $this->route->addTarget('data', $content); |
| 56 | - if(isset($this->route->getParams()['ajax']) && $this->route->getParams()['ajax'] == true){ |
|
| 56 | + if (isset($this->route->getParams()['ajax']) && $this->route->getParams()['ajax'] == true) { |
|
| 57 | 57 | $this->response->setContent(json_encode($content)); |
| 58 | 58 | $this->response->setHeaders(['Content-Type' => 'application/json']); |
| 59 | 59 | } |
| 60 | 60 | } elseif (!is_null($content)) $this->response->setContent($content); |
| 61 | - }else |
|
| 61 | + } else |
|
| 62 | 62 | $this->response->setStatusCode(404); |
| 63 | 63 | } |
| 64 | 64 | |
@@ -71,17 +71,17 @@ discard block |
||
| 71 | 71 | { |
| 72 | 72 | $reflector = new ReflectionClass($this->route->getTarget('controller')); |
| 73 | 73 | if (!$reflector->isInstantiable()) |
| 74 | - throw new \Exception('Target [' . $this->route->getTarget('controller') . '] is not instantiable.'); |
|
| 74 | + throw new \Exception('Target ['.$this->route->getTarget('controller').'] is not instantiable.'); |
|
| 75 | 75 | $constructor = $reflector->getConstructor(); |
| 76 | 76 | if (is_null($constructor)) { |
| 77 | 77 | $class = $this->route->getTarget('controller'); |
| 78 | - return call_user_func_array($this->route->getTarget('di'),[$class]); |
|
| 78 | + return call_user_func_array($this->route->getTarget('di'), [$class]); |
|
| 79 | 79 | } |
| 80 | 80 | $dependencies = $constructor->getParameters(); |
| 81 | 81 | $arguments = []; |
| 82 | 82 | foreach ($dependencies as $dep) { |
| 83 | 83 | $class = $dep->getClass()->name; |
| 84 | - array_push($arguments, call_user_func_array($this->route->getTarget('di'),[$class])); |
|
| 84 | + array_push($arguments, call_user_func_array($this->route->getTarget('di'), [$class])); |
|
| 85 | 85 | } |
| 86 | 86 | return $reflector->newInstanceArgs($arguments); |
| 87 | 87 | } |
@@ -49,17 +49,21 @@ discard block |
||
| 49 | 49 | } |
| 50 | 50 | if($count == count($this->route->getParameters()) || ($this->route->getParameters() == '' && $count == 0)) { |
| 51 | 51 | $dependencies = array_merge($dependencies, ($this->route->getParameters() == '') ? [] : $this->route->getParameters()); |
| 52 | - if ($this->response->getStatusCode() == 202) |
|
| 53 | - $this->response->setStatusCode(200); |
|
| 52 | + if ($this->response->getStatusCode() == 202) {
|
|
| 53 | + $this->response->setStatusCode(200); |
|
| 54 | + } |
|
| 54 | 55 | if (is_array($content = $reflectionMethod->invokeArgs($this->getController(), $dependencies))){ |
| 55 | 56 | $this->route->addTarget('data', $content); |
| 56 | 57 | if(isset($this->route->getParams()['ajax']) && $this->route->getParams()['ajax'] == true){ |
| 57 | 58 | $this->response->setContent(json_encode($content)); |
| 58 | 59 | $this->response->setHeaders(['Content-Type' => 'application/json']); |
| 59 | 60 | } |
| 60 | - } elseif (!is_null($content)) $this->response->setContent($content); |
|
| 61 | - }else |
|
| 62 | - $this->response->setStatusCode(404); |
|
| 61 | + } elseif (!is_null($content)) {
|
|
| 62 | + $this->response->setContent($content); |
|
| 63 | + } |
|
| 64 | + } else {
|
|
| 65 | + $this->response->setStatusCode(404); |
|
| 66 | + } |
|
| 63 | 67 | } |
| 64 | 68 | |
| 65 | 69 | |
@@ -70,8 +74,9 @@ discard block |
||
| 70 | 74 | private function getController() |
| 71 | 75 | { |
| 72 | 76 | $reflector = new ReflectionClass($this->route->getTarget('controller')); |
| 73 | - if (!$reflector->isInstantiable()) |
|
| 74 | - throw new \Exception('Target [' . $this->route->getTarget('controller') . '] is not instantiable.'); |
|
| 77 | + if (!$reflector->isInstantiable()) {
|
|
| 78 | + throw new \Exception('Target [' . $this->route->getTarget('controller') . '] is not instantiable.');
|
|
| 79 | + } |
|
| 75 | 80 | $constructor = $reflector->getConstructor(); |
| 76 | 81 | if (is_null($constructor)) { |
| 77 | 82 | $class = $this->route->getTarget('controller'); |
@@ -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 | } |