1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JetFire\Routing\Dispatcher; |
4
|
|
|
|
5
|
|
|
use JetFire\Routing\Response; |
6
|
|
|
use JetFire\Routing\ResponseInterface; |
7
|
|
|
use JetFire\Routing\Route; |
8
|
|
|
use JetFire\Routing\RouteCollection; |
9
|
|
|
use JetFire\Routing\Router; |
10
|
|
|
use ReflectionClass; |
11
|
|
|
use ReflectionMethod; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class ControllerDispatcher |
15
|
|
|
* @package JetFire\Routing\Dispatcher |
16
|
|
|
*/ |
17
|
|
|
class ControllerDispatcher implements DispatcherInterface |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var Router |
22
|
|
|
*/ |
23
|
|
|
private $router; |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param Router $router |
28
|
|
|
*/ |
29
|
|
|
public function __construct(Router $router) |
30
|
|
|
{ |
31
|
|
|
$this->router = $router; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @throws \Exception |
37
|
|
|
*/ |
38
|
|
|
public function call() |
39
|
|
|
{ |
40
|
|
|
|
41
|
|
View Code Duplication |
if (!class_exists($this->router->route->getTarget('controller'))) { |
|
|
|
|
42
|
|
|
throw new \Exception('Class not found : "' . $this->router->route->getTarget('controller') . '"'); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$classInstance = [ |
46
|
|
|
Route::class => $this->router->route, |
47
|
|
|
Response::class => $this->router->response, |
48
|
|
|
RouteCollection::class => $this->router->collection, |
49
|
|
|
]; |
50
|
|
|
|
51
|
|
|
$reflectionMethod = new ReflectionMethod($this->router->route->getTarget('controller'), $this->router->route->getTarget('action')); |
52
|
|
|
$dependencies = []; |
53
|
|
|
$count = 0; |
54
|
|
|
|
55
|
|
|
foreach ($reflectionMethod->getParameters() as $arg) { |
56
|
|
|
if (!is_null($arg->getClass())) { |
57
|
|
|
if (isset($classInstance[$arg->getClass()->name])) { |
58
|
|
|
$dependencies[] = $classInstance[$arg->getClass()->name]; |
59
|
|
|
} else { |
60
|
|
|
$dependencies[] = call_user_func_array($this->router->route->getTarget('di'), [$arg->getClass()->name]); |
61
|
|
|
} |
62
|
|
|
} else { |
63
|
|
|
$count++; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
if ($count == count($this->router->route->getParameters()) || ($this->router->route->getParameters() == '' && $count == 0)) { |
68
|
|
|
$dependencies = array_merge($dependencies, ($this->router->route->getParameters() == '') ? [] : $this->router->route->getParameters()); |
69
|
|
|
$content = $reflectionMethod->invokeArgs($this->getController($classInstance), $dependencies); |
70
|
|
View Code Duplication |
if ($content instanceof ResponseInterface) { |
|
|
|
|
71
|
|
|
$this->router->response = $content; |
72
|
|
|
} else { |
73
|
|
|
if (is_array($content)) { |
74
|
|
|
$this->router->route->addTarget('data', $content); |
75
|
|
|
$content = json_encode($content); |
76
|
|
|
} |
77
|
|
|
$this->router->callMiddleware('between'); |
78
|
|
|
$this->router->response->setContent($content); |
79
|
|
|
} |
80
|
|
|
} else { |
81
|
|
|
$this->router->response->setStatusCode(404); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param array $classInstance |
88
|
|
|
* @return object |
89
|
|
|
* @throws \Exception |
90
|
|
|
*/ |
91
|
|
|
private function getController($classInstance = []) |
92
|
|
|
{ |
93
|
|
|
$reflector = new ReflectionClass($this->router->route->getTarget('controller')); |
94
|
|
View Code Duplication |
if (!$reflector->isInstantiable()) { |
|
|
|
|
95
|
|
|
throw new \Exception('Target [' . $this->router->route->getTarget('controller') . '] is not instantiable.'); |
96
|
|
|
} |
97
|
|
|
$constructor = $reflector->getConstructor(); |
98
|
|
|
if (is_null($constructor)) { |
99
|
|
|
$class = $this->router->route->getTarget('controller'); |
100
|
|
|
return call_user_func_array($this->router->route->getTarget('di'), [$class]); |
101
|
|
|
} |
102
|
|
|
$dependencies = []; |
103
|
|
|
foreach ($constructor->getParameters() as $dep) { |
104
|
|
|
$class = $dep->getClass()->name; |
105
|
|
|
if (isset($classInstance[$class])) { |
106
|
|
|
$dependencies[] = $classInstance[$class]; |
107
|
|
|
} else { |
108
|
|
|
$dependencies[] = call_user_func_array($this->router->route->getTarget('di'), [$class]); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
return $reflector->newInstanceArgs($dependencies); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
} |
115
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.