diff --git a/core/core.services.yml b/core/core.services.yml index 49b2708..b014e5d 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -354,6 +354,9 @@ services: class: Drupal\Core\Database\Connection factory: Drupal\Core\Database\Database::getConnection arguments: [default] + database.driver.discovery: + class: Drupal\Core\Database\DatabaseDriverDiscovery + arguments: ['@app.root'] datetime.time: class: Drupal\Component\Datetime\Time arguments: ['@request_stack'] diff --git a/core/includes/install.core.inc b/core/includes/install.core.inc index a869d90..97b409b 100644 --- a/core/includes/install.core.inc +++ b/core/includes/install.core.inc @@ -327,8 +327,9 @@ function install_begin_request($class_loader, &$install_state) { date_default_timezone_set('Australia/Sydney'); } + $app_root = dirname(dirname(__DIR__)); $site_path = DrupalKernel::findSitePath($request, FALSE); - Settings::initialize(dirname(dirname(__DIR__)), $site_path, $class_loader); + Settings::initialize($app_root, $site_path, $class_loader); // Ensure that procedural dependencies are loaded as early as possible, // since the error/exception handlers depend on them. @@ -358,6 +359,11 @@ function install_begin_request($class_loader, &$install_state) { ->register('string_translation', 'Drupal\Core\StringTranslation\TranslationManager') ->addArgument(new Reference('language.default')); + // Register the database driver discovery service. + $container + ->register('database.driver.discovery', 'Drupal\Core\Database\DatabaseDriverDiscovery') + ->addArgument($app_root); + // Register the stream wrapper manager. $container ->register('stream_wrapper_manager', 'Drupal\Core\StreamWrapper\StreamWrapperManager') diff --git a/core/includes/install.inc b/core/includes/install.inc index 96b366c..4d7a8c4 100644 --- a/core/includes/install.inc +++ b/core/includes/install.inc @@ -6,7 +6,6 @@ */ use Drupal\Component\Utility\Unicode; -use Drupal\Core\Database\DatabaseDriverDiscovery; use Symfony\Component\HttpFoundation\RedirectResponse; use Drupal\Component\Utility\Crypt; use Drupal\Component\Utility\OpCodeCache; @@ -144,14 +143,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::getInstallableDriversInstallerNames + * \Drupal::service('database.driver.discovery')->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::getInstallableDriversInstallerNames instead. See https://www.drupal.org/node/2896416.', E_USER_DEPRECATED); - return (new DatabaseDriverDiscovery())->getInstallableDriversInstallerNames(); + @trigger_error('drupal_detect_database_types() is deprecated in Drupal 8.5.0, will be removed before Drupal 9.0.0. Use \Drupal::service(\'database.driver.discovery\')->getInstallableDriversInstallerNames instead. See https://www.drupal.org/node/2896416.', E_USER_DEPRECATED); + return \Drupal::service('database.driver.discovery')->getInstallableDriversInstallerNames(); } /** @@ -161,14 +160,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::getInstallableDriversInstallers + * \Drupal::service('database.driver.discovery')->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::getInstallableDriversInstallers instead. See https://www.drupal.org/node/2896416.', E_USER_DEPRECATED); - return (new DatabaseDriverDiscovery())->getInstallableDriversInstallers(); + @trigger_error('drupal_get_database_types() is deprecated in Drupal 8.5.0, will be removed before Drupal 9.0.0. Use \Drupal::service(\'database.driver.discovery\')->getInstallableDriversInstallers instead. See https://www.drupal.org/node/2896416.', E_USER_DEPRECATED); + return \Drupal::service('database.driver.discovery')->getInstallableDriversInstallers(); } /** @@ -1090,11 +1089,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::getInstaller instead. + * \Drupal::service('database.driver.discovery')->getInstaller instead. * * @see https://www.drupal.org/node/2896416 */ function db_installer_object($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); + @trigger_error('db_installer_object() is deprecated in Drupal 8.5.0, will be removed before Drupal 9.0.0. Use \Drupal::service(\'database.driver.discovery\')->getInstaller instead. See https://www.drupal.org/node/2896416.', E_USER_DEPRECATED); + return \Drupal::service('database.driver.discovery')->getInstaller($driver); } diff --git a/core/lib/Drupal/Core/Database/Database.php b/core/lib/Drupal/Core/Database/Database.php index 5ae87ea..8ce683a 100644 --- a/core/lib/Drupal/Core/Database/Database.php +++ b/core/lib/Drupal/Core/Database/Database.php @@ -462,8 +462,7 @@ public static function convertDbUrlToConnectionInfo($url, $root) { // Discover if the URL has a driver namespace. $scheme = $uri->getScheme(); - $discovery = new DatabaseDriverDiscovery(); - $drivers = $discovery->discoverDatabaseDrivers(); + $drivers = \Drupal::service('database.driver.discovery')->discoverDatabaseDrivers(); if (!isset($drivers[$scheme])) { throw new \RuntimeException("Drupal database driver not available: '" . $uri->getScheme() . "'"); } diff --git a/core/lib/Drupal/Core/Database/DatabaseDriverDiscovery.php b/core/lib/Drupal/Core/Database/DatabaseDriverDiscovery.php index 1b9699b..0c54e0a 100644 --- a/core/lib/Drupal/Core/Database/DatabaseDriverDiscovery.php +++ b/core/lib/Drupal/Core/Database/DatabaseDriverDiscovery.php @@ -8,11 +8,28 @@ class DatabaseDriverDiscovery { /** + * The app root. + * + * @var string + */ + protected $appRoot; + + /** * Discovered database drivers. * * @var array */ - protected static $discoveredDrivers; + protected $discoveredDrivers; + + /** + * Constructs a DatabaseDriverDiscovery object. + * + * @param string $app_root + * The app root. + */ + public function __construct($app_root) { + $this->appRoot = $app_root; + } /** * Returns locations of directories for database drivers discovery. @@ -26,7 +43,7 @@ class DatabaseDriverDiscovery { protected function getDiscoveryLocations() { return [ 'Drupal\\Core\\Database\\Driver' => __DIR__ . '/Driver', - 'Drupal\\Driver\\Database' => __DIR__ . '/../../../../../drivers/lib/Drupal/Driver/Database', + 'Drupal\\Driver\\Database' => $this->appRoot . '/drivers/lib/Drupal/Driver/Database', ]; } @@ -92,6 +109,7 @@ public function getInstaller($driver_name) { * @return array * An associative array with key the database driver name, and value an * associative array bearing the following keys and values: + * - 'namespace' => the PHP namespace for this driver; * - 'connectionClass' => the FQCN of the Connection class for this driver; * - 'installer' => the database driver installer object; * - 'installable' => whether the database driver can be installed. @@ -100,8 +118,8 @@ public function getInstaller($driver_name) { * When there are duplicated driver names. */ public function discoverDatabaseDrivers() { - if (isset(static::$discoveredDrivers)) { - return static::$discoveredDrivers; + if (isset($this->discoveredDrivers)) { + return $this->discoveredDrivers; } // Scan the discovery locations and build an array of candidate discovered @@ -131,30 +149,21 @@ public function discoverDatabaseDrivers() { if (!class_exists($connection_class)) { continue; } - static::$discoveredDrivers[$driver_name]['namespace'] = $namespace; - static::$discoveredDrivers[$driver_name]['connectionClass'] = $connection_class; + $this->discoveredDrivers[$driver_name]['namespace'] = $namespace; + $this->discoveredDrivers[$driver_name]['connectionClass'] = $connection_class; $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(); + $this->discoveredDrivers[$driver_name]['installer'] = $installer; + $this->discoveredDrivers[$driver_name]['installable'] = $installer->installable(); } else { - static::$discoveredDrivers[$driver_name]['installer'] = NULL; - static::$discoveredDrivers[$driver_name]['installable'] = FALSE; + $this->discoveredDrivers[$driver_name]['installer'] = NULL; + $this->discoveredDrivers[$driver_name]['installable'] = FALSE; } } - return static::$discoveredDrivers; - } - - /** - * Resets the static list of discovered database drivers. - * - * Should be only used for testing purposes. - */ - public function resetDiscoveredDatabaseDrivers() { - static::$discoveredDrivers = NULL; + return $this->discoveredDrivers; } } diff --git a/core/tests/Drupal/KernelTests/Core/Database/DatabaseDriverDiscoveryTest.php b/core/tests/Drupal/KernelTests/Core/Database/DatabaseDriverDiscoveryTest.php index 15f8646..834e9ab 100644 --- a/core/tests/Drupal/KernelTests/Core/Database/DatabaseDriverDiscoveryTest.php +++ b/core/tests/Drupal/KernelTests/Core/Database/DatabaseDriverDiscoveryTest.php @@ -33,6 +33,7 @@ public function setUp() { // Database unit test namespace. $mock_builder = $this->getMockBuilder(DatabaseDriverDiscovery::class); $this->databaseDriverDiscovery = $mock_builder + ->disableOriginalConstructor() ->setMethods(['getDiscoveryLocations']) ->getMock(); $this->databaseDriverDiscovery @@ -42,10 +43,6 @@ public function setUp() { 'Drupal\\Core\\Database\\Driver' => __DIR__ . '/../../../../../lib/Drupal/Core/Database/Driver', 'Drupal\\Tests\\Core\\Database' => __DIR__ . '/../../../Tests/Core/Database', ])); - - // Reset the static list of drivers to allow rescanning the locations - // during the test. - $this->databaseDriverDiscovery->resetDiscoveredDatabaseDrivers(); } /** diff --git a/core/tests/Drupal/Tests/Core/Database/UrlConversionTest.php b/core/tests/Drupal/KernelTests/Core/Database/UrlConversionTest.php similarity index 96% rename from core/tests/Drupal/Tests/Core/Database/UrlConversionTest.php rename to core/tests/Drupal/KernelTests/Core/Database/UrlConversionTest.php index 8b764ef..66cbe14 100644 --- a/core/tests/Drupal/Tests/Core/Database/UrlConversionTest.php +++ b/core/tests/Drupal/KernelTests/Core/Database/UrlConversionTest.php @@ -1,16 +1,15 @@ root . '/core/includes/bootstrap.inc'; // Set up virtual filesystem. + $container = new ContainerBuilder(); + $container + ->register('database.driver.discovery', 'Drupal\Core\Database\DatabaseDriverDiscovery') + ->addArgument($this->root); + \Drupal::setContainer($container); Database::addConnectionInfo('default', 'test-runner', $this->getDatabaseConnectionInfo()['default']); $test_db = new TestDatabase(); $this->siteDirectory = $test_db->getTestSitePath(); diff --git a/core/tests/Drupal/Tests/Listeners/DeprecationListenerTrait.php b/core/tests/Drupal/Tests/Listeners/DeprecationListenerTrait.php index fd624d8..3c9e8d6 100644 --- a/core/tests/Drupal/Tests/Listeners/DeprecationListenerTrait.php +++ b/core/tests/Drupal/Tests/Listeners/DeprecationListenerTrait.php @@ -97,9 +97,9 @@ public static function getSkippedDeprecations() { 'Drupal\system\Plugin\views\field\BulkForm is deprecated in Drupal 8.5.x, will be removed before Drupal 9.0.0. Use \Drupal\views\Plugin\views\field\BulkForm instead. See https://www.drupal.org/node/2916716.', 'The numeric plugin for watchdog.wid field is deprecated in Drupal 8.4.0 and will be removed before Drupal 9.0.0. Must use standard plugin instead. See https://www.drupal.org/node/2876378.', 'The numeric plugin for watchdog.uid field is deprecated in Drupal 8.4.0 and will be removed before Drupal 9.0.0. Must use standard plugin instead. See https://www.drupal.org/node/2876378.', - '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.', - '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.', - '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.', + 'drupal_detect_database_types() is deprecated in Drupal 8.5.0, will be removed before Drupal 9.0.0. Use \Drupal::service(\'database.driver.discovery\')->getInstallableDriversInstallerNames instead. See https://www.drupal.org/node/2896416.', + 'drupal_get_database_types() is deprecated in Drupal 8.5.0, will be removed before Drupal 9.0.0. Use \Drupal::service(\'database.driver.discovery\')->getInstallableDriversInstallers instead. See https://www.drupal.org/node/2896416.', + 'db_installer_object() is deprecated in Drupal 8.5.0, will be removed before Drupal 9.0.0. Use \Drupal::service(\'database.driver.discovery\')->getInstaller instead. See https://www.drupal.org/node/2896416.', 'The in_operator plugin for watchdog.type filter is deprecated in Drupal 8.4.0 and will be removed before Drupal 9.0.0. Must use dblog_types plugin instead. See https://www.drupal.org/node/2876378.', 'Using an instance of "Twig_Filter_Function" for filter "testfilter" is deprecated since version 1.21. Use Twig_SimpleFilter instead.', 'The Twig_Function class is deprecated since version 1.12 and will be removed in 2.0. Use Twig_SimpleFunction instead.',