diff --git a/core/includes/install.inc b/core/includes/install.inc index 21f75fb..96b366c 100644 --- a/core/includes/install.inc +++ b/core/includes/install.inc @@ -144,15 +144,14 @@ function drupal_install_profile_distribution_version() { * An array of database types compiled into PHP. * * @deprecated in Drupal 8.5.0, will be removed before Drupal 9.0.0. Use - * \Drupal\Core\Database\DatabaseDriverDiscovery::detectDatabaseTypes + * \Drupal\Core\Database\DatabaseDriverDiscovery::getInstallableDriversInstallerNames * instead. * * @see https://www.drupal.org/node/2896416 */ function drupal_detect_database_types() { - @trigger_error('drupal_detect_database_types() is deprecated in Drupal 8.5.0, will be removed before Drupal 9.0.0. Use \Drupal\Core\Database\DatabaseDriverDiscovery::detectDatabaseTypes instead. See https://www.drupal.org/node/2896416.', E_USER_DEPRECATED); - $discovery = new DatabaseDriverDiscovery(); - return $discovery->detectDatabaseTypes(); + @trigger_error('drupal_detect_database_types() is deprecated in Drupal 8.5.0, will be removed before Drupal 9.0.0. Use \Drupal\Core\Database\DatabaseDriverDiscovery::getInstallableDriversInstallerNames instead. See https://www.drupal.org/node/2896416.', E_USER_DEPRECATED); + return (new DatabaseDriverDiscovery())->getInstallableDriversInstallerNames(); } /** @@ -162,14 +161,14 @@ function drupal_detect_database_types() { * An array of available database driver installer objects. * * @deprecated in Drupal 8.5.0, will be removed before Drupal 9.0.0. Use - * \Drupal\Core\Database\DatabaseDriverDiscovery::getDatabaseTypes instead. + * \Drupal\Core\Database\DatabaseDriverDiscovery::getInstallableDriversInstallers + * instead. * * @see https://www.drupal.org/node/2896416 */ function drupal_get_database_types() { - @trigger_error('drupal_get_database_types() is deprecated in Drupal 8.5.0, will be removed before Drupal 9.0.0. Use \Drupal\Core\Database\DatabaseDriverDiscovery::getDatabaseTypes instead. See https://www.drupal.org/node/2896416.', E_USER_DEPRECATED); - $discovery = new DatabaseDriverDiscovery(); - return $discovery->getDatabaseTypes(); + @trigger_error('drupal_get_database_types() is deprecated in Drupal 8.5.0, will be removed before Drupal 9.0.0. Use \Drupal\Core\Database\DatabaseDriverDiscovery::getInstallableDriversInstallers instead. See https://www.drupal.org/node/2896416.', E_USER_DEPRECATED); + return (new DatabaseDriverDiscovery())->getInstallableDriversInstallers(); } /** @@ -1091,12 +1090,11 @@ function install_profile_info($profile, $langcode = 'en') { * A class defining the requirements and tasks for installing the database. * * @deprecated in Drupal 8.5.0, will be removed before Drupal 9.0.0. Use - * \Drupal\Core\Database\DatabaseDriverDiscovery::getInstallerObject instead. + * \Drupal\Core\Database\DatabaseDriverDiscovery::getInstaller instead. * * @see https://www.drupal.org/node/2896416 */ function db_installer_object($driver) { - @trigger_error('drupal_get_database_types() is deprecated in Drupal 8.5.0, will be removed before Drupal 9.0.0. Use \Drupal\Core\Database\DatabaseDriverDiscovery::getInstallerObject instead. See https://www.drupal.org/node/2896416.', E_USER_DEPRECATED); - $discovery = new DatabaseDriverDiscovery(); - return $discovery->getInstallerObject($driver); + @trigger_error('db_installer_object() is deprecated in Drupal 8.5.0, will be removed before Drupal 9.0.0. Use \Drupal\Core\Database\DatabaseDriverDiscovery::getInstaller instead. See https://www.drupal.org/node/2896416.', E_USER_DEPRECATED); + return (new DatabaseDriverDiscovery())->getInstaller($driver); } diff --git a/core/lib/Drupal/Core/Database/Database.php b/core/lib/Drupal/Core/Database/Database.php index f2d841a..12d8f41 100644 --- a/core/lib/Drupal/Core/Database/Database.php +++ b/core/lib/Drupal/Core/Database/Database.php @@ -367,8 +367,8 @@ throw new DriverNotSpecifiedException('Driver not specified for this database connection: ' . $key); } - $namespace = !empty(self::$databaseInfo[$key][$target]['namespace']) ? self::$databaseInfo[$key][$target]['namespace'] : NULL; - $driver_class = static::getDatabaseConnectionClass($driver, $namespace); + $namespace = static::getDatabaseDriverNamespace(self::$databaseInfo[$key][$target]); + $driver_class = $namespace . '\\Connection'; $pdo_connection = $driver_class::open(self::$databaseInfo[$key][$target]); $new_connection = new $driver_class($pdo_connection, self::$databaseInfo[$key][$target]); @@ -476,7 +476,8 @@ public static function convertDbUrlToConnectionInfo($url, $root) { 'namespace' => $namespace, ]; - $driver_class = static::getDatabaseConnectionClass($connection_options['driver'], $connection_options['namespace']); + $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"); } @@ -511,8 +512,8 @@ public static function getConnectionInfoAsUrl($key = 'default') { */ public static function convertConnectionInfoToUrl($db_info) { - $namespace = isset($db_info['default']['namespace']) ? $db_info['default']['namespace'] : NULL; - $driver_class = static::getDatabaseConnectionClass($db_info['default']['driver'], $namespace); + $namespace = static::getDatabaseDriverNamespace($db_info['default']); + $driver_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. @@ -533,25 +534,23 @@ public static function convertConnectionInfoToUrl($db_info) { } /** - * Gets the fully qualified connection class name for a database driver. + * Gets the PHP namespace of a database driver. * - * @param string $driver - * The database driver to get the class for. - * @param string $namespace - * A custom namespace to use. + * @param array $connection_info + * The database connection information, as defined in settings.php. The + * structure of this array depends on the database driver it is connecting + * to. * * @return string - * The fully qualified class name of the driver's database connection class. + * The PHP namespace of the driver's database. */ - public static function getDatabaseConnectionClass($driver, $namespace) { - if ($namespace) { - $driver_class = $namespace . '\\Connection'; + public static function getDatabaseDriverNamespace(array $connection_info) { + if (isset($connection_info['namespace']) && $connection_info['namespace'] !== NULL) { + return $connection_info['namespace']; } - else { - // Fallback for Drupal 7 settings.php. - $driver_class = 'Drupal\\Core\\Database\\Driver\\' . $driver . '\\Connection'; - } - return $driver_class; + + // Fallback for Drupal 7 settings.php. + return 'Drupal\\Core\\Database\\Driver\\' . $connection_info['driver']; } } diff --git a/core/lib/Drupal/Core/Database/DatabaseDriverDiscovery.php b/core/lib/Drupal/Core/Database/DatabaseDriverDiscovery.php index 0f5a9f2..dba8564 100644 --- a/core/lib/Drupal/Core/Database/DatabaseDriverDiscovery.php +++ b/core/lib/Drupal/Core/Database/DatabaseDriverDiscovery.php @@ -3,135 +3,138 @@ namespace Drupal\Core\Database; /** - * Component used to perform database driver - * discovery. + * Performs database driver discovery. */ class DatabaseDriverDiscovery { /** - * The Drupal root. + * Directory locations for database drivers. * - * @var string - */ - protected $drupalRoot; - - /** - * Available locations for drivers + * See composer.json for discoverable namespaces. * - * @var mixed[] + * @var array */ - protected $discoveryLocations; + protected static $discoveryLocations = [ + 'Drupal\\Core\\Database\\Driver' => __DIR__ . '/Driver', + 'Drupal\\Driver\\Database' => __DIR__ . '/../../../../../drivers/lib/Drupal/Driver/Database', + ]; /** - * Get an instance of DatabaseDriverDiscovery. + * Discovered database drivers. + * + * @var array */ - public function __construct() { - // Hard-coded DRUPAL_ROOT (because bootstrap.inc is not accessible). - $this->drupalRoot = dirname(dirname(dirname(dirname(dirname(__DIR__))))); - // See composer.json for discoverable namespaces. - $this->discoveryLocations = [ - 'Drupal\\Core\\Database\\Driver' => '/core/lib/Drupal/Core/Database/Driver', - 'Drupal\\Driver\\Database' => '/drivers/lib/Drupal/Driver/Database' - ]; - } + protected static $discoveredDrivers; /** - * Detects all supported databases that are compiled into PHP. + * Returns the installer name of the installable database drivers. * - * @return - * An array of database types compiled into PHP. + * @return \Drupal\Core\StringTranslation\TranslatableMarkup[] + * An associative array with key the database driver name, and value the + * name of database driver as presented by the installer. */ - public function detectDatabaseTypes() { - $databases = $this->getDatabaseTypes(); + public function getInstallableDriversInstallerNames() { + $installable_drivers_installers_names = []; - foreach ($databases as $driver => $installer) { - $databases[$driver] = $installer->name(); + foreach ($this->getInstallableDriversInstallers() as $driver_name => $installer) { + $installable_drivers_installers_names[$driver_name] = $installer->name(); } - return $databases; + return $installable_drivers_installers_names; } /** - * Returns information about available database drivers. + * Returns the installer objects of the installable database drivers. * * @return \Drupal\Core\Database\Install\Tasks[] - * An array of available database driver installer objects. + * An associative array with key the database driver name, and value the + * installer object. */ - public function getDatabaseTypes() { - - $databases = []; + public function getInstallableDriversInstallers() { + $installable_drivers_installers = []; - foreach ($this->discoverDatabaseDrivers() as $driver => $info) { + foreach ($this->discoverDatabaseDrivers() as $driver_name => $info) { if ($info['installable'] === TRUE) { - $databases[$driver] = $info['installer']; + $installable_drivers_installers[$driver_name] = $info['installer']; } } // Usability: unconditionally put the MySQL driver on top. - if (isset($databases['mysql'])) { - $mysql_database = $databases['mysql']; - unset($databases['mysql']); - $databases = ['mysql' => $mysql_database] + $databases; + if (isset($installable_drivers_installers['mysql'])) { + $mysql_driver_installer = $installable_drivers_installers['mysql']; + unset($installable_drivers_installers['mysql']); + $installable_drivers_installers = ['mysql' => $mysql_driver_installer] + $installable_drivers_installers; } - return $databases; + return $installable_drivers_installers; } /** * Returns a database installer object. * - * @param $driver + * @param string $driver_name * The name of the driver. * * @return \Drupal\Core\Database\Install\Tasks * A class defining the requirements and tasks for installing the database. */ - public function getInstallerObject($driver) { - $drivers = $this->discoverDatabaseDrivers(); - return $drivers[$driver]['installer']; + public function getInstaller($driver_name) { + return $this->discoverDatabaseDrivers()[$driver_name]['installer']; } /** - * Discover the available database drivers. + * Discovers the available database drivers. + * + * @return array + * An associative array with key the database driver name, and value an + * associative array bearing the following keys and values: + * - 'connectionClass' => the FQCN of the Connection class for this driver; + * - 'installer' => the database driver installer object; + * - 'installable' => whether the database driver can be installed. */ public function discoverDatabaseDrivers() { + if (isset(static::$discoveredDrivers)) { + return static::$discoveredDrivers; + } - $drivers = []; - - // The internal database driver name is any valid PHP identifier. - $files = []; - foreach ($this->discoveryLocations as $namespace => $location) { - if (is_dir($this->drupalRoot . $location)) { - if ($handle = @opendir($this->drupalRoot . $location)) { - while (FALSE !== ($driverName = readdir($handle))) { - // Skip this file if it matches the nomask or starts with a dot. - if ($driverName[0] != '.') { - if (isset($files[$driverName])) { - throw new \Exception("Conflicting database driver name: " . $driverName); - } - $files[$driverName] = $namespace . '\\' . $driverName; + // Scan the discovery locations and build an array of candidate discovered + // drivers. + $candidate_drivers = []; + foreach (static::$discoveryLocations as $namespace => $location) { + if (is_dir($location) && ($handle = @opendir($location))) { + // Each database driver name should correspond to a subdirectory of the + // location being scanned. + while (($driver_name = readdir($handle)) != FALSE) { + // Skip this file if it starts with a dot. + if ($driver_name[0] != '.') { + if (isset($candidate_drivers[$driver_name])) { + throw new \RuntimeException("Duplicate database driver name found: " . $driver_name); } + $candidate_drivers[$driver_name] = $namespace . '\\' . $driver_name; } } } } - foreach ($files as $driver => $namespace) { - $drivers[$driver]['namespace'] = $namespace; + // Loops through the candidates and build the array of discovered drivers. + // Use class_exist to check that actual classes for Connection and + // Install\Tasks are defined and can be loaded. + foreach ($candidate_drivers as $driver_name => $namespace) { + static::$discoveredDrivers[$driver_name]['namespace'] = $namespace; - $connectionClass = $namespace . '\\Connection'; - if (class_exists($connectionClass)) { - $drivers[$driver]['connectionClass'] = $connectionClass; + $connection_class = $namespace . '\\Connection'; + if (class_exists($connection_class)) { + static::$discoveredDrivers[$driver_name]['connectionClass'] = $connection_class; } - $installTasksClass = $namespace . '\\Install\\Tasks'; - if (class_exists($installTasksClass)) { - $installer = new $installTasksClass(); - $drivers[$driver]['installer'] = $installer; - $drivers[$driver]['installable'] = $installer->installable(); + $install_tasks_class = $namespace . '\\Install\\Tasks'; + if (class_exists($install_tasks_class)) { + $installer = new $install_tasks_class(); + static::$discoveredDrivers[$driver_name]['installer'] = $installer; + static::$discoveredDrivers[$driver_name]['installable'] = $installer->installable(); } } - return $drivers; + return static::$discoveredDrivers; } } diff --git a/core/modules/system/tests/src/Kernel/Scripts/DbCommandBaseTest.php b/core/modules/system/tests/src/Kernel/Scripts/DbCommandBaseTest.php index 7604ab4..f8eef85 100644 --- a/core/modules/system/tests/src/Kernel/Scripts/DbCommandBaseTest.php +++ b/core/modules/system/tests/src/Kernel/Scripts/DbCommandBaseTest.php @@ -1,4 +1,8 @@