You are here

function environment_switch in Environment 6

Same name and namespace in other branches
  1. 8 environment.module \environment_switch()
  2. 7 environment.module \environment_switch()

Switches between two environments.

Parameters

String $target_env: Name of the environment to change to.

Boolean $force: (optional) Whether to trigger a change even if the environment is the currently set one. Defaults to FALSE.

Return value

Return messages telling what has happened.

4 calls to environment_switch()
drush_environment_switch in ./environment.drush.inc
Implementation of drush_hook_COMMAND for environment_switch.
environment_force_init in modules/environment_force/environment_force.module
Implementation of hook_init().
environment_switch_confirm_submit in ./environment.admin.inc
Handler for switch environment confirmation.
environment_switch_environment_submit in ./environment.admin.inc
Submit callback to switch environment if changed.

File

./environment.module, line 129

Code

function environment_switch($target_env, $force = FALSE) {
  $result = FALSE;
  $messages = array();
  $target_state = environment_load($target_env);
  $workflow = $target_state['workflow'];
  $current_env = environment_current($workflow);
  $override = variable_get('environment_override', '');
  if ($current_env == $target_env) {
    drupal_set_message(t("The current environment is already set to '!environment'.", array(
      '!environment' => $target_env,
    )), 'error');

    // Since this option is only available in drush, only display it to drush users.
    if (function_exists('drush_print')) {
      drush_print("To force the environment switch to run anyway, use the '--force' flag.");
    }
  }
  if (!$force && !empty($override)) {
    drupal_set_message(t("The current environment is overriden with '!override'.", array(
      '!override' => $override,
    )), 'error');

    // Since this option is only available in drush, only display it to drush users.
    if (function_exists('drush_print')) {
      drush_print("To force the environment switch to run anyway, use the '--force' flag.");
    }
  }
  elseif ($current_env != $target_env || $force) {
    if (empty($target_state)) {
      drupal_set_message(t('Environment !environment does not exist.', array(
        '!environment' => $target_env,
      )), 'warning');
    }
    else {
      environment_set($target_env);
      module_invoke_all('environment_switch', $target_env, $current_env, $workflow);
      drupal_flush_all_caches();
      drupal_set_message('Cleared cache.');
      $result = TRUE;
    }
  }
  return $result;
}