function Connection::createConnectionOptionsFromUrl

Same name in this branch
  1. 10 core/modules/sqlite/src/Driver/Database/sqlite/Connection.php \Drupal\sqlite\Driver\Database\sqlite\Connection::createConnectionOptionsFromUrl()
Same name in other branches
  1. 9 core/modules/sqlite/src/Driver/Database/sqlite/Connection.php \Drupal\sqlite\Driver\Database\sqlite\Connection::createConnectionOptionsFromUrl()
  2. 9 core/lib/Drupal/Core/Database/Connection.php \Drupal\Core\Database\Connection::createConnectionOptionsFromUrl()
  3. 8.9.x core/lib/Drupal/Core/Database/Driver/sqlite/Connection.php \Drupal\Core\Database\Driver\sqlite\Connection::createConnectionOptionsFromUrl()
  4. 8.9.x core/lib/Drupal/Core/Database/Connection.php \Drupal\Core\Database\Connection::createConnectionOptionsFromUrl()
  5. 11.x core/modules/sqlite/src/Driver/Database/sqlite/Connection.php \Drupal\sqlite\Driver\Database\sqlite\Connection::createConnectionOptionsFromUrl()
  6. 11.x 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()

File

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

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("The database connection URL '{$url}' is invalid. The minimum requirement is: '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;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.