| Total Complexity | 42 |
| Total Lines | 277 |
| Duplicated Lines | 0 % |
| Changes | 6 | ||
| Bugs | 0 | Features | 0 |
Complex classes like ApplicationLoader often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ApplicationLoader, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class ApplicationLoader |
||
| 22 | { |
||
| 23 | /** |
||
| 24 | * 应用程序 |
||
| 25 | * |
||
| 26 | * @var Application |
||
| 27 | */ |
||
| 28 | protected $application; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * 模块加载器 |
||
| 32 | * |
||
| 33 | * @var ModuleLoader[] |
||
| 34 | */ |
||
| 35 | protected $moduleLoader; |
||
| 36 | |||
| 37 | |||
| 38 | /** |
||
| 39 | * @var array |
||
| 40 | */ |
||
| 41 | protected $actionableModules; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var array |
||
| 45 | */ |
||
| 46 | protected $reachableModules; |
||
| 47 | |||
| 48 | public function __construct(Application $application) |
||
| 49 | { |
||
| 50 | $this->application = $application; |
||
| 51 | $this->actionableModules = []; |
||
| 52 | $this->reachableModules = []; |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * 加载APP |
||
| 57 | */ |
||
| 58 | public function load() |
||
| 59 | { |
||
| 60 | $this->loadVendorIfExist(); |
||
| 61 | $this->loadGlobalConfig(); |
||
| 62 | $this->loadModule(); |
||
| 63 | $this->prepareModule(); |
||
| 64 | $this->activeModule(); |
||
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
| 68 | * 加载模块 |
||
| 69 | */ |
||
| 70 | protected function loadModule() |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * 调试模式 |
||
| 94 | * |
||
| 95 | * @return bool |
||
| 96 | */ |
||
| 97 | public static function isDebug() |
||
| 98 | { |
||
| 99 | return boolval(defined('SUDA_DEBUG') ? constant('SUDA_DEBUG') : false); |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * 加载路由 |
||
| 104 | */ |
||
| 105 | public function loadRoute() |
||
| 127 | } |
||
| 128 | |||
| 129 | protected function loadRouteFromModules() |
||
| 130 | { |
||
| 131 | foreach ($this->application->getModules() as $name => $module) { |
||
| 132 | if ($module->getStatus() === Module::REACHABLE) { |
||
| 133 | call_user_func([$this->moduleLoader[$name], 'toReachable']); |
||
| 134 | } |
||
| 135 | } |
||
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * 加载全局配置 |
||
| 140 | */ |
||
| 141 | public function loadGlobalConfig() |
||
| 142 | { |
||
| 143 | $resource = $this->application->getResource(); |
||
| 144 | if ($configPath = $resource->getConfigResourcePath('config/config')) { |
||
| 145 | $this->application->getConfig()->load($configPath); |
||
| 146 | } |
||
| 147 | if ($listenerPath = $resource->getConfigResourcePath('config/listener')) { |
||
| 148 | $this->application->loadEvent($listenerPath); |
||
| 149 | } |
||
| 150 | } |
||
| 151 | |||
| 152 | /** |
||
| 153 | * 加载额外vendor |
||
| 154 | */ |
||
| 155 | public function loadVendorIfExist() |
||
| 156 | { |
||
| 157 | $vendorAutoload = $this->application->getPath() . '/vendor/autoload.php'; |
||
| 158 | if (FileSystem::exist($vendorAutoload)) { |
||
| 159 | Loader::requireOnce($vendorAutoload); |
||
| 160 | } |
||
| 161 | } |
||
| 162 | |||
| 163 | /** |
||
| 164 | * 准备数据源 |
||
| 165 | * |
||
| 166 | * @throws SQLException |
||
| 167 | */ |
||
| 168 | public function loadDataSource() |
||
| 169 | { |
||
| 170 | Database::loadApplication($this->application); |
||
| 171 | $dataSource = Database::getDefaultDataSource(); |
||
| 172 | $this->application->setDataSource($dataSource); |
||
| 173 | } |
||
| 174 | |||
| 175 | /** |
||
| 176 | * 准备模块 |
||
| 177 | */ |
||
| 178 | protected function prepareModule() |
||
| 179 | { |
||
| 180 | foreach ($this->application->getModules()->all() as $name => $module) { |
||
| 181 | $this->moduleLoader[$name] = new ModuleLoader($this->application, $module); |
||
| 182 | $this->moduleLoader[$name]->toLoad(); // 切换到加载状态 |
||
| 183 | $this->moduleLoader[$name]->loadExtraModuleResourceLibrary(); // 加载二外的模块源 |
||
| 184 | } |
||
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * 激活模块 |
||
| 189 | */ |
||
| 190 | protected function activeModule() |
||
| 191 | { |
||
| 192 | foreach ($this->application->getModules()->all() as $name => $module) { |
||
| 193 | if ($module->getStatus() !== Module::LOADED) { |
||
| 194 | $this->moduleLoader[$name]->toActive(); |
||
| 195 | } |
||
| 196 | } |
||
| 197 | } |
||
| 198 | |||
| 199 | /** |
||
| 200 | * 注册模块 |
||
| 201 | */ |
||
| 202 | protected function registerModule() |
||
| 203 | { |
||
| 204 | $extractPath = $this->application->getDataPath() . '/extract-module'; |
||
| 205 | FileSystem::make($extractPath); |
||
| 206 | foreach ($this->application->getModulePaths() as $path) { |
||
| 207 | $this->registerModuleFrom($path, $extractPath); |
||
| 208 | } |
||
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * 设置模块状态 |
||
| 213 | */ |
||
| 214 | protected function setModuleStatus() |
||
| 215 | { |
||
| 216 | $active = $this->application->getManifest('module.active', $this->actionableModules); |
||
| 217 | $reachable = $this->application->getManifest('module.reachable', $this->reachableModules); |
||
| 218 | $this->setModuleActive($this->application->getModules(), $active); |
||
| 219 | $this->setModuleReachable($this->application->getModules(), $reachable); |
||
| 220 | } |
||
| 221 | |||
| 222 | /** |
||
| 223 | * 注册模块下的模块 |
||
| 224 | * |
||
| 225 | * @param string $path |
||
| 226 | * @param string $extractPath |
||
| 227 | */ |
||
| 228 | protected function registerModuleFrom(string $path, string $extractPath) |
||
| 229 | { |
||
| 230 | $modules = new ModuleBag; |
||
| 231 | foreach (ModuleBuilder::scan($path, $extractPath) as $module) { |
||
| 232 | $modules->add($module); |
||
| 233 | } |
||
| 234 | $this->prepareModuleConfig($path, $modules); |
||
| 235 | } |
||
| 236 | |||
| 237 | /** |
||
| 238 | * 获取模块的配置 |
||
| 239 | * |
||
| 240 | * @param string $path |
||
| 241 | * @param ModuleBag $modules |
||
| 242 | */ |
||
| 243 | protected function prepareModuleConfig(string $path, ModuleBag $modules) |
||
| 244 | { |
||
| 245 | $config = $this->getModuleDirectoryConfig($path); |
||
| 246 | $moduleNames = array_keys($modules->all()); |
||
| 247 | // 获取模块文件夹模块配置 |
||
| 248 | $load = $config['load'] ?? $moduleNames; |
||
| 249 | $active = $config['active'] ?? $load; |
||
| 250 | $reachable = $config['reachable'] ?? $active; |
||
| 251 | // 获取允许加载的模块 |
||
| 252 | $load = $this->application->getManifest('module.load', $load); |
||
| 253 | $this->loadModuleFromBag($modules, $load); |
||
| 254 | $this->actionableModules = array_merge($this->actionableModules, $active); |
||
| 255 | $this->reachableModules = array_merge($this->reachableModules, $reachable); |
||
| 256 | } |
||
| 257 | |||
| 258 | /** |
||
| 259 | * 获取目录的模板配置 |
||
| 260 | * |
||
| 261 | * @param string $path |
||
| 262 | * @return array |
||
| 263 | */ |
||
| 264 | protected function getModuleDirectoryConfig(string $path) |
||
| 265 | { |
||
| 266 | $resource = new Resource([$path]); |
||
| 267 | $configPath = $resource->getConfigResourcePath('config'); |
||
| 268 | if ($configPath) { |
||
| 269 | return Config::loadConfig($configPath, $this->application->getConfig()) ?? []; |
||
| 270 | } |
||
| 271 | return []; |
||
| 272 | } |
||
| 273 | |||
| 274 | protected function loadModuleFromBag(ModuleBag $bag, array $load) |
||
| 280 | } |
||
| 281 | } |
||
| 282 | } |
||
| 283 | |||
| 284 | protected function setModuleActive(ModuleBag $bag, array $active) |
||
| 285 | { |
||
| 286 | foreach ($active as $moduleName) { |
||
| 287 | if ($module = $bag->get($moduleName)) { |
||
| 288 | $module->setStatus(Module::ACTIVE); |
||
| 289 | } |
||
| 290 | } |
||
| 291 | } |
||
| 292 | |||
| 293 | protected function setModuleReachable(ModuleBag $bag, array $reachable) |
||
| 294 | { |
||
| 302 |