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