1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nymfonya\Component\Http; |
4
|
|
|
|
5
|
|
|
use Nymfonya\Component\Http\Headers; |
6
|
|
|
use Nymfonya\Component\Http\Interfaces\KernelInterface; |
7
|
|
|
use Nymfonya\Component\Http\Response; |
8
|
|
|
use Nymfonya\Component\Http\Interfaces\KernelEventsInterface; |
9
|
|
|
use Nymfonya\Component\Pubsub\Dispatcher; |
10
|
|
|
use Nymfonya\Component\Pubsub\Event; |
11
|
|
|
|
12
|
|
|
class Kernel implements KernelInterface |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
const PATH_CONFIG = '/../config/'; |
16
|
|
|
|
17
|
|
|
use \Nymfonya\Component\Http\Reuse\TKernel; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* instanciate |
21
|
|
|
* |
22
|
|
|
* @param string $env |
23
|
|
|
* @param string $path |
24
|
|
|
*/ |
25
|
28 |
|
public function __construct(string $env, string $path) |
26
|
|
|
{ |
27
|
28 |
|
$this->init($env, $path); |
28
|
28 |
|
$this->dispatcher->publish( |
29
|
28 |
|
new Event( |
30
|
28 |
|
get_class($this), |
31
|
28 |
|
KernelEventsInterface::EVENT_KERNEL_BOOT, |
32
|
28 |
|
$this |
33
|
|
|
) |
34
|
|
|
); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* set controller namespace |
39
|
|
|
* |
40
|
|
|
* @param string $ctrlNamespace |
41
|
|
|
* @return KernelInterface |
42
|
|
|
*/ |
43
|
28 |
|
public function setNameSpace(string $ctrlNamespace): KernelInterface |
44
|
|
|
{ |
45
|
28 |
|
$this->spacename = $ctrlNamespace; |
46
|
28 |
|
return $this; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* retrieve kernel instance classname from container |
51
|
|
|
* |
52
|
|
|
* @return string |
53
|
|
|
*/ |
54
|
1 |
|
public function getBundleClassname(): string |
55
|
|
|
{ |
56
|
1 |
|
return get_called_class(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* set pubsub dispatcher |
61
|
|
|
* |
62
|
|
|
* @param Dispatcher $dispatcher |
|
|
|
|
63
|
|
|
* @return KernelInterface |
64
|
|
|
*/ |
65
|
28 |
|
public function setDispatcher(Dispatcher $dispatcher = null): KernelInterface |
66
|
|
|
{ |
67
|
28 |
|
if ($dispatcher instanceof Dispatcher) { |
68
|
1 |
|
$this->dispatcher = $dispatcher; |
69
|
1 |
|
return $this; |
70
|
|
|
} |
71
|
28 |
|
$this->dispatcher = new Dispatcher(); |
72
|
28 |
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* run app |
77
|
|
|
* |
78
|
|
|
* @param array $groups |
79
|
|
|
* @return KernelInterface |
80
|
|
|
*/ |
81
|
3 |
|
public function run(array $groups = []): KernelInterface |
82
|
|
|
{ |
83
|
3 |
|
$routerGroups = (empty($groups)) |
84
|
1 |
|
? $this->router->compile() |
85
|
3 |
|
: $groups; |
86
|
3 |
|
if (!empty($routerGroups)) { |
87
|
2 |
|
$this->setClassname($routerGroups); |
88
|
2 |
|
if (class_exists($this->className)) { |
89
|
1 |
|
$this->setController(); |
90
|
1 |
|
$this->setReflector(); |
91
|
1 |
|
$this->setActions(); |
92
|
1 |
|
$this->setAction($routerGroups); |
93
|
|
|
//->setActionAnnotations(); |
94
|
1 |
|
$this->setMiddleware(); |
95
|
|
|
} else { |
96
|
1 |
|
$this->error = true; |
97
|
1 |
|
$this->errorCode = Response::HTTP_SERVICE_UNAVAILABLE; |
98
|
|
|
} |
99
|
|
|
} |
100
|
3 |
|
return $this; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* set controller action from router groups |
105
|
|
|
* |
106
|
|
|
* @param array $routerGrps |
107
|
|
|
* @return void |
108
|
|
|
*/ |
109
|
10 |
|
public function setAction(array $routerGrps) |
110
|
|
|
{ |
111
|
10 |
|
$this->action = isset($routerGrps[1]) ? strtolower($routerGrps[1]) : ''; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* dispatch response |
116
|
|
|
* |
117
|
|
|
* @return KernelInterface |
118
|
|
|
*/ |
119
|
1 |
|
public function send(): KernelInterface |
120
|
|
|
{ |
121
|
1 |
|
if ($this->getError()) { |
122
|
1 |
|
$this->res |
123
|
1 |
|
->setCode($this->errorCode) |
124
|
1 |
|
->setContent([ |
125
|
1 |
|
Response::_ERROR => $this->error, |
126
|
1 |
|
Response::_ERROR_CODE => $this->errorCode, |
127
|
1 |
|
Response::_ERROR_MSG => $this->errorMsg, |
128
|
|
|
]) |
129
|
1 |
|
->getHeaderManager() |
130
|
1 |
|
->add( |
131
|
1 |
|
Headers::CONTENT_TYPE, |
132
|
1 |
|
'application/json; charset=utf-8' |
133
|
|
|
); |
134
|
1 |
|
$this->getLogger()->warning($this->errorMsg); |
135
|
|
|
} else { |
136
|
1 |
|
$this->getLogger()->debug('Response sent'); |
137
|
|
|
} |
138
|
1 |
|
$this->getResponse()->send(); |
139
|
1 |
|
return $this; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* shutdown kernel |
144
|
|
|
* |
145
|
|
|
* @return void |
|
|
|
|
146
|
|
|
*/ |
147
|
1 |
|
public function shutdown(int $code = 0) |
148
|
|
|
{ |
149
|
1 |
|
throw new \Exception('shutdown', $code); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.