| Total Complexity | 41 | 
| Total Lines | 211 | 
| Duplicated Lines | 0 % | 
| Changes | 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 | ||
| 20 | class ApplicationLoader | ||
| 21 | { | ||
| 22 | /** | ||
| 23 | * 应用程序 | ||
| 24 | * | ||
| 25 | * @var Application | ||
| 26 | */ | ||
| 27 | protected $application; | ||
| 28 | |||
| 29 | /** | ||
| 30 | * 模块加载器 | ||
| 31 | * | ||
| 32 | * @var ModuleLoader[] | ||
| 33 | */ | ||
| 34 | protected $moduleLoader; | ||
| 35 | |||
| 36 | |||
| 37 | public function __construct(Application $application) | ||
| 38 |     { | ||
| 39 | $this->application = $application; | ||
| 40 | } | ||
| 41 | |||
| 42 | public function load() | ||
| 43 |     { | ||
| 44 | $this->loadVendorIfExist(); | ||
| 45 | $this->loadGlobalConfig(); | ||
| 46 | $this->registerModule(); | ||
| 47 | $this->prepareModule(); | ||
| 48 | $this->activeModule(); | ||
| 49 | } | ||
| 50 | |||
| 51 | |||
| 52 | public function loadRoute() | ||
| 57 | } | ||
| 58 | } | ||
| 59 | } | ||
| 60 | |||
| 61 | |||
| 62 | public function loadGlobalConfig() | ||
| 63 |     { | ||
| 64 | $resource = $this->application->getResource(); | ||
| 65 |         if ($configPath = $resource->getConfigResourcePath('config/config')) { | ||
| 66 | $this->application->getConfig()->load($configPath); | ||
| 67 | } | ||
| 68 |         if ($listenerPath = $resource->getConfigResourcePath('config/listener')) { | ||
| 69 | $this->application->loadEvent($listenerPath); | ||
| 70 | } | ||
| 71 | } | ||
| 72 | |||
| 73 | public function loadVendorIfExist() | ||
| 74 |     { | ||
| 75 | $vendorAutoload = $this->application->getPath().'/vendor/autoload.php'; | ||
| 76 |         if (FileSystem::exist($vendorAutoload)) { | ||
| 77 | require_once $vendorAutoload; | ||
| 78 | } | ||
| 79 | } | ||
| 80 | |||
| 81 | /** | ||
| 82 | * @throws SQLException | ||
| 83 | */ | ||
| 84 | public function loadDataSource() | ||
| 85 |     { | ||
| 86 |         $dataSource = $this->getDataSourceGroup('default'); | ||
| 87 | $this->application->setDataSource($dataSource); | ||
| 88 | } | ||
| 89 | |||
| 90 | /** | ||
| 91 | * @param string $groupName | ||
| 92 | * @return DataSource | ||
| 93 | * @throws SQLException | ||
| 94 | */ | ||
| 95 | public function getDataSourceGroup(string $groupName):DataSource | ||
| 115 | } | ||
| 116 | |||
| 117 | /** | ||
| 118 | * @param DataSource $source | ||
| 119 | * @param Observer $observer | ||
| 120 | * @param string $name | ||
| 121 | * @param string $type | ||
| 122 | * @param string $mode | ||
| 123 | * @param array $config | ||
| 124 | * @throws SQLException | ||
| 125 | */ | ||
| 126 | protected function addDataSource( | ||
| 127 | DataSource $source, | ||
| 128 | Observer $observer, | ||
| 129 | string $name, | ||
| 130 | string $type, | ||
| 131 | string $mode, | ||
| 132 | array $config | ||
| 133 |     ) { | ||
| 134 | $mode = strtolower($mode); | ||
| 135 | $data = DataSource::new($type, $config, $name); | ||
| 136 | $data->setObserver($observer); | ||
| 137 |         if (strlen($mode) > 0) { | ||
| 138 |             if (strpos($mode, 'read') !== false || strpos($mode, 'slave') !== false) { | ||
| 139 | $source->addRead($data); | ||
| 140 | } | ||
| 141 |             if (strpos($mode, 'write') !== false) { | ||
| 142 | $source->addWrite($data); | ||
| 143 | } | ||
| 144 |             if (strpos($mode, 'master') !== false) { | ||
| 145 | $source->add($data); | ||
| 146 | } | ||
| 147 |         } else { | ||
| 148 | $source->add($data); | ||
| 149 | } | ||
| 150 | } | ||
| 151 | |||
| 152 | protected function prepareModule() | ||
| 153 |     { | ||
| 154 |         foreach ($this->application->getModules()->all() as $name => $module) { | ||
| 155 | $this->moduleLoader[$name] = new ModuleLoader($this->application, $module); | ||
| 156 | $this->moduleLoader[$name]->toLoad(); | ||
| 157 | $this->moduleLoader[$name]->loadExtraModuleResourceLibrary(); | ||
| 158 | } | ||
| 159 | } | ||
| 160 | |||
| 161 | protected function activeModule() | ||
| 162 |     { | ||
| 163 |         foreach ($this->application->getModules()->all() as $name => $module) { | ||
| 164 |             if ($module->getStatus() !== Module::LOADED) { | ||
| 165 | $this->moduleLoader[$name]->toActive(); | ||
| 166 | } | ||
| 167 | } | ||
| 168 | } | ||
| 169 | |||
| 170 | protected function registerModule() | ||
| 171 |     { | ||
| 172 | $extractPath = SUDA_DATA .'/extract-module'; | ||
| 173 | FileSystem::make($extractPath); | ||
| 174 |         foreach ($this->application->getModulePaths() as $path) { | ||
| 175 | $this->registerModuleFrom($path, $extractPath); | ||
| 176 | } | ||
| 177 | } | ||
| 178 | |||
| 179 | protected function registerModuleFrom(string $path, string $extractPath) | ||
| 180 |     { | ||
| 181 | $modules = new ModuleBag; | ||
| 182 |         foreach (ModuleBuilder::scan($path, $extractPath) as $module) { | ||
| 183 | $modules->add($module); | ||
| 184 | } | ||
| 185 | $this->assignModuleToApplication($path, $modules); | ||
| 186 | } | ||
| 187 | |||
| 188 | protected function assignModuleToApplication(string $path, ModuleBag $modules) | ||
| 203 | } | ||
| 204 | |||
| 205 | |||
| 206 | |||
| 207 | protected function loadModules(ModuleBag $bag, array $load) | ||
| 213 | } | ||
| 214 | } | ||
| 215 | } | ||
| 216 | |||
| 217 | protected function setModuleActive(ModuleBag $bag, array $active) | ||
| 222 | } | ||
| 223 | } | ||
| 224 | } | ||
| 225 | |||
| 226 | protected function setModuleReachable(ModuleBag $bag, array $reachable) | ||
| 235 |