function Database::convertDbUrlToConnectionInfo

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Database/Database.php \Drupal\Core\Database\Database::convertDbUrlToConnectionInfo()
  2. 8.9.x core/lib/Drupal/Core/Database/Database.php \Drupal\Core\Database\Database::convertDbUrlToConnectionInfo()
  3. 10 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|bool|null $root: (deprecated) The root directory of the Drupal installation.

bool|null $include_test_drivers: (optional) Whether to include test extensions. If FALSE, all 'tests' directories are excluded in the search. When NULL will be determined by the extension_discovery_scan_tests setting.

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.

14 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 516

Class

Database
Primary front-controller for the database system.

Namespace

Drupal\Core\Database

Code

public static function convertDbUrlToConnectionInfo(string $url, $root = NULL, ?bool $include_test_drivers = NULL) : array {
  if ($root !== NULL) {
    if (is_bool($root)) {
      $include_test_drivers = $root;
    }
    else {
      @trigger_error("Passing a string \$root value to " . __METHOD__ . "() is deprecated in drupal:11.3.0 and will be removed in drupal:12.0.0. There is no replacement. See https://www.drupal.org/node/3511287", E_USER_DEPRECATED);
    }
  }
  // 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}'");
  }
  $driverName = $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.
  $url_components = parse_url($url);
  $url_component_query = $url_components['query'] ?? '';
  parse_str($url_component_query, $query);
  // Use the driver name as the module name when the module name is not
  // provided.
  $module = $query['module'] ?? $driverName;
  $driverNamespace = "Drupal\\{$module}\\Driver\\Database\\{$driverName}";
  /** @var \Drupal\Core\Extension\DatabaseDriver $driver */
  $driver = self::getDriverList()->includeTestDrivers($include_test_drivers)
    ->get($driverNamespace);
  // 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.
  $additional_class_loader = new ClassLoader();
  $additional_class_loader->addPsr4($driverNamespace . '\\', $driver->getPath());
  $additional_class_loader->register();
  $connection_class = $driverNamespace . '\\Connection';
  if (!class_exists($connection_class)) {
    throw new \InvalidArgumentException("Can not convert '{$url}' to a database connection, class '{$connection_class}' does not exist");
  }
  // When the database driver is extending another database driver, then
  // add autoload info for the parent database driver as well.
  $autoloadInfo = $driver->getAutoloadInfo();
  if (isset($autoloadInfo['dependencies'])) {
    foreach ($autoloadInfo['dependencies'] as $dependency) {
      $additional_class_loader->addPsr4($dependency['namespace'] . '\\', $dependency['autoload']);
    }
  }
  $additional_class_loader->register(TRUE);
  $options = $connection_class::createConnectionOptionsFromUrl($url, NULL);
  // Add the necessary information to autoload code.
  // @see \Drupal\Core\Site\Settings::initialize()
  $options['autoload'] = $driver->getPath() . DIRECTORY_SEPARATOR;
  if (isset($autoloadInfo['dependencies'])) {
    $options['dependencies'] = $autoloadInfo['dependencies'];
  }
  return $options;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.