|
1
|
|
|
<?php |
|
2
|
|
|
namespace Fyuze\Http; |
|
3
|
|
|
|
|
4
|
|
|
use Closure; |
|
5
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
6
|
|
|
use ReflectionClass; |
|
7
|
|
|
use ReflectionParameter; |
|
8
|
|
|
use Fyuze\Http\Exception\NotFoundException; |
|
9
|
|
|
use Fyuze\Kernel\Registry; |
|
10
|
|
|
use Fyuze\Routing\Router; |
|
11
|
|
|
|
|
12
|
|
|
class Kernel |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var Registry |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $registry; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* |
|
21
|
|
|
* @var Router |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $router; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param Registry $registry |
|
27
|
|
|
* @param Router $router |
|
28
|
|
|
*/ |
|
29
|
5 |
|
public function __construct(Registry $registry, Router $router) |
|
30
|
|
|
{ |
|
31
|
5 |
|
$this->registry = $registry; |
|
32
|
5 |
|
$this->router = $router; |
|
33
|
5 |
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param ServerRequestInterface $request |
|
37
|
|
|
* @return Response|mixed |
|
38
|
|
|
*/ |
|
39
|
5 |
|
public function handle(ServerRequestInterface $request) |
|
40
|
|
|
{ |
|
41
|
|
|
try { |
|
42
|
|
|
|
|
43
|
5 |
|
$route = $this->router->resolve($request); |
|
44
|
|
|
|
|
45
|
2 |
|
$body = $this->resolve($route->getAction(), $request->getQueryParams()); |
|
46
|
|
|
|
|
47
|
1 |
|
$response = Response::create($body); |
|
48
|
|
|
|
|
49
|
5 |
|
} catch (NotFoundException $e) { |
|
50
|
|
|
|
|
51
|
3 |
|
$response = Response::create('<body>Not Found</body>', 404); |
|
52
|
|
|
|
|
53
|
4 |
|
} catch (\Exception $e) { |
|
54
|
|
|
|
|
55
|
1 |
|
$response = Response::create( |
|
56
|
1 |
|
sprintf('<body>An unkown error has occurred: %s</body>', $e->getMessage()), |
|
57
|
|
|
500 |
|
58
|
1 |
|
); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
5 |
|
$this->registry->add('response', $response); |
|
62
|
|
|
|
|
63
|
5 |
|
return $this->registry->make('response'); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Method injection resolver |
|
68
|
|
|
* |
|
69
|
|
|
* @param $action |
|
70
|
|
|
* @param $params |
|
71
|
|
|
* @return mixed |
|
72
|
|
|
*/ |
|
73
|
2 |
|
protected function resolve($action, $params) |
|
74
|
|
|
{ |
|
75
|
2 |
|
if ($action instanceof Closure) { |
|
76
|
|
|
|
|
77
|
1 |
|
return $action($params); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
1 |
|
list($controller, $method) = $action; |
|
81
|
|
|
|
|
82
|
1 |
|
$reflect = new ReflectionClass($controller); |
|
83
|
|
|
|
|
84
|
1 |
|
foreach (array_filter($reflect->getMethod($method)->getParameters(), $this->getParams()) as $param) { |
|
85
|
1 |
|
array_unshift($params, $this->registry->make( |
|
86
|
1 |
|
$param->getClass()->getName() |
|
87
|
1 |
|
)); |
|
88
|
1 |
|
} |
|
89
|
|
|
|
|
90
|
1 |
|
return $reflect->getMethod($method)->invokeArgs( |
|
91
|
1 |
|
$this->registry->make($controller), |
|
92
|
|
|
$params |
|
93
|
1 |
|
); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @return Closure |
|
98
|
|
|
*/ |
|
99
|
|
|
protected function getParams() |
|
100
|
|
|
{ |
|
101
|
1 |
|
return function (ReflectionParameter $param) { |
|
102
|
1 |
|
return $param->getClass(); |
|
103
|
1 |
|
}; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|