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