function Database::convertDbUrlToConnectionInfo

Same name in other branches
  1. 9 core/lib/Drupal/Core/Database/Database.php \Drupal\Core\Database\Database::convertDbUrlToConnectionInfo()
  2. 10 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.

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.

10 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.
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.
TestSiteApplicationTest::addTestDatabase in core/tests/Drupal/Tests/Scripts/TestSiteApplicationTest.php
Adds the installed test site to the database connection info.

... See full list

File

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

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.
    $module = NULL;
    $connection_class = NULL;
    $url_components = parse_url($url);
    if (isset($url_components['query'])) {
        parse_str($url_components['query'], $query);
        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 = $custom_connection_class = $namespace . '\\Connection';
        }
    }
    if (!$module) {
        // Determine the connection class to use. Discover if the URL has a valid
        // driver scheme. Try with Drupal 8 style custom drivers first, since
        // those can override/extend the core ones.
        $connection_class = $custom_connection_class = "Drupal\\Driver\\Database\\{$driver}\\Connection";
        if (!class_exists($connection_class)) {
            // If the URL is not relative to a custom driver, try with core ones.
            $connection_class = "Drupal\\Core\\Database\\Driver\\{$driver}\\Connection";
        }
    }
    if (!class_exists($connection_class)) {
        throw new \InvalidArgumentException("Can not convert '{$url}' to a database connection, class '{$custom_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;
}

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