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..dba5ce0 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,13 @@ 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(); + return \Drupal::service('database.driver.discovery')->getInstallableDriversInstallerNames(); } /** @@ -161,14 +159,13 @@ 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(); + return \Drupal::service('database.driver.discovery')->getInstallableDriversInstallers(); } /** @@ -1090,11 +1087,10 @@ 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); + 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/modules/system/src/Tests/Update/UpdatePathTestBase.php b/core/modules/system/src/Tests/Update/UpdatePathTestBase.php index 05719fb..b5529c5 100644 --- a/core/modules/system/src/Tests/Update/UpdatePathTestBase.php +++ b/core/modules/system/src/Tests/Update/UpdatePathTestBase.php @@ -325,6 +325,9 @@ protected function runDbTasks() { $container ->register('string_translation', 'Drupal\Core\StringTranslation\TranslationManager') ->addArgument(new Reference('language.default')); + $container + ->register('database.driver.discovery', 'Drupal\Core\Database\DatabaseDriverDiscovery') + ->addArgument(__DIR__ . '/../../../../../..'); \Drupal::setContainer($container); require_once __DIR__ . '/../../../../../includes/install.inc'; diff --git a/core/tests/Drupal/FunctionalTests/Update/UpdatePathTestBase.php b/core/tests/Drupal/FunctionalTests/Update/UpdatePathTestBase.php index 4aca500..c59d096 100644 --- a/core/tests/Drupal/FunctionalTests/Update/UpdatePathTestBase.php +++ b/core/tests/Drupal/FunctionalTests/Update/UpdatePathTestBase.php @@ -161,6 +161,11 @@ protected function setUp() { $kernel = TestRunnerKernel::createFromRequest($request, $autoloader); $kernel->loadLegacyIncludes(); + $container = new ContainerBuilder(); + $container + ->register('database.driver.discovery', 'Drupal\Core\Database\DatabaseDriverDiscovery') + ->addArgument($this->root); + \Drupal::setContainer($container); $this->changeDatabasePrefix(); $this->runDbTasks(); // Allow classes to set database dump files. @@ -400,6 +405,9 @@ protected function runDbTasks() { $container ->register('string_translation', 'Drupal\Core\StringTranslation\TranslationManager') ->addArgument(new Reference('language.default')); + $container + ->register('database.driver.discovery', 'Drupal\Core\Database\DatabaseDriverDiscovery') + ->addArgument($this->root); \Drupal::setContainer($container); require_once __DIR__ . '/../../../../includes/install.inc'; 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/KernelTests/KernelTestBase.php b/core/tests/Drupal/KernelTests/KernelTestBase.php index 82e1036..a051c14 100644 --- a/core/tests/Drupal/KernelTests/KernelTestBase.php +++ b/core/tests/Drupal/KernelTests/KernelTestBase.php @@ -263,6 +263,11 @@ protected function bootEnvironment() { require_once $this->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/Core/Database/UrlConversionTest.php b/core/tests/Drupal/Tests/Core/Database/UrlConversionTest.php index 8b764ef..2e56e0f 100644 --- a/core/tests/Drupal/Tests/Core/Database/UrlConversionTest.php +++ b/core/tests/Drupal/Tests/Core/Database/UrlConversionTest.php @@ -3,6 +3,7 @@ namespace Drupal\Tests\Core\Database; use Drupal\Core\Database\Database; +use Drupal\Core\DependencyInjection\ContainerBuilder; use Drupal\Tests\UnitTestCase; /** @@ -13,6 +14,18 @@ class UrlConversionTest extends UnitTestCase { /** + * {@inheritdoc} + */ + protected function setUp() { + parent::setUp(); + $container = new ContainerBuilder(); + $container + ->register('database.driver.discovery', 'Drupal\Core\Database\DatabaseDriverDiscovery') + ->addArgument(__DIR__ . '/../../../../../..'); + \Drupal::setContainer($container); + } + + /** * @covers ::convertDbUrlToConnectionInfo * * @dataProvider providerConvertDbUrlToConnectionInfo @@ -55,7 +68,7 @@ public function providerConvertDbUrlToConnectionInfo() { ]; return [ [$root1, $url1, $database_array1], - [$root2, $url2, $database_array2] + [$root2, $url2, $database_array2], ]; } diff --git a/core/tests/Drupal/Tests/Listeners/DeprecationListenerTrait.php b/core/tests/Drupal/Tests/Listeners/DeprecationListenerTrait.php index fd624d8..b7445fb 100644 --- a/core/tests/Drupal/Tests/Listeners/DeprecationListenerTrait.php +++ b/core/tests/Drupal/Tests/Listeners/DeprecationListenerTrait.php @@ -97,9 +97,6 @@ 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.', '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.',