You are here

public static function Database::convertDbUrlToConnectionInfo in Drupal 10

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Database/Database.php \Drupal\Core\Database\Database::convertDbUrlToConnectionInfo()
  2. 9 core/lib/Drupal/Core/Database/Database.php \Drupal\Core\Database\Database::convertDbUrlToConnectionInfo()

Converts a URL to a database connection info array.

Parameters

string $url: The URL.

string $root: The root directory of the Drupal installation.

Return value

array The database connection info.

Throws

\InvalidArgumentException Exception thrown when the provided URL does not meet the minimum requirements.

\RuntimeException Exception thrown when a module provided database driver does not exist.

11 calls to Database::convertDbUrlToConnectionInfo()
DbCommandBase::getDatabaseConnection in core/lib/Drupal/Core/Command/DbCommandBase.php
Parse input options decide on a database.
KernelTestBase::getDatabaseConnectionInfo in core/tests/Drupal/KernelTests/KernelTestBase.php
Returns the Database connection info to be used for this test.
KernelTestBaseDatabaseDriverModuleTest::getDatabaseConnectionInfo in core/tests/Drupal/KernelTests/KernelTestBaseDatabaseDriverModuleTest.php
Returns the Database connection info to be used for this test.
TestSetupTrait::changeDatabasePrefix in core/lib/Drupal/Core/Test/TestSetupTrait.php
Changes the database connection to the prefixed one.
TestSetupTraitTest::testChangeDatabasePrefix in core/tests/Drupal/Tests/Core/Test/TestSetupTraitTest.php
Tests the SIMPLETEST_DB environment variable is used.

... See full list

File

core/lib/Drupal/Core/Database/Database.php, line 465

Class

Database
Primary front-controller for the database system.

Namespace

Drupal\Core\Database

Code

public static function convertDbUrlToConnectionInfo($url, $root) {

  // Check that the URL is well formed, starting with 'scheme://', where
  // 'scheme' is a database driver name.
  if (preg_match('/^(.*):\\/\\//', $url, $matches) !== 1) {
    throw new \InvalidArgumentException("Missing scheme in URL '{$url}'");
  }
  $driver = $matches[1];

  // Determine if the database driver is provided by a module.
  // @todo https://www.drupal.org/project/drupal/issues/3250999. Refactor when
  // all database drivers are provided by modules.
  $module = NULL;
  $connection_class = NULL;
  $url_components = parse_url($url);
  $url_component_query = $url_components['query'] ?? '';
  parse_str($url_component_query, $query);

  // Add the module key for core database drivers when the module key is not
  // set.
  if (!isset($query['module']) && in_array($driver, [
    'mysql',
    'pgsql',
    'sqlite',
  ], TRUE)) {
    $query['module'] = $driver;
  }
  if (isset($query['module']) && $query['module']) {
    $module = $query['module'];

    // Set up an additional autoloader. We don't use the main autoloader as
    // this method can be called before Drupal is installed and is never
    // called during regular runtime.
    $namespace = "Drupal\\{$module}\\Driver\\Database\\{$driver}";
    $psr4_base_directory = Database::findDriverAutoloadDirectory($namespace, $root, TRUE);
    $additional_class_loader = new ClassLoader();
    $additional_class_loader
      ->addPsr4($namespace . '\\', $psr4_base_directory);
    $additional_class_loader
      ->register(TRUE);
    $connection_class = $namespace . '\\Connection';
  }
  if (!$module) {

    // Determine the connection class to use. Discover if the URL has a valid
    // driver scheme for a Drupal 8 style custom driver.
    // @todo Remove this in Drupal 10.
    $connection_class = "Drupal\\Driver\\Database\\{$driver}\\Connection";
  }
  if (!class_exists($connection_class)) {
    throw new \InvalidArgumentException("Can not convert '{$url}' to a database connection, class '{$connection_class}' does not exist");
  }
  $options = $connection_class::createConnectionOptionsFromUrl($url, $root);

  // If the driver is provided by a module add the necessary information to
  // autoload the code.
  // @see \Drupal\Core\Site\Settings::initialize()
  if (isset($psr4_base_directory)) {
    $options['autoload'] = $psr4_base_directory;
  }
  return $options;
}