|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace suda\application\loader; |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
use suda\application\Application; |
|
8
|
|
|
use suda\application\builder\ApplicationBuilder; |
|
9
|
|
|
use suda\application\exception\ApplicationException; |
|
10
|
|
|
use suda\application\Module; |
|
11
|
|
|
|
|
12
|
|
|
class ModuleLoaderUtil |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* 应用程序 |
|
16
|
|
|
* |
|
17
|
|
|
* @var Application |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $application; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* 运行环境 |
|
23
|
|
|
* |
|
24
|
|
|
* @var Module |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $module; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* 模块加载器 |
|
30
|
|
|
* |
|
31
|
|
|
* @param Application $application |
|
32
|
|
|
* @param Module $module |
|
33
|
|
|
*/ |
|
34
|
|
|
public function __construct(Application $application, Module $module) |
|
35
|
|
|
{ |
|
36
|
|
|
$this->module = $module; |
|
37
|
|
|
$this->application = $application; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* 检查依赖项目 |
|
42
|
|
|
* |
|
43
|
|
|
* @return void |
|
44
|
|
|
*/ |
|
45
|
|
|
protected function checkRequirements() |
|
46
|
|
|
{ |
|
47
|
|
|
$this->checkFrameworkVersion(); |
|
48
|
|
|
if ($require = $this->module->getProperty('require')) { |
|
49
|
|
|
foreach ($require as $module => $version) { |
|
50
|
|
|
$this->checkModuleRequirements($module, $version); |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param string $module |
|
57
|
|
|
* @param string $version |
|
58
|
|
|
*/ |
|
59
|
|
|
private function checkModuleRequirements(string $module, string $version) |
|
60
|
|
|
{ |
|
61
|
|
|
$target = $this->application->find($module); |
|
62
|
|
|
if ($target === null) { |
|
63
|
|
|
throw new ApplicationException( |
|
64
|
|
|
sprintf('%s module need %s %s but not exist', $this->module->getFullName(), $module, $version), |
|
65
|
|
|
ApplicationException::ERR_MODULE_REQUIREMENTS |
|
66
|
|
|
); |
|
67
|
|
|
} |
|
68
|
|
|
if (static::versionCompare($version, $target->getVersion()) !== true) { |
|
69
|
|
|
throw new ApplicationException( |
|
70
|
|
|
sprintf( |
|
71
|
|
|
'%s module need %s version %s', |
|
72
|
|
|
$this->module->getFullName(), |
|
73
|
|
|
$target->getName(), |
|
74
|
|
|
$target->getVersion() |
|
75
|
|
|
), |
|
76
|
|
|
ApplicationException::ERR_MODULE_REQUIREMENTS |
|
77
|
|
|
); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* 检查模块需求 |
|
83
|
|
|
* |
|
84
|
|
|
* @return void |
|
85
|
|
|
*/ |
|
86
|
|
|
protected function checkFrameworkVersion() |
|
87
|
|
|
{ |
|
88
|
|
|
if ($version = $this->module->getProperty('suda')) { |
|
89
|
|
|
if (static::versionCompare($version, SUDA_VERSION) !== true) { |
|
90
|
|
|
throw new ApplicationException( |
|
91
|
|
|
sprintf('%s module need suda version %s', $this->module->getFullName(), $version), |
|
92
|
|
|
ApplicationException::ERR_FRAMEWORK_VERSION |
|
93
|
|
|
); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* 导入 ClassLoader 配置 |
|
100
|
|
|
* |
|
101
|
|
|
* @param array $import |
|
102
|
|
|
* @param string $relativePath |
|
103
|
|
|
* @return void |
|
104
|
|
|
*/ |
|
105
|
|
|
protected function importClassLoader(array $import, string $relativePath) |
|
106
|
|
|
{ |
|
107
|
|
|
ApplicationBuilder::importClassLoader($this->application->loader(), $import, $relativePath); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* 比较版本 |
|
112
|
|
|
* |
|
113
|
|
|
* @param string $version 比较用的版本,包含比较符号 |
|
114
|
|
|
* @param string $compire 对比的版本 |
|
115
|
|
|
* @return bool |
|
116
|
|
|
*/ |
|
117
|
|
|
public static function versionCompare(string $version, string $compire) |
|
118
|
|
|
{ |
|
119
|
|
|
if (preg_match('/^(<=?|>=?|<>|!=)(.+)$/i', $version, $match)) { |
|
120
|
|
|
list($s, $op, $ver) = $match; |
|
121
|
|
|
return version_compare($compire, $ver, $op); |
|
122
|
|
|
} |
|
123
|
|
|
return version_compare($compire, $version, '>='); |
|
124
|
|
|
} |
|
125
|
|
|
} |