Complex classes like Setup 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Setup, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 55 | class Setup { |
||
| 56 | /** @var SystemConfig */ |
||
| 57 | protected $config; |
||
| 58 | /** @var IniGetWrapper */ |
||
| 59 | protected $iniWrapper; |
||
| 60 | /** @var IL10N */ |
||
| 61 | protected $l10n; |
||
| 62 | /** @var Defaults */ |
||
| 63 | protected $defaults; |
||
| 64 | /** @var ILogger */ |
||
| 65 | protected $logger; |
||
| 66 | /** @var ISecureRandom */ |
||
| 67 | protected $random; |
||
| 68 | /** @var Installer */ |
||
| 69 | protected $installer; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @param SystemConfig $config |
||
| 73 | * @param IniGetWrapper $iniWrapper |
||
| 74 | * @param IL10N $l10n |
||
| 75 | * @param Defaults $defaults |
||
| 76 | * @param ILogger $logger |
||
| 77 | * @param ISecureRandom $random |
||
| 78 | * @param Installer $installer |
||
| 79 | */ |
||
| 80 | public function __construct(SystemConfig $config, |
||
| 81 | IniGetWrapper $iniWrapper, |
||
| 82 | IL10N $l10n, |
||
| 83 | Defaults $defaults, |
||
| 84 | ILogger $logger, |
||
| 85 | ISecureRandom $random, |
||
| 86 | Installer $installer |
||
| 87 | ) { |
||
| 88 | $this->config = $config; |
||
| 89 | $this->iniWrapper = $iniWrapper; |
||
| 90 | $this->l10n = $l10n; |
||
| 91 | $this->defaults = $defaults; |
||
| 92 | $this->logger = $logger; |
||
| 93 | $this->random = $random; |
||
| 94 | $this->installer = $installer; |
||
| 95 | } |
||
| 96 | |||
| 97 | static protected $dbSetupClasses = [ |
||
| 98 | 'mysql' => \OC\Setup\MySQL::class, |
||
| 99 | 'pgsql' => \OC\Setup\PostgreSQL::class, |
||
| 100 | 'oci' => \OC\Setup\OCI::class, |
||
| 101 | 'sqlite' => \OC\Setup\Sqlite::class, |
||
| 102 | 'sqlite3' => \OC\Setup\Sqlite::class, |
||
| 103 | ]; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Wrapper around the "class_exists" PHP function to be able to mock it |
||
| 107 | * @param string $name |
||
| 108 | * @return bool |
||
| 109 | */ |
||
| 110 | protected function class_exists($name) { |
||
| 111 | return class_exists($name); |
||
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Wrapper around the "is_callable" PHP function to be able to mock it |
||
| 116 | * @param string $name |
||
| 117 | * @return bool |
||
| 118 | */ |
||
| 119 | protected function is_callable($name) { |
||
| 120 | return is_callable($name); |
||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Wrapper around \PDO::getAvailableDrivers |
||
| 125 | * |
||
| 126 | * @return array |
||
| 127 | */ |
||
| 128 | protected function getAvailableDbDriversForPdo() { |
||
| 129 | return \PDO::getAvailableDrivers(); |
||
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Get the available and supported databases of this instance |
||
| 134 | * |
||
| 135 | * @param bool $allowAllDatabases |
||
| 136 | * @return array |
||
| 137 | * @throws Exception |
||
| 138 | */ |
||
| 139 | public function getSupportedDatabases($allowAllDatabases = false) { |
||
| 140 | $availableDatabases = [ |
||
| 141 | 'sqlite' => [ |
||
| 142 | 'type' => 'pdo', |
||
| 143 | 'call' => 'sqlite', |
||
| 144 | 'name' => 'SQLite', |
||
| 145 | ], |
||
| 146 | 'mysql' => [ |
||
| 147 | 'type' => 'pdo', |
||
| 148 | 'call' => 'mysql', |
||
| 149 | 'name' => 'MySQL/MariaDB', |
||
| 150 | ], |
||
| 151 | 'pgsql' => [ |
||
| 152 | 'type' => 'pdo', |
||
| 153 | 'call' => 'pgsql', |
||
| 154 | 'name' => 'PostgreSQL', |
||
| 155 | ], |
||
| 156 | 'oci' => [ |
||
| 157 | 'type' => 'function', |
||
| 158 | 'call' => 'oci_connect', |
||
| 159 | 'name' => 'Oracle', |
||
| 160 | ], |
||
| 161 | ]; |
||
| 162 | if ($allowAllDatabases) { |
||
| 163 | $configuredDatabases = array_keys($availableDatabases); |
||
| 164 | } else { |
||
| 165 | $configuredDatabases = $this->config->getValue('supportedDatabases', |
||
| 166 | ['sqlite', 'mysql', 'pgsql']); |
||
| 167 | } |
||
| 168 | if(!is_array($configuredDatabases)) { |
||
| 169 | throw new Exception('Supported databases are not properly configured.'); |
||
| 170 | } |
||
| 171 | |||
| 172 | $supportedDatabases = array(); |
||
| 173 | |||
| 174 | foreach($configuredDatabases as $database) { |
||
| 175 | if(array_key_exists($database, $availableDatabases)) { |
||
| 176 | $working = false; |
||
| 177 | $type = $availableDatabases[$database]['type']; |
||
| 178 | $call = $availableDatabases[$database]['call']; |
||
| 179 | |||
| 180 | if ($type === 'function') { |
||
| 181 | $working = $this->is_callable($call); |
||
| 182 | } elseif($type === 'pdo') { |
||
| 183 | $working = in_array($call, $this->getAvailableDbDriversForPdo(), true); |
||
| 184 | } |
||
| 185 | if($working) { |
||
| 186 | $supportedDatabases[$database] = $availableDatabases[$database]['name']; |
||
| 187 | } |
||
| 188 | } |
||
| 189 | } |
||
| 190 | |||
| 191 | return $supportedDatabases; |
||
| 192 | } |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Gathers system information like database type and does |
||
| 196 | * a few system checks. |
||
| 197 | * |
||
| 198 | * @return array of system info, including an "errors" value |
||
| 199 | * in case of errors/warnings |
||
| 200 | */ |
||
| 201 | public function getSystemInfo($allowAllDatabases = false) { |
||
| 202 | $databases = $this->getSupportedDatabases($allowAllDatabases); |
||
| 203 | |||
| 204 | $dataDir = $this->config->getValue('datadirectory', \OC::$SERVERROOT.'/data'); |
||
| 205 | |||
| 206 | $errors = []; |
||
| 207 | |||
| 208 | // Create data directory to test whether the .htaccess works |
||
| 209 | // Notice that this is not necessarily the same data directory as the one |
||
| 210 | // that will effectively be used. |
||
| 211 | if(!file_exists($dataDir)) { |
||
| 212 | @mkdir($dataDir); |
||
|
|
|||
| 213 | } |
||
| 214 | $htAccessWorking = true; |
||
| 215 | if (is_dir($dataDir) && is_writable($dataDir)) { |
||
| 216 | // Protect data directory here, so we can test if the protection is working |
||
| 217 | self::protectDataDirectory(); |
||
| 218 | |||
| 219 | try { |
||
| 220 | $util = new \OC_Util(); |
||
| 221 | $htAccessWorking = $util->isHtaccessWorking(\OC::$server->getConfig()); |
||
| 222 | } catch (\OC\HintException $e) { |
||
| 223 | $errors[] = [ |
||
| 224 | 'error' => $e->getMessage(), |
||
| 225 | 'hint' => $e->getHint(), |
||
| 226 | ]; |
||
| 227 | $htAccessWorking = false; |
||
| 228 | } |
||
| 229 | } |
||
| 230 | |||
| 231 | if (\OC_Util::runningOnMac()) { |
||
| 232 | $errors[] = [ |
||
| 233 | 'error' => $this->l10n->t( |
||
| 234 | 'Mac OS X is not supported and %s will not work properly on this platform. ' . |
||
| 235 | 'Use it at your own risk! ', |
||
| 236 | [$this->defaults->getName()] |
||
| 237 | ), |
||
| 238 | 'hint' => $this->l10n->t('For the best results, please consider using a GNU/Linux server instead.'), |
||
| 239 | ]; |
||
| 240 | } |
||
| 241 | |||
| 242 | if($this->iniWrapper->getString('open_basedir') !== '' && PHP_INT_SIZE === 4) { |
||
| 243 | $errors[] = [ |
||
| 244 | 'error' => $this->l10n->t( |
||
| 245 | 'It seems that this %s instance is running on a 32-bit PHP environment and the open_basedir has been configured in php.ini. ' . |
||
| 246 | 'This will lead to problems with files over 4 GB and is highly discouraged.', |
||
| 247 | [$this->defaults->getName()] |
||
| 248 | ), |
||
| 249 | 'hint' => $this->l10n->t('Please remove the open_basedir setting within your php.ini or switch to 64-bit PHP.'), |
||
| 250 | ]; |
||
| 251 | } |
||
| 252 | |||
| 253 | return array( |
||
| 254 | 'hasSQLite' => isset($databases['sqlite']), |
||
| 255 | 'hasMySQL' => isset($databases['mysql']), |
||
| 256 | 'hasPostgreSQL' => isset($databases['pgsql']), |
||
| 257 | 'hasOracle' => isset($databases['oci']), |
||
| 258 | 'databases' => $databases, |
||
| 259 | 'directory' => $dataDir, |
||
| 260 | 'htaccessWorking' => $htAccessWorking, |
||
| 261 | 'errors' => $errors, |
||
| 262 | ); |
||
| 263 | } |
||
| 264 | |||
| 265 | /** |
||
| 266 | * @param $options |
||
| 267 | * @return array |
||
| 268 | */ |
||
| 269 | public function install($options) { |
||
| 270 | $l = $this->l10n; |
||
| 271 | |||
| 272 | $error = array(); |
||
| 273 | $dbType = $options['dbtype']; |
||
| 274 | |||
| 275 | if(empty($options['adminlogin'])) { |
||
| 276 | $error[] = $l->t('Set an admin username.'); |
||
| 277 | } |
||
| 278 | if(empty($options['adminpass'])) { |
||
| 279 | $error[] = $l->t('Set an admin password.'); |
||
| 280 | } |
||
| 281 | if(empty($options['directory'])) { |
||
| 282 | $options['directory'] = \OC::$SERVERROOT."/data"; |
||
| 283 | } |
||
| 284 | |||
| 285 | if (!isset(self::$dbSetupClasses[$dbType])) { |
||
| 286 | $dbType = 'sqlite'; |
||
| 287 | } |
||
| 288 | |||
| 289 | $username = htmlspecialchars_decode($options['adminlogin']); |
||
| 290 | $password = htmlspecialchars_decode($options['adminpass']); |
||
| 291 | $dataDir = htmlspecialchars_decode($options['directory']); |
||
| 292 | |||
| 293 | $class = self::$dbSetupClasses[$dbType]; |
||
| 294 | /** @var \OC\Setup\AbstractDatabase $dbSetup */ |
||
| 295 | $dbSetup = new $class($l, $this->config, $this->logger, $this->random); |
||
| 296 | $error = array_merge($error, $dbSetup->validate($options)); |
||
| 297 | |||
| 298 | // validate the data directory |
||
| 299 | if ((!is_dir($dataDir) && !mkdir($dataDir)) || !is_writable($dataDir)) { |
||
| 300 | $error[] = $l->t("Can't create or write into the data directory %s", array($dataDir)); |
||
| 301 | } |
||
| 302 | |||
| 303 | if (!empty($error)) { |
||
| 304 | return $error; |
||
| 305 | } |
||
| 306 | |||
| 307 | $request = \OC::$server->getRequest(); |
||
| 308 | |||
| 309 | //no errors, good |
||
| 310 | if(isset($options['trusted_domains']) |
||
| 311 | && is_array($options['trusted_domains'])) { |
||
| 312 | $trustedDomains = $options['trusted_domains']; |
||
| 313 | } else { |
||
| 314 | $trustedDomains = [$request->getInsecureServerHost()]; |
||
| 315 | } |
||
| 316 | |||
| 317 | //use sqlite3 when available, otherwise sqlite2 will be used. |
||
| 318 | if ($dbType === 'sqlite' && class_exists('SQLite3')) { |
||
| 319 | $dbType = 'sqlite3'; |
||
| 320 | } |
||
| 321 | |||
| 322 | //generate a random salt that is used to salt the local user passwords |
||
| 323 | $salt = $this->random->generate(30); |
||
| 324 | // generate a secret |
||
| 325 | $secret = $this->random->generate(48); |
||
| 326 | |||
| 327 | //write the config file |
||
| 328 | $this->config->setValues([ |
||
| 329 | 'passwordsalt' => $salt, |
||
| 330 | 'secret' => $secret, |
||
| 331 | 'trusted_domains' => $trustedDomains, |
||
| 332 | 'datadirectory' => $dataDir, |
||
| 333 | 'overwrite.cli.url' => $request->getServerProtocol() . '://' . $request->getInsecureServerHost() . \OC::$WEBROOT, |
||
| 334 | 'dbtype' => $dbType, |
||
| 335 | 'version' => implode('.', \OCP\Util::getVersion()), |
||
| 336 | ]); |
||
| 337 | |||
| 338 | try { |
||
| 339 | $dbSetup->initialize($options); |
||
| 340 | $dbSetup->setupDatabase($username); |
||
| 341 | // apply necessary migrations |
||
| 342 | $dbSetup->runMigrations(); |
||
| 343 | } catch (\OC\DatabaseSetupException $e) { |
||
| 344 | $error[] = [ |
||
| 345 | 'error' => $e->getMessage(), |
||
| 346 | 'hint' => $e->getHint(), |
||
| 347 | ]; |
||
| 348 | return $error; |
||
| 349 | } catch (Exception $e) { |
||
| 350 | $error[] = [ |
||
| 351 | 'error' => 'Error while trying to create admin user: ' . $e->getMessage(), |
||
| 352 | 'hint' => '', |
||
| 353 | ]; |
||
| 354 | return $error; |
||
| 355 | } |
||
| 356 | |||
| 357 | //create the user and group |
||
| 358 | $user = null; |
||
| 359 | try { |
||
| 360 | $user = \OC::$server->getUserManager()->createUser($username, $password); |
||
| 361 | if (!$user) { |
||
| 362 | $error[] = "User <$username> could not be created."; |
||
| 363 | } |
||
| 364 | } catch(Exception $exception) { |
||
| 365 | $error[] = $exception->getMessage(); |
||
| 366 | } |
||
| 367 | |||
| 368 | if (empty($error)) { |
||
| 369 | $config = \OC::$server->getConfig(); |
||
| 370 | $config->setAppValue('core', 'installedat', microtime(true)); |
||
| 371 | $config->setAppValue('core', 'lastupdatedat', microtime(true)); |
||
| 372 | $config->setAppValue('core', 'vendor', $this->getVendor()); |
||
| 373 | |||
| 374 | $group =\OC::$server->getGroupManager()->createGroup('admin'); |
||
| 375 | $group->addUser($user); |
||
| 376 | |||
| 377 | // Install shipped apps and specified app bundles |
||
| 378 | Installer::installShippedApps(); |
||
| 379 | $bundleFetcher = new BundleFetcher(\OC::$server->getL10N('lib')); |
||
| 380 | $defaultInstallationBundles = $bundleFetcher->getDefaultInstallationBundle(); |
||
| 381 | foreach($defaultInstallationBundles as $bundle) { |
||
| 382 | try { |
||
| 383 | $this->installer->installAppBundle($bundle); |
||
| 384 | } catch (Exception $e) {} |
||
| 385 | } |
||
| 386 | |||
| 387 | // create empty file in data dir, so we can later find |
||
| 388 | // out that this is indeed an ownCloud data directory |
||
| 389 | file_put_contents($config->getSystemValue('datadirectory', \OC::$SERVERROOT.'/data').'/.ocdata', ''); |
||
| 390 | |||
| 391 | // Update .htaccess files |
||
| 392 | self::updateHtaccess(); |
||
| 393 | self::protectDataDirectory(); |
||
| 394 | |||
| 395 | self::installBackgroundJobs(); |
||
| 396 | |||
| 397 | //and we are done |
||
| 398 | $config->setSystemValue('installed', true); |
||
| 399 | |||
| 400 | // Create a session token for the newly created user |
||
| 401 | // The token provider requires a working db, so it's not injected on setup |
||
| 402 | /* @var $userSession User\Session */ |
||
| 403 | $userSession = \OC::$server->getUserSession(); |
||
| 404 | $defaultTokenProvider = \OC::$server->query(DefaultTokenProvider::class); |
||
| 405 | $userSession->setTokenProvider($defaultTokenProvider); |
||
| 406 | $userSession->login($username, $password); |
||
| 407 | $userSession->createSessionToken($request, $userSession->getUser()->getUID(), $username, $password); |
||
| 408 | } |
||
| 409 | |||
| 410 | return $error; |
||
| 411 | } |
||
| 412 | |||
| 413 | public static function installBackgroundJobs() { |
||
| 414 | $jobList = \OC::$server->getJobList(); |
||
| 415 | $jobList->add(DefaultTokenCleanupJob::class); |
||
| 416 | $jobList->add(Rotate::class); |
||
| 417 | } |
||
| 418 | |||
| 419 | /** |
||
| 420 | * @return string Absolute path to htaccess |
||
| 421 | */ |
||
| 422 | private function pathToHtaccess() { |
||
| 425 | |||
| 426 | /** |
||
| 427 | * Append the correct ErrorDocument path for Apache hosts |
||
| 428 | * @return bool True when success, False otherwise |
||
| 429 | */ |
||
| 430 | public static function updateHtaccess() { |
||
| 431 | $config = \OC::$server->getSystemConfig(); |
||
| 432 | |||
| 433 | // For CLI read the value from overwrite.cli.url |
||
| 507 | |||
| 508 | public static function protectDataDirectory() { |
||
| 530 | |||
| 531 | /** |
||
| 532 | * Return vendor from which this version was published |
||
| 533 | * |
||
| 534 | * @return string Get the vendor |
||
| 535 | * |
||
| 536 | * Copy of \OC\Updater::getVendor() |
||
| 537 | */ |
||
| 538 | private function getVendor() { |
||
| 544 | } |
||
| 545 |
If you suppress an error, we recommend checking for the error condition explicitly: