mirror of
https://github.com/dg/dibi.git
synced 2025-08-02 20:27:35 +02:00
Dibi\Driver::connect() replaced with constructor (BC break)
This commit is contained in:
@@ -28,7 +28,7 @@ class Connection implements IConnection
|
||||
/** @var array Current connection configuration */
|
||||
private $config;
|
||||
|
||||
/** @var Driver */
|
||||
/** @var Driver|null */
|
||||
private $driver;
|
||||
|
||||
/** @var Translator|null */
|
||||
@@ -76,25 +76,7 @@ class Connection implements IConnection
|
||||
Helpers::alias($config, 'host', 'hostname');
|
||||
Helpers::alias($config, 'result|formatDate', 'resultDate');
|
||||
Helpers::alias($config, 'result|formatDateTime', 'resultDateTime');
|
||||
|
||||
if (!isset($config['driver'])) {
|
||||
$config['driver'] = 'mysqli';
|
||||
}
|
||||
|
||||
if ($config['driver'] instanceof Driver) {
|
||||
$this->driver = $config['driver'];
|
||||
$config['driver'] = get_class($this->driver);
|
||||
} elseif (is_subclass_of($config['driver'], Driver::class)) {
|
||||
$this->driver = new $config['driver'];
|
||||
} else {
|
||||
$class = preg_replace(['#\W#', '#sql#'], ['_', 'Sql'], ucfirst(strtolower($config['driver'])));
|
||||
$class = "Dibi\\Drivers\\{$class}Driver";
|
||||
if (!class_exists($class)) {
|
||||
throw new Exception("Unable to create instance of dibi driver '$class'.");
|
||||
}
|
||||
$this->driver = new $class;
|
||||
}
|
||||
|
||||
$config['driver'] = $config['driver'] ?? 'mysqli';
|
||||
$config['name'] = $name;
|
||||
$this->config = $config;
|
||||
|
||||
@@ -133,9 +115,19 @@ class Connection implements IConnection
|
||||
*/
|
||||
final public function connect(): void
|
||||
{
|
||||
if (is_subclass_of($this->config['driver'], Driver::class)) {
|
||||
$class = $this->config['driver'];
|
||||
} else {
|
||||
$class = preg_replace(['#\W#', '#sql#'], ['_', 'Sql'], ucfirst(strtolower($this->config['driver'])));
|
||||
$class = "Dibi\\Drivers\\{$class}Driver";
|
||||
if (!class_exists($class)) {
|
||||
throw new Exception("Unable to create instance of dibi driver '$class'.");
|
||||
}
|
||||
}
|
||||
|
||||
$event = $this->onEvent ? new Event($this, Event::CONNECT) : null;
|
||||
try {
|
||||
$this->driver->connect($this->config);
|
||||
$this->driver = new $class($this->config);
|
||||
$this->connected = true;
|
||||
if ($event) {
|
||||
$this->onEvent($event->done());
|
||||
@@ -155,8 +147,10 @@ class Connection implements IConnection
|
||||
*/
|
||||
final public function disconnect(): void
|
||||
{
|
||||
$this->driver->disconnect();
|
||||
$this->connected = false;
|
||||
if ($this->connected) {
|
||||
$this->driver->disconnect();
|
||||
$this->connected = false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@@ -30,7 +30,7 @@ class FirebirdDriver implements Dibi\Driver
|
||||
|
||||
public const ERROR_EXCEPTION_THROWN = -836;
|
||||
|
||||
/** @var resource|null */
|
||||
/** @var resource */
|
||||
private $connection;
|
||||
|
||||
/** @var resource|null */
|
||||
@@ -43,20 +43,12 @@ class FirebirdDriver implements Dibi\Driver
|
||||
/**
|
||||
* @throws Dibi\NotSupportedException
|
||||
*/
|
||||
public function __construct()
|
||||
public function __construct(array &$config)
|
||||
{
|
||||
if (!extension_loaded('interbase')) {
|
||||
throw new Dibi\NotSupportedException("PHP extension 'interbase' is not loaded.");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Connects to a database.
|
||||
* @throws Dibi\Exception
|
||||
*/
|
||||
public function connect(array &$config): void
|
||||
{
|
||||
Helpers::alias($config, 'database', 'db');
|
||||
|
||||
if (isset($config['resource'])) {
|
||||
|
@@ -40,7 +40,7 @@ class MySqliDriver implements Dibi\Driver
|
||||
|
||||
public const ERROR_DATA_TRUNCATED = 1265;
|
||||
|
||||
/** @var \mysqli|null */
|
||||
/** @var \mysqli */
|
||||
private $connection;
|
||||
|
||||
/** @var bool Is buffered (seekable and countable)? */
|
||||
@@ -50,20 +50,12 @@ class MySqliDriver implements Dibi\Driver
|
||||
/**
|
||||
* @throws Dibi\NotSupportedException
|
||||
*/
|
||||
public function __construct()
|
||||
public function __construct(array &$config)
|
||||
{
|
||||
if (!extension_loaded('mysqli')) {
|
||||
throw new Dibi\NotSupportedException("PHP extension 'mysqli' is not loaded.");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Connects to a database.
|
||||
* @throws Dibi\Exception
|
||||
*/
|
||||
public function connect(array &$config): void
|
||||
{
|
||||
mysqli_report(MYSQLI_REPORT_OFF);
|
||||
if (isset($config['resource'])) {
|
||||
$this->connection = $config['resource'];
|
||||
|
@@ -26,7 +26,7 @@ class OdbcDriver implements Dibi\Driver
|
||||
{
|
||||
use Dibi\Strict;
|
||||
|
||||
/** @var resource|null */
|
||||
/** @var resource */
|
||||
private $connection;
|
||||
|
||||
/** @var int|null Affected rows */
|
||||
@@ -36,20 +36,12 @@ class OdbcDriver implements Dibi\Driver
|
||||
/**
|
||||
* @throws Dibi\NotSupportedException
|
||||
*/
|
||||
public function __construct()
|
||||
public function __construct(array &$config)
|
||||
{
|
||||
if (!extension_loaded('odbc')) {
|
||||
throw new Dibi\NotSupportedException("PHP extension 'odbc' is not loaded.");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Connects to a database.
|
||||
* @throws Dibi\Exception
|
||||
*/
|
||||
public function connect(array &$config): void
|
||||
{
|
||||
if (isset($config['resource'])) {
|
||||
$this->connection = $config['resource'];
|
||||
} else {
|
||||
|
@@ -29,7 +29,7 @@ class OracleDriver implements Dibi\Driver
|
||||
{
|
||||
use Dibi\Strict;
|
||||
|
||||
/** @var resource|null */
|
||||
/** @var resource */
|
||||
private $connection;
|
||||
|
||||
/** @var bool */
|
||||
@@ -45,20 +45,12 @@ class OracleDriver implements Dibi\Driver
|
||||
/**
|
||||
* @throws Dibi\NotSupportedException
|
||||
*/
|
||||
public function __construct()
|
||||
public function __construct(array &$config)
|
||||
{
|
||||
if (!extension_loaded('oci8')) {
|
||||
throw new Dibi\NotSupportedException("PHP extension 'oci8' is not loaded.");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Connects to a database.
|
||||
* @throws Dibi\Exception
|
||||
*/
|
||||
public function connect(array &$config): void
|
||||
{
|
||||
$foo = &$config['charset'];
|
||||
|
||||
if (isset($config['formatDate']) || isset($config['formatDateTime'])) {
|
||||
|
@@ -45,20 +45,12 @@ class PdoDriver implements Dibi\Driver
|
||||
/**
|
||||
* @throws Dibi\NotSupportedException
|
||||
*/
|
||||
public function __construct()
|
||||
public function __construct(array &$config)
|
||||
{
|
||||
if (!extension_loaded('pdo')) {
|
||||
throw new Dibi\NotSupportedException("PHP extension 'pdo' is not loaded.");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Connects to a database.
|
||||
* @throws Dibi\Exception
|
||||
*/
|
||||
public function connect(array &$config): void
|
||||
{
|
||||
$foo = &$config['dsn'];
|
||||
$foo = &$config['options'];
|
||||
Helpers::alias($config, 'resource', 'pdo');
|
||||
|
@@ -28,7 +28,7 @@ class PostgreDriver implements Dibi\Driver
|
||||
{
|
||||
use Dibi\Strict;
|
||||
|
||||
/** @var resource|null */
|
||||
/** @var resource */
|
||||
private $connection;
|
||||
|
||||
/** @var int|null Affected rows */
|
||||
@@ -38,20 +38,12 @@ class PostgreDriver implements Dibi\Driver
|
||||
/**
|
||||
* @throws Dibi\NotSupportedException
|
||||
*/
|
||||
public function __construct()
|
||||
public function __construct(array &$config)
|
||||
{
|
||||
if (!extension_loaded('pgsql')) {
|
||||
throw new Dibi\NotSupportedException("PHP extension 'pgsql' is not loaded.");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Connects to a database.
|
||||
* @throws Dibi\Exception
|
||||
*/
|
||||
public function connect(array &$config): void
|
||||
{
|
||||
$error = null;
|
||||
if (isset($config['resource'])) {
|
||||
$this->connection = $config['resource'];
|
||||
|
@@ -27,7 +27,7 @@ class Sqlite3Driver implements Dibi\Driver
|
||||
{
|
||||
use Dibi\Strict;
|
||||
|
||||
/** @var SQLite3|null */
|
||||
/** @var SQLite3 */
|
||||
private $connection;
|
||||
|
||||
/** @var string Date format */
|
||||
@@ -40,20 +40,12 @@ class Sqlite3Driver implements Dibi\Driver
|
||||
/**
|
||||
* @throws Dibi\NotSupportedException
|
||||
*/
|
||||
public function __construct()
|
||||
public function __construct(array &$config)
|
||||
{
|
||||
if (!extension_loaded('sqlite3')) {
|
||||
throw new Dibi\NotSupportedException("PHP extension 'sqlite3' is not loaded.");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Connects to a database.
|
||||
* @throws Dibi\Exception
|
||||
*/
|
||||
public function connect(array &$config): void
|
||||
{
|
||||
if (isset($config['dbcharset']) || isset($config['charset'])) {
|
||||
throw new Dibi\NotSupportedException('Options dbcharset and charset are not longer supported.');
|
||||
}
|
||||
|
@@ -29,7 +29,7 @@ class SqlsrvDriver implements Dibi\Driver
|
||||
{
|
||||
use Dibi\Strict;
|
||||
|
||||
/** @var resource|null */
|
||||
/** @var resource */
|
||||
private $connection;
|
||||
|
||||
/** @var int|null Affected rows */
|
||||
@@ -42,20 +42,12 @@ class SqlsrvDriver implements Dibi\Driver
|
||||
/**
|
||||
* @throws Dibi\NotSupportedException
|
||||
*/
|
||||
public function __construct()
|
||||
public function __construct(array &$config)
|
||||
{
|
||||
if (!extension_loaded('sqlsrv')) {
|
||||
throw new Dibi\NotSupportedException("PHP extension 'sqlsrv' is not loaded.");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Connects to a database.
|
||||
* @throws Dibi\Exception
|
||||
*/
|
||||
public function connect(array &$config): void
|
||||
{
|
||||
Helpers::alias($config, 'options|UID', 'username');
|
||||
Helpers::alias($config, 'options|PWD', 'password');
|
||||
Helpers::alias($config, 'options|Database', 'database');
|
||||
|
@@ -25,12 +25,6 @@ interface IDataSource extends \Countable, \IteratorAggregate
|
||||
*/
|
||||
interface Driver
|
||||
{
|
||||
/**
|
||||
* Connects to a database.
|
||||
* @throws Exception
|
||||
*/
|
||||
function connect(array &$config): void;
|
||||
|
||||
/**
|
||||
* Disconnects from a database.
|
||||
* @throws Exception
|
||||
|
Reference in New Issue
Block a user