1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author jan huang <[email protected]> |
4
|
|
|
* @copyright 2016 |
5
|
|
|
* |
6
|
|
|
* @see https://www.github.com/janhuang |
7
|
|
|
* @see http://www.fast-d.cn/ |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace FastD; |
11
|
|
|
|
12
|
|
|
use Exception; |
13
|
|
|
use FastD\Config\Config; |
14
|
|
|
use FastD\Container\Container; |
15
|
|
|
use FastD\Container\ServiceProviderInterface; |
16
|
|
|
use FastD\Http\HttpException; |
17
|
|
|
use FastD\Http\Response; |
18
|
|
|
use FastD\Http\ServerRequest; |
19
|
|
|
use FastD\Logger\Logger; |
20
|
|
|
use FastD\ServiceProvider\ConfigServiceProvider; |
21
|
|
|
use Psr\Http\Message\ResponseInterface; |
22
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Class Application. |
26
|
|
|
*/ |
27
|
|
|
class Application extends Container |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* The FastD version. |
31
|
|
|
* |
32
|
|
|
* @const string |
33
|
|
|
*/ |
34
|
|
|
const VERSION = '3.1.0'; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var Application |
38
|
|
|
*/ |
39
|
|
|
public static $app; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
protected $path; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var string |
48
|
|
|
*/ |
49
|
|
|
protected $name; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var bool |
53
|
|
|
*/ |
54
|
|
|
protected $booted = false; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* AppKernel constructor. |
58
|
|
|
* |
59
|
|
|
* @param $path |
60
|
|
|
*/ |
61
|
60 |
|
public function __construct($path) |
62
|
|
|
{ |
63
|
60 |
|
$this->path = $path; |
64
|
|
|
|
65
|
60 |
|
static::$app = $this; |
66
|
|
|
|
67
|
60 |
|
$this->add('app', $this); |
68
|
|
|
|
69
|
60 |
|
$this->bootstrap(); |
70
|
60 |
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return string |
74
|
|
|
*/ |
75
|
60 |
|
public function getName() |
76
|
|
|
{ |
77
|
60 |
|
return $this->name; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return bool |
82
|
|
|
*/ |
83
|
5 |
|
public function isBooted() |
84
|
|
|
{ |
85
|
5 |
|
return $this->booted; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return string |
90
|
|
|
*/ |
91
|
60 |
|
public function getPath() |
92
|
|
|
{ |
93
|
60 |
|
return $this->path; |
94
|
|
|
} |
95
|
|
|
|
96
|
60 |
|
public function bootstrap() |
97
|
|
|
{ |
98
|
60 |
|
if (!$this->booted) { |
99
|
60 |
|
$config = load($this->path.'/config/app.php'); |
100
|
|
|
|
101
|
60 |
|
$this->name = $config['name']; |
102
|
|
|
|
103
|
60 |
|
$this->add('config', new Config($config)); |
104
|
60 |
|
$this->add('logger', new Logger(app()->getName())); |
105
|
|
|
|
106
|
60 |
|
$this->registerServicesProviders($config['services']); |
107
|
60 |
|
unset($config); |
108
|
60 |
|
$this->booted = true; |
109
|
60 |
|
} |
110
|
60 |
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param ServiceProviderInterface[] $services |
114
|
|
|
*/ |
115
|
60 |
|
protected function registerServicesProviders(array $services) |
116
|
|
|
{ |
117
|
60 |
|
$this->register(new ConfigServiceProvider()); |
118
|
60 |
|
foreach ($services as $service) { |
119
|
60 |
|
$this->register(new $service()); |
120
|
60 |
|
} |
121
|
60 |
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param ServerRequestInterface $request |
125
|
|
|
* |
126
|
|
|
* @return Response |
127
|
|
|
*/ |
128
|
17 |
|
public function handleRequest(ServerRequestInterface $request) |
129
|
|
|
{ |
130
|
17 |
|
$this->add('request', $request); |
131
|
|
|
|
132
|
|
|
try { |
133
|
17 |
|
$response = $this->get('dispatcher')->dispatch($request); |
134
|
17 |
|
$this->add('response', $response); |
135
|
17 |
|
return $response; |
136
|
5 |
|
} catch (Exception $exception) { |
137
|
5 |
|
return $this->handleException($exception); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param Response $response |
143
|
|
|
*/ |
144
|
5 |
|
public function handleResponse(Response $response) |
145
|
|
|
{ |
146
|
5 |
|
$response->send(); |
147
|
5 |
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @param Exception $e |
151
|
|
|
* |
152
|
|
|
* @return Http\JsonResponse |
153
|
|
|
*/ |
154
|
10 |
|
public function handleException(Exception $e) |
155
|
|
|
{ |
156
|
10 |
|
$this->add('exception', $e); |
157
|
|
|
|
158
|
10 |
|
$statusCode = ($e instanceof HttpException) ? $e->getStatusCode() : $e->getCode(); |
159
|
|
|
|
160
|
10 |
|
if (!array_key_exists($statusCode, Response::$statusTexts)) { |
161
|
5 |
|
$statusCode = 502; |
162
|
5 |
|
} |
163
|
|
|
|
164
|
10 |
|
$handle = config()->get('exception.response'); |
165
|
|
|
|
166
|
10 |
|
return json($handle($e), $statusCode); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @return int |
171
|
|
|
*/ |
172
|
|
|
public function run() |
173
|
|
|
{ |
174
|
|
|
$request = ServerRequest::createServerRequestFromGlobals(); |
175
|
|
|
|
176
|
|
|
$response = $this->handleRequest($request); |
177
|
|
|
|
178
|
|
|
$this->handleResponse($response); |
179
|
|
|
|
180
|
|
|
return $this->shutdown($request, $response); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @param ServerRequestInterface $request |
185
|
|
|
* @param ResponseInterface $response |
186
|
|
|
* |
187
|
|
|
* @return int |
188
|
|
|
*/ |
189
|
8 |
|
public function shutdown(ServerRequestInterface $request, ResponseInterface $response) |
190
|
|
|
{ |
191
|
8 |
|
logger()->log($response->getStatusCode(), $request->getMethod().' '.$request->getUri()->getPath()); |
192
|
|
|
|
193
|
8 |
|
$this->offsetUnset('request'); |
194
|
8 |
|
$this->offsetUnset('response'); |
195
|
8 |
|
$this->offsetUnset('exception'); |
196
|
8 |
|
unset($request, $response); |
197
|
|
|
|
198
|
8 |
|
return 0; |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|