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