| Total Complexity | 45 |
| Total Lines | 264 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like InstallConfig 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 InstallConfig, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class InstallConfig |
||
| 14 | { |
||
| 15 | use InstallEnvironmentAware; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * List of preferred DB classes in order |
||
| 19 | * |
||
| 20 | * @var array |
||
| 21 | */ |
||
| 22 | protected $preferredDatabases = [ |
||
| 23 | 'MySQLPDODatabase', |
||
| 24 | 'MySQLDatabase', |
||
| 25 | ]; |
||
| 26 | |||
| 27 | public function __construct($basePath = null) |
||
| 28 | { |
||
| 29 | $this->initBaseDir($basePath); |
||
| 30 | } |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Get database config from the current environment |
||
| 34 | * |
||
| 35 | * @param array $request Request object |
||
| 36 | * @param array $databaseClasses Supported database config |
||
| 37 | * @param bool $realPassword Set to true to get the real password. If false, any non-posted |
||
| 38 | * password will be redacted as '********'. Note: Posted passwords are considered disclosed and |
||
| 39 | * never redacted. |
||
| 40 | * @return array |
||
| 41 | */ |
||
| 42 | public function getDatabaseConfig($request, $databaseClasses, $realPassword = true) |
||
| 43 | { |
||
| 44 | // Get config from request |
||
| 45 | if (isset($request['db']['type'])) { |
||
| 46 | $type = $request['db']['type']; |
||
| 47 | if (isset($request['db'][$type])) { |
||
| 48 | $config = $request['db'][$type]; |
||
| 49 | // The posted placeholder must be substituted with the real password |
||
| 50 | if (isset($config['password']) && $config['password'] === Installer::PASSWORD_PLACEHOLDER) { |
||
| 51 | $config['password'] = Environment::getEnv('SS_DATABASE_PASSWORD') ?: ''; |
||
| 52 | } |
||
| 53 | return array_merge([ 'type' => $type ], $config); |
||
| 54 | } |
||
| 55 | } |
||
| 56 | |||
| 57 | // Guess database config |
||
| 58 | return [ |
||
| 59 | 'type' => $this->getDatabaseClass($databaseClasses), |
||
| 60 | 'server' => Environment::getEnv('SS_DATABASE_SERVER') ?: 'localhost', |
||
| 61 | 'username' => Environment::getEnv('SS_DATABASE_USERNAME') ?: 'root', |
||
| 62 | 'password' => $realPassword |
||
| 63 | ? (Environment::getEnv('SS_DATABASE_PASSWORD') ?: '') |
||
| 64 | : Installer::PASSWORD_PLACEHOLDER, // Avoid password disclosure |
||
| 65 | 'database' => Environment::getEnv('SS_DATABASE_NAME') ?: 'SS_mysite', |
||
| 66 | 'path' => Environment::getEnv('SS_DATABASE_PATH') |
||
| 67 | ?: Environment::getEnv('SS_SQLITE_DATABASE_PATH') // sqlite compat |
||
| 68 | ?: null, |
||
| 69 | 'key' => Environment::getEnv('SS_DATABASE_KEY') |
||
| 70 | ?: Environment::getEnv('SS_SQLITE_DATABASE_KEY') // sqlite compat |
||
| 71 | ?: null, |
||
| 72 | ]; |
||
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Get admin config from the environment |
||
| 77 | * |
||
| 78 | * @param array $request |
||
| 79 | * @param bool $realPassword Set to true to get the real password. If false, any non-posted |
||
| 80 | * password will be redacted as '********'. Note: Posted passwords are considered disclosed and |
||
| 81 | * never redacted. |
||
| 82 | * @return array |
||
| 83 | */ |
||
| 84 | public function getAdminConfig($request, $realPassword = true) |
||
| 85 | { |
||
| 86 | if (isset($request['admin'])) { |
||
| 87 | $config = $request['admin']; |
||
| 88 | if (isset($config['password']) && $config['password'] === Installer::PASSWORD_PLACEHOLDER) { |
||
| 89 | $config['password'] = Environment::getEnv('SS_DEFAULT_ADMIN_PASSWORD') ?: ''; |
||
| 90 | } |
||
| 91 | return $request['admin']; |
||
| 92 | } |
||
| 93 | |||
| 94 | return [ |
||
| 95 | 'username' => Environment::getEnv('SS_DEFAULT_ADMIN_USERNAME') ?: 'admin', |
||
| 96 | 'password' => $realPassword |
||
| 97 | ? (Environment::getEnv('SS_DEFAULT_ADMIN_PASSWORD') ?: '') |
||
| 98 | : Installer::PASSWORD_PLACEHOLDER, // Avoid password disclosure |
||
| 99 | ]; |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Check if this site has already been installed |
||
| 104 | * |
||
| 105 | * @return bool |
||
| 106 | */ |
||
| 107 | public function alreadyInstalled() |
||
| 108 | { |
||
| 109 | if (file_exists($this->getEnvPath())) { |
||
| 110 | return true; |
||
| 111 | } |
||
| 112 | if (!file_exists($this->getConfigPath())) { |
||
| 113 | return false; |
||
| 114 | } |
||
| 115 | $configContents = file_get_contents($this->getConfigPath()); |
||
| 116 | if (strstr($configContents, '$databaseConfig')) { |
||
| 117 | return true; |
||
| 118 | } |
||
| 119 | if (strstr($configContents, '$database')) { |
||
| 120 | return true; |
||
| 121 | } |
||
| 122 | return false; |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @return string |
||
| 127 | */ |
||
| 128 | protected function getConfigPath() |
||
| 129 | { |
||
| 130 | return $this->getBaseDir() . $this->getProjectDir() . DIRECTORY_SEPARATOR . '_config.php'; |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @return string |
||
| 135 | */ |
||
| 136 | protected function getEnvPath() |
||
| 137 | { |
||
| 138 | return $this->getBaseDir() . '.env'; |
||
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Database configs available for configuration |
||
| 143 | * |
||
| 144 | * @param array $databaseClasses |
||
| 145 | * @return string |
||
| 146 | */ |
||
| 147 | protected function getDatabaseClass($databaseClasses) |
||
| 148 | { |
||
| 149 | $envDatabase = Environment::getEnv('SS_DATABASE_CLASS'); |
||
| 150 | if ($envDatabase) { |
||
| 151 | return $envDatabase; |
||
| 152 | } |
||
| 153 | |||
| 154 | // Check supported versions |
||
| 155 | foreach ($this->preferredDatabases as $candidate) { |
||
| 156 | if (!empty($databaseClasses[$candidate]['supported'])) { |
||
| 157 | return $candidate; |
||
| 158 | } |
||
| 159 | } |
||
| 160 | return null; |
||
| 161 | } |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Get string representation of the framework version |
||
| 165 | * |
||
| 166 | * @return string |
||
| 167 | */ |
||
| 168 | public function getFrameworkVersion() |
||
| 169 | { |
||
| 170 | $composerLockPath = BASE_PATH . '/composer.lock'; |
||
| 171 | if (!file_exists($composerLockPath)) { |
||
| 172 | return 'unknown'; |
||
| 173 | } |
||
| 174 | $lockData = json_decode(file_get_contents($composerLockPath), true); |
||
| 175 | if (json_last_error() || empty($lockData['packages'])) { |
||
| 176 | return 'unknown'; |
||
| 177 | } |
||
| 178 | foreach ($lockData['packages'] as $package) { |
||
| 179 | if ($package['name'] === 'silverstripe/framework') { |
||
| 180 | return $package['version']; |
||
| 181 | } |
||
| 182 | } |
||
| 183 | return 'unknown'; |
||
| 184 | } |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Check if stats should be sent |
||
| 188 | * |
||
| 189 | * @param array $request |
||
| 190 | * @return bool |
||
| 191 | */ |
||
| 192 | public function canSendStats($request) |
||
| 193 | { |
||
| 194 | return !empty($request['stats']); |
||
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Get configured locales |
||
| 199 | * |
||
| 200 | * @return array |
||
| 201 | */ |
||
| 202 | public function getLocales() |
||
| 203 | { |
||
| 204 | return [ |
||
| 205 | 'af_ZA' => 'Afrikaans (South Africa)', |
||
| 206 | 'ar_EG' => 'Arabic (Egypt)', |
||
| 207 | 'hy_AM' => 'Armenian (Armenia)', |
||
| 208 | 'ast_ES' => 'Asturian (Spain)', |
||
| 209 | 'az_AZ' => 'Azerbaijani (Azerbaijan)', |
||
| 210 | 'bs_BA' => 'Bosnian (Bosnia and Herzegovina)', |
||
| 211 | 'bg_BG' => 'Bulgarian (Bulgaria)', |
||
| 212 | 'ca_ES' => 'Catalan (Spain)', |
||
| 213 | 'zh_CN' => 'Chinese (China)', |
||
| 214 | 'zh_TW' => 'Chinese (Taiwan)', |
||
| 215 | 'hr_HR' => 'Croatian (Croatia)', |
||
| 216 | 'cs_CZ' => 'Czech (Czech Republic)', |
||
| 217 | 'da_DK' => 'Danish (Denmark)', |
||
| 218 | 'nl_NL' => 'Dutch (Netherlands)', |
||
| 219 | 'en_GB' => 'English (United Kingdom)', |
||
| 220 | 'en_US' => 'English (United States)', |
||
| 221 | 'eo_XX' => 'Esperanto', |
||
| 222 | 'et_EE' => 'Estonian (Estonia)', |
||
| 223 | 'fo_FO' => 'Faroese (Faroe Islands)', |
||
| 224 | 'fi_FI' => 'Finnish (Finland)', |
||
| 225 | 'fr_FR' => 'French (France)', |
||
| 226 | 'de_DE' => 'German (Germany)', |
||
| 227 | 'el_GR' => 'Greek (Greece)', |
||
| 228 | 'he_IL' => 'Hebrew (Israel)', |
||
| 229 | 'hu_HU' => 'Hungarian (Hungary)', |
||
| 230 | 'is_IS' => 'Icelandic (Iceland)', |
||
| 231 | 'id_ID' => 'Indonesian (Indonesia)', |
||
| 232 | 'it_IT' => 'Italian (Italy)', |
||
| 233 | 'ja_JP' => 'Japanese (Japan)', |
||
| 234 | 'km_KH' => 'Khmer (Cambodia)', |
||
| 235 | 'lc_XX' => 'LOLCAT', |
||
| 236 | 'lv_LV' => 'Latvian (Latvia)', |
||
| 237 | 'lt_LT' => 'Lithuanian (Lithuania)', |
||
| 238 | 'ms_MY' => 'Malay (Malaysia)', |
||
| 239 | 'mi_NZ' => 'Maori (New Zealand)', |
||
| 240 | 'ne_NP' => 'Nepali (Nepal)', |
||
| 241 | 'nb_NO' => 'Norwegian', |
||
| 242 | 'fa_IR' => 'Persian (Iran)', |
||
| 243 | 'pl_PL' => 'Polish (Poland)', |
||
| 244 | 'pt_BR' => 'Portuguese (Brazil)', |
||
| 245 | 'pa_IN' => 'Punjabi (India)', |
||
| 246 | 'ro_RO' => 'Romanian (Romania)', |
||
| 247 | 'ru_RU' => 'Russian (Russia)', |
||
| 248 | 'sr_RS' => 'Serbian (Serbia)', |
||
| 249 | 'si_LK' => 'Sinhalese (Sri Lanka)', |
||
| 250 | 'sk_SK' => 'Slovak (Slovakia)', |
||
| 251 | 'sl_SI' => 'Slovenian (Slovenia)', |
||
| 252 | 'es_AR' => 'Spanish (Argentina)', |
||
| 253 | 'es_MX' => 'Spanish (Mexico)', |
||
| 254 | 'es_ES' => 'Spanish (Spain)', |
||
| 255 | 'sv_SE' => 'Swedish (Sweden)', |
||
| 256 | 'th_TH' => 'Thai (Thailand)', |
||
| 257 | 'tr_TR' => 'Turkish (Turkey)', |
||
| 258 | 'uk_UA' => 'Ukrainian (Ukraine)', |
||
| 259 | 'uz_UZ' => 'Uzbek (Uzbekistan)', |
||
| 260 | 'vi_VN' => 'Vietnamese (Vietnam)', |
||
| 261 | ]; |
||
| 262 | } |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Get theme selected |
||
| 266 | * |
||
| 267 | * @param $request |
||
| 268 | * @return string |
||
| 269 | */ |
||
| 270 | public function getTheme($request) |
||
| 277 | } |
||
| 278 | } |
||
| 279 |