1.20.x bootstrap.inc backdrop_bootstrap($phase = NULL, $new_phase = TRUE)

Ensures Backdrop is bootstrapped to the specified phase.

In order to bootstrap Backdrop from another PHP script, you can use this code:

  define('BACKDROP_ROOT', '/path/to/backdrop');
  require_once BACKDROP_ROOT . '/core/includes/bootstrap.inc';
  backdrop_bootstrap(BACKDROP_BOOTSTRAP_FULL);

Parameters

int $phase: A constant telling which phase to bootstrap to. When you bootstrap to a particular phase, all earlier phases are run automatically. Possible values:

boolean $new_phase: A boolean, set to FALSE if calling backdrop_bootstrap from inside a function called from backdrop_bootstrap (recursion).

Return value

int: The most recently completed phase.

Throws

Exception This function rethrows any exceptions it encounters, unless Backdrop has not yet been installed, in which case it will redirect to the installer.

File

includes/bootstrap.inc, line 3027
Functions that need to be loaded on every Backdrop request.

Code

function backdrop_bootstrap($phase = NULL, $new_phase = TRUE) {
  // Not backdrop_static(), because does not depend on any run-time information.
  static $phases = array(
    BACKDROP_BOOTSTRAP_CONFIGURATION,
    BACKDROP_BOOTSTRAP_PAGE_CACHE,
    BACKDROP_BOOTSTRAP_DATABASE,
    BACKDROP_BOOTSTRAP_LOCK,
    BACKDROP_BOOTSTRAP_VARIABLES,
    BACKDROP_BOOTSTRAP_SESSION,
    BACKDROP_BOOTSTRAP_PAGE_HEADER,
    BACKDROP_BOOTSTRAP_LANGUAGE,
    BACKDROP_BOOTSTRAP_FULL,
  );
  // Not backdrop_static(), because the only legitimate API to control this is to
  // call backdrop_bootstrap() with a new phase parameter.
  static $final_phase;
  // Not backdrop_static(), because it's impossible to roll back to an earlier
  // bootstrap state.
  static $stored_phase = -1;

  if (isset($phase)) {
    // When not recursing, store the phase name so it's not forgotten while
    // recursing but take care of not going backwards.
    if ($new_phase && $phase >= $stored_phase) {
      $final_phase = $phase;
    }

    // Enter the requested phase if it is after the current phase.
    while ($phases && $phase > $stored_phase && $final_phase > $stored_phase) {
      $current_phase = array_shift($phases);

      // This function is re-entrant. Only update the completed phase when the
      // current call actually resulted in a progress in the bootstrap process.
      if ($current_phase > $stored_phase) {
        $stored_phase = $current_phase;
      }

      try {
        switch ($current_phase) {
          case BACKDROP_BOOTSTRAP_CONFIGURATION:
            _backdrop_bootstrap_configuration();
            break;

          case BACKDROP_BOOTSTRAP_PAGE_CACHE:
            _backdrop_bootstrap_page_cache();
            break;

          case BACKDROP_BOOTSTRAP_DATABASE:
            _backdrop_bootstrap_database();
            break;

          case BACKDROP_BOOTSTRAP_LOCK:
            require_once BACKDROP_ROOT . '/' . settings_get('lock_inc', 'core/includes/lock.inc');
            lock_initialize();
            break;

          case BACKDROP_BOOTSTRAP_VARIABLES:
            _backdrop_bootstrap_variables();
            break;

          case BACKDROP_BOOTSTRAP_SESSION:
            require_once BACKDROP_ROOT . '/' . settings_get('session_inc', 'core/includes/session.inc');
            backdrop_session_initialize();
            break;

          case BACKDROP_BOOTSTRAP_PAGE_HEADER:
            _backdrop_bootstrap_page_header();
            break;

          case BACKDROP_BOOTSTRAP_LANGUAGE:
            backdrop_language_initialize();
            break;

          case BACKDROP_BOOTSTRAP_FULL:
            require_once BACKDROP_ROOT . '/core/includes/common.inc';
            _backdrop_bootstrap_full();
            break;
        }
      }
      catch (Exception $e) {
        // Check if we have a valid installation in the first place.
        if (!backdrop_bootstrap_is_installed()) {
          include_once BACKDROP_ROOT . '/core/includes/install.inc';
          install_goto('core/install.php');
        }

        // If Backdrop is installed correctly, rethrow any exceptions to the
        // front-end. The error will be displayed with _backdrop_log_error().
        throw $e;
      }
    }
  }
  return $stored_phase;
}