function Database::getConnectionInfoAsUrl

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Database/Database.php \Drupal\Core\Database\Database::getConnectionInfoAsUrl()
  2. 8.9.x core/lib/Drupal/Core/Database/Database.php \Drupal\Core\Database\Database::getConnectionInfoAsUrl()
  3. 10 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
Tests specifying a prefix for different connections.
DbCommandBaseTest::testSpecifyDbUrl in core/modules/system/tests/src/Kernel/Scripts/DbCommandBaseTest.php
Tests 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
Tests ::getConnectionInfoAsUrl() exception for invalid arguments.

File

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

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 = $db_info['default']['namespace'];
    // 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.
    $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.