From 9a14103cd05d07bb6162ba5012fea0dd3878b9cc Mon Sep 17 00:00:00 2001 From: Adam Daley Date: Mon, 25 May 2026 01:59:13 +0100 Subject: [PATCH] Add database configuration retrieval and normalize driver (#3605) Add getDatabaseConfig and normalize DB driver --- src/di.php | 2 +- .../Doctrine/DriverManagerFactory.php | 30 +++++++- .../FOSSBilling_DriverManagerFactoryTest.php | 72 +++++++++++++++++++ 3 files changed, 102 insertions(+), 2 deletions(-) create mode 100644 tests-legacy/library/FOSSBilling/FOSSBilling_DriverManagerFactoryTest.php diff --git a/src/di.php b/src/di.php index 0004388ba..ca9505e0a 100644 --- a/src/di.php +++ b/src/di.php @@ -82,7 +82,7 @@ $di['crypt'] = function () use ($di) { */ $di['pdo'] = function () { $debugConfig = Config::getProperty('debug_and_monitoring', []); - $dbConfig = Config::getProperty('db'); + $dbConfig = DriverManagerFactory::getDatabaseConfig(); $driverOptions = [ PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, ]; diff --git a/src/library/FOSSBilling/Doctrine/DriverManagerFactory.php b/src/library/FOSSBilling/Doctrine/DriverManagerFactory.php index 9df9f50a0..5579abfe8 100644 --- a/src/library/FOSSBilling/Doctrine/DriverManagerFactory.php +++ b/src/library/FOSSBilling/Doctrine/DriverManagerFactory.php @@ -38,6 +38,26 @@ class DriverManagerFactory 'latin1', ]; + /** + * Returns the database configuration in the shape expected by Doctrine. + * + * Older FOSSBilling versions used db.type=mysql and some installs may not + * have been rewritten by the config patcher before a database connection is + * requested during an upgrade. + */ + public static function getDatabaseConfig(): array + { + $dbConfig = Config::getProperty('db', []); + if (!is_array($dbConfig)) { + throw new Exception('Database configuration is invalid.'); + } + + $dbConfig['driver'] ??= self::normalizeDriver($dbConfig['type'] ?? 'pdo_mysql'); + $dbConfig['port'] ??= '3306'; + + return $dbConfig; + } + /** * Creates and returns a Doctrine DBAL Connection instance. * @@ -47,7 +67,7 @@ class DriverManagerFactory */ public static function getConnection(array $driverOptions = []): Connection { - $dbConfig = Config::getProperty('db'); + $dbConfig = self::getDatabaseConfig(); $requiredKeys = ['driver', 'host', 'port', 'name', 'user', 'password']; foreach ($requiredKeys as $key) { @@ -78,4 +98,12 @@ class DriverManagerFactory return DriverManager::getConnection($connectionParams); } + + private static function normalizeDriver(string $driver): string + { + return match ($driver) { + 'mysql' => 'pdo_mysql', + default => $driver, + }; + } } diff --git a/tests-legacy/library/FOSSBilling/FOSSBilling_DriverManagerFactoryTest.php b/tests-legacy/library/FOSSBilling/FOSSBilling_DriverManagerFactoryTest.php new file mode 100644 index 000000000..17db7f749 --- /dev/null +++ b/tests-legacy/library/FOSSBilling/FOSSBilling_DriverManagerFactoryTest.php @@ -0,0 +1,72 @@ +originalConfigContents = $configContents; + } + + public function testLegacyMysqlTypeIsNormalizedBeforeConnecting(): void + { + $this->writeDbConfig([ + 'type' => 'mysql', + 'host' => '127.0.0.1', + 'name' => 'fossbilling', + 'user' => 'fossbilling', + 'password' => 'secret', + ]); + + $dbConfig = DriverManagerFactory::getDatabaseConfig(); + $connection = DriverManagerFactory::getConnection(); + + self::assertSame('pdo_mysql', $dbConfig['driver']); + self::assertSame('3306', $dbConfig['port']); + self::assertSame('pdo_mysql', $connection->getParams()['driver']); + self::assertSame('3306', $connection->getParams()['port']); + } + + protected function tearDown(): void + { + if ($this->originalConfigContents !== null) { + file_put_contents(PATH_CONFIG, $this->originalConfigContents); + clearstatcache(true, PATH_CONFIG); + + if (function_exists('opcache_invalidate')) { + @opcache_invalidate(PATH_CONFIG, true); + } + } + + @unlink(Symfony\Component\Filesystem\Path::changeExtension(PATH_CONFIG, 'old.php')); + } + + private function writeDbConfig(array $dbConfig): void + { + $config = FOSSBilling\Config::getConfig(); + $config['db'] = $dbConfig; + + $reflection = new ReflectionClass(FOSSBilling\Config::class); + $method = $reflection->getMethod('prettyPrintArrayToPHP'); + $rendered = $method->invoke(null, $config); + + file_put_contents(PATH_CONFIG, $rendered); + clearstatcache(true, PATH_CONFIG); + + if (function_exists('opcache_invalidate')) { + @opcache_invalidate(PATH_CONFIG, true); + } + } +}