You are here

function install_drupal in Drupal 10

Same name and namespace in other branches
  1. 8 core/includes/install.core.inc \install_drupal()
  2. 7 includes/install.core.inc \install_drupal()
  3. 9 core/includes/install.core.inc \install_drupal()

Installs Drupal either interactively or via an array of passed-in settings.

The Drupal installation happens in a series of steps, which may be spread out over multiple page requests. Each request begins by trying to determine the last completed installation step (also known as a "task"), if one is available from a previous request. Control is then passed to the task handler, which processes the remaining tasks that need to be run until (a) an error is thrown, (b) a new page needs to be displayed, or (c) the installation finishes (whichever happens first).

Parameters

$class_loader: The class loader. Normally Composer's ClassLoader, as included by the front controller, but may also be decorated; e.g., \Symfony\Component\ClassLoader\ApcClassLoader.

$settings: An optional array of installation settings. Leave this empty for a normal, interactive, browser-based installation intended to occur over multiple page requests. Alternatively, if an array of settings is passed in, the installer will attempt to use it to perform the installation in a single page request (optimized for the command line) and not send any output intended for the web browser. See install_state_defaults() for a list of elements that are allowed to appear in this array.

callable $callback: (optional) A callback to allow command line processes to update a progress bar. The callback is passed the $install_state variable.

See also

install_state_defaults()

3 calls to install_drupal()
FunctionalTestSetupTrait::doInstall in core/lib/Drupal/Core/Test/FunctionalTestSetupTrait.php
Execute the non-interactive installer.
install.php in core/install.php
Initiates a browser-based installation of Drupal.
InstallCommand::install in core/lib/Drupal/Core/Command/InstallCommand.php
Installs Drupal with specified installation profile.

File

core/includes/install.core.inc, line 105
API functions for installing Drupal.

Code

function install_drupal($class_loader, $settings = [], callable $callback = NULL) {
  global $install_state;

  // Initialize the installation state with the settings that were passed in,
  // as well as a boolean indicating whether or not this is an interactive
  // installation.
  $interactive = empty($settings);
  $install_state = $settings + [
    'interactive' => $interactive,
  ] + install_state_defaults();
  try {

    // Begin the page request. This adds information about the current state of
    // the Drupal installation to the passed-in array.
    install_begin_request($class_loader, $install_state);

    // Based on the installation state, run the remaining tasks for this page
    // request, and collect any output.
    $output = install_run_tasks($install_state, $callback);
  } catch (InstallerException $e) {

    // In the non-interactive installer, exceptions are always thrown directly.
    if (!$install_state['interactive']) {
      throw $e;
    }
    $output = [
      '#title' => $e
        ->getTitle(),
      '#markup' => $e
        ->getMessage(),
    ];
  }

  // After execution, all tasks might be complete, in which case
  // $install_state['installation_finished'] is TRUE. In case the last task
  // has been processed, remove the global $install_state, so other code can
  // reliably check whether it is running during the installer.
  // @see \Drupal\Core\Installer\InstallerKernel::installationAttempted()
  $state = $install_state;
  if (!empty($install_state['installation_finished'])) {
    unset($GLOBALS['install_state']);

    // If installation is finished ensure any further container rebuilds do not
    // use the installer's service provider.
    unset($GLOBALS['conf']['container_service_providers']['InstallerServiceProvider']);
  }

  // All available tasks for this page request are now complete. Interactive
  // installations can send output to the browser or redirect the user to the
  // next page.
  if ($state['interactive']) {

    // If a session has been initiated in this request, make sure to save it.
    if (\Drupal::request()
      ->hasSession()) {
      \Drupal::request()
        ->getSession()
        ->save();
    }
    if ($state['parameters_changed']) {

      // Redirect to the correct page if the URL parameters have changed.
      install_goto(install_redirect_url($state));
    }
    elseif (isset($output)) {

      // Display a page only if some output is available. Otherwise it is
      // possible that we are printing a JSON page and theme output should
      // not be shown.
      install_display_output($output, $state);
    }
    elseif ($state['installation_finished']) {

      // Redirect to the newly installed site.
      $finish_url = '';
      if (isset($install_state['profile_info']['distribution']['install']['finish_url'])) {
        $finish_url = $install_state['profile_info']['distribution']['install']['finish_url'];
      }
      install_goto($finish_url);
    }
  }
}