function Connection::createUrlFromConnectionOptions

Same name in this branch
  1. 9 core/modules/sqlite/src/Driver/Database/sqlite/Connection.php \Drupal\sqlite\Driver\Database\sqlite\Connection::createUrlFromConnectionOptions()
Same name in other branches
  1. 8.9.x core/lib/Drupal/Core/Database/Driver/sqlite/Connection.php \Drupal\Core\Database\Driver\sqlite\Connection::createUrlFromConnectionOptions()
  2. 8.9.x core/lib/Drupal/Core/Database/Connection.php \Drupal\Core\Database\Connection::createUrlFromConnectionOptions()
  3. 10 core/modules/sqlite/src/Driver/Database/sqlite/Connection.php \Drupal\sqlite\Driver\Database\sqlite\Connection::createUrlFromConnectionOptions()
  4. 10 core/lib/Drupal/Core/Database/Connection.php \Drupal\Core\Database\Connection::createUrlFromConnectionOptions()
  5. 11.x core/modules/sqlite/src/Driver/Database/sqlite/Connection.php \Drupal\sqlite\Driver\Database\sqlite\Connection::createUrlFromConnectionOptions()
  6. 11.x core/lib/Drupal/Core/Database/Connection.php \Drupal\Core\Database\Connection::createUrlFromConnectionOptions()

Creates a URL from an array of database connection options.

@internal This method should only be called from \Drupal\Core\Database\Database::getConnectionInfoAsUrl().

Parameters

array $connection_options: The array of connection options for a database connection. An additional key of 'module' is added by Database::getConnectionInfoAsUrl() for drivers provided my contributed or custom modules for convenience.

Return value

string The connection info as a URL.

Throws

\InvalidArgumentException Exception thrown when the provided array of connection options does not meet the minimum requirements.

See also

\Drupal\Core\Database\Database::getConnectionInfoAsUrl()

1 method overrides Connection::createUrlFromConnectionOptions()
Connection::createUrlFromConnectionOptions in core/modules/sqlite/src/Driver/Database/sqlite/Connection.php
Creates a URL from an array of database connection options.

File

core/lib/Drupal/Core/Database/Connection.php, line 2128

Class

Connection
Base Database API class.

Namespace

Drupal\Core\Database

Code

public static function createUrlFromConnectionOptions(array $connection_options) {
    if (!isset($connection_options['driver'], $connection_options['database'])) {
        throw new \InvalidArgumentException("As a minimum, the connection options array must contain at least the 'driver' and 'database' keys");
    }
    $user = '';
    if (isset($connection_options['username'])) {
        $user = $connection_options['username'];
        if (isset($connection_options['password'])) {
            $user .= ':' . $connection_options['password'];
        }
        $user .= '@';
    }
    $host = empty($connection_options['host']) ? 'localhost' : $connection_options['host'];
    $db_url = $connection_options['driver'] . '://' . $user . $host;
    if (isset($connection_options['port'])) {
        $db_url .= ':' . $connection_options['port'];
    }
    $db_url .= '/' . $connection_options['database'];
    // Add the module when the driver is provided by a module.
    if (isset($connection_options['module'])) {
        $db_url .= '?module=' . $connection_options['module'];
    }
    if (isset($connection_options['prefix']) && $connection_options['prefix'] !== '') {
        $db_url .= '#' . $connection_options['prefix'];
    }
    return $db_url;
}

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