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