function Database::getConnectionInfoAsUrl

Same name in other branches
  1. 9 core/lib/Drupal/Core/Database/Database.php \Drupal\Core\Database\Database::getConnectionInfoAsUrl()
  2. 10 core/lib/Drupal/Core/Database/Database.php \Drupal\Core\Database\Database::getConnectionInfoAsUrl()
  3. 11.x core/lib/Drupal/Core/Database/Database.php \Drupal\Core\Database\Database::getConnectionInfoAsUrl()

Gets database connection info as a URL.

Parameters

string $key: (Optional) The database connection key.

Return value

string The connection info as a URL.

Throws

\RuntimeException When the database connection is not defined.

5 calls to Database::getConnectionInfoAsUrl()
DbCommandBaseTest::testPrefix in core/modules/system/tests/src/Kernel/Scripts/DbCommandBaseTest.php
Test specifying a prefix for different connections.
DbCommandBaseTest::testSpecifyDbUrl in core/modules/system/tests/src/Kernel/Scripts/DbCommandBaseTest.php
Test supplying database connection as a url.
PhpUnitTestRunner::runCommand in core/lib/Drupal/Core/Test/PhpUnitTestRunner.php
Executes the PHPUnit command.
UrlConversionTest::testGetConnectionInfoAsUrl in core/tests/Drupal/Tests/Core/Database/UrlConversionTest.php
@covers ::getConnectionInfoAsUrl
UrlConversionTest::testGetInvalidArgumentGetConnectionInfoAsUrl in core/tests/Drupal/Tests/Core/Database/UrlConversionTest.php
Test ::getConnectionInfoAsUrl() exception for invalid arguments.

File

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

Class

Database
Primary front-controller for the database system.

Namespace

Drupal\Core\Database

Code

public static function getConnectionInfoAsUrl($key = 'default') {
    $db_info = static::getConnectionInfo($key);
    if (empty($db_info) || empty($db_info['default'])) {
        throw new \RuntimeException("Database connection {$key} not defined or missing the 'default' settings");
    }
    $namespace = static::getDatabaseDriverNamespace($db_info['default']);
    // If the driver namespace is within a Drupal module, add the module name
    // to the connection options to make it easy for the connection class's
    // createUrlFromConnectionOptions() method to add it to the URL.
    if (static::isWithinModuleNamespace($namespace)) {
        $db_info['default']['module'] = explode('\\', $namespace)[1];
    }
    $connection_class = $namespace . '\\Connection';
    return $connection_class::createUrlFromConnectionOptions($db_info['default']);
}

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