You are here

public static function Connection::createConnectionOptionsFromUrl in Drupal 9

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

See also

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

1 call to Connection::createConnectionOptionsFromUrl()
Connection::createConnectionOptionsFromUrl in core/lib/Drupal/Core/Database/Driver/sqlite/Connection.php
Creates an array of database connection options from a URL.
1 method overrides Connection::createConnectionOptionsFromUrl()
Connection::createConnectionOptionsFromUrl in core/lib/Drupal/Core/Database/Driver/sqlite/Connection.php
Creates an array of database connection options from a URL.

File

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

Class

Connection
Base Database API class.

Namespace

Drupal\Core\Database

Code

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

  // Remove leading slash from the URL path.
  if ($url_components['path'][0] === '/') {
    $url_components['path'] = substr($url_components['path'], 1);
  }

  // Use reflection to get the namespace of the class being called.
  $reflector = new \ReflectionClass(static::class);
  $database = [
    'driver' => $url_components['scheme'],
    'username' => $url_components['user'],
    'password' => $url_components['pass'],
    'host' => $url_components['host'],
    'database' => $url_components['path'],
    'namespace' => $reflector
      ->getNamespaceName(),
  ];
  if (isset($url_components['port'])) {
    $database['port'] = $url_components['port'];
  }
  if (!empty($url_components['fragment'])) {
    $database['prefix'] = $url_components['fragment'];
  }
  return $database;
}