You are here

public static function Database::convertDbUrlToConnectionInfo in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Database/Database.php \Drupal\Core\Database\Database::convertDbUrlToConnectionInfo()

Converts a URL to a database connection info array.

Parameters

string $url: The URL.

string $root: The root directory of the Drupal installation.

Return value

array The database connection info.

Throws

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

3 calls to Database::convertDbUrlToConnectionInfo()
BrowserTestBase::changeDatabasePrefix in core/modules/simpletest/src/BrowserTestBase.php
Changes the database connection to the prefixed one.
DbCommandBase::getDatabaseConnection in core/lib/Drupal/Core/Command/DbCommandBase.php
Parse input options decide on a database.
KernelTestBase::getDatabaseConnectionInfo in core/tests/Drupal/KernelTests/KernelTestBase.php
Returns the Database connection info to be used for this test.

File

core/lib/Drupal/Core/Database/Database.php, line 462
Contains \Drupal\Core\Database\Database.

Class

Database
Primary front-controller for the database system.

Namespace

Drupal\Core\Database

Code

public static function convertDbUrlToConnectionInfo($url, $root) {
  $info = parse_url($url);
  if (!isset($info['scheme'], $info['host'], $info['path'])) {
    throw new \InvalidArgumentException('Minimum requirement: driver://host/database');
  }
  $info += array(
    'user' => '',
    'pass' => '',
    'fragment' => '',
  );

  // A SQLite database path with two leading slashes indicates a system path.
  // Otherwise the path is relative to the Drupal root.
  if ($info['path'][0] === '/') {
    $info['path'] = substr($info['path'], 1);
  }
  if ($info['scheme'] === 'sqlite' && $info['path'][0] !== '/') {
    $info['path'] = $root . '/' . $info['path'];
  }
  $database = array(
    'driver' => $info['scheme'],
    'username' => $info['user'],
    'password' => $info['pass'],
    'host' => $info['host'],
    'database' => $info['path'],
  );
  if (isset($info['port'])) {
    $database['port'] = $info['port'];
  }
  return $database;
}