Merge remote-tracking branch 'upstream/develop' into 4.6

Conflicts:
	system/Database/Database.php
This commit is contained in:
kenjis 2024-09-05 10:30:40 +09:00
commit 31e8f9f9c4
No known key found for this signature in database
GPG Key ID: BD254878922AF198

View File

@ -13,6 +13,8 @@ declare(strict_types=1);
namespace CodeIgniter\Database;
use CodeIgniter\Exceptions\ConfigException;
use CodeIgniter\Exceptions\CriticalError;
use CodeIgniter\Exceptions\InvalidArgumentException;
/**
@ -54,6 +56,8 @@ class Database
throw new InvalidArgumentException('You have not selected a database type to connect to.');
}
assert($this->checkDbExtension($params['DBDriver']));
$this->connections[$alias] = $this->initDriver($params['DBDriver'], 'Connection', $params);
return $this->connections[$alias];
@ -124,9 +128,9 @@ class Database
/**
* Creates a database object.
*
* @param string $driver Driver name. FQCN can be used.
* @param string $class 'Connection'|'Forge'|'Utils'
* @param array|object $argument The constructor parameter.
* @param string $driver Driver name. FQCN can be used.
* @param string $class 'Connection'|'Forge'|'Utils'
* @param array|ConnectionInterface $argument The constructor parameter or DB connection
*
* @return BaseConnection|BaseUtils|Forge
*/
@ -138,4 +142,43 @@ class Database
return new $classname($argument);
}
/**
* Check the PHP database extension is loaded.
*
* @param string $driver DB driver or FQCN for custom driver
*/
private function checkDbExtension(string $driver): bool
{
if (str_contains($driver, '\\')) {
// Cannot check a fully qualified classname for a custom driver.
return true;
}
$extensionMap = [
// DBDriver => PHP extension
'MySQLi' => 'mysqli',
'SQLite3' => 'sqlite3',
'Postgre' => 'pgsql',
'SQLSRV' => 'sqlsrv',
'OCI8' => 'oci8',
];
$extension = $extensionMap[$driver] ?? '';
if ($extension === '') {
$message = 'Invalid DBDriver name: "' . $driver . '"';
throw new ConfigException($message);
}
if (extension_loaded($extension)) {
return true;
}
$message = 'The required PHP extension "' . $extension . '" is not loaded.'
. ' Install and enable it to use "' . $driver . '" driver.';
throw new CriticalError($message);
}
}