| Total Complexity | 40 |
| Total Lines | 255 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like ModuleLoader 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 ModuleLoader, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class ModuleLoader |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * 应用程序 |
||
| 20 | * |
||
| 21 | * @var Application |
||
| 22 | */ |
||
| 23 | protected $application; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * 运行环境 |
||
| 27 | * |
||
| 28 | * @var Module |
||
| 29 | */ |
||
| 30 | protected $module; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * 模块加载器 |
||
| 34 | * |
||
| 35 | * @param Application $application |
||
| 36 | * @param Module $module |
||
| 37 | */ |
||
| 38 | public function __construct(Application $application, Module $module) |
||
| 42 | } |
||
| 43 | |||
| 44 | public function toLoad() |
||
| 45 | { |
||
| 46 | $this->loadVendorIfExist(); |
||
| 47 | $this->loadShareLibrary(); |
||
| 48 | $this->application->debug()->info('loaded - '.$this->module->getFullName()); |
||
| 49 | } |
||
| 50 | |||
| 51 | public function toActive() |
||
| 52 | { |
||
| 53 | $this->loadEventListener(); |
||
| 54 | $this->loadExtraModuleResourceLibrary(); |
||
| 55 | $this->application->debug()->info('active = '.$this->module->getFullName()); |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @throws Exception |
||
| 60 | */ |
||
| 61 | public function toReachable() |
||
| 62 | { |
||
| 63 | $this->loadRoute(); |
||
| 64 | $this->application->debug()->info('reachable # '.$this->module->getFullName()); |
||
| 65 | } |
||
| 66 | |||
| 67 | public function toRunning() |
||
| 68 | { |
||
| 69 | $this->checkFrameworkVersion(); |
||
| 70 | $this->checkRequirements(); |
||
| 71 | $this->loadPrivateLibrary(); |
||
| 72 | $this->application->setRunning($this->module); |
||
| 73 | $this->application->debug()->info('run + '.$this->module->getFullName()); |
||
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * 检查框架依赖 |
||
| 78 | * |
||
| 79 | * @return void |
||
| 80 | */ |
||
| 81 | protected function checkRequirements() |
||
| 82 | { |
||
| 83 | if ($require = $this->module->getProperty('require')) { |
||
| 84 | foreach ($require as $module => $version) { |
||
| 85 | $this->assertModuleVersion($module, $version); |
||
| 86 | } |
||
| 87 | } |
||
| 88 | } |
||
| 89 | |||
| 90 | protected function assertModuleVersion(string $module, string $version) |
||
| 91 | { |
||
| 92 | $target = $this->application->find($module); |
||
| 93 | if ($target === null) { |
||
| 94 | throw new ApplicationException( |
||
| 95 | sprintf('%s module need %s %s but not exist', $this->module->getFullName(), $module, $version), |
||
| 96 | ApplicationException::ERR_MODULE_REQUIREMENTS |
||
| 97 | ); |
||
| 98 | } |
||
| 99 | if (static::versionCompare($version, $target->getVersion()) !== true) { |
||
| 100 | throw new ApplicationException( |
||
| 101 | sprintf( |
||
| 102 | '%s module need %s version %s', |
||
| 103 | $this->module->getFullName(), |
||
| 104 | $target->getName(), |
||
| 105 | $target->getVersion() |
||
| 106 | ), |
||
| 107 | ApplicationException::ERR_MODULE_REQUIREMENTS |
||
| 108 | ); |
||
| 109 | } |
||
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * 检查模块需求 |
||
| 114 | * |
||
| 115 | * @return void |
||
| 116 | */ |
||
| 117 | protected function checkFrameworkVersion() |
||
| 118 | { |
||
| 119 | if ($sudaVersion = $this->module->getProperty('suda')) { |
||
| 120 | if (static::versionCompare($sudaVersion, SUDA_VERSION) !== true) { |
||
| 121 | throw new ApplicationException( |
||
| 122 | sprintf('%s module need suda version %s', $this->module->getFullName(), $sudaVersion), |
||
| 123 | ApplicationException::ERR_FRAMEWORK_VERSION |
||
| 124 | ); |
||
| 125 | } |
||
| 126 | } |
||
| 127 | } |
||
| 128 | protected function loadShareLibrary() |
||
| 129 | { |
||
| 130 | $import = $this->module->getProperty('import.share', []); |
||
| 131 | if (count($import)) { |
||
| 132 | $this->importClassLoader($import, $this->module->getPath()); |
||
| 133 | } |
||
| 134 | } |
||
| 135 | |||
| 136 | protected function loadPrivateLibrary() |
||
| 137 | { |
||
| 138 | $import = $this->module->getProperty('import.src', []); |
||
| 139 | if (count($import)) { |
||
| 140 | $this->importClassLoader($import, $this->module->getPath()); |
||
| 141 | } |
||
| 142 | } |
||
| 143 | |||
| 144 | public function loadVendorIfExist() |
||
| 145 | { |
||
| 146 | $vendorAutoload = $this->module->getPath().'/vendor/autoload.php'; |
||
| 147 | if (FileSystem::exist($vendorAutoload)) { |
||
| 148 | require_once $vendorAutoload; |
||
| 149 | } |
||
| 150 | } |
||
| 151 | |||
| 152 | protected function loadExtraModuleResourceLibrary() |
||
| 153 | { |
||
| 154 | $import = $this->module->getConfig('module-resource', []); |
||
| 155 | if (count($import)) { |
||
| 156 | foreach ($import as $name => $path) { |
||
| 157 | if ($module = $this->application->find($name)) { |
||
| 158 | $module->getResource()->addResourcePath( |
||
| 159 | Resource::getPathByRelativePath($path, $this->module->getPath()) |
||
| 160 | ); |
||
| 161 | } |
||
| 162 | } |
||
| 163 | } |
||
| 164 | } |
||
| 165 | |||
| 166 | protected function loadEventListener() |
||
| 167 | { |
||
| 168 | if ($path = $this->module->getResource()->getConfigResourcePath('config/event')) { |
||
| 169 | $event = Config::loadConfig($path, [ |
||
| 170 | 'module' => $this->module->getName(), |
||
| 171 | 'property' => $this->module->getProperty(), |
||
| 172 | 'config' => $this->module->getConfig(), |
||
| 173 | ]); |
||
| 174 | if (is_array($event)) { |
||
| 175 | $this->application->event()->load($event); |
||
| 176 | } |
||
| 177 | } |
||
| 178 | } |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @throws Exception |
||
| 182 | */ |
||
| 183 | protected function loadRoute() |
||
| 184 | { |
||
| 185 | foreach ($this->application->getRouteGroup() as $group) { |
||
| 186 | $this->loadRouteGroup($group); |
||
| 187 | } |
||
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * 导入 ClassLoader 配置 |
||
| 192 | * |
||
| 193 | * @param array $import |
||
| 194 | * @param string $relativePath |
||
| 195 | * @return void |
||
| 196 | */ |
||
| 197 | protected function importClassLoader(array $import, string $relativePath) |
||
| 198 | { |
||
| 199 | ApplicationBuilder::importClassLoader($this->application->loader(), $import, $relativePath); |
||
| 200 | } |
||
| 201 | |||
| 202 | /** |
||
| 203 | * 加载路由组 |
||
| 204 | * |
||
| 205 | * @param string $groupName |
||
| 206 | * @return void |
||
| 207 | * @throws Exception |
||
| 208 | */ |
||
| 209 | protected function loadRouteGroup(string $groupName) |
||
| 210 | { |
||
| 211 | $group = $groupName === 'default' ? '': '-'. $groupName; |
||
| 212 | if ($path = $this->module->getResource()->getConfigResourcePath('config/route'.$group)) { |
||
| 213 | $routeConfig = Config::loadConfig($path, [ |
||
| 214 | 'module' => $this->module->getName(), |
||
| 215 | 'group' => $groupName, |
||
| 216 | 'property' => $this->module->getProperty(), |
||
| 217 | 'config' => $this->module->getConfig(), |
||
| 218 | ]); |
||
| 219 | if ($routeConfig !== null) { |
||
| 220 | $prefix = $this->module->getConfig('route-prefix.'.$groupName, ''); |
||
| 221 | $this->loadRouteConfig($prefix, $groupName, $routeConfig); |
||
| 222 | } |
||
| 223 | } |
||
| 224 | } |
||
| 225 | |||
| 226 | /** |
||
| 227 | * 加载模块路由配置 |
||
| 228 | * |
||
| 229 | * @param string $prefix |
||
| 230 | * @param string $groupName |
||
| 231 | * @param array $routeConfig |
||
| 232 | * @return void |
||
| 233 | * @throws Exception |
||
| 234 | */ |
||
| 235 | protected function loadRouteConfig(string $prefix, string $groupName, array $routeConfig) |
||
| 236 | { |
||
| 237 | $module = $this->module->getFullName(); |
||
| 238 | foreach ($routeConfig as $name => $config) { |
||
| 239 | $exname = $this->application->getRouteName($name, $module, $groupName); |
||
| 240 | $method = $config['method'] ?? []; |
||
| 241 | $attributes = []; |
||
| 242 | $attributes['module'] = $module; |
||
| 243 | $attributes['config'] = $config; |
||
| 244 | $attributes['group'] = $groupName; |
||
| 245 | $attributes['route'] = $exname; |
||
| 246 | $uri = $config['uri'] ?? '/'; |
||
| 247 | $anti = array_key_exists('anti-prefix', $config) && $config['anti-prefix']; |
||
| 248 | if ($anti) { |
||
| 249 | $uri = '/'.trim($uri, '/'); |
||
| 250 | } else { |
||
| 251 | $uri = '/'.trim($prefix . $uri, '/'); |
||
| 252 | } |
||
| 253 | $this->application->request($method, $exname, $uri, $attributes); |
||
| 254 | } |
||
| 255 | } |
||
| 256 | |||
| 257 | /** |
||
| 258 | * 比较版本 |
||
| 259 | * |
||
| 260 | * @param string $version 比较用的版本,包含比较符号 |
||
| 261 | * @param string $compire 对比的版本 |
||
| 262 | * @return bool |
||
| 263 | */ |
||
| 264 | protected static function versionCompare(string $version, string $compire) |
||
| 271 | } |
||
| 272 | } |
||
| 273 |