1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace suda\application; |
4
|
|
|
|
5
|
|
|
use suda\framework\loader\Loader; |
6
|
|
|
use suda\database\exception\SQLException; |
7
|
|
|
use suda\application\loader\ApplicationLoader; |
8
|
|
|
use suda\application\loader\ApplicationBaseLoader; |
9
|
|
|
use suda\application\exception\ApplicationException; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* 模块化应用 |
13
|
|
|
*/ |
14
|
|
|
class ApplicationModule extends ApplicationContext |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* 模块集合 |
19
|
|
|
* |
20
|
|
|
* @var ModuleBag |
21
|
|
|
*/ |
22
|
|
|
protected $module; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* 字符串包 |
26
|
|
|
* |
27
|
|
|
* @var LanguageBag |
28
|
|
|
*/ |
29
|
|
|
protected $language; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* 模块路径 |
33
|
|
|
* |
34
|
|
|
* @var string[] |
35
|
|
|
*/ |
36
|
|
|
protected $modulePaths; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* 运行的模块 |
40
|
|
|
* |
41
|
|
|
* @var Module |
42
|
|
|
*/ |
43
|
|
|
protected $running; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* 创建应用 |
47
|
|
|
* |
48
|
|
|
* @param string $path |
49
|
|
|
* @param array $manifest |
50
|
|
|
* @param Loader $loader |
51
|
|
|
* @param string|null $dataPath |
52
|
|
|
*/ |
53
|
|
|
public function __construct(string $path, array $manifest, Loader $loader, ?string $dataPath = null) |
54
|
|
|
{ |
55
|
|
|
parent::__construct($path, $manifest, $loader, $dataPath); |
56
|
|
|
$this->module = new ModuleBag; |
57
|
|
|
$this->event = new Event($this); |
58
|
|
|
$this->initModulePath($manifest); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* 准备运行环境 |
63
|
|
|
* |
64
|
|
|
* @return ApplicationLoader |
65
|
|
|
* @throws SQLException |
66
|
|
|
*/ |
67
|
|
|
public function load() |
68
|
|
|
{ |
69
|
|
|
$appLoader = new ApplicationLoader($this); |
70
|
|
|
$this->debug->time('loading application'); |
71
|
|
|
$appLoader->load(); |
72
|
|
|
$this->event->exec('application:load-config', [$this->config, $this]); |
73
|
|
|
$boot = $this->debug->timeEnd('loading application'); |
74
|
|
|
$this->debug->time('loading data-source'); |
75
|
|
|
$appLoader->loadDataSource(); |
76
|
|
|
$load = $this->debug->timeEnd('loading data-source'); |
77
|
|
|
$this->debug->recordTiming('boot', $boot + $load); |
78
|
|
|
$this->event->exec('application:load-environment', [$this->config, $this]); |
79
|
|
|
return $appLoader; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* 添加模块 |
84
|
|
|
* |
85
|
|
|
* @param Module $module |
86
|
|
|
* @return void |
87
|
|
|
*/ |
88
|
|
|
public function add(Module $module) |
89
|
|
|
{ |
90
|
|
|
$this->module->add($module); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* 查找模块 |
95
|
|
|
* |
96
|
|
|
* @param string $name |
97
|
|
|
* @return Module|null |
98
|
|
|
*/ |
99
|
|
|
public function find(string $name): ?Module |
100
|
|
|
{ |
101
|
|
|
return $this->module->get($name); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* 获取模块 |
106
|
|
|
* |
107
|
|
|
* @param string $name |
108
|
|
|
* @return Module |
109
|
|
|
*/ |
110
|
|
|
public function get(string $name): Module |
111
|
|
|
{ |
112
|
|
|
if (($module = $this->find($name)) !== null) { |
113
|
|
|
return $module; |
114
|
|
|
} |
115
|
|
|
throw new ApplicationException(sprintf('module %s not exist', $name), ApplicationException::ERR_MODULE_NAME); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* 初始化属性 |
120
|
|
|
* |
121
|
|
|
* @param array $manifest |
122
|
|
|
* @return void |
123
|
|
|
*/ |
124
|
|
|
protected function initModulePath(array $manifest) |
125
|
|
|
{ |
126
|
|
|
if (array_key_exists('module-paths', $manifest)) { |
127
|
|
|
$modulePaths = $manifest['module-paths']; |
128
|
|
|
foreach ($modulePaths as $name => $path) { |
129
|
|
|
$this->modulePaths[] = Resource::getPathByRelativePath($path, $this->path); |
130
|
|
|
} |
131
|
|
|
} else { |
132
|
|
|
$this->modulePaths = [Resource::getPathByRelativePath('modules', $this->path)]; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Get 模块路径 |
138
|
|
|
* |
139
|
|
|
* @return string[] |
140
|
|
|
*/ |
141
|
|
|
public function getModulePaths() |
142
|
|
|
{ |
143
|
|
|
return $this->modulePaths; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* 语言翻译 |
149
|
|
|
* |
150
|
|
|
* @param string|null $message |
151
|
|
|
* @param mixed ...$args |
152
|
|
|
* @return string |
153
|
|
|
*/ |
154
|
|
|
public function _(?string $message, ...$args): string |
155
|
|
|
{ |
156
|
|
|
return $this->language->interpolate($message, ...$args); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Get 字符串包 |
161
|
|
|
* |
162
|
|
|
* @return LanguageBag |
163
|
|
|
*/ |
164
|
|
|
public function getLanguage() |
165
|
|
|
{ |
166
|
|
|
return $this->language; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Set 字符串包 |
171
|
|
|
* |
172
|
|
|
* @param LanguageBag $language 字符串包 |
173
|
|
|
* |
174
|
|
|
* @return self |
175
|
|
|
*/ |
176
|
|
|
public function setLanguage(LanguageBag $language) |
177
|
|
|
{ |
178
|
|
|
$this->language = $language; |
179
|
|
|
|
180
|
|
|
return $this; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Get 运行的模块 |
185
|
|
|
* |
186
|
|
|
* @return Module |
187
|
|
|
*/ |
188
|
|
|
public function getRunning() |
189
|
|
|
{ |
190
|
|
|
return $this->running; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @param Module $running |
196
|
|
|
* @return $this |
197
|
|
|
*/ |
198
|
|
|
public function setRunning(Module $running) |
199
|
|
|
{ |
200
|
|
|
$this->running = $running; |
201
|
|
|
return $this; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Get 模块集合 |
206
|
|
|
* |
207
|
|
|
* @return ModuleBag |
208
|
|
|
*/ |
209
|
|
|
public function getModules(): ModuleBag |
210
|
|
|
{ |
211
|
|
|
return $this->module; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Set 模块集合 |
216
|
|
|
* |
217
|
|
|
* @param ModuleBag $module 模块集合 |
218
|
|
|
* |
219
|
|
|
* @return self |
220
|
|
|
*/ |
221
|
|
|
public function setModule(ModuleBag $module) |
222
|
|
|
{ |
223
|
|
|
$this->module = $module; |
224
|
|
|
|
225
|
|
|
return $this; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* 解析资源名 |
230
|
|
|
* |
231
|
|
|
* @param string $name |
232
|
|
|
* @param string|null $default |
233
|
|
|
* @param string|null $groupName |
234
|
|
|
* @return array |
235
|
|
|
*/ |
236
|
|
|
public function parseSourceName(string $name, ?string $default = null, ?string $groupName = null) |
237
|
|
|
{ |
238
|
|
|
if (strpos($name, ':') !== false) { |
239
|
|
|
$pos = strrpos($name, ':'); |
240
|
|
|
$module = substr($name, 0, $pos); |
241
|
|
|
$name = substr($name, $pos + 1); |
242
|
|
|
if (strlen($module) === 0) { |
243
|
|
|
$module = $default; |
244
|
|
|
} |
245
|
|
|
} else { |
246
|
|
|
$module = $default; |
247
|
|
|
} |
248
|
|
|
if ($module !== null && strpos($module, '@') !== false) { |
249
|
|
|
list($module, $groupName) = explode('@', $module, 2); |
250
|
|
|
$module = strlen($module) ? $module : $default; |
251
|
|
|
} |
252
|
|
|
return [$module, $groupName, $name]; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* 获取模板下的资源名 |
257
|
|
|
* |
258
|
|
|
* @param string $name |
259
|
|
|
* @param string|null $default |
260
|
|
|
* @return string |
261
|
|
|
*/ |
262
|
|
|
public function getModuleSourceName(string $name, ?string $default = null): string |
263
|
|
|
{ |
264
|
|
|
if (strpos($name, ':') > 0) { |
265
|
|
|
list($module, $group, $name) = $this->parseSourceName($name, $default); |
266
|
|
|
} else { |
267
|
|
|
$module = $default; |
268
|
|
|
} |
269
|
|
|
if ($module !== null && ($moduleObj = $this->find($module))) { |
270
|
|
|
return $moduleObj->getFullName() . ':' . $name; |
271
|
|
|
} |
272
|
|
|
return $name; |
273
|
|
|
} |
274
|
|
|
} |
275
|
|
|
|