Database::convertDbUrlToConnectionInfo

public static Database::convertDbUrlToConnectionInfo($url, $root)

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.

File

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

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;
}
doc_Drupal
2016-10-29 08:59:30
Comments
Leave a Comment

Please login to continue.