function drupal_rewrite_settings

Replaces values in settings.php with values in the submitted array.

Parameters

$settings: An array of settings that need to be updated.

2 calls to drupal_rewrite_settings()
drupal_install_config_directories in drupal/core/includes/install.inc
Creates the config directory and ensures it is operational.
install_settings_form_submit in drupal/core/includes/install.core.inc
Form submission handler for install_settings_form().

File

drupal/core/includes/install.inc, line 185
API functions for installing modules and themes.

Code

function drupal_rewrite_settings($settings = array()) {
  drupal_static_reset('conf_path');
  $settings_file = conf_path(FALSE) . '/settings.php';

  // Build list of setting names and insert the values into the global namespace.
  $keys = array();
  foreach ($settings as $setting => $data) {
    $GLOBALS[$setting] = $data['value'];
    $keys[] = $setting;
  }
  $buffer = NULL;
  $contents = file_get_contents(DRUPAL_ROOT . '/' . $settings_file);
  if ($contents !== FALSE) {

    // Step through each token in settings.php and replace any variables that
    // are in the passed-in array.
    $replacing_variable = FALSE;
    foreach (token_get_all($contents) as $token) {

      // Strip off the leading "$" before comparing the variable name.
      if (is_array($token) && $token[0] == T_VARIABLE && ($variable_name = substr($token[1], 1)) && in_array($variable_name, $keys)) {

        // Write the new value to settings.php in the following format:
        //    $[setting] = '[value]'; // [comment]
        $setting = $settings[$variable_name];
        $buffer .= '$' . $variable_name . ' = ' . var_export($setting['value'], TRUE) . ';';
        if (!empty($setting['comment'])) {
          $buffer .= ' // ' . $setting['comment'];
        }
        unset($settings[$variable_name]);
        $replacing_variable = TRUE;
      }
      else {

        // Write a regular token (that is not part of a variable we're
        // replacing) to settings.php directly.
        if (!$replacing_variable) {
          $buffer .= is_array($token) ? $token[1] : $token;
        }

        // When we hit a semicolon, we are done with the code that defines the
        // variable that is being replaced.
        if ($token == ';') {
          $replacing_variable = FALSE;
        }
      }
    }

    // Add required settings that were missing from settings.php.
    foreach ($settings as $setting => $data) {
      if (!empty($data['required'])) {
        $buffer .= "\${$setting} = " . var_export($data['value'], TRUE) . ";\n";
      }
    }

    // Write the new settings file.
    if (file_put_contents(DRUPAL_ROOT . '/' . $settings_file, $buffer) === FALSE) {
      throw new Exception(st('Failed to modify %settings. Verify the file permissions.', array(
        '%settings' => $settings_file,
      )));
    }
  }
  else {
    throw new Exception(st('Failed to open %settings. Verify the file permissions.', array(
      '%settings' => $settings_file,
    )));
  }
}