function Database::convertDbUrlToConnectionInfo

Same name 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. 11.x 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.

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.

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 545

Class

Database
Primary front-controller for the database system.

Namespace

Drupal\Core\Database

Code

public static function convertDbUrlToConnectionInfo($url, $root, ?bool $include_test_drivers = NULL) {
    // 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);
    // Add the module key for core database drivers when the module key is not
    // set.
    if (!isset($query['module']) && in_array($driverName, [
        'mysql',
        'pgsql',
        'sqlite',
    ], TRUE)) {
        $query['module'] = $driverName;
    }
    if (!isset($query['module'])) {
        throw new \InvalidArgumentException("Can not convert '{$url}' to a database connection, the module providing the driver '{$driverName}' is not specified");
    }
    $driverNamespace = "Drupal\\{$query['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, $root);
    // 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.