@@ -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->route->getTarget('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->route->getTarget('di'), [$controller]); |
|
| 240 | + if (is_null($constructor)) {
|
|
| 241 | + return call_user_func_array($this->route->getTarget('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->route->getTarget('di'), [$arg->getClass()->name])); |
| 236 | 251 | } |
| 237 | 252 | } |