1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace suda\application; |
4
|
|
|
|
5
|
|
|
use suda\application\Resource as ApplicationResource; |
6
|
|
|
use suda\framework\arrayobject\ArrayDotAccess; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* 模块名 |
10
|
|
|
*/ |
11
|
|
|
class Module |
12
|
|
|
{ |
13
|
|
|
const LOADED = 1; |
14
|
|
|
const ACTIVE = 2; |
15
|
|
|
const REACHABLE = 3; |
16
|
|
|
const RUNNING = 4; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* 全局唯一名称 |
20
|
|
|
* |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $unique; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* 模块名 |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $name; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* 版本 |
34
|
|
|
* |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
protected $version; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* 资源路径 |
41
|
|
|
* |
42
|
|
|
* @var ApplicationResource |
43
|
|
|
*/ |
44
|
|
|
protected $resource; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* 状态 |
48
|
|
|
* |
49
|
|
|
* @var int |
50
|
|
|
*/ |
51
|
|
|
protected $status; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* 模块配置 |
55
|
|
|
* |
56
|
|
|
* @var array |
57
|
|
|
*/ |
58
|
|
|
protected $config; |
59
|
|
|
|
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @var array |
63
|
|
|
*/ |
64
|
|
|
protected $property; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* 路径 |
68
|
|
|
* |
69
|
|
|
* @var string |
70
|
|
|
*/ |
71
|
|
|
protected $path; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* 创建模块 |
75
|
|
|
* @param string $name |
76
|
|
|
* @param string $version |
77
|
|
|
* @param string $path |
78
|
|
|
* @param array $property |
79
|
|
|
* @param array $config |
80
|
|
|
*/ |
81
|
|
|
public function __construct(string $name, string $version, string $path, array $property, array $config = []) |
82
|
|
|
{ |
83
|
|
|
$this->name = $name; |
84
|
|
|
$this->version = $version; |
85
|
|
|
$this->path = $path; |
86
|
|
|
$this->config = $config; |
87
|
|
|
$this->property = $property; |
88
|
|
|
$this->resource = new ApplicationResource; |
89
|
|
|
$this->status = Module::REACHABLE; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Get 版本 |
94
|
|
|
* |
95
|
|
|
* @return string |
96
|
|
|
*/ |
97
|
|
|
public function getVersion(): string |
98
|
|
|
{ |
99
|
|
|
return $this->version; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Get 模块名 |
104
|
|
|
* |
105
|
|
|
* @return string |
106
|
|
|
*/ |
107
|
|
|
public function getName(): string |
108
|
|
|
{ |
109
|
|
|
return $this->name; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* 获取全名 |
114
|
|
|
* |
115
|
|
|
* @return string |
116
|
|
|
*/ |
117
|
|
|
public function getFullName(): string |
118
|
|
|
{ |
119
|
|
|
return $this->getName() . ':' . $this->getVersion(); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* 获取链接安全名 |
124
|
|
|
* |
125
|
|
|
* @return string |
126
|
|
|
*/ |
127
|
|
|
public function getUriSafeName(): string |
128
|
|
|
{ |
129
|
|
|
return $this->getName() . '/' . $this->getVersion(); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Get 资源路径 |
134
|
|
|
* |
135
|
|
|
* @return ApplicationResource |
136
|
|
|
*/ |
137
|
|
|
public function getResource(): ApplicationResource |
138
|
|
|
{ |
139
|
|
|
return $this->resource; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Set 资源路径 |
144
|
|
|
* |
145
|
|
|
* @param ApplicationResource $resource 资源路径 |
146
|
|
|
* |
147
|
|
|
* @return $this |
148
|
|
|
*/ |
149
|
|
|
public function setResource(ApplicationResource $resource) |
150
|
|
|
{ |
151
|
|
|
$this->resource = $resource; |
152
|
|
|
return $this; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* 设置状态 |
157
|
|
|
* |
158
|
|
|
* @param integer $status |
159
|
|
|
* @return void |
160
|
|
|
*/ |
161
|
|
|
public function setStatus(int $status) |
162
|
|
|
{ |
163
|
|
|
$this->status = $status; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Get 模块配置 |
168
|
|
|
* |
169
|
|
|
* @param string|null $name |
170
|
|
|
* @param mixed $default |
171
|
|
|
* @return mixed |
172
|
|
|
*/ |
173
|
|
|
public function getConfig(string $name = null, $default = null) |
174
|
|
|
{ |
175
|
|
|
if ($name !== null) { |
176
|
|
|
return ArrayDotAccess::get($this->config, $name, $default); |
177
|
|
|
} |
178
|
|
|
return $this->config; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @param string|null $name |
183
|
|
|
* @param mixed $default |
184
|
|
|
* @return mixed |
185
|
|
|
*/ |
186
|
|
|
public function getProperty(string $name = null, $default = null) |
187
|
|
|
{ |
188
|
|
|
if ($name !== null) { |
189
|
|
|
return ArrayDotAccess::get($this->property, $name, $default); |
190
|
|
|
} |
191
|
|
|
return $this->property; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @param string $name |
196
|
|
|
*/ |
197
|
|
|
public function setName(string $name): void |
198
|
|
|
{ |
199
|
|
|
$this->name = $name; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @param string $version |
204
|
|
|
*/ |
205
|
|
|
public function setVersion(string $version): void |
206
|
|
|
{ |
207
|
|
|
$this->version = $version; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @param array $config |
212
|
|
|
*/ |
213
|
|
|
public function setConfig(array $config): void |
214
|
|
|
{ |
215
|
|
|
$this->config = $config; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* @param array $property |
220
|
|
|
*/ |
221
|
|
|
public function setProperty(array $property): void |
222
|
|
|
{ |
223
|
|
|
$this->property = $property; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* @param string $path |
228
|
|
|
*/ |
229
|
|
|
public function setPath(string $path): void |
230
|
|
|
{ |
231
|
|
|
$this->path = $path; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* @return int |
236
|
|
|
*/ |
237
|
|
|
public function getStatus(): int |
238
|
|
|
{ |
239
|
|
|
return $this->status; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* @return string |
244
|
|
|
*/ |
245
|
|
|
public function getPath(): string |
246
|
|
|
{ |
247
|
|
|
return $this->path; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* @return string |
252
|
|
|
*/ |
253
|
|
|
public function getUnique(): string |
254
|
|
|
{ |
255
|
|
|
return strlen($this->unique) > 0 ? $this->unique : $this->getFullName(); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* @param string $unique |
260
|
|
|
*/ |
261
|
|
|
public function setUnique(string $unique): void |
262
|
|
|
{ |
263
|
|
|
$this->unique = $unique; |
264
|
|
|
} |
265
|
|
|
} |
266
|
|
|
|