1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace suda\application\loader; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use suda\framework\Config; |
7
|
|
|
use suda\application\Module; |
8
|
|
|
use suda\framework\filesystem\FileSystem; |
9
|
|
|
use suda\framework\loader\Loader; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* 应用程序 |
13
|
|
|
*/ |
14
|
|
|
class ModuleLoader extends ModuleLoaderUtil |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* 将模块设置为加载状态 |
18
|
|
|
* - 载入共享代码 |
19
|
|
|
* - 载入其他依赖 |
20
|
|
|
*/ |
21
|
|
|
public function toLoad() |
22
|
|
|
{ |
23
|
|
|
$this->loadVendorIfExist(); |
24
|
|
|
$this->loadShareLibrary(); |
25
|
|
|
$this->application->debug()->debug('loaded - ' . $this->module->getFullName()); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* 将模块激活 |
30
|
|
|
* - 载入模块配置文件 |
31
|
|
|
* - 载入事件监控 |
32
|
|
|
*/ |
33
|
|
|
public function toActive() |
34
|
|
|
{ |
35
|
|
|
$this->loadConfig(); |
36
|
|
|
$this->application->debug()->debug('active = ' . $this->module->getFullName()); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* 载入模块私有代码库 |
41
|
|
|
*/ |
42
|
|
|
public function toRunning() |
43
|
|
|
{ |
44
|
|
|
$this->checkRequirements(); |
45
|
|
|
$this->loadPrivateLibrary(); |
46
|
|
|
$this->application->setRunning($this->module); |
47
|
|
|
$this->application->debug()->debug('run + ' . $this->module->getFullName()); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* 加载模块额外资源 |
52
|
|
|
*/ |
53
|
|
|
public function loadExtraModuleResourceLibrary() |
54
|
|
|
{ |
55
|
|
|
$resource = $this->module->getProperty('module-resource', []); |
56
|
|
|
if (count($resource)) { |
57
|
|
|
$parent = $this->module->getPath(); |
58
|
|
|
foreach ($resource as $name => $path) { |
59
|
|
|
if ($find = $this->application->find($name)) { |
60
|
|
|
$find->getResource()->registerResourcePath($parent, $path); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* 注册共享库自动加载 |
69
|
|
|
*/ |
70
|
|
|
protected function loadShareLibrary() |
71
|
|
|
{ |
72
|
|
|
$import = $this->module->getProperty('import.share', []); |
73
|
|
|
if (count($import)) { |
74
|
|
|
$this->importClassLoader($import, $this->module->getPath()); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* 注册私有库自动加载 |
80
|
|
|
*/ |
81
|
|
|
protected function loadPrivateLibrary() |
82
|
|
|
{ |
83
|
|
|
$import = $this->module->getProperty('import.src', []); |
84
|
|
|
if (count($import)) { |
85
|
|
|
$this->importClassLoader($import, $this->module->getPath()); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* 载入依赖自动加载 |
91
|
|
|
*/ |
92
|
|
|
public function loadVendorIfExist() |
93
|
|
|
{ |
94
|
|
|
$vendorAutoload = $this->module->getPath() . '/vendor/autoload.php'; |
95
|
|
|
if (FileSystem::exist($vendorAutoload)) { |
96
|
|
|
Loader::requireOnce($vendorAutoload); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* 载入模块配置 |
102
|
|
|
*/ |
103
|
|
|
protected function loadConfig() |
104
|
|
|
{ |
105
|
|
|
$this->loadModuleConfig($this->module); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* 载入模块配置 |
110
|
|
|
* - 基础配置文件 config |
111
|
|
|
* - 事件监控器 |
112
|
|
|
* @param Module $module |
113
|
|
|
*/ |
114
|
|
|
protected function loadModuleConfig(Module $module) |
115
|
|
|
{ |
116
|
|
|
$this->loadBaseConfig($module); |
117
|
|
|
$this->loadEventListener($module); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param Module $module |
122
|
|
|
*/ |
123
|
|
|
protected function loadBaseConfig(Module $module) |
124
|
|
|
{ |
125
|
|
|
$path = $module->getResource()->getConfigResourcePath('config/config'); |
126
|
|
|
if ($path !== null && ($config = Config::loadConfig($path, $module->getProperty())) !== null) { |
127
|
|
|
$module->setConfig($config); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param Module $module |
133
|
|
|
*/ |
134
|
|
|
protected function loadEventListener(Module $module) |
135
|
|
|
{ |
136
|
|
|
if ($path = $module->getResource()->getConfigResourcePath('config/event')) { |
137
|
|
|
$event = Config::loadConfig($path, [ |
138
|
|
|
'module' => $this->module->getName(), |
139
|
|
|
'property' => $this->module->getProperty(), |
140
|
|
|
'config' => $this->module->getConfig(), |
141
|
|
|
]); |
142
|
|
|
if (is_array($event)) { |
143
|
|
|
$this->application->event()->load($event); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|