diff --git a/core/lib/Drupal/Core/Database/Connection.php b/core/lib/Drupal/Core/Database/Connection.php index 7ab5f2c..262cf3b 100644 --- a/core/lib/Drupal/Core/Database/Connection.php +++ b/core/lib/Drupal/Core/Database/Connection.php @@ -1484,7 +1484,8 @@ public function __sleep() { * @param \Psr\Http\Message\UriInterface $uri * The URI to be converted to a connection array. * @param string $root - * The root directory of the Drupal installation. + * The root directory of the Drupal installation. Some database drivers, + * like for example SQLite, need this information. * @param array $connection_options * The connection options. * @@ -1498,9 +1499,13 @@ public static function convertDbUrlToConnectionInfoHelper(UriInterface $uri, $ro if (!empty($port)) { $connection_options['port'] = $port; } - $user_info = explode(':', $uri->getUserInfo(), 2); - $connection_options['username'] = $user_info[0]; - $connection_options['password'] = isset($user_info[1]) ? $user_info[1] : ''; + + $user_info = $uri->getUserInfo(); + if (!empty($user_info)) { + $user_info_elements = explode(':', $user_info, 2); + $connection_options['username'] = $user_info_elements[0]; + $connection_options['password'] = isset($user_info_elements[1]) ? $user_info_elements[1] : ''; + } return $connection_options; } @@ -1517,15 +1522,17 @@ public static function convertDbUrlToConnectionInfoHelper(UriInterface $uri, $ro * @param \Psr\Http\Message\UriInterface $uri * The URI to connect to the database. * - * @return $this|\Psr\Http\Message\UriInterface + * @return \Psr\Http\Message\UriInterface * The URI to connect to the database. * * @see \Drupal\Core\Database\Database::getConnectionInfoAsUrl() */ public static function getConnectionInfoAsUrlHelper(array $connection_options, UriInterface $uri) { - $user = isset($connection_options['username']) ? $connection_options['username'] : ''; - $password = isset($connection_options['password']) ? $connection_options['password'] : ''; - $uri = $uri->withUserInfo($user, $password); + $username = isset($connection_options['username']) ? $connection_options['username'] : NULL; + $password = isset($connection_options['password']) ? $connection_options['password'] : NULL; + if ($username) { + $uri = $uri->withUserInfo($username, $password); + } if (!empty($connection_options['port'])) { $uri = $uri->withPort($connection_options['port']); } diff --git a/core/lib/Drupal/Core/Database/Database.php b/core/lib/Drupal/Core/Database/Database.php index 12d8f41..5ae87ea 100644 --- a/core/lib/Drupal/Core/Database/Database.php +++ b/core/lib/Drupal/Core/Database/Database.php @@ -368,10 +368,10 @@ } $namespace = static::getDatabaseDriverNamespace(self::$databaseInfo[$key][$target]); - $driver_class = $namespace . '\\Connection'; + $connection_class = $namespace . '\\Connection'; - $pdo_connection = $driver_class::open(self::$databaseInfo[$key][$target]); - $new_connection = new $driver_class($pdo_connection, self::$databaseInfo[$key][$target]); + $pdo_connection = $connection_class::open(self::$databaseInfo[$key][$target]); + $new_connection = new $connection_class($pdo_connection, self::$databaseInfo[$key][$target]); $new_connection->setTarget($target); $new_connection->setKey($key); @@ -450,21 +450,26 @@ public static function ignoreTarget($key, $target) { * @throws \InvalidArgumentException * Exception thrown when the provided URL does not meet the minimum * requirements. + * @throws \RuntimeException + * When the database driver is not available, or the URL can not be + * converted to a database connection array. */ public static function convertDbUrlToConnectionInfo($url, $root) { $uri = new Uri($url); if (empty($uri->getHost()) || empty($uri->getScheme()) || empty($uri->getPath())) { throw new \InvalidArgumentException('Minimum requirement: driver://host/database'); } + // Discover if the URL has a driver namespace. $scheme = $uri->getScheme(); $discovery = new DatabaseDriverDiscovery(); $drivers = $discovery->discoverDatabaseDrivers(); if (!isset($drivers[$scheme])) { - throw new \Exception("Drupal database driver not available: '" . $uri->getScheme() . "'"); + throw new \RuntimeException("Drupal database driver not available: '" . $uri->getScheme() . "'"); } $namespace = $drivers[$scheme]['namespace']; - $fragment = $uri->getFragment(); + + // Build the connection information array. $connection_options = [ 'driver' => $uri->getScheme(), 'host' => $uri->getHost(), @@ -472,16 +477,18 @@ public static function convertDbUrlToConnectionInfo($url, $root) { // Note that additional leading slashes have meaning for some database // drivers. 'database' => substr($uri->getPath(), 1), - 'prefix' => $fragment ?: NULL, + 'prefix' => $uri->getFragment() ?: NULL, 'namespace' => $namespace, ]; + // Ensure the Connection class exists. $namespace = static::getDatabaseDriverNamespace($connection_options); - $driver_class = $namespace . '\\Connection'; - if (!class_exists($driver_class)) { - throw new \RuntimeException("Can not convert $url to a database connection, class $driver_class does not exist"); + $connection_class = $namespace . '\\Connection'; + if (!class_exists($connection_class)) { + throw new \RuntimeException("Can not convert $url to a database connection, class $connection_class does not exist"); } - return $driver_class::convertDbUrlToConnectionInfoHelper($uri, $root, $connection_options); + + return $connection_class::convertDbUrlToConnectionInfoHelper($uri, $root, $connection_options); } /** @@ -492,6 +499,9 @@ public static function convertDbUrlToConnectionInfo($url, $root) { * * @return string * The connection info as a URL. + * + * @throws \RuntimeException + * When the database connection is not defined. */ public static function getConnectionInfoAsUrl($key = 'default') { $db_info = static::getConnectionInfo($key); @@ -513,7 +523,7 @@ public static function getConnectionInfoAsUrl($key = 'default') { public static function convertConnectionInfoToUrl($db_info) { $namespace = static::getDatabaseDriverNamespace($db_info['default']); - $driver_class = $namespace . '\\Connection'; + $connection_class = $namespace . '\\Connection'; // Some database driver do not need a host setting to work but in order to // be converted into a URL they do. @@ -529,7 +539,7 @@ public static function convertConnectionInfoToUrl($db_info) { $uri = $uri->withFragment($db_info['default']['prefix']['default']); } - $uri = $driver_class::getConnectionInfoAsUrlHelper($db_info['default'], $uri); + $uri = $connection_class::getConnectionInfoAsUrlHelper($db_info['default'], $uri); return (string) $uri; } diff --git a/core/lib/Drupal/Core/Database/Driver/sqlite/Connection.php b/core/lib/Drupal/Core/Database/Driver/sqlite/Connection.php index 6eade7e..ef81c47 100644 --- a/core/lib/Drupal/Core/Database/Driver/sqlite/Connection.php +++ b/core/lib/Drupal/Core/Database/Driver/sqlite/Connection.php @@ -440,15 +440,15 @@ public function getFullQualifiedTableName($table) { /** * {@inheritdoc} */ - public static function convertDbUrlToConnectionInfoHelper(UriInterface $uri, $root, array $database) { + public static function convertDbUrlToConnectionInfoHelper(UriInterface $uri, $root, array $connection_options) { // A SQLite database with a leading slash indicates a system path. // Otherwise the path is relative to the Drupal root. - if ($database['database'][0] !== '/') { - $database['database'] = $root . '/' . $database['database']; + if ($connection_options['database'][0] !== '/') { + $connection_options['database'] = $root . '/' . $connection_options['database']; } // The host setting is meaningless for SQLite databases. - unset($database['host']); - return $database; + unset($connection_options['host']); + return $connection_options; } /** diff --git a/core/modules/system/tests/src/Kernel/Scripts/DbCommandBaseTest.php b/core/modules/system/tests/src/Kernel/Scripts/DbCommandBaseTest.php index f8eef85..eff9f41 100644 --- a/core/modules/system/tests/src/Kernel/Scripts/DbCommandBaseTest.php +++ b/core/modules/system/tests/src/Kernel/Scripts/DbCommandBaseTest.php @@ -1,4 +1,5 @@ execute([ - '-db-url' => Database::getConnectionInfoAsUrl('default') + '-db-url' => Database::getConnectionInfoAsUrl() ]); $this->assertEquals('db-tools', $command->getDatabaseConnection($command_tester->getInput())->getKey()); Database::removeConnection('db-tools'); $command_tester->execute([ - '--database-url' => Database::getConnectionInfoAsUrl('default') + '--database-url' => Database::getConnectionInfoAsUrl() ]); $this->assertEquals('db-tools', $command->getDatabaseConnection($command_tester->getInput())->getKey()); } @@ -89,7 +90,7 @@ public function testPrefix() { $this->assertEquals('extra', $command->getDatabaseConnection($command_tester->getInput())->tablePrefix()); $command_tester->execute([ - '-db-url' => Database::getConnectionInfoAsUrl('default'), + '-db-url' => Database::getConnectionInfoAsUrl(), '--prefix' => 'extra2', ]); $this->assertEquals('extra2', $command->getDatabaseConnection($command_tester->getInput())->tablePrefix());