1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-07-31 05:50:42 +02:00

[ticket/16741] Database tools to use Doctrine

PHPBB3-16741
This commit is contained in:
Máté Bartus
2021-07-31 12:06:08 +02:00
committed by Tristan Darricau
parent aee5e373bc
commit 98134abe20
15 changed files with 877 additions and 3435 deletions

View File

@@ -16,6 +16,7 @@ namespace phpbb\db\doctrine;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Types\Type;
use InvalidArgumentException;
use phpbb\config_php_file;
use phpbb\exception\runtime_exception;
@@ -25,8 +26,6 @@ use phpbb\exception\runtime_exception;
*/
class connection_factory
{
use driver_convertor;
/**
* Creates a Doctrine DBAL connection from phpBB configuration.
*
@@ -37,7 +36,7 @@ class connection_factory
* @throws runtime_exception If the database connection could not be established.
* @throws InvalidArgumentException If the provided driver name is not a valid phpBB database driver.
*/
public static function get_connection(config_php_file $config) : Connection
public static function get_connection(config_php_file $config): Connection
{
$driver = $config->get('dbms');
$host = $config->get('dbhost');
@@ -77,7 +76,7 @@ class connection_factory
?string $user = null,
?string $password = null,
?string $name = null,
?string $port = null) : Connection
?string $port = null): Connection
{
$available_drivers = DriverManager::getAvailableDrivers();
if (!in_array($driver, $available_drivers))
@@ -97,7 +96,10 @@ class connection_factory
try
{
return DriverManager::getConnection($params);
$connection = DriverManager::getConnection($params);
Type::addType(case_insensitive_string::CASE_INSENSITIVE_STRING, case_insensitive_string::class);
$connection->getDatabasePlatform()->registerDoctrineTypeMapping('varchar_ci', case_insensitive_string::CASE_INSENSITIVE_STRING);
return $connection;
}
catch (Exception $e)
{
@@ -105,6 +107,52 @@ class connection_factory
}
}
/**
* Converts phpBB driver names to Doctrine's equivalent.
*
* @param string $driver_name phpBB database driver name.
*
* @return string Doctrine DBAL's driver name.
*
* @throws InvalidArgumentException If $driver_name is not a valid phpBB database driver.
*/
private static function to_doctrine_driver(string $driver_name): string
{
// Normalize driver name.
$name = str_replace('phpbb\db\driver', '', $driver_name);
$name = preg_replace('/mysql$/i', 'mysqli', $name);
$name = trim($name, '\\');
switch ($name)
{
case 'mssql_odbc':
case 'mssqlnative':
$name = 'pdo_sqlsrv';
break;
case 'mysqli':
$name = 'pdo_mysql';
break;
case 'oracle':
$name = 'oci8';
break;
case 'postgres':
$name = 'pdo_pgsql';
break;
case 'sqlite3':
$name = 'pdo_sqlite';
break;
default:
throw new InvalidArgumentException('Invalid phpBB database driver provided: ' . $driver_name);
}
return $name;
}
/*
* Disable constructor.
*/