1
0
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:
David Grudl
2018-05-02 12:00:09 +02:00
parent d4f30ddf5b
commit 51fa3b9086
10 changed files with 32 additions and 108 deletions

View File

@@ -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;
}
}

View File

@@ -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'])) {

View File

@@ -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'];

View File

@@ -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 {

View File

@@ -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'])) {

View File

@@ -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');

View File

@@ -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'];

View File

@@ -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.');
}

View File

@@ -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');

View File

@@ -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