1
|
|
|
<?php |
2
|
|
|
namespace nebula; |
3
|
|
|
|
4
|
|
|
use nebula\application\Route; |
5
|
|
|
use nebula\application\Debugger; |
6
|
|
|
use nebula\application\Resource; |
7
|
|
|
use nebula\component\debug\Debug; |
8
|
|
|
use nebula\component\loader\Path; |
9
|
|
|
use nebula\application\Application; |
10
|
|
|
use nebula\component\loader\Loader; |
11
|
|
|
use nebula\application\config\Config; |
12
|
|
|
use nebula\component\runnable\Runnable; |
13
|
|
|
use nebula\application\filesystem\FileSystem; |
14
|
|
|
use nebula\component\debug\log\logger\FileLogger; |
15
|
|
|
use nebula\component\debug\log\logger\NullLogger; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* 应用 |
19
|
|
|
* |
20
|
|
|
*/ |
21
|
|
|
class NebulaApplication |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* 应用路径 |
25
|
|
|
* |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected $path; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* 类加载器 |
32
|
|
|
* |
33
|
|
|
* @var Loader |
34
|
|
|
*/ |
35
|
|
|
protected $classLoader; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* 调试工具 |
39
|
|
|
* |
40
|
|
|
* @var Debug |
41
|
|
|
*/ |
42
|
|
|
protected $debugger; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* 路由 |
46
|
|
|
* |
47
|
|
|
* @var Route |
48
|
|
|
*/ |
49
|
|
|
protected $route; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* 配置信息 |
53
|
|
|
* |
54
|
|
|
* @var Config |
55
|
|
|
*/ |
56
|
|
|
protected $config; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* 应用引用程序 |
60
|
|
|
* |
61
|
|
|
* @var Application |
62
|
|
|
*/ |
63
|
|
|
protected $application; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* 静态实例 |
67
|
|
|
* |
68
|
|
|
* @var NebulaApplication |
69
|
|
|
*/ |
70
|
|
|
protected static $instance; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* 返回实例 |
74
|
|
|
* |
75
|
|
|
* @return NebulaApplication |
76
|
|
|
*/ |
77
|
|
|
public static function instance() |
78
|
|
|
{ |
79
|
|
|
if (isset(static::$instance)) { |
80
|
|
|
return static::$instance; |
81
|
|
|
} |
82
|
|
|
return static::$instance = new static; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Get 类加载器 |
87
|
|
|
* |
88
|
|
|
* @return Loader |
89
|
|
|
*/ |
90
|
|
|
public function getClassLoader() |
91
|
|
|
{ |
92
|
|
|
return $this->classLoader; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Set 类加载器 |
97
|
|
|
* |
98
|
|
|
* @param Loader $classLoader 类加载器 |
99
|
|
|
* |
100
|
|
|
* @return self |
101
|
|
|
*/ |
102
|
|
|
public function setClassLoader(Loader $classLoader) |
103
|
|
|
{ |
104
|
|
|
$this->classLoader = $classLoader; |
105
|
|
|
|
106
|
|
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Get 调试工具 |
111
|
|
|
* |
112
|
|
|
* @return Debug |
113
|
|
|
*/ |
114
|
|
|
public function getDebugger() |
115
|
|
|
{ |
116
|
|
|
return $this->debugger; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Set 调试工具 |
121
|
|
|
* |
122
|
|
|
* @param Debug $debugger 调试工具 |
123
|
|
|
* |
124
|
|
|
* @return self |
125
|
|
|
*/ |
126
|
|
|
public function setDebugger(Debug $debugger) |
127
|
|
|
{ |
128
|
|
|
$this->debugger = $debugger; |
129
|
|
|
|
130
|
|
|
return $this; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Get 配置信息 |
136
|
|
|
* |
137
|
|
|
* @return Config |
138
|
|
|
*/ |
139
|
|
|
public function getConfig() |
140
|
|
|
{ |
141
|
|
|
return $this->config; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Set 配置信息 |
146
|
|
|
* |
147
|
|
|
* @param Config $config 配置信息 |
148
|
|
|
* |
149
|
|
|
* @return self |
150
|
|
|
*/ |
151
|
|
|
public function setConfig(Config $config) |
152
|
|
|
{ |
153
|
|
|
$this->config = $config; |
154
|
|
|
|
155
|
|
|
return $this; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Get 路由 |
160
|
|
|
* |
161
|
|
|
* @return Route |
162
|
|
|
*/ |
163
|
|
|
public function getRoute() |
164
|
|
|
{ |
165
|
|
|
return $this->route; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Set 路由 |
170
|
|
|
* |
171
|
|
|
* @param Route $route 路由 |
172
|
|
|
* |
173
|
|
|
* @return self |
174
|
|
|
*/ |
175
|
|
|
public function setRoute(Route $route) |
176
|
|
|
{ |
177
|
|
|
$this->route = $route; |
178
|
|
|
|
179
|
|
|
return $this; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
public static function configuration(string $path) |
183
|
|
|
{ |
184
|
|
|
$nebula = static::instance(); |
185
|
|
|
$nebula->path = $path; |
186
|
|
|
if (!FileSystem::exist($path)) { |
187
|
|
|
FileSystem::copyDir(NEBULA_RESOURCE.'/application', $path); |
188
|
|
|
} |
189
|
|
|
$config = new Config; |
190
|
|
|
$manifast = $config->loadConfig($path.'/manifast.json'); |
191
|
|
|
$config->set('app', $manifast); |
192
|
|
|
$nebula->setConfig($config); |
193
|
|
|
$nebula->initiation(); |
194
|
|
|
return $nebula; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* 初始化操作 |
199
|
|
|
* |
200
|
|
|
* @return void |
201
|
|
|
*/ |
202
|
|
|
protected function initiation() |
203
|
|
|
{ |
204
|
|
|
$this->initDebugger(); |
205
|
|
|
$this->getDebugger()->info('initiation nebula application'); |
206
|
|
|
$this->initBaseConfig(); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* 初始化路由组 |
212
|
|
|
* |
213
|
|
|
* @return void |
214
|
|
|
*/ |
215
|
|
|
private function initBaseConfig() |
216
|
|
|
{ |
217
|
|
|
if (!$this->config->has('app.route.active')) { |
218
|
|
|
$this->config->set('app.route.active', ['default']); |
219
|
|
|
} |
220
|
|
|
if (defined('NEBULA_ROUTE_GROUPS')) { |
221
|
|
|
$this->config->set('app.route.active', \explode(',', constant('NEBULA_ROUTE_GROUPS'))); |
222
|
|
|
} |
223
|
|
|
if (!$this->config->has('app.import')) { |
224
|
|
|
$this->config->set('app.import', [ |
225
|
|
|
0 => \constant('NEBULA_APP').'/' .'share', |
226
|
|
|
]); |
227
|
|
|
} |
228
|
|
|
if (!$this->config->has('app.resource')) { |
229
|
|
|
$this->config->set('app.resource', [ |
230
|
|
|
0 => \constant('NEBULA_APP').'/' .'resource', |
231
|
|
|
]); |
232
|
|
|
} |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
|
236
|
|
|
private function initDebugger() |
237
|
|
|
{ |
238
|
|
|
$dataPath = Path::toAbsolutePath(defined('NEBULA_DATA') ? constant('NEBULA_DATA') : '~/nebula-data'); |
239
|
|
|
if (\is_writable(dirname($dataPath))) { |
240
|
|
|
$logger = new FileLogger( |
241
|
|
|
[ |
242
|
|
|
'save-path' => $dataPath.'/logs', |
243
|
|
|
'save-zip-path' => $dataPath.'/logs/zip', |
244
|
|
|
'log-format' => '%message%', |
245
|
|
|
'save-pack-path' => $dataPath.'/logs/dump', |
246
|
|
|
] |
247
|
|
|
); |
248
|
|
|
FileSystem::makes($dataPath.'/logs/zip'); |
249
|
|
|
FileSystem::makes($dataPath.'/logs/dump'); |
250
|
|
|
} else { |
251
|
|
|
$logger = new NullLogger; |
252
|
|
|
} |
253
|
|
|
$debugger = new Debugger; |
254
|
|
|
$debugger->setLogger($logger); |
255
|
|
|
$this->setDebugger($debugger); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* 运行应用 |
261
|
|
|
* |
262
|
|
|
* @return void |
263
|
|
|
*/ |
264
|
|
|
public function run() |
265
|
|
|
{ |
266
|
|
|
$className = $this->config->get('app.application', Application::class); |
267
|
|
|
$this->application = Runnable::newClassInstance($className); |
268
|
|
|
$this->application->setNebula($this); |
269
|
|
|
$this->application->initiation(); |
270
|
|
|
} |
271
|
|
|
} |
272
|
|
|
|