@@ -83,7 +83,7 @@ |
||
| 83 | 83 | } |
| 84 | 84 | |
| 85 | 85 | /** |
| 86 | - * @return bool |
|
| 86 | + * @return null|boolean |
|
| 87 | 87 | */ |
| 88 | 88 | public function match() |
| 89 | 89 | { |
@@ -32,7 +32,7 @@ discard block |
||
| 32 | 32 | private $dispatcher = [ |
| 33 | 33 | 'isTemplate' => 'JetFire\Routing\Dispatcher\TemplateDispatcher', |
| 34 | 34 | 'isController' => 'JetFire\Routing\Dispatcher\ControllerDispatcher', |
| 35 | - 'isControllerAndTemplate' => ['JetFire\Routing\Dispatcher\ControllerDispatcher','JetFire\Routing\Dispatcher\TemplateDispatcher'], |
|
| 35 | + 'isControllerAndTemplate' => ['JetFire\Routing\Dispatcher\ControllerDispatcher', 'JetFire\Routing\Dispatcher\TemplateDispatcher'], |
|
| 36 | 36 | ]; |
| 37 | 37 | |
| 38 | 38 | /** |
@@ -46,14 +46,14 @@ discard block |
||
| 46 | 46 | /** |
| 47 | 47 | * @param array $resolver |
| 48 | 48 | */ |
| 49 | - public function setResolver($resolver = []){ |
|
| 49 | + public function setResolver($resolver = []) { |
|
| 50 | 50 | $this->resolver = $resolver; |
| 51 | 51 | } |
| 52 | 52 | |
| 53 | 53 | /** |
| 54 | 54 | * @param string $resolver |
| 55 | 55 | */ |
| 56 | - public function addResolver($resolver){ |
|
| 56 | + public function addResolver($resolver) { |
|
| 57 | 57 | $this->resolver[] = $resolver; |
| 58 | 58 | } |
| 59 | 59 | |
@@ -78,7 +78,7 @@ discard block |
||
| 78 | 78 | * @param $class |
| 79 | 79 | * @return mixed|void |
| 80 | 80 | */ |
| 81 | - public function addDispatcher($method,$class){ |
|
| 81 | + public function addDispatcher($method, $class) { |
|
| 82 | 82 | $this->dispatcher[$method] = $class; |
| 83 | 83 | } |
| 84 | 84 | |
@@ -87,10 +87,10 @@ discard block |
||
| 87 | 87 | */ |
| 88 | 88 | public function match() |
| 89 | 89 | { |
| 90 | - foreach($this->resolver as $resolver){ |
|
| 91 | - if(is_array($target = call_user_func([$this,$resolver]))) { |
|
| 90 | + foreach ($this->resolver as $resolver) { |
|
| 91 | + if (is_array($target = call_user_func([$this, $resolver]))) { |
|
| 92 | 92 | $this->setTarget($target); |
| 93 | - if($this->router->middleware->globalMiddleware() === false || $this->router->middleware->blockMiddleware() === false || $this->router->middleware->classMiddleware() === false || $this->router->middleware->routeMiddleware() === false) |
|
| 93 | + if ($this->router->middleware->globalMiddleware() === false || $this->router->middleware->blockMiddleware() === false || $this->router->middleware->classMiddleware() === false || $this->router->middleware->routeMiddleware() === false) |
|
| 94 | 94 | return null; |
| 95 | 95 | $this->router->response->setStatusCode(202); |
| 96 | 96 | return true; |
@@ -102,7 +102,7 @@ discard block |
||
| 102 | 102 | /** |
| 103 | 103 | * @param array $target |
| 104 | 104 | */ |
| 105 | - public function setTarget($target = []){ |
|
| 105 | + public function setTarget($target = []) { |
|
| 106 | 106 | $index = isset($this->request['collection_index']) ? $this->request['collection_index'] : 0; |
| 107 | 107 | $this->router->route->setDetail($this->request); |
| 108 | 108 | $this->router->route->setTarget($target); |
@@ -113,10 +113,10 @@ discard block |
||
| 113 | 113 | /** |
| 114 | 114 | * @return array|bool |
| 115 | 115 | */ |
| 116 | - public function isControllerAndTemplate(){ |
|
| 117 | - if(is_array($ctrl = $this->isController())) { |
|
| 116 | + public function isControllerAndTemplate() { |
|
| 117 | + if (is_array($ctrl = $this->isController())) { |
|
| 118 | 118 | if (is_array($tpl = $this->isTemplate())) { |
| 119 | - return array_merge(array_merge($ctrl, $tpl),[ |
|
| 119 | + return array_merge(array_merge($ctrl, $tpl), [ |
|
| 120 | 120 | 'dispatcher' => $this->dispatcher['isControllerAndTemplate'] |
| 121 | 121 | ]); |
| 122 | 122 | } |
@@ -132,10 +132,10 @@ discard block |
||
| 132 | 132 | { |
| 133 | 133 | foreach ($this->router->getConfig()['templateExtension'] as $extension) { |
| 134 | 134 | for ($i = 0; $i < $this->router->collection->countRoutes; ++$i) { |
| 135 | - $url = explode('/', str_replace($this->router->collection->getRoutes('prefix_' . $i), '',$this->router->route->getUrl())); |
|
| 135 | + $url = explode('/', str_replace($this->router->collection->getRoutes('prefix_'.$i), '', $this->router->route->getUrl())); |
|
| 136 | 136 | $end = array_pop($url); |
| 137 | 137 | $url = implode('/', array_map('ucwords', $url)).'/'.$end; |
| 138 | - if (is_file(($template = rtrim($this->router->collection->getRoutes('view_dir_' . $i), '/') . $url . $extension))) { |
|
| 138 | + if (is_file(($template = rtrim($this->router->collection->getRoutes('view_dir_'.$i), '/').$url.$extension))) { |
|
| 139 | 139 | $this->request['collection_index'] = $i; |
| 140 | 140 | return [ |
| 141 | 141 | 'dispatcher' => $this->dispatcher['isTemplate'], |
@@ -156,13 +156,13 @@ discard block |
||
| 156 | 156 | { |
| 157 | 157 | $routes = array_slice(explode('/', $this->router->route->getUrl()), 1); |
| 158 | 158 | $i = 0; |
| 159 | - do{ |
|
| 160 | - $route = ('/' . $routes[0] == $this->router->collection->getRoutes('prefix_' . $i)) ? array_slice($routes, 1) : $routes; |
|
| 159 | + do { |
|
| 160 | + $route = ('/'.$routes[0] == $this->router->collection->getRoutes('prefix_'.$i)) ? array_slice($routes, 1) : $routes; |
|
| 161 | 161 | if (isset($route[0])) { |
| 162 | - $class = (class_exists($this->router->collection->getRoutes('ctrl_namespace_' . $i). ucfirst($route[0]) . 'Controller')) |
|
| 163 | - ? $this->router->collection->getRoutes('ctrl_namespace_' . $i). ucfirst($route[0]) . 'Controller' |
|
| 164 | - : ucfirst($route[0]) . 'Controller'; |
|
| 165 | - $route[1] = isset($route[1])?$route[1]:'index'; |
|
| 162 | + $class = (class_exists($this->router->collection->getRoutes('ctrl_namespace_'.$i).ucfirst($route[0]).'Controller')) |
|
| 163 | + ? $this->router->collection->getRoutes('ctrl_namespace_'.$i).ucfirst($route[0]).'Controller' |
|
| 164 | + : ucfirst($route[0]).'Controller'; |
|
| 165 | + $route[1] = isset($route[1]) ? $route[1] : 'index'; |
|
| 166 | 166 | if (method_exists($class, $route[1])) { |
| 167 | 167 | $this->request['parameters'] = array_slice($route, 2); |
| 168 | 168 | $this->request['collection_index'] = $i; |
@@ -175,7 +175,7 @@ discard block |
||
| 175 | 175 | } |
| 176 | 176 | } |
| 177 | 177 | ++$i; |
| 178 | - }while($i < $this->router->collection->countRoutes); |
|
| 178 | + }while ($i < $this->router->collection->countRoutes); |
|
| 179 | 179 | return false; |
| 180 | 180 | } |
| 181 | 181 | |
@@ -90,8 +90,9 @@ |
||
| 90 | 90 | foreach($this->resolver as $resolver){ |
| 91 | 91 | if(is_array($target = call_user_func([$this,$resolver]))) { |
| 92 | 92 | $this->setTarget($target); |
| 93 | - if($this->router->middleware->globalMiddleware() === false || $this->router->middleware->blockMiddleware() === false || $this->router->middleware->classMiddleware() === false || $this->router->middleware->routeMiddleware() === false) |
|
| 94 | - return null; |
|
| 93 | + if($this->router->middleware->globalMiddleware() === false || $this->router->middleware->blockMiddleware() === false || $this->router->middleware->classMiddleware() === false || $this->router->middleware->routeMiddleware() === false) {
|
|
| 94 | + return null; |
|
| 95 | + } |
|
| 95 | 96 | $this->router->response->setStatusCode(202); |
| 96 | 97 | return true; |
| 97 | 98 | } |
@@ -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; |
@@ -48,13 +48,13 @@ discard block |
||
| 48 | 48 | ? $count++ |
| 49 | 49 | : $dependencies[] = call_user_func_array($this->route->getTarget('di'), [$arg->getClass()->name]); |
| 50 | 50 | } |
| 51 | - if($count == count($this->route->getParameters())) { |
|
| 51 | + if ($count == count($this->route->getParameters())) { |
|
| 52 | 52 | $dependencies = array_merge($dependencies, ($this->route->getParameters() == '') ? [] : $this->route->getParameters()); |
| 53 | 53 | if ($this->response->getStatusCode() == 202) |
| 54 | 54 | $this->response->setStatusCode(200); |
| 55 | 55 | if (is_array($content = $reflectionMethod->invokeArgs($this->getController(), $dependencies))) $this->route->addTarget('data', $content); |
| 56 | 56 | elseif (!is_null($content)) $this->response->setContent($content); |
| 57 | - }else |
|
| 57 | + } else |
|
| 58 | 58 | $this->response->setStatusCode(404); |
| 59 | 59 | } |
| 60 | 60 | |
@@ -67,17 +67,17 @@ discard block |
||
| 67 | 67 | { |
| 68 | 68 | $reflector = new ReflectionClass($this->route->getTarget('controller')); |
| 69 | 69 | if (!$reflector->isInstantiable()) |
| 70 | - throw new \Exception('Target [' . $this->route->getTarget('controller') . '] is not instantiable.'); |
|
| 70 | + throw new \Exception('Target ['.$this->route->getTarget('controller').'] is not instantiable.'); |
|
| 71 | 71 | $constructor = $reflector->getConstructor(); |
| 72 | 72 | if (is_null($constructor)) { |
| 73 | 73 | $class = $this->route->getTarget('controller'); |
| 74 | - return call_user_func_array($this->route->getTarget('di'),[$class]); |
|
| 74 | + return call_user_func_array($this->route->getTarget('di'), [$class]); |
|
| 75 | 75 | } |
| 76 | 76 | $dependencies = $constructor->getParameters(); |
| 77 | 77 | $arguments = []; |
| 78 | 78 | foreach ($dependencies as $dep) { |
| 79 | 79 | $class = $dep->getClass()->name; |
| 80 | - array_push($arguments, call_user_func_array($this->route->getTarget('di'),[$class])); |
|
| 80 | + array_push($arguments, call_user_func_array($this->route->getTarget('di'), [$class])); |
|
| 81 | 81 | } |
| 82 | 82 | return $reflector->newInstanceArgs($arguments); |
| 83 | 83 | } |
@@ -50,12 +50,17 @@ discard block |
||
| 50 | 50 | } |
| 51 | 51 | if($count == count($this->route->getParameters())) { |
| 52 | 52 | $dependencies = array_merge($dependencies, ($this->route->getParameters() == '') ? [] : $this->route->getParameters()); |
| 53 | - if ($this->response->getStatusCode() == 202) |
|
| 54 | - $this->response->setStatusCode(200); |
|
| 55 | - if (is_array($content = $reflectionMethod->invokeArgs($this->getController(), $dependencies))) $this->route->addTarget('data', $content); |
|
| 56 | - elseif (!is_null($content)) $this->response->setContent($content); |
|
| 57 | - }else |
|
| 58 | - $this->response->setStatusCode(404); |
|
| 53 | + if ($this->response->getStatusCode() == 202) {
|
|
| 54 | + $this->response->setStatusCode(200); |
|
| 55 | + } |
|
| 56 | + if (is_array($content = $reflectionMethod->invokeArgs($this->getController(), $dependencies))) {
|
|
| 57 | + $this->route->addTarget('data', $content);
|
|
| 58 | + } elseif (!is_null($content)) {
|
|
| 59 | + $this->response->setContent($content); |
|
| 60 | + } |
|
| 61 | + } else {
|
|
| 62 | + $this->response->setStatusCode(404); |
|
| 63 | + } |
|
| 59 | 64 | } |
| 60 | 65 | |
| 61 | 66 | |
@@ -66,8 +71,9 @@ discard block |
||
| 66 | 71 | private function getController() |
| 67 | 72 | { |
| 68 | 73 | $reflector = new ReflectionClass($this->route->getTarget('controller')); |
| 69 | - if (!$reflector->isInstantiable()) |
|
| 70 | - throw new \Exception('Target [' . $this->route->getTarget('controller') . '] is not instantiable.'); |
|
| 74 | + if (!$reflector->isInstantiable()) {
|
|
| 75 | + throw new \Exception('Target [' . $this->route->getTarget('controller') . '] is not instantiable.');
|
|
| 76 | + } |
|
| 71 | 77 | $constructor = $reflector->getConstructor(); |
| 72 | 78 | if (is_null($constructor)) { |
| 73 | 79 | $class = $this->route->getTarget('controller'); |