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

Returns all supported database driver installer objects.

Return value

\Drupal\Core\Database\Install\Tasks[] An array of available database driver installer objects.

Deprecated

in drupal:10.2.0 and is removed from drupal:11.0.0. Use DatabaseDriverList::getList() instead.

See also

https://www.drupal.org/node/3258175

1 call to drupal_get_database_types()
drupal_detect_database_types in core/includes/install.inc
Detects all supported databases that are compiled into PHP.

File

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

Code

function drupal_get_database_types() {
  @trigger_error('drupal_get_database_types() is deprecated in drupal:10.2.0 and is removed from drupal:11.0.0. Use DatabaseDriverList::getList() instead. See https://www.drupal.org/node/3258175', E_USER_DEPRECATED);
  $databases = [];
  $drivers = [];

  // The internal database driver name is any valid PHP identifier.
  $mask = ExtensionDiscovery::PHP_FUNCTION_PATTERN;

  // Find drivers in the Drupal\Driver namespace.
  // @todo remove discovering in the Drupal\Driver namespace in D10.

  /** @var \Drupal\Core\File\FileSystemInterface $file_system */
  $file_system = \Drupal::service('file_system');
  $files = [];
  if (is_dir(DRUPAL_ROOT . '/drivers/lib/Drupal/Driver/Database')) {
    $files = $file_system
      ->scanDirectory(DRUPAL_ROOT . '/drivers/lib/Drupal/Driver/Database/', $mask, [
      'recurse' => FALSE,
    ]);
  }
  foreach ($files as $file) {
    if (file_exists($file->uri . '/Install/Tasks.php')) {

      // The namespace doesn't need to be added here, because
      // db_installer_object() will find it.
      $drivers[$file->filename] = NULL;
    }
  }

  // Find drivers in Drupal module namespaces.

  /** @var \Composer\Autoload\ClassLoader $class_loader */
  $class_loader = \Drupal::service('class_loader');

  // We cannot use the file cache because it does not always exist.
  $extension_discovery = new ExtensionDiscovery(DRUPAL_ROOT, FALSE, []);
  $modules = $extension_discovery
    ->scan('module');
  foreach ($modules as $module) {
    $module_driver_path = DRUPAL_ROOT . '/' . $module
      ->getPath() . '/src/Driver/Database';
    if (is_dir($module_driver_path)) {
      $driver_files = $file_system
        ->scanDirectory($module_driver_path, $mask, [
        'recurse' => FALSE,
      ]);
      foreach ($driver_files as $driver_file) {
        $tasks_file = $module_driver_path . '/' . $driver_file->filename . '/Install/Tasks.php';
        if (file_exists($tasks_file)) {
          $namespace = 'Drupal\\' . $module
            ->getName() . '\\Driver\\Database\\' . $driver_file->filename;

          // Add the driver with its own classes' namespace.
          $drivers[$driver_file->filename] = $namespace;

          // The directory needs to be added to the autoloader, because this is
          // early in the installation process: the module hasn't been enabled
          // yet and the database connection info array (including its 'autoload'
          // key) hasn't been created yet.
          $class_loader
            ->addPsr4($namespace . '\\', $module
            ->getPath() . '/src/Driver/Database/' . $driver_file->filename);
        }
      }
    }
  }
  foreach ($drivers as $driver => $namespace) {
    $installer_class = $namespace . "\\Install\\Tasks";
    $installer = new $installer_class();
    if ($installer
      ->installable()) {
      $databases[$driver] = $installer;
    }
  }

  // Usability: unconditionally put the MySQL driver on top.
  if (isset($databases['mysql'])) {
    $mysql_database = $databases['mysql'];
    unset($databases['mysql']);
    $databases = [
      'mysql' => $mysql_database,
    ] + $databases;
  }
  return $databases;
}