| Total Complexity | 86 |
| Total Lines | 458 |
| Duplicated Lines | 0 % |
| Changes | 8 | ||
| Bugs | 1 | 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 |
||
| 24 | class Bootstrap |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * Bootstrap constructor. |
||
| 28 | * |
||
| 29 | * @param bool $standalone |
||
| 30 | * - true to boot outside of elkarte |
||
| 31 | * - false to bootstrap the main elkarte site. |
||
| 32 | * @throws \Elk_Exception |
||
| 33 | */ |
||
| 34 | public function __construct($standalone = true) |
||
| 35 | { |
||
| 36 | // Bootstrap only once. |
||
| 37 | if (defined('ELKBOOT')) |
||
| 38 | { |
||
| 39 | return true; |
||
| 40 | } |
||
| 41 | |||
| 42 | // We're going to set a few globals |
||
| 43 | global $time_start, $ssi_error_reporting, $db_show_debug; |
||
| 44 | |||
| 45 | // Your on the clock |
||
| 46 | $time_start = microtime(true); |
||
| 47 | |||
| 48 | // Unless settings.php tells us otherwise |
||
| 49 | $db_show_debug = false; |
||
| 50 | |||
| 51 | // Report errors but not depreciated ones |
||
| 52 | $ssi_error_reporting = error_reporting(E_ALL | E_STRICT & ~8192); |
||
| 53 | |||
| 54 | // Get the things needed for ALL modes |
||
| 55 | $this->bringUpBasics(); |
||
| 56 | |||
| 57 | // Going to run from the side entrance and not directly from inside elkarte |
||
| 58 | if ($standalone) |
||
| 59 | { |
||
| 60 | $this->ssi_main(); |
||
| 61 | } |
||
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Calls the various initialization functions in the needed order |
||
| 66 | */ |
||
| 67 | public function bringUpBasics() |
||
| 68 | { |
||
| 69 | $this->setConstants(); |
||
| 70 | $this->setRusage(); |
||
| 71 | $this->clearGloballs(); |
||
| 72 | $this->loadSettingsFile(); |
||
| 73 | $this->validatePaths(); |
||
| 74 | $this->loadDependants(); |
||
| 75 | $this->loadAutoloader(); |
||
| 76 | $this->checkMaintance(); |
||
| 77 | $this->setDebug(); |
||
| 78 | $this->bringUp(); |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Set the core constants, you know the ones we often forget to |
||
| 83 | * update on new releases. |
||
| 84 | */ |
||
| 85 | private function setConstants() |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Get initial resource usage |
||
| 103 | */ |
||
| 104 | private function setRusage() |
||
| 117 | } |
||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * If they glo, they need to be cleaned. |
||
| 122 | */ |
||
| 123 | private function clearGloballs() |
||
| 131 | } |
||
| 132 | } |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Loads the settings values into the global space |
||
| 137 | */ |
||
| 138 | private function loadSettingsFile() |
||
| 139 | { |
||
| 140 | // All those wonderful things found in settings |
||
| 141 | global $maintenance, $mtitle, $msubject, $mmessage, $mbname, $language, $boardurl, $webmaster_email; |
||
| 142 | global $cookiename, $db_type, $db_server, $db_port, $db_name, $db_user, $db_passwd; |
||
| 143 | global $ssi_db_user, $ssi_db_passwd, $db_prefix, $db_persist, $db_error_send, $cache_accelerator; |
||
| 144 | global $cache_uid, $cache_password, $cache_enable, $cache_memcached, $db_show_debug; |
||
| 145 | global $cachedir, $boarddir, $sourcedir, $extdir, $languagedir, $ignore_install_dir; |
||
| 146 | |||
| 147 | // Where the Settings.php file is located |
||
| 148 | $settings_loc = __DIR__ . '/Settings.php'; |
||
| 149 | |||
| 150 | // First thing: if the install dir exists, just send anybody there |
||
| 151 | // The ignore_install_dir var is for developers only. Do not add it on production sites |
||
| 152 | if (file_exists('install') && (file_exists('install/install.php') || file_exists('install/upgrade.php'))) |
||
| 153 | { |
||
| 154 | if (file_exists($settings_loc)) |
||
| 155 | { |
||
| 156 | require_once($settings_loc); |
||
| 157 | } |
||
| 158 | |||
| 159 | if (empty($ignore_install_dir)) |
||
| 160 | { |
||
| 161 | if (file_exists($settings_loc) && empty($_SESSION['installing'])) |
||
| 162 | { |
||
| 163 | $redirec_file = 'upgrade.php'; |
||
| 164 | } |
||
| 165 | else |
||
| 166 | { |
||
| 167 | $redirec_file = 'install.php'; |
||
| 168 | } |
||
| 169 | |||
| 170 | $version_running = str_replace('ElkArte ', '', FORUM_VERSION); |
||
| 171 | $proto = 'http' . (!empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) === 'on' ? 's' : ''); |
||
| 172 | $port = empty($_SERVER['SERVER_PORT']) || $_SERVER['SERVER_PORT'] === '80' ? '' : ':' . $_SERVER['SERVER_PORT']; |
||
| 173 | $host = empty($_SERVER['HTTP_HOST']) ? $_SERVER['SERVER_NAME'] . $port : $_SERVER['HTTP_HOST']; |
||
| 174 | $path = strtr(dirname($_SERVER['PHP_SELF']), '\\', '/') == '/' ? '' : strtr(dirname($_SERVER['PHP_SELF']), '\\', '/'); |
||
| 175 | |||
| 176 | header('Location:' . $proto . '://' . $host . $path . '/install/' . $redirec_file . '?v=' . $version_running); |
||
| 177 | die(); |
||
| 178 | } |
||
| 179 | } |
||
| 180 | else |
||
| 181 | { |
||
| 182 | require_once($settings_loc); |
||
| 183 | } |
||
| 184 | } |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Validate the paths set in Settings.php, correct as needed and move |
||
| 188 | * them to constants. |
||
| 189 | */ |
||
| 190 | private function validatePaths() |
||
| 191 | { |
||
| 192 | global $boarddir, $sourcedir, $cachedir, $extdir, $languagedir; |
||
| 193 | |||
| 194 | // Make sure the paths are correct... at least try to fix them. |
||
| 195 | if (!file_exists($boarddir) && file_exists(__DIR__ . '/agreement.txt')) |
||
| 196 | { |
||
| 197 | $boarddir = __DIR__; |
||
| 198 | } |
||
| 199 | |||
| 200 | if (!file_exists($sourcedir . '/SiteDispatcher.class.php') && file_exists($boarddir . '/sources')) |
||
| 201 | { |
||
| 202 | $sourcedir = $boarddir . '/sources'; |
||
| 203 | } |
||
| 204 | |||
| 205 | // Check that directories which didn't exist in past releases are initialized. |
||
| 206 | if ((empty($cachedir) || !file_exists($cachedir)) && file_exists($boarddir . '/cache')) |
||
| 207 | { |
||
| 208 | $cachedir = $boarddir . '/cache'; |
||
| 209 | } |
||
| 210 | |||
| 211 | if ((empty($extdir) || !file_exists($extdir)) && file_exists($sourcedir . '/ext')) |
||
| 212 | { |
||
| 213 | $extdir = $sourcedir . '/ext'; |
||
| 214 | } |
||
| 215 | |||
| 216 | if ((empty($languagedir) || !file_exists($languagedir)) && file_exists($boarddir . '/themes/default/languages')) |
||
| 217 | { |
||
| 218 | $languagedir = $boarddir . '/themes/default/languages'; |
||
| 219 | } |
||
| 220 | |||
| 221 | // Time to forget about variables and go with constants! |
||
| 222 | define('BOARDDIR', $boarddir); |
||
| 223 | define('CACHEDIR', $cachedir); |
||
| 224 | define('EXTDIR', $extdir); |
||
| 225 | define('LANGUAGEDIR', $languagedir); |
||
| 226 | define('SOURCEDIR', $sourcedir); |
||
| 227 | define('ADMINDIR', $sourcedir . '/admin'); |
||
| 228 | define('CONTROLLERDIR', $sourcedir . '/controllers'); |
||
| 229 | define('SUBSDIR', $sourcedir . '/subs'); |
||
| 230 | define('ADDONSDIR', $boarddir . '/addons'); |
||
| 231 | unset($boarddir, $cachedir, $sourcedir, $languagedir, $extdir); |
||
| 232 | } |
||
| 233 | |||
| 234 | /** |
||
| 235 | * We require access to several important files, so load them upfront |
||
| 236 | */ |
||
| 237 | private function loadDependants() |
||
| 238 | { |
||
| 239 | // Files we cannot live without. |
||
| 240 | require_once(SOURCEDIR . '/QueryString.php'); |
||
| 241 | require_once(SOURCEDIR . '/Session.php'); |
||
| 242 | require_once(SOURCEDIR . '/Subs.php'); |
||
| 243 | require_once(SOURCEDIR . '/Logging.php'); |
||
| 244 | require_once(SOURCEDIR . '/Load.php'); |
||
| 245 | require_once(SOURCEDIR . '/Security.php'); |
||
| 246 | require_once(SUBSDIR . '/Cache.subs.php'); |
||
| 247 | } |
||
| 248 | |||
| 249 | /** |
||
| 250 | * The autoloader will take care most requests for files |
||
| 251 | */ |
||
| 252 | private function loadAutoloader() |
||
| 253 | { |
||
| 254 | // Initialize the class Autoloader |
||
| 255 | require_once(SOURCEDIR . '/Autoloader.class.php'); |
||
| 256 | $autoloader = Elk_Autoloader::instance(); |
||
| 257 | $autoloader->setupAutoloader(array(SOURCEDIR, SUBSDIR, CONTROLLERDIR, ADMINDIR, ADDONSDIR)); |
||
| 258 | $autoloader->register(SOURCEDIR, '\\ElkArte'); |
||
| 259 | $autoloader->register(SOURCEDIR . '/subs/BBC', '\\BBC'); |
||
| 260 | } |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Check if we are in maintance mode, if so end here. |
||
| 264 | */ |
||
| 265 | private function checkMaintance() |
||
| 273 | } |
||
| 274 | } |
||
| 275 | |||
| 276 | /** |
||
| 277 | * If you like lots of debug information in error messages and below the footer |
||
| 278 | * then set $db_show_debug to true in settings. Don't do this on a production site. |
||
| 279 | */ |
||
| 280 | private function setDebug() |
||
| 288 | } |
||
| 289 | } |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Time to see what has been requested, by whom and dispatch it to the proper handler |
||
| 293 | */ |
||
| 294 | private function bringUp() |
||
| 318 | } |
||
| 319 | |||
| 320 | /** |
||
| 321 | * If you are running SSI standalone, you need to call this function after bootstrap is |
||
| 322 | * initialized. |
||
| 323 | * |
||
| 324 | * @throws \Elk_Exception |
||
| 325 | */ |
||
| 326 | public function ssi_main() |
||
| 455 | } |
||
| 456 | } |
||
| 457 | |||
| 458 | /** |
||
| 459 | * Used to ensure SSI requests are valid and not a probing attempt |
||
| 460 | */ |
||
| 461 | private function _validRequestCheck() |
||
| 482 | } |
||
| 483 | } |
||
| 484 | } |
||
| 485 |