/
Kernel.php
417 lines (377 loc) · 10.4 KB
/
Kernel.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
<?php
/**
* slince application library
* @author Tao <taosikai@yeah.net>
*/
namespace Slince\Application;
use Slince\Config\Repository;
use Slince\Di\Container;
use Slince\Event\Dispatcher;
use Slince\Event\Event;
use Slince\Application\Exception\LogicException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Slince\Config\Config;
use Slince\Routing\Router;
use Slince\Routing\RequestContext;
use Slince\Routing\RouteBuilder;
abstract class Kernel
{
/**
* Container Instance
* @var Container
*/
protected $container;
/**
* 注册的application
* @var array
*/
protected $applications = [];
/**
* 被调度的application
* @var ApplicationInterface
*/
protected $application;
/**
* 是否是debug模式
* @var boolean
*/
protected $debug;
/**
* 命令空间
* @var
*/
protected $namespace;
/**
* 项目根目录
* @var string
*/
protected $rootPath;
/**
* 当前核心实例
* @var Kernel
*/
protected static $kernel;
public function __construct($debug = false)
{
$this->debug = $debug;
$this->rootPath = $this->getRootPath();
$this->initialize();
$this->dispatchEvent(EventStore::KERNEL_INITED);
static::$kernel = $this;
}
/**
* 初始化kernel
*/
protected function initialize()
{
$this->registerErrorHandler();
$this->container = $this->createContainer();
$this->registerServices($this->container);
$this->registerConfigs($this->container->get('config'));
$this->registerEvents($this->container->get('dispatcher'));
$this->registerRoutes($this->container->get('router')->getRouteBuilder());
//注册所有的桥
$this->initializeBridges();
$this->initializeApplications();
}
/**
* 注册错误和异常的捕获事件
* return void
*/
protected function registerErrorHandler()
{
set_error_handler(function ($errno, $errstr, $errfile, $errline = '', $errcontext = '') {
$this->dispatchEvent(EventStore::ERROR_OCCURRED, func_get_args());
});
set_exception_handler(function (\Exception $excetion) {
$this->dispatchEvent(EventStore::EXCEPTION_OCCURRED, ['exception' => $excetion]);
});
register_shutdown_function(function () {
if ($error = error_get_last()) {
$this->dispatchEvent(EventStore::ERROR_OCCURRED, $error);
}
});
}
/**
* 实例化注册的application
* @return void
*/
protected function initializeApplications()
{
foreach ($this->registerApplications() as $application) {
$this->applications[$application->getName()] = $application;
}
}
/**
* 实例化桥工作
* @return void
*/
protected function initializeBridges()
{
foreach ($this->registerBridges() as $bridge) {
$bridge->initialize($this->getContainer());
}
}
/**
* 注册所有的bridge
* @return array
*/
abstract public function registerBridges();
/**
* 注册所有application
* @return array
*/
abstract public function registerApplications();
/**
* 注册service
* @param Container $container
*/
abstract public function registerServices(Container $container);
/**
* 注册config
* @param Config $config
*/
abstract public function registerConfigs(Config $config);
/**
* 注册route
*
* @param RouteBuilder $routes
*/
abstract public function registerRoutes(RouteBuilder $routes);
/**
* 注册事件监听
*
* @param Dispatcher $dispatcher
*/
abstract public function registerEvents(Dispatcher $dispatcher);
/**
* 获取DI容器
* @return Container
*/
public function getContainer()
{
return $this->container;
}
/**
* 获取router
* @return Router
*/
public function getRouter()
{
return $this->container->get('router');
}
/**
* 是否工作在debug模式下
* @return boolean
*/
public function debug()
{
return $this->debug;
}
/**
* 运行项目
*/
public function run()
{
$request = Request::createFromGlobals();
$response = $this->handleRequest($request);
$this->sendResponse($response);
}
/**
* 发送response到客户端
* @param Response $response
*/
public function sendResponse(Response $response)
{
$response->send();
exit();
}
/**
* 开始处理请求
* @param Request $request
* @return Response
*/
public function handleRequest(Request $request)
{
$this->setParameter('request', $request);
//绑定Request到RequestContext
$context = $this->bindRequestToContext($request, RequestContext::create());
$this->getRouter()->setContext($context);
$route = $this->getRouter()->match($request->getPathInfo());
$this->setParameter('route', $route); //匹配出来的route存入核心缓存
//request匹配完毕,待派发
$this->dispatchEvent(EventStore::PROCESS_REQUEST, [
'request' => $request,
'route' => $route
]);
$action = $route->getAction();
if (is_callable($action)) {
$response = $this->runCallableAction($action, $route->getParameters());
} else {
list($applicationName, $controller, $action) = explode('@', $action);
$this->setParameter('application', $applicationName);
$this->setParameter('controller', $controller);
$this->setParameter('action', $action);
$response = $this->runApplication($applicationName, $controller, $action, $route->getParameters());
}
return $response;
}
/**
* 核心内存缓存参数读取
* @param string $name
* @param mixed $default
*/
public function getParameter($name, $default = null)
{
return $this->container->get('kernelCache')->get($name, $default);
}
/**
* 核心内存缓存设置
* @param string $name
* @param mixed $value
*/
public function setParameter($name, $value)
{
$this->container->get('kernelCache')->set($name, $value);
}
/**
* 派发事件
* @param string $eventName
* @param array $parameters
*/
public function dispatchEvent($eventName, array $parameters = [])
{
$event = new Event($eventName, $this, $parameters);
$this->container->get('dispatcher')->dispatch($eventName, $event);
}
/**
* @param $name
* @return ApplicationInterface
*/
public function getApplication($name)
{
return isset($this->applications[$name]) ? $this->applications[$name] : null;
}
/**
* 获取当前正在运行的application
* @return ApplicationInterface
*/
function getDispatchedApplication()
{
return $this->application;
}
/**
* 获取当前application的命名空间
* @return string
*/
function getNamespace()
{
if (is_null($this->namespace)) {
$this->namespace = strstr(get_class($this), '\\', true);
}
return $this->namespace;
}
/**
* 获取项目root path
* @return string
*/
public function getRootPath()
{
if (is_null($this->rootPath)) {
$reflection = new \ReflectionObject($this);
$this->rootPath = dirname(dirname($reflection->getFileName()));
}
return $this->rootPath;
}
/**
* 配置文件目录
* @return string
*/
public function getConfigPath()
{
return $this->getRootPath() . '/config';
}
/**
* 日志文件目录
* @return string
*/
public function getLogPath()
{
return $this->getRootPath() . '/tmp/logs';
}
/**
* 缓存文件目录
* @return string
*/
public function getCachePath()
{
return $this->getRootPath() . '/tmp/cache';
}
/**
* 调度回调
* @param string $action
* @param array $parameters
* @throws LogicException
* @return Response
*/
protected function runCallableAction($action, $parameters = [])
{
$response = call_user_func($action, $parameters);
if (!$response instanceof Response) {
throw new LogicException("Route action must return a response instance");
}
return $response;
}
/**
* 运行application
* @param string $name
* @param string $controller
* @param string $action
* @param array $parameters
* @throws LogicException
*/
protected function runApplication($name, $controller, $action, $parameters = [])
{
if (!isset($this->applications[$name])) {
throw new LogicException("Application {$name} is not fund");
}
$this->application = $this->applications[$name];
return $this->application->run($this, $controller, $action, $parameters);
}
/**
* 绑定request到routing的context
* @param Request $request
* @param RequestContext $context
* @return RequestContext
*/
protected function bindRequestToContext(Request $request, RequestContext $context)
{
$context->setBaseUrl($request->getBaseUrl());
$context->setPathInfo($request->getPathInfo());
$context->setMethod($request->getMethod());
$context->setHost($request->getHost());
$context->setScheme($request->getScheme());
$context->setHttpPort($request->getPort());
$context->setHttpPort($request->isSecure() ? null : $request->getPort());
$context->setHttpsPort($request->isSecure() ? $request->getPort() : null);
$context->setQueryString($request->server->get('QUERY_STRING', ''));
return $context;
}
/**
* 创建DI容器
* @return Container
*/
protected function createContainer()
{
return new Container();
}
/**
* 获取正在运行的kernel实例
* @return Kernel
*/
public static function instance()
{
return static::$kernel;
}
}