You are here

public static function Connection::createConnectionOptionsFromUrl in Drupal 8

Same name in this branch
  1. 8 core/lib/Drupal/Core/Database/Connection.php \Drupal\Core\Database\Connection::createConnectionOptionsFromUrl()
  2. 8 core/lib/Drupal/Core/Database/Driver/sqlite/Connection.php \Drupal\Core\Database\Driver\sqlite\Connection::createConnectionOptionsFromUrl()
Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Database/Driver/sqlite/Connection.php \Drupal\Core\Database\Driver\sqlite\Connection::createConnectionOptionsFromUrl()

Creates an array of database connection options from a URL.

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

Parameters

string $url: The URL.

string $root: The root directory of the Drupal installation. Some database drivers, like for example SQLite, need this information.

Return value

array The connection options.

Throws

\InvalidArgumentException Exception thrown when the provided URL does not meet the minimum requirements.

Overrides Connection::createConnectionOptionsFromUrl

See also

\Drupal\Core\Database\Database::convertDbUrlToConnectionInfo()

File

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

Class

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

Namespace

Drupal\Core\Database\Driver\sqlite

Code

public static function createConnectionOptionsFromUrl($url, $root) {
  $database = parent::createConnectionOptionsFromUrl($url, $root);

  // A SQLite database path with two leading slashes indicates a system path.
  // Otherwise the path is relative to the Drupal root.
  $url_components = parse_url($url);
  if ($url_components['path'][0] === '/') {
    $url_components['path'] = substr($url_components['path'], 1);
  }
  if ($url_components['path'][0] === '/' || $url_components['path'] === ':memory:') {
    $database['database'] = $url_components['path'];
  }
  else {
    $database['database'] = $root . '/' . $url_components['path'];
  }

  // User credentials and system port are irrelevant for SQLite.
  unset($database['username'], $database['password'], $database['port']);
  return $database;
}