| Conditions | 41 |
| Paths | 4554 |
| Total Lines | 179 |
| Code Lines | 98 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 226 | public function doBuild($quiet = false, $populate = true, $testMode = false) |
||
| 227 | { |
||
| 228 | if ($quiet) { |
||
| 229 | DB::quiet(); |
||
| 230 | } else { |
||
| 231 | $conn = DB::get_conn(); |
||
| 232 | // Assumes database class is like "MySQLDatabase" or "MSSQLDatabase" (suffixed with "Database") |
||
| 233 | $dbType = substr(get_class($conn), 0, -8); |
||
| 234 | $dbVersion = $conn->getVersion(); |
||
| 235 | $databaseName = $conn->getSelectedDatabase(); |
||
| 236 | |||
| 237 | if (Director::is_cli()) { |
||
| 238 | echo sprintf("\n\nBuilding database %s using %s %s\n\n", $databaseName, $dbType, $dbVersion); |
||
| 239 | } else { |
||
| 240 | echo sprintf("<h2>Building database %s using %s %s</h2>", $databaseName, $dbType, $dbVersion); |
||
| 241 | } |
||
| 242 | } |
||
| 243 | |||
| 244 | // Set up the initial database |
||
| 245 | if (!DB::is_active()) { |
||
| 246 | if (!$quiet) { |
||
| 247 | echo '<p><b>Creating database</b></p>'; |
||
| 248 | } |
||
| 249 | |||
| 250 | // Load parameters from existing configuration |
||
| 251 | $databaseConfig = DB::getConfig(); |
||
| 252 | if (empty($databaseConfig) && empty($_REQUEST['db'])) { |
||
| 253 | throw new BadMethodCallException("No database configuration available"); |
||
| 254 | } |
||
| 255 | $parameters = (!empty($databaseConfig)) ? $databaseConfig : $_REQUEST['db']; |
||
| 256 | |||
| 257 | // Check database name is given |
||
| 258 | if (empty($parameters['database'])) { |
||
| 259 | throw new BadMethodCallException( |
||
| 260 | "No database name given; please give a value for SS_DATABASE_NAME or set SS_DATABASE_CHOOSE_NAME" |
||
| 261 | ); |
||
| 262 | } |
||
| 263 | $database = $parameters['database']; |
||
| 264 | |||
| 265 | // Establish connection |
||
| 266 | unset($parameters['database']); |
||
| 267 | DB::connect($parameters); |
||
| 268 | |||
| 269 | // Check to ensure that the re-instated SS_DATABASE_SUFFIX functionality won't unexpectedly |
||
| 270 | // rename the database. To be removed for SS5 |
||
| 271 | if ($suffix = Environment::getEnv('SS_DATABASE_SUFFIX')) { |
||
| 272 | $previousName = preg_replace("/{$suffix}$/", '', $database); |
||
| 273 | |||
| 274 | if (!isset($_GET['force_suffix_rename']) && DB::get_conn()->databaseExists($previousName)) { |
||
| 275 | throw new DatabaseException( |
||
| 276 | "SS_DATABASE_SUFFIX was previously broken, but has now been fixed. This will result in your " |
||
| 277 | . "database being named \"{$database}\" instead of \"{$previousName}\" from now on. If this " |
||
| 278 | . "change is intentional, please visit dev/build?force_suffix_rename=1 to continue" |
||
| 279 | ); |
||
| 280 | } |
||
| 281 | } |
||
| 282 | |||
| 283 | // Create database |
||
| 284 | DB::create_database($database); |
||
| 285 | } |
||
| 286 | |||
| 287 | // Build the database. Most of the hard work is handled by DataObject |
||
| 288 | $dataClasses = ClassInfo::subclassesFor(DataObject::class); |
||
| 289 | array_shift($dataClasses); |
||
| 290 | |||
| 291 | if (!$quiet) { |
||
| 292 | if (Director::is_cli()) { |
||
| 293 | echo "\nCREATING DATABASE TABLES\n\n"; |
||
| 294 | } else { |
||
| 295 | echo "\n<p><b>Creating database tables</b></p><ul>\n\n"; |
||
| 296 | } |
||
| 297 | } |
||
| 298 | |||
| 299 | $showRecordCounts = (boolean)$this->config()->show_record_counts; |
||
| 300 | |||
| 301 | // Initiate schema update |
||
| 302 | $dbSchema = DB::get_schema(); |
||
| 303 | $dbSchema->schemaUpdate(function () use ($dataClasses, $testMode, $quiet, $showRecordCounts) { |
||
| 304 | $dataObjectSchema = DataObject::getSchema(); |
||
| 305 | |||
| 306 | foreach ($dataClasses as $dataClass) { |
||
| 307 | // Check if class exists before trying to instantiate - this sidesteps any manifest weirdness |
||
| 308 | if (!class_exists($dataClass)) { |
||
| 309 | continue; |
||
| 310 | } |
||
| 311 | |||
| 312 | // Check if this class should be excluded as per testing conventions |
||
| 313 | $SNG = singleton($dataClass); |
||
| 314 | if (!$testMode && $SNG instanceof TestOnly) { |
||
| 315 | continue; |
||
| 316 | } |
||
| 317 | $tableName = $dataObjectSchema->tableName($dataClass); |
||
| 318 | |||
| 319 | // Log data |
||
| 320 | if (!$quiet) { |
||
| 321 | if ($showRecordCounts && DB::get_schema()->hasTable($tableName)) { |
||
| 322 | try { |
||
| 323 | $count = DB::query("SELECT COUNT(*) FROM \"$tableName\"")->value(); |
||
| 324 | $countSuffix = " ($count records)"; |
||
| 325 | } catch (\Exception $e) { |
||
| 326 | $countSuffix = " (error getting record count)"; |
||
| 327 | } |
||
| 328 | } else { |
||
| 329 | $countSuffix = ""; |
||
| 330 | } |
||
| 331 | |||
| 332 | if (Director::is_cli()) { |
||
| 333 | echo " * $tableName$countSuffix\n"; |
||
| 334 | } else { |
||
| 335 | echo "<li>$tableName$countSuffix</li>\n"; |
||
| 336 | } |
||
| 337 | } |
||
| 338 | |||
| 339 | // Instruct the class to apply its schema to the database |
||
| 340 | $SNG->requireTable(); |
||
| 341 | } |
||
| 342 | }); |
||
| 343 | ClassInfo::reset_db_cache(); |
||
| 344 | |||
| 345 | if (!$quiet && !Director::is_cli()) { |
||
| 346 | echo "</ul>"; |
||
| 347 | } |
||
| 348 | |||
| 349 | if ($populate) { |
||
| 350 | if (!$quiet) { |
||
| 351 | if (Director::is_cli()) { |
||
| 352 | echo "\nCREATING DATABASE RECORDS\n\n"; |
||
| 353 | } else { |
||
| 354 | echo "\n<p><b>Creating database records</b></p><ul>\n\n"; |
||
| 355 | } |
||
| 356 | } |
||
| 357 | |||
| 358 | // Remap obsolete class names |
||
| 359 | $remappingConfig = $this->config()->get('classname_value_remapping'); |
||
| 360 | $remappingFields = $this->getClassNameRemappingFields(); |
||
| 361 | foreach ($remappingFields as $className => $fieldNames) { |
||
| 362 | foreach ($fieldNames as $fieldName) { |
||
| 363 | foreach ($remappingConfig as $oldClassName => $newClassName) { |
||
| 364 | $this->updateLegacyClassNames($className, $fieldName, $oldClassName, $newClassName); |
||
| 365 | } |
||
| 366 | } |
||
| 367 | } |
||
| 368 | |||
| 369 | // Require all default records |
||
| 370 | foreach ($dataClasses as $dataClass) { |
||
| 371 | // Check if class exists before trying to instantiate - this sidesteps any manifest weirdness |
||
| 372 | // Test_ indicates that it's the data class is part of testing system |
||
| 373 | if (strpos($dataClass, 'Test_') === false && class_exists($dataClass)) { |
||
| 374 | if (!$quiet) { |
||
| 375 | if (Director::is_cli()) { |
||
| 376 | echo " * $dataClass\n"; |
||
| 377 | } else { |
||
| 378 | echo "<li>$dataClass</li>\n"; |
||
| 379 | } |
||
| 380 | } |
||
| 381 | |||
| 382 | DataObject::singleton($dataClass)->requireDefaultRecords(); |
||
| 383 | } |
||
| 384 | } |
||
| 385 | |||
| 386 | if (!$quiet && !Director::is_cli()) { |
||
| 387 | echo "</ul>"; |
||
| 388 | } |
||
| 389 | } |
||
| 390 | |||
| 391 | touch(TEMP_PATH |
||
| 392 | . DIRECTORY_SEPARATOR |
||
| 393 | . 'database-last-generated-' |
||
| 394 | . str_replace(array('\\', '/', ':'), '.', Director::baseFolder())); |
||
| 395 | |||
| 396 | if (isset($_REQUEST['from_installer'])) { |
||
| 397 | echo "OK"; |
||
| 398 | } |
||
| 399 | |||
| 400 | if (!$quiet) { |
||
| 401 | echo (Director::is_cli()) ? "\n Database build completed!\n\n" :"<p>Database build completed!</p>"; |
||
| 402 | } |
||
| 403 | |||
| 404 | ClassInfo::reset_db_cache(); |
||
| 405 | } |
||
| 535 |