diff --git a/core/lib/Drupal/Core/Database/Connection.php b/core/lib/Drupal/Core/Database/Connection.php index cea4ed6..8ec1892 100644 --- a/core/lib/Drupal/Core/Database/Connection.php +++ b/core/lib/Drupal/Core/Database/Connection.php @@ -1291,6 +1291,14 @@ protected function generateTemporaryTableName() { abstract public function driver(); /** + * Returns the database driver PHP namespace. + * + * @return string + * The PHP namespace of the database driver. + */ + abstract public static function getNamespace(); + + /** * Returns the version of the database server. */ public function version() { @@ -1479,8 +1487,8 @@ public function __sleep() { * This method should not be called. Use * \Drupal\Core\Database\Database::convertDbUrlToConnectionInfo() instead. * - * @param array $url_components - * The URL components as resulting from a parse_url() call. + * @param string $url + * The URL. * @param string $root * The root directory of the Drupal installation. Some database drivers, * like for example SQLite, need this information. @@ -1488,10 +1496,19 @@ public function __sleep() { * @return array * The connection options. * + * @throws \InvalidArgumentException + * Exception thrown when the provided URL does not meet the minimum + * requirements. + * * @see \Drupal\Core\Database\Database::convertDbUrlToConnectionInfo() */ - public static function getConnectionInfoFromUrl(array $url_components, $root) { - $url_components += [ + public static function getConnectionInfoFromUrl($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' => '', @@ -1508,7 +1525,7 @@ public static function getConnectionInfoFromUrl(array $url_components, $root) { 'password' => $url_components['pass'], 'host' => $url_components['host'], 'database' => $url_components['path'], - 'namespace' => "Drupal\\Core\\Database\\Driver\\{$url_components['scheme']}", + 'namespace' => static::getNamespace(), ]; if (isset($url_components['port'])) { diff --git a/core/lib/Drupal/Core/Database/Database.php b/core/lib/Drupal/Core/Database/Database.php index 3012426..b304266 100644 --- a/core/lib/Drupal/Core/Database/Database.php +++ b/core/lib/Drupal/Core/Database/Database.php @@ -453,23 +453,23 @@ public static function ignoreTarget($key, $target) { * converted to a database connection array. */ 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'); + if (preg_match('/^(.*):\/\//', $url, $matches) !== 1) { + throw new \InvalidArgumentException("Missing scheme in URL '$url'"); } + $driver = $matches[1]; // Discover if the URL has a valid driver scheme. Try with core drivers // first. - $connection_class = "Drupal\\Core\\Database\\Driver\\{$info['scheme']}\\Connection"; + $connection_class = "Drupal\\Core\\Database\\Driver\\{$driver}\\Connection"; if (!class_exists($connection_class)) { // If the URL is not relative to a core driver, try with custom ones. - $connection_class = "Drupal\\Driver\\Database\\{$info['scheme']}\\Connection"; + $connection_class = "Drupal\\Driver\\Database\\{$driver}\\Connection"; if (!class_exists($connection_class)) { - throw new \RuntimeException("Can not convert '$url' to a database connection, class '$connection_class' does not exist"); + throw new \InvalidArgumentException("Can not convert '$url' to a database connection, class '$connection_class' does not exist"); } } - return $connection_class::getConnectionInfoFromUrl($info, $root); + return $connection_class::getConnectionInfoFromUrl($url, $root); } /** @@ -494,7 +494,7 @@ public static function getConnectionInfoAsUrl($key = 'default') { } /** - * Gets the PHP namespace of a database driver. + * Gets the PHP namespace of a database driver from the connection info. * * @param array $connection_info * The database connection information, as defined in settings.php. The @@ -504,7 +504,7 @@ public static function getConnectionInfoAsUrl($key = 'default') { * @return string * The PHP namespace of the driver's database. */ - public static function getDatabaseDriverNamespace(array $connection_info) { + protected static function getDatabaseDriverNamespace(array $connection_info) { if (isset($connection_info['namespace']) && $connection_info['namespace'] !== NULL) { return $connection_info['namespace']; } diff --git a/core/lib/Drupal/Core/Database/Driver/mysql/Connection.php b/core/lib/Drupal/Core/Database/Driver/mysql/Connection.php index 164f68a..a847658 100644 --- a/core/lib/Drupal/Core/Database/Driver/mysql/Connection.php +++ b/core/lib/Drupal/Core/Database/Driver/mysql/Connection.php @@ -226,6 +226,13 @@ public function driver() { return 'mysql'; } + /** + * {@inheritdoc} + */ + public static function getNamespace() { + return __NAMESPACE__; + } + public function databaseType() { return 'mysql'; } diff --git a/core/lib/Drupal/Core/Database/Driver/pgsql/Connection.php b/core/lib/Drupal/Core/Database/Driver/pgsql/Connection.php index e696f24..a57f45d 100644 --- a/core/lib/Drupal/Core/Database/Driver/pgsql/Connection.php +++ b/core/lib/Drupal/Core/Database/Driver/pgsql/Connection.php @@ -299,6 +299,13 @@ public function driver() { return 'pgsql'; } + /** + * {@inheritdoc} + */ + public static function getNamespace() { + return __NAMESPACE__; + } + public function databaseType() { return 'pgsql'; } diff --git a/core/lib/Drupal/Core/Database/Driver/sqlite/Connection.php b/core/lib/Drupal/Core/Database/Driver/sqlite/Connection.php index d79b2c9..e687f94 100644 --- a/core/lib/Drupal/Core/Database/Driver/sqlite/Connection.php +++ b/core/lib/Drupal/Core/Database/Driver/sqlite/Connection.php @@ -372,6 +372,13 @@ public function driver() { return 'sqlite'; } + /** + * {@inheritdoc} + */ + public static function getNamespace() { + return __NAMESPACE__; + } + public function databaseType() { return 'sqlite'; } @@ -439,11 +446,12 @@ public function getFullQualifiedTableName($table) { /** * {@inheritdoc} */ - public static function getConnectionInfoFromUrl(array $url_components, $root) { - $database = parent::getConnectionInfoFromUrl($url_components, $root); + public static function getConnectionInfoFromUrl($url, $root) { + $database = parent::getConnectionInfoFromUrl($url, $root); // A SQLite database path with two leading slashes indicates a system path. // Otherwise the path is relative to the Drupal root. + $url_components = parse_url($url); if ($url_components['path'][0] === '/') { $url_components['path'] = substr($url_components['path'], 1); } diff --git a/core/modules/simpletest/simpletest.module b/core/modules/simpletest/simpletest.module index 6670e5a..96802a0 100644 --- a/core/modules/simpletest/simpletest.module +++ b/core/modules/simpletest/simpletest.module @@ -316,9 +316,11 @@ function simpletest_phpunit_configuration_filepath() { */ function simpletest_phpunit_run_command(array $unescaped_test_classnames, $phpunit_file, &$status = NULL, &$output = NULL) { global $base_url; - // Setup an environment variable containing the database connection so that - // functional tests can connect to the database. - putenv('SIMPLETEST_DB=' . Database::getConnectionInfoAsUrl()); + if (Database::getConnectionInfo()) { + // Setup an environment variable containing the database connection so that + // functional tests can connect to the database. + putenv('SIMPLETEST_DB=' . Database::getConnectionInfoAsUrl()); + } // Setup an environment variable containing the base URL, if it is available. // This allows functional tests to browse the site under test. When running diff --git a/core/tests/Drupal/Tests/Core/Database/Stub/StubConnection.php b/core/tests/Drupal/Tests/Core/Database/Stub/StubConnection.php index 4692dff..97b2eee 100644 --- a/core/tests/Drupal/Tests/Core/Database/Stub/StubConnection.php +++ b/core/tests/Drupal/Tests/Core/Database/Stub/StubConnection.php @@ -44,6 +44,13 @@ public function driver() { /** * {@inheritdoc} */ + public static function getNamespace() { + return __NAMESPACE__; + } + + /** + * {@inheritdoc} + */ public function databaseType() { return 'stub'; }