mirror of
https://github.com/FOSSBilling/FOSSBilling.git
synced 2026-12-03 21:54:07 +09:00
Add database configuration retrieval and normalize driver (#3605)
Add getDatabaseConfig and normalize DB driver
This commit is contained in:
1 parent
c30c1f772c
commit
9a14103cd0
3 files changed
+102
-2
No files matched your search
+1
-1
@@ -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,
|
||||
];
|
||||
|
||||
@@ -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,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use FOSSBilling\Doctrine\DriverManagerFactory;
|
||||
use PHPUnit\Framework\Attributes\Group;
|
||||
|
||||
#[Group('Core')]
|
||||
final class FOSSBilling_DriverManagerFactoryTest extends PHPUnit\Framework\TestCase
|
||||
{
|
||||
private ?string $originalConfigContents = null;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$configContents = file_get_contents(PATH_CONFIG);
|
||||
if ($configContents === false) {
|
||||
self::fail('Failed to read the FOSSBilling config file.');
|
||||
}
|
||||
|
||||
$this->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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user