1
|
|
|
<?php |
2
|
|
|
namespace suda\application; |
3
|
|
|
|
4
|
|
|
use function array_key_exists; |
5
|
|
|
use function sprintf; |
6
|
|
|
use suda\framework\loader\Loader; |
7
|
|
|
use suda\application\exception\ApplicationException; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* 基础应用程序 |
11
|
|
|
*/ |
12
|
|
|
class ApplicationBase extends ApplicationContext |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* 模块集合 |
16
|
|
|
* |
17
|
|
|
* @var ModuleBag |
18
|
|
|
*/ |
19
|
|
|
protected $module; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* 字符串包 |
23
|
|
|
* |
24
|
|
|
* @var LanguageBag |
25
|
|
|
*/ |
26
|
|
|
protected $language; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* 模块路径 |
30
|
|
|
* |
31
|
|
|
* @var string[] |
32
|
|
|
*/ |
33
|
|
|
protected $modulePaths; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* 运行的模块 |
37
|
|
|
* |
38
|
|
|
* @var Module |
39
|
|
|
*/ |
40
|
|
|
protected $running; |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* 创建应用 |
45
|
|
|
* |
46
|
|
|
* @param string $path |
47
|
|
|
* @param array $manifest |
48
|
|
|
* @param Loader $loader |
49
|
|
|
* @param string|null $dataPath |
50
|
|
|
*/ |
51
|
|
|
public function __construct(string $path, array $manifest, Loader $loader, ?string $dataPath = null) |
52
|
|
|
{ |
53
|
|
|
parent::__construct($path, $manifest, $loader, $dataPath); |
54
|
|
|
$this->module = new ModuleBag; |
55
|
|
|
$this->initProperty($manifest); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* 添加模块 |
60
|
|
|
* |
61
|
|
|
* @param Module $module |
62
|
|
|
* @return void |
63
|
|
|
*/ |
64
|
|
|
public function add(Module $module) |
65
|
|
|
{ |
66
|
|
|
$this->module->add($module); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* 查找模块 |
71
|
|
|
* |
72
|
|
|
* @param string $name |
73
|
|
|
* @return Module|null |
74
|
|
|
*/ |
75
|
|
|
public function find(string $name):?Module |
76
|
|
|
{ |
77
|
|
|
return $this->module->get($name); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* 获取模块 |
82
|
|
|
* |
83
|
|
|
* @param string $name |
84
|
|
|
* @return Module |
85
|
|
|
*/ |
86
|
|
|
public function get(string $name): Module |
87
|
|
|
{ |
88
|
|
|
if (($module = $this->find($name)) !== null) { |
89
|
|
|
return $module; |
90
|
|
|
} |
91
|
|
|
throw new ApplicationException(sprintf('module %s not exist', $name), ApplicationException::ERR_MODULE_NAME); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* 初始化属性 |
96
|
|
|
* |
97
|
|
|
* @param array $manifest |
98
|
|
|
* @return void |
99
|
|
|
*/ |
100
|
|
|
protected function initProperty(array $manifest) |
101
|
|
|
{ |
102
|
|
|
if (array_key_exists('module-paths', $manifest)) { |
103
|
|
|
$modulePaths = $manifest['module-paths']; |
104
|
|
|
foreach ($modulePaths as $name => $path) { |
105
|
|
|
$this->modulePaths[] = Resource::getPathByRelativePath($path, $this->path); |
106
|
|
|
} |
107
|
|
|
} else { |
108
|
|
|
$this->modulePaths = [ Resource::getPathByRelativePath('modules', $this->path) ]; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Get 模块路径 |
114
|
|
|
* |
115
|
|
|
* @return string[] |
116
|
|
|
*/ |
117
|
|
|
public function getModulePaths() |
118
|
|
|
{ |
119
|
|
|
return $this->modulePaths; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* 语言翻译 |
125
|
|
|
* |
126
|
|
|
* @param string|null $message |
127
|
|
|
* @param mixed ...$args |
128
|
|
|
* @return string |
129
|
|
|
*/ |
130
|
|
|
public function _(?string $message, ...$args):string |
131
|
|
|
{ |
132
|
|
|
return $this->language->interpolate($message, ...$args); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Get 字符串包 |
137
|
|
|
* |
138
|
|
|
* @return LanguageBag |
139
|
|
|
*/ |
140
|
|
|
public function getLanguage() |
141
|
|
|
{ |
142
|
|
|
return $this->language; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Set 字符串包 |
147
|
|
|
* |
148
|
|
|
* @param LanguageBag $language 字符串包 |
149
|
|
|
* |
150
|
|
|
* @return self |
151
|
|
|
*/ |
152
|
|
|
public function setLanguage(LanguageBag $language) |
153
|
|
|
{ |
154
|
|
|
$this->language = $language; |
155
|
|
|
|
156
|
|
|
return $this; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Get 运行的模块 |
161
|
|
|
* |
162
|
|
|
* @return Module |
163
|
|
|
*/ |
164
|
|
|
public function getRunning() |
165
|
|
|
{ |
166
|
|
|
return $this->running; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @param Module $running |
172
|
|
|
* @return $this |
173
|
|
|
*/ |
174
|
|
|
public function setRunning(Module $running) |
175
|
|
|
{ |
176
|
|
|
$this->running = $running; |
177
|
|
|
return $this; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Get 模块集合 |
182
|
|
|
* |
183
|
|
|
* @return ModuleBag |
184
|
|
|
*/ |
185
|
|
|
public function getModules():ModuleBag |
186
|
|
|
{ |
187
|
|
|
return $this->module; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Set 模块集合 |
192
|
|
|
* |
193
|
|
|
* @param ModuleBag $module 模块集合 |
194
|
|
|
* |
195
|
|
|
* @return self |
196
|
|
|
*/ |
197
|
|
|
public function setModule(ModuleBag $module) |
198
|
|
|
{ |
199
|
|
|
$this->module = $module; |
200
|
|
|
|
201
|
|
|
return $this; |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|