@@ -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'); |
@@ -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()) || ($this->route->getParameters() == '' && $count == 0)) { |
|
51 | + if ($count == count($this->route->getParameters()) || ($this->route->getParameters() == '' && $count == 0)) { |
|
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 | } |
@@ -46,7 +46,7 @@ discard block |
||
46 | 46 | if (is_array($blocks)) { |
47 | 47 | foreach ($blocks as $block) |
48 | 48 | if (class_exists($block)) |
49 | - if($this->callHandler($block) === false) return false; |
|
49 | + if ($this->callHandler($block) === false) return false; |
|
50 | 50 | } |
51 | 51 | elseif (is_string($blocks) && class_exists($blocks)) |
52 | 52 | return $this->callHandler($blocks); |
@@ -63,10 +63,10 @@ discard block |
||
63 | 63 | $ctrl = str_replace('\\', '/', $this->router->route->getTarget('controller')); |
64 | 64 | if (isset($this->router->collection->middleware['class_middleware'][$ctrl]) && class_exists($this->router->route->getTarget('controller'))) { |
65 | 65 | $classes = $this->router->collection->middleware['class_middleware'][$ctrl]; |
66 | - if(is_array($classes)){ |
|
66 | + if (is_array($classes)) { |
|
67 | 67 | foreach ($classes as $class) |
68 | - if($this->callHandler($class) === false)return false; |
|
69 | - }elseif(is_string($classes)) |
|
68 | + if ($this->callHandler($class) === false)return false; |
|
69 | + }elseif (is_string($classes)) |
|
70 | 70 | return $this->callHandler($classes); |
71 | 71 | } |
72 | 72 | } |
@@ -81,10 +81,10 @@ discard block |
||
81 | 81 | if (isset($this->router->collection->middleware['route_middleware'])) |
82 | 82 | if (isset($this->router->route->getPath()['middleware']) && class_exists($this->router->collection->middleware['route_middleware'][$this->router->route->getPath()['middleware']])) { |
83 | 83 | $classes = $this->router->collection->middleware['route_middleware'][$this->router->route->getPath()['middleware']]; |
84 | - if(is_array($classes)){ |
|
84 | + if (is_array($classes)) { |
|
85 | 85 | foreach ($classes as $class) |
86 | - if($this->callHandler($class) === false)return false; |
|
87 | - }elseif(is_string($classes)) |
|
86 | + if ($this->callHandler($class) === false)return false; |
|
87 | + }elseif (is_string($classes)) |
|
88 | 88 | return $this->callHandler($classes); |
89 | 89 | } |
90 | 90 | return null; |
@@ -94,8 +94,8 @@ discard block |
||
94 | 94 | * @param $class |
95 | 95 | * @return mixed |
96 | 96 | */ |
97 | - private function callHandler($class){ |
|
98 | - $instance = call_user_func($this->router->getConfig()['di'],$class); |
|
97 | + private function callHandler($class) { |
|
98 | + $instance = call_user_func($this->router->getConfig()['di'], $class); |
|
99 | 99 | if (method_exists($instance, 'handle')) { |
100 | 100 | $reflectionMethod = new ReflectionMethod($instance, 'handle'); |
101 | 101 | $dependencies = []; |
@@ -112,8 +112,8 @@ discard block |
||
112 | 112 | * @param $class |
113 | 113 | * @return Route|RouteCollection|Router|mixed |
114 | 114 | */ |
115 | - private function getClass($class){ |
|
116 | - switch($class){ |
|
115 | + private function getClass($class) { |
|
116 | + switch ($class) { |
|
117 | 117 | case 'JetFire\Routing\Route': |
118 | 118 | return $this->router->route; |
119 | 119 | case 'JetFire\Routing\Router': |
@@ -121,7 +121,7 @@ discard block |
||
121 | 121 | case 'JetFire\Routing\RouteCollection': |
122 | 122 | return $this->router->collection; |
123 | 123 | default: |
124 | - return call_user_func_array($this->router->getConfig()['di'],[$class]); |
|
124 | + return call_user_func_array($this->router->getConfig()['di'], [$class]); |
|
125 | 125 | } |
126 | 126 | } |
127 | 127 | } |
@@ -29,9 +29,10 @@ discard block |
||
29 | 29 | */ |
30 | 30 | public function globalMiddleware() |
31 | 31 | { |
32 | - if (isset($this->router->collection->middleware['global_middleware'])) |
|
33 | - foreach ($this->router->collection->middleware['global_middleware'] as $class) |
|
34 | - if (class_exists($class)) return $this->callHandler($class); |
|
32 | + if (isset($this->router->collection->middleware['global_middleware'])) { |
|
33 | + foreach ($this->router->collection->middleware['global_middleware'] as $class) |
|
34 | + if (class_exists($class)) return $this->callHandler($class); |
|
35 | + } |
|
35 | 36 | return null; |
36 | 37 | } |
37 | 38 | |
@@ -40,16 +41,18 @@ discard block |
||
40 | 41 | */ |
41 | 42 | public function blockMiddleware() |
42 | 43 | { |
43 | - if (isset($this->router->collection->middleware['block_middleware'])) |
|
44 | - if (isset($this->router->collection->middleware['block_middleware'][$this->router->route->getTarget('block')])) { |
|
45 | - $blocks = $this->router->collection->middleware['block_middleware'][$this->router->route->getTarget('block')]; |
|
44 | + if (isset($this->router->collection->middleware['block_middleware'])) { |
|
45 | + if (isset($this->router->collection->middleware['block_middleware'][$this->router->route->getTarget('block')])) { |
|
46 | + $blocks = $this->router->collection->middleware['block_middleware'][$this->router->route->getTarget('block')]; |
|
47 | + } |
|
46 | 48 | if (is_array($blocks)) { |
47 | - foreach ($blocks as $block) |
|
48 | - if (class_exists($block)) |
|
49 | - if($this->callHandler($block) === false) return false; |
|
50 | - } |
|
51 | - elseif (is_string($blocks) && class_exists($blocks)) |
|
52 | - return $this->callHandler($blocks); |
|
49 | + foreach ($blocks as $block) { |
|
50 | + if (class_exists($block)) |
|
51 | + if($this->callHandler($block) === false) return false; |
|
52 | + } |
|
53 | + } elseif (is_string($blocks) && class_exists($blocks)) { |
|
54 | + return $this->callHandler($blocks); |
|
55 | + } |
|
53 | 56 | } |
54 | 57 | return null; |
55 | 58 | } |
@@ -64,10 +67,12 @@ discard block |
||
64 | 67 | if (isset($this->router->collection->middleware['class_middleware'][$ctrl]) && class_exists($this->router->route->getTarget('controller'))) { |
65 | 68 | $classes = $this->router->collection->middleware['class_middleware'][$ctrl]; |
66 | 69 | if(is_array($classes)){ |
67 | - foreach ($classes as $class) |
|
68 | - if($this->callHandler($class) === false)return false; |
|
69 | - }elseif(is_string($classes)) |
|
70 | - return $this->callHandler($classes); |
|
70 | + foreach ($classes as $class) { |
|
71 | + if($this->callHandler($class) === false)return false; |
|
72 | + } |
|
73 | + } elseif(is_string($classes)) { |
|
74 | + return $this->callHandler($classes); |
|
75 | + } |
|
71 | 76 | } |
72 | 77 | } |
73 | 78 | return null; |
@@ -78,14 +83,17 @@ discard block |
||
78 | 83 | */ |
79 | 84 | public function routeMiddleware() |
80 | 85 | { |
81 | - if (isset($this->router->collection->middleware['route_middleware'])) |
|
82 | - if (isset($this->router->route->getPath()['middleware']) && class_exists($this->router->collection->middleware['route_middleware'][$this->router->route->getPath()['middleware']])) { |
|
83 | - $classes = $this->router->collection->middleware['route_middleware'][$this->router->route->getPath()['middleware']]; |
|
86 | + if (isset($this->router->collection->middleware['route_middleware'])) { |
|
87 | + if (isset($this->router->route->getPath()['middleware']) && class_exists($this->router->collection->middleware['route_middleware'][$this->router->route->getPath()['middleware']])) { |
|
88 | + $classes = $this->router->collection->middleware['route_middleware'][$this->router->route->getPath()['middleware']]; |
|
89 | + } |
|
84 | 90 | if(is_array($classes)){ |
85 | - foreach ($classes as $class) |
|
86 | - if($this->callHandler($class) === false)return false; |
|
87 | - }elseif(is_string($classes)) |
|
88 | - return $this->callHandler($classes); |
|
91 | + foreach ($classes as $class) { |
|
92 | + if($this->callHandler($class) === false)return false; |
|
93 | + } |
|
94 | + } elseif(is_string($classes)) { |
|
95 | + return $this->callHandler($classes); |
|
96 | + } |
|
89 | 97 | } |
90 | 98 | return null; |
91 | 99 | } |
@@ -99,9 +107,10 @@ discard block |
||
99 | 107 | if (method_exists($instance, 'handle')) { |
100 | 108 | $reflectionMethod = new ReflectionMethod($instance, 'handle'); |
101 | 109 | $dependencies = []; |
102 | - foreach ($reflectionMethod->getParameters() as $arg) |
|
103 | - if (!is_null($arg->getClass())) |
|
104 | - $dependencies[] = $this->getClass($arg->getClass()->name); |
|
110 | + foreach ($reflectionMethod->getParameters() as $arg) { |
|
111 | + if (!is_null($arg->getClass())) |
|
112 | + $dependencies[] = $this->getClass($arg->getClass()->name); |
|
113 | + } |
|
105 | 114 | $dependencies = array_merge($dependencies, [$this->router->route]); |
106 | 115 | return $reflectionMethod->invokeArgs($instance, $dependencies); |
107 | 116 | } |