You are here

public static function Connection::createUrlFromConnectionOptions in Drupal 8

Same name in this branch
  1. 8 core/lib/Drupal/Core/Database/Connection.php \Drupal\Core\Database\Connection::createUrlFromConnectionOptions()
  2. 8 core/lib/Drupal/Core/Database/Driver/sqlite/Connection.php \Drupal\Core\Database\Driver\sqlite\Connection::createUrlFromConnectionOptions()
Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Database/Driver/sqlite/Connection.php \Drupal\Core\Database\Driver\sqlite\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.

Overrides Connection::createUrlFromConnectionOptions

See also

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

File

core/lib/Drupal/Core/Database/Driver/sqlite/Connection.php, line 470

Class

Connection
SQLite implementation of \Drupal\Core\Database\Connection.

Namespace

Drupal\Core\Database\Driver\sqlite

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");
  }
  $db_url = 'sqlite://localhost/' . $connection_options['database'];
  if (isset($connection_options['prefix']['default']) && $connection_options['prefix']['default'] !== NULL && $connection_options['prefix']['default'] !== '') {
    $db_url .= '#' . $connection_options['prefix']['default'];
  }
  return $db_url;
}