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\Exceptions\ServiceNotFoundException; |
16
|
|
|
use FastD\Container\ServiceProviderInterface; |
17
|
|
|
use FastD\Http\HttpException; |
18
|
|
|
use FastD\Http\Response; |
19
|
|
|
use FastD\Http\ServerRequest; |
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 (release candidate)'; |
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
|
53 |
|
public function __construct($path) |
62
|
|
|
{ |
63
|
53 |
|
$this->path = $path; |
64
|
|
|
|
65
|
53 |
|
static::$app = $this; |
66
|
|
|
|
67
|
53 |
|
$this->add('app', $this); |
68
|
|
|
|
69
|
53 |
|
$this->bootstrap(); |
70
|
53 |
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return string |
74
|
|
|
*/ |
75
|
45 |
|
public function getName() |
76
|
|
|
{ |
77
|
45 |
|
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
|
55 |
|
public function getPath() |
92
|
|
|
{ |
93
|
55 |
|
return $this->path; |
94
|
|
|
} |
95
|
|
|
|
96
|
53 |
|
public function bootstrap() |
97
|
|
|
{ |
98
|
53 |
|
if (!$this->booted) { |
99
|
53 |
|
$config = load($this->path.'/config/app.php'); |
100
|
|
|
|
101
|
53 |
|
$this->name = $config['name']; |
102
|
|
|
|
103
|
53 |
|
$this->add('config', new Config($config)); |
104
|
53 |
|
$this->registerServicesProviders($config['services']); |
105
|
53 |
|
unset($config); |
106
|
53 |
|
$this->booted = true; |
107
|
53 |
|
} |
108
|
53 |
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param ServiceProviderInterface[] $services |
112
|
|
|
*/ |
113
|
53 |
|
protected function registerServicesProviders(array $services) |
114
|
|
|
{ |
115
|
53 |
|
$this->register(new ConfigServiceProvider()); |
116
|
53 |
|
foreach ($services as $service) { |
117
|
53 |
|
$this->register(new $service()); |
118
|
53 |
|
} |
119
|
53 |
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param ServerRequestInterface $request |
123
|
|
|
* |
124
|
|
|
* @return Response |
125
|
|
|
*/ |
126
|
16 |
|
public function handleRequest(ServerRequestInterface $request) |
127
|
|
|
{ |
128
|
16 |
|
$this->add('request', $request); |
129
|
|
|
try { |
130
|
16 |
|
return $this->get('dispatcher')->dispatch($request); |
131
|
5 |
|
} catch (Exception $exception) { |
132
|
5 |
|
return $this->handleException($request, $exception); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param Response $response |
138
|
|
|
*/ |
139
|
5 |
|
public function handleResponse(Response $response) |
140
|
|
|
{ |
141
|
5 |
|
$response->send(); |
142
|
5 |
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param ServerRequestInterface $request |
146
|
|
|
* @param Exception $e |
147
|
|
|
* |
148
|
|
|
* @return Http\JsonResponse |
149
|
|
|
*/ |
150
|
10 |
|
public function handleException(ServerRequestInterface $request, Exception $e) |
151
|
|
|
{ |
152
|
10 |
|
$statusCode = ($e instanceof HttpException) ? $e->getStatusCode() : $e->getCode(); |
153
|
|
|
|
154
|
10 |
|
if (!array_key_exists($statusCode, Response::$statusTexts)) { |
155
|
5 |
|
$statusCode = 502; |
156
|
5 |
|
} |
157
|
|
|
|
158
|
10 |
|
$handle = config()->get('exception.handle'); |
159
|
|
|
|
160
|
10 |
|
$response = json($handle($e), $statusCode); |
161
|
|
|
// minimal mode |
162
|
|
|
try { |
163
|
10 |
|
logger()->error($request->getMethod().' '.$request->getUri()->getPath(), [ |
164
|
8 |
|
'ip' => get_local_ip(), |
165
|
8 |
|
'status' => $response->getStatusCode(), |
166
|
8 |
|
'get' => $request->getQueryParams(), |
167
|
8 |
|
'post' => $request->getParsedBody(), |
168
|
8 |
|
'file' => $e->getFile(), |
169
|
8 |
|
'line' => $e->getLine(), |
170
|
8 |
|
'trace' => explode("\n", $e->getTraceAsString()), |
171
|
8 |
|
]); |
172
|
10 |
|
} catch (ServiceNotFoundException $e) { |
|
|
|
|
173
|
|
|
} |
174
|
|
|
|
175
|
10 |
|
return $response; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @return int |
180
|
|
|
*/ |
181
|
|
|
public function run() |
182
|
|
|
{ |
183
|
|
|
$request = ServerRequest::createServerRequestFromGlobals(); |
184
|
|
|
|
185
|
|
|
$response = $this->handleRequest($request); |
186
|
|
|
|
187
|
|
|
$this->handleResponse($response); |
188
|
|
|
|
189
|
|
|
return $this->shutdown($request, $response); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @param ServerRequestInterface $request |
194
|
|
|
* @param ResponseInterface $response |
195
|
|
|
* |
196
|
|
|
* @return int |
197
|
|
|
*/ |
198
|
5 |
|
public function shutdown(ServerRequestInterface $request, ResponseInterface $response) |
199
|
|
|
{ |
200
|
|
|
try { |
201
|
5 |
|
logger()->info($request->getMethod().' '.$request->getUri()->getPath(), [ |
202
|
4 |
|
'ip' => get_local_ip(), |
203
|
4 |
|
'status' => $response->getStatusCode(), |
204
|
4 |
|
'get' => $request->getQueryParams(), |
205
|
4 |
|
'post' => $request->getParsedBody(), |
206
|
4 |
|
]); |
207
|
|
|
|
208
|
4 |
|
return 0; |
209
|
1 |
|
} catch (ServiceNotFoundException $e) { |
|
|
|
|
210
|
|
|
} |
211
|
|
|
|
212
|
1 |
|
return 0; |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
|