| Total Complexity | 88 |
| Total Lines | 486 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Bootstrap 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 Bootstrap, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 36 | class Bootstrap |
||
| 37 | { |
||
| 38 | /** @var array What is returned by the function getrusage. */ |
||
| 39 | protected $rusage_start = []; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Bootstrap constructor. |
||
| 43 | * |
||
| 44 | * @param bool $standalone |
||
| 45 | * - True to boot outside elkarte |
||
| 46 | * - False to bootstrap the main elkarte site. |
||
| 47 | */ |
||
| 48 | public function __construct($standalone = true) |
||
| 49 | { |
||
| 50 | // Bootstrap only once. |
||
| 51 | if (!defined('ELKBOOT')) |
||
| 52 | { |
||
| 53 | // We're going to set a few globals |
||
| 54 | global $time_start, $ssi_error_reporting, $db_show_debug; |
||
| 55 | |||
| 56 | // Your on the clock |
||
| 57 | $time_start = microtime(true); |
||
| 58 | |||
| 59 | // Unless settings.php tells us otherwise |
||
| 60 | $db_show_debug = false; |
||
| 61 | |||
| 62 | // Report errors but not depreciated ones |
||
| 63 | $ssi_error_reporting = error_reporting(E_ALL & ~E_DEPRECATED); |
||
| 64 | |||
| 65 | // Get the things needed for ALL modes |
||
| 66 | $this->bringUpBasics(); |
||
| 67 | |||
| 68 | // Going to run from the side entrance and not directly from inside elkarte |
||
| 69 | if ($standalone) |
||
| 70 | { |
||
| 71 | $this->ssi_main(); |
||
| 72 | } |
||
| 73 | } |
||
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Calls various initialization functions in the necessary order |
||
| 78 | */ |
||
| 79 | public function bringUpBasics() |
||
| 80 | { |
||
| 81 | $this->setConstants(); |
||
| 82 | $this->setRusage(); |
||
| 83 | $this->clearGlobals(); |
||
| 84 | $this->loadSettingsFile(); |
||
| 85 | $this->validatePaths(); |
||
| 86 | $this->loadDependants(); |
||
| 87 | $this->loadAutoloader(); |
||
| 88 | $this->checkMaintance(); |
||
| 89 | $this->setDebug(); |
||
| 90 | $this->bringUp(); |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Set the core constants, you know the ones we often forget to |
||
| 95 | * update on new releases. |
||
| 96 | */ |
||
| 97 | private function setConstants() |
||
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Get initial resource usage |
||
| 116 | */ |
||
| 117 | private function setRusage() |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * If they glo, they need to be cleaned. |
||
| 124 | */ |
||
| 125 | private function clearGlobals() |
||
| 133 | } |
||
| 134 | } |
||
| 135 | } |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Loads the Settings.php values into the global space |
||
| 139 | */ |
||
| 140 | private function loadSettingsFile() |
||
| 141 | { |
||
| 142 | // All those wonderful things found in settings |
||
| 143 | global $maintenance, $mtitle, $msubject, $mmessage, $mbname, $language, $boardurl, $webmaster_email; |
||
| 144 | global $cookiename, $db_type, $db_server, $db_port, $db_name, $db_user, $db_passwd; |
||
| 145 | global $ssi_db_user, $ssi_db_passwd, $db_prefix, $db_persist, $db_error_send; |
||
| 146 | global $cache_uid, $cache_password, $cache_enable, $cache_servers, $cache_accelerator; |
||
| 147 | global $db_show_debug, $url_format, $cachedir, $boarddir, $sourcedir, $extdir, $languagedir; |
||
| 148 | |||
| 149 | // Where the Settings.php file is located |
||
| 150 | $settings_location = __DIR__ . '/Settings.php'; |
||
| 151 | |||
| 152 | // First thing: prefer the presence of a lock file to short-circuit any installer logic |
||
| 153 | // The IGNORE_INSTALL_DIR constant is intended for development only. |
||
| 154 | if (file_exists(__DIR__ . '/installed.lock')) |
||
| 155 | { |
||
| 156 | require_once($settings_location); |
||
| 157 | return; |
||
| 158 | } |
||
| 159 | |||
| 160 | // No lock present: decide if we should redirect to install/upgrade or continue |
||
| 161 | $this->handleNoLockInstallState($settings_location); |
||
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Decides installation vs upgrade vs a normal load when no lock file is present. |
||
| 166 | * |
||
| 167 | * Rules: |
||
| 168 | * - If Settings.php exists and $install_time is set (not 0/empty) and installer exists → redirect to upgrade.php |
||
| 169 | * - If Settings.php exists and $install_time is 0/empty and installer exists → redirect to install.php |
||
| 170 | * - If Settings.php does not exist and installer exists → redirect to install.php |
||
| 171 | * - Otherwise, just continue (and Settings.php will be loaded by caller if present) |
||
| 172 | * |
||
| 173 | * @param string $settings_location Absolute path to Settings.php |
||
| 174 | */ |
||
| 175 | private function handleNoLockInstallState($settings_location) |
||
| 176 | { |
||
| 177 | // Values defined in Settings |
||
| 178 | global $maintenance, $mtitle, $msubject, $mmessage, $mbname, $language, $boardurl, $webmaster_email; |
||
| 179 | global $cookiename, $db_type, $db_server, $db_port, $db_name, $db_user, $db_passwd; |
||
| 180 | global $ssi_db_user, $ssi_db_passwd, $db_prefix, $db_persist, $db_error_send; |
||
| 181 | global $cache_uid, $cache_password, $cache_enable, $cache_servers, $cache_accelerator; |
||
| 182 | global $db_show_debug, $url_format, $cachedir, $boarddir, $sourcedir, $extdir, $languagedir; |
||
| 183 | |||
| 184 | $hasInstallDir = is_dir(__DIR__ . '/install'); |
||
| 185 | $hasInstall = $hasInstallDir && file_exists(__DIR__ . '/install/install.php'); |
||
| 186 | $hasUpgrade = $hasInstallDir && file_exists(__DIR__ . '/install/upgrade.php'); |
||
| 187 | $hasSettings = file_exists($settings_location); |
||
| 188 | |||
| 189 | if ($hasSettings) |
||
| 190 | { |
||
| 191 | // Load to get $install_time and set the globals |
||
| 192 | require_once($settings_location); |
||
| 193 | } |
||
| 194 | |||
| 195 | // If we have an installer directory available, decide the proper entry |
||
| 196 | if (!defined('IGNORE_INSTALL_DIR') && ($hasInstall || $hasUpgrade)) |
||
| 197 | { |
||
| 198 | $redirect_file = 'install.php'; |
||
| 199 | |||
| 200 | if ($hasSettings) |
||
| 201 | { |
||
| 202 | // If install_time is non-empty and non-zero, prefer upgrade flow when available |
||
| 203 | $isInstalled = !empty($install_time) && $install_time !== '0'; |
||
| 204 | if ($isInstalled && $hasUpgrade && empty($_SESSION['installing'])) |
||
| 205 | { |
||
| 206 | $redirect_file = 'upgrade.php'; |
||
| 207 | } |
||
| 208 | } |
||
| 209 | |||
| 210 | // Build a safe, relative redirect to avoid host header issues |
||
| 211 | $version_running = defined('FORUM_VERSION') ? str_replace('ElkArte ', '', FORUM_VERSION) : ''; |
||
| 212 | header('Location: install/' . $redirect_file . '?v=' . $version_running); |
||
| 213 | die(); |
||
| 214 | } |
||
| 215 | |||
| 216 | // No installer available, nothing to redirect to; simply return |
||
| 217 | } |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Validate the paths set in Settings.php, correct as needed and move them to constants. |
||
| 221 | */ |
||
| 222 | private function validatePaths() |
||
| 223 | { |
||
| 224 | global $boarddir, $sourcedir, $cachedir, $extdir, $languagedir; |
||
| 225 | |||
| 226 | // Make sure the paths are correct... at least try to fix them. |
||
| 227 | if (!file_exists($boarddir) && file_exists(__DIR__ . '/bootstrap.php')) |
||
| 228 | { |
||
| 229 | $boarddir = __DIR__; |
||
| 230 | } |
||
| 231 | |||
| 232 | if (!file_exists($sourcedir . '/SiteDispatcher.class.php') && file_exists($boarddir . '/sources')) |
||
| 233 | { |
||
| 234 | $sourcedir = $boarddir . '/sources'; |
||
| 235 | } |
||
| 236 | |||
| 237 | // Check that directories which didn't exist in past releases are initialized. |
||
| 238 | if ((empty($cachedir) || !file_exists($cachedir)) && file_exists($boarddir . '/cache')) |
||
| 239 | { |
||
| 240 | $cachedir = $boarddir . '/cache'; |
||
| 241 | } |
||
| 242 | |||
| 243 | if ((empty($extdir) || !file_exists($extdir)) && file_exists($sourcedir . '/ext')) |
||
| 244 | { |
||
| 245 | $extdir = $sourcedir . '/ext'; |
||
| 246 | } |
||
| 247 | |||
| 248 | if ((empty($languagedir) || !file_exists($languagedir)) && file_exists($sourcedir . '/Languages/Index')) |
||
| 249 | { |
||
| 250 | $languagedir = $sourcedir . '/ElkArte/Languages'; |
||
| 251 | } |
||
| 252 | |||
| 253 | // Time to forget about variables and go with constants! |
||
| 254 | define('BOARDDIR', $boarddir); |
||
| 255 | define('CACHEDIR', $cachedir); |
||
| 256 | define('EXTDIR', $extdir); |
||
| 257 | define('LANGUAGEDIR', $languagedir); |
||
| 258 | define('SOURCEDIR', $sourcedir); |
||
| 259 | define('ADMINDIR', $sourcedir . '/ElkArte/AdminController'); |
||
| 260 | define('CONTROLLERDIR', $sourcedir . '/ElkArte/Controller'); |
||
| 261 | define('SUBSDIR', $sourcedir . '/subs'); |
||
| 262 | define('ADDONSDIR', $boarddir . '/Addons'); |
||
| 263 | define('ELKARTEDIR', $sourcedir . '/ElkArte'); |
||
| 264 | unset($boarddir, $cachedir, $sourcedir, $languagedir, $extdir); |
||
| 265 | } |
||
| 266 | |||
| 267 | /** |
||
| 268 | * We require access to several important files, so load them upfront |
||
| 269 | */ |
||
| 270 | private function loadDependants() |
||
| 271 | { |
||
| 272 | // Files we cannot live without. |
||
| 273 | require_once(SOURCEDIR . '/QueryString.php'); |
||
| 274 | require_once(SOURCEDIR . '/Session.php'); |
||
| 275 | require_once(SOURCEDIR . '/Subs.php'); |
||
| 276 | require_once(SOURCEDIR . '/Logging.php'); |
||
| 277 | require_once(SOURCEDIR . '/Load.php'); |
||
| 278 | require_once(SOURCEDIR . '/Security.php'); |
||
| 279 | require_once(SUBSDIR . '/Cache.subs.php'); |
||
| 280 | } |
||
| 281 | |||
| 282 | /** |
||
| 283 | * The autoloader will take care of most requests for files |
||
| 284 | */ |
||
| 285 | private function loadAutoloader() |
||
| 286 | { |
||
| 287 | require_once(EXTDIR . '/ClassLoader.php'); |
||
| 288 | |||
| 289 | $loader = new ClassLoader(); |
||
| 290 | $loader->setPsr4('ElkArte\\', SOURCEDIR . '/ElkArte'); |
||
| 291 | $loader->setPsr4('BBC\\', SOURCEDIR . '/ElkArte/BBC'); |
||
| 292 | $loader->setPsr4('Addons\\', BOARDDIR . '/Addons'); |
||
| 293 | $loader->register(); |
||
| 294 | } |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Check if we are in maintenance mode, if so end here. |
||
| 298 | */ |
||
| 299 | private function checkMaintance() |
||
| 320 | } |
||
| 321 | |||
| 322 | /** |
||
| 323 | * If you like lots of debug information in error messages and below the footer |
||
| 324 | * then set $db_show_debug to true in settings. Don't do this on a production site. |
||
| 325 | */ |
||
| 326 | private function setDebug() |
||
| 335 | } |
||
| 336 | } |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Time to see what has been requested, by whom and dispatch it to the proper handler |
||
| 340 | */ |
||
| 341 | private function bringUp() |
||
| 364 | } |
||
| 365 | } |
||
| 366 | |||
| 367 | /** |
||
| 368 | * If you are running SSI standalone, you need to call this function after bootstrap is |
||
| 369 | * initialized. |
||
| 370 | */ |
||
| 371 | public function ssi_main() |
||
| 504 | } |
||
| 505 | |||
| 506 | /** |
||
| 507 | * Used to ensure SSI requests are valid and not a probing attempt |
||
| 508 | */ |
||
| 509 | private function _validRequestCheck() |
||
| 522 | } |
||
| 523 | } |
||
| 524 | } |
||
| 525 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths