|
1
|
|
|
<?php |
|
2
|
|
|
namespace nebula\application; |
|
3
|
|
|
|
|
4
|
|
|
use Exception; |
|
5
|
|
|
use nebula\Context; |
|
6
|
|
|
use nebula\NebulaApplication; |
|
7
|
|
|
use nebula\application\Resource; |
|
8
|
|
|
use nebula\component\loader\Path; |
|
9
|
|
|
use nebula\application\config\Config; |
|
10
|
|
|
use nebula\component\request\Request; |
|
11
|
|
|
use nebula\application\module\Manager; |
|
12
|
|
|
use nebula\application\filesystem\FileSystem; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* 应用程序加载器 |
|
16
|
|
|
* |
|
17
|
|
|
* 负责加载 |
|
18
|
|
|
*/ |
|
19
|
|
|
class Application |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* 应用 |
|
23
|
|
|
* |
|
24
|
|
|
* @var Context |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $context; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* 资源管理 |
|
30
|
|
|
* |
|
31
|
|
|
* @var Resource |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $resource; |
|
34
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* 模块管理器 |
|
38
|
|
|
* |
|
39
|
|
|
* @var Manager |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $manager; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* 应用路径 |
|
45
|
|
|
* |
|
46
|
|
|
* @var string |
|
47
|
|
|
*/ |
|
48
|
|
|
protected $path; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* 数据路径 |
|
52
|
|
|
* |
|
53
|
|
|
* @var string |
|
54
|
|
|
*/ |
|
55
|
|
|
protected $dataPath; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Get 资源管理 |
|
59
|
|
|
* |
|
60
|
|
|
* @return Resource |
|
61
|
|
|
*/ |
|
62
|
|
|
public function getResource() |
|
63
|
|
|
{ |
|
64
|
|
|
return $this->resource; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Set 资源管理 |
|
69
|
|
|
* |
|
70
|
|
|
* @param Resource $resource 资源管理 |
|
71
|
|
|
* |
|
72
|
|
|
* @return self |
|
73
|
|
|
*/ |
|
74
|
|
|
public function setResource(Resource $resource) |
|
75
|
|
|
{ |
|
76
|
|
|
$this->resource = $resource; |
|
77
|
|
|
|
|
78
|
|
|
return $this; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Get 应用 |
|
83
|
|
|
* |
|
84
|
|
|
* @return Context |
|
85
|
|
|
*/ |
|
86
|
|
|
public function getContext() |
|
87
|
|
|
{ |
|
88
|
|
|
return $this->context; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Set 应用 |
|
93
|
|
|
* |
|
94
|
|
|
* @param Context $context 应用 |
|
95
|
|
|
* |
|
96
|
|
|
* @return self |
|
97
|
|
|
*/ |
|
98
|
|
|
public function setContext(Context $context) |
|
99
|
|
|
{ |
|
100
|
|
|
$this->context = $context; |
|
101
|
|
|
|
|
102
|
|
|
return $this; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* 初始化 |
|
107
|
|
|
* |
|
108
|
|
|
* @return void |
|
109
|
|
|
*/ |
|
110
|
|
|
public function initialization() |
|
111
|
|
|
{ |
|
112
|
|
|
$this->initResource(); |
|
113
|
|
|
$this->registerModuleManager(); |
|
114
|
|
|
$this->importClassLoader($this->getConfig('app.import', [])); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* 处理请求 |
|
119
|
|
|
* |
|
120
|
|
|
* @param Request $request |
|
121
|
|
|
* @return boolean |
|
122
|
|
|
*/ |
|
123
|
|
|
public function onRequest(/** @scrutinizer ignore-unused */ Request $request): bool |
|
124
|
|
|
{ |
|
125
|
|
|
return false; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* 处理异常 |
|
130
|
|
|
* |
|
131
|
|
|
* @param Exception $exception |
|
132
|
|
|
* @return boolean |
|
133
|
|
|
*/ |
|
134
|
|
|
public function onException(/** @scrutinizer ignore-unused */Exception $exception): bool |
|
135
|
|
|
{ |
|
136
|
|
|
return false; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* 初始化资源 |
|
141
|
|
|
* |
|
142
|
|
|
* @return void |
|
143
|
|
|
*/ |
|
144
|
|
|
protected function initResource() |
|
145
|
|
|
{ |
|
146
|
|
|
$resource = new Resource; |
|
147
|
|
|
foreach ($this->getConfig('app.resource') as $path) { |
|
148
|
|
|
if (Path::isRelativePath($path)) { |
|
149
|
|
|
$path = Resource::getPathByRelativedPath($path, constant('NEBULA_APP')); |
|
150
|
|
|
} |
|
151
|
|
|
$resource->addResourcePath($path); |
|
152
|
|
|
} |
|
153
|
|
|
$this->setResource($resource); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* 注册模块 |
|
158
|
|
|
* |
|
159
|
|
|
* @return void |
|
160
|
|
|
*/ |
|
161
|
|
|
protected function registerModuleManager() |
|
162
|
|
|
{ |
|
163
|
|
|
$this->manager = new Manager($this->getContext()); |
|
164
|
|
|
$this->manager->registerModule(); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* 导入 ClassLoader 配置 |
|
170
|
|
|
* |
|
171
|
|
|
* @param array $import |
|
172
|
|
|
* @return void |
|
173
|
|
|
*/ |
|
174
|
|
|
public function importClassLoader(array $import) |
|
175
|
|
|
{ |
|
176
|
|
|
foreach ($import as $name => $path) { |
|
177
|
|
|
if (Path::isRelativePath($path)) { |
|
178
|
|
|
$path = Resource::getPathByRelativedPath($path, constant('NEBULA_APP')); |
|
179
|
|
|
} |
|
180
|
|
|
if (\is_numeric($name)) { |
|
181
|
|
|
$this->getContext()->getClassLoader()->addIncludePath($path); |
|
182
|
|
|
} elseif (is_file($path)) { |
|
183
|
|
|
$this->getContext()->getClassLoader()->import($path); |
|
184
|
|
|
} else { |
|
185
|
|
|
$this->getContext()->getClassLoader()->addIncludePath($path, $name); |
|
186
|
|
|
} |
|
187
|
|
|
} |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* 获取配置 |
|
192
|
|
|
* |
|
193
|
|
|
* @param string $name |
|
194
|
|
|
* @param mixed $default |
|
195
|
|
|
* @return mixed |
|
196
|
|
|
*/ |
|
197
|
|
|
public function getConfig(string $name, $default = null) |
|
198
|
|
|
{ |
|
199
|
|
|
return $this->getContext()->getConfig()->get($name, $default); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* Get 模块管理器 |
|
204
|
|
|
* |
|
205
|
|
|
* @return Manager |
|
206
|
|
|
*/ |
|
207
|
|
|
public function getManager() |
|
208
|
|
|
{ |
|
209
|
|
|
return $this->manager; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* Set 模块管理器 |
|
214
|
|
|
* |
|
215
|
|
|
* @param Manager $manager 模块管理器 |
|
216
|
|
|
* |
|
217
|
|
|
* @return self |
|
218
|
|
|
*/ |
|
219
|
|
|
public function setManager(Manager $manager) |
|
220
|
|
|
{ |
|
221
|
|
|
$this->manager = $manager; |
|
222
|
|
|
|
|
223
|
|
|
return $this; |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
/** |
|
227
|
|
|
* Get 应用路径 |
|
228
|
|
|
* |
|
229
|
|
|
* @return string |
|
230
|
|
|
*/ |
|
231
|
|
|
public function getPath() |
|
232
|
|
|
{ |
|
233
|
|
|
return $this->path; |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* Set 应用路径 |
|
238
|
|
|
* |
|
239
|
|
|
* @param string $path 应用路径 |
|
240
|
|
|
* |
|
241
|
|
|
* @return self |
|
242
|
|
|
*/ |
|
243
|
|
|
public function setPath(string $path) |
|
244
|
|
|
{ |
|
245
|
|
|
$this->path = $path; |
|
246
|
|
|
|
|
247
|
|
|
return $this; |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
/** |
|
251
|
|
|
* Get 数据路径 |
|
252
|
|
|
* |
|
253
|
|
|
* @return string |
|
254
|
|
|
*/ |
|
255
|
|
|
public function getDataPath() |
|
256
|
|
|
{ |
|
257
|
|
|
return $this->dataPath; |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
/** |
|
261
|
|
|
* Set 数据路径 |
|
262
|
|
|
* |
|
263
|
|
|
* @param string $dataPath 数据路径 |
|
264
|
|
|
* |
|
265
|
|
|
* @return self |
|
266
|
|
|
*/ |
|
267
|
|
|
public function setDataPath(string $dataPath) |
|
268
|
|
|
{ |
|
269
|
|
|
$this->dataPath = $dataPath; |
|
270
|
|
|
|
|
271
|
|
|
return $this; |
|
272
|
|
|
} |
|
273
|
|
|
} |
|
274
|
|
|
|