|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App; |
|
4
|
|
|
|
|
5
|
|
|
use App\Http\Response; |
|
6
|
|
|
use App\Http\Headers; |
|
7
|
|
|
|
|
8
|
|
|
class Kernel |
|
9
|
|
|
{ |
|
10
|
|
|
|
|
11
|
|
|
const PATH_CONFIG = '/../config/'; |
|
12
|
|
|
|
|
13
|
|
|
use \App\Reuse\TKernel; |
|
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* instanciate |
|
17
|
|
|
* |
|
18
|
|
|
*/ |
|
19
|
26 |
|
public function __construct(string $env, string $path) |
|
20
|
|
|
{ |
|
21
|
26 |
|
$this->init($env, $path); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* set controller namespace |
|
26
|
|
|
* |
|
27
|
|
|
* @param string $ctrlNamespace |
|
28
|
|
|
* @return Kernel |
|
29
|
|
|
*/ |
|
30
|
26 |
|
public function setNameSpace(string $ctrlNamespace): Kernel |
|
31
|
|
|
{ |
|
32
|
26 |
|
$this->nameSpace = $ctrlNamespace; |
|
33
|
26 |
|
return $this; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* run app |
|
38
|
|
|
* |
|
39
|
|
|
* @param array $routerGroups |
|
40
|
|
|
* @return Kernel |
|
41
|
|
|
*/ |
|
42
|
3 |
|
public function run(array $groups = []): Kernel |
|
43
|
|
|
{ |
|
44
|
3 |
|
$routerGroups = ($groups) |
|
45
|
2 |
|
? $groups |
|
46
|
3 |
|
: $this->router->compile(); |
|
47
|
3 |
|
if ($routerGroups) { |
|
48
|
2 |
|
$this->setClassname($routerGroups); |
|
49
|
2 |
|
if (class_exists($this->className)) { |
|
50
|
1 |
|
$this->setController(); |
|
51
|
1 |
|
$this->setReflector(); |
|
52
|
1 |
|
$this->setActions(); |
|
53
|
1 |
|
$this->setAction( |
|
54
|
1 |
|
$routerGroups, |
|
55
|
1 |
|
$this->getRequest()->getMethod() |
|
|
|
|
|
|
56
|
|
|
); |
|
57
|
|
|
//->setActionAnnotations(); |
|
58
|
1 |
|
$this->setMiddleware(); |
|
59
|
|
|
} else { |
|
60
|
1 |
|
$this->error = true; |
|
61
|
1 |
|
$this->errorCode = Response::HTTP_SERVICE_UNAVAILABLE; |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
3 |
|
return $this; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* set controller action from router groups |
|
69
|
|
|
* |
|
70
|
|
|
* @param array $routerGrps |
|
71
|
|
|
* @return void |
|
72
|
|
|
*/ |
|
73
|
10 |
|
public function setAction(array $routerGrps) |
|
74
|
|
|
{ |
|
75
|
10 |
|
$this->action = isset($routerGrps[1]) ? strtolower($routerGrps[1]) : ''; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* dispatch response |
|
80
|
|
|
* |
|
81
|
|
|
* @return Kernel |
|
82
|
|
|
*/ |
|
83
|
1 |
|
public function send(): Kernel |
|
84
|
|
|
{ |
|
85
|
1 |
|
if ($this->getError()) { |
|
86
|
1 |
|
$this->res |
|
87
|
1 |
|
->setCode($this->errorCode) |
|
88
|
1 |
|
->setContent([ |
|
89
|
1 |
|
Response::_ERROR => $this->error, |
|
90
|
1 |
|
Response::_ERROR_CODE => $this->errorCode, |
|
91
|
1 |
|
Response::_ERROR_MSG => $this->errorMsg, |
|
92
|
|
|
]) |
|
93
|
1 |
|
->getHeaderManager() |
|
94
|
1 |
|
->add( |
|
95
|
1 |
|
Headers::CONTENT_TYPE, |
|
96
|
1 |
|
'application/json; charset=utf-8' |
|
97
|
|
|
); |
|
98
|
1 |
|
$this->getLogger()->warning($this->errorMsg); |
|
99
|
|
|
} else { |
|
100
|
1 |
|
$this->getLogger()->debug('Response sent'); |
|
101
|
|
|
} |
|
102
|
1 |
|
$this->getResponse()->send(); |
|
103
|
1 |
|
return $this; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* shutdown kernel |
|
108
|
|
|
* |
|
109
|
|
|
* @return void |
|
110
|
|
|
*/ |
|
111
|
1 |
|
public function shutdown(int $code = 0) |
|
112
|
|
|
{ |
|
113
|
1 |
|
throw new \Exception('shutdown', $code); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|