From 51fa3b9086bc3fbe8c2f1620119cdfad3c1fd422 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Wed, 2 May 2018 12:00:09 +0200 Subject: [PATCH] Dibi\Driver::connect() replaced with constructor (BC break) --- src/Dibi/Connection.php | 40 ++++++++++++----------------- src/Dibi/Drivers/FirebirdDriver.php | 12 ++------- src/Dibi/Drivers/MySqliDriver.php | 12 ++------- src/Dibi/Drivers/OdbcDriver.php | 12 ++------- src/Dibi/Drivers/OracleDriver.php | 12 ++------- src/Dibi/Drivers/PdoDriver.php | 10 +------- src/Dibi/Drivers/PostgreDriver.php | 12 ++------- src/Dibi/Drivers/Sqlite3Driver.php | 12 ++------- src/Dibi/Drivers/SqlsrvDriver.php | 12 ++------- src/Dibi/interfaces.php | 6 ----- 10 files changed, 32 insertions(+), 108 deletions(-) diff --git a/src/Dibi/Connection.php b/src/Dibi/Connection.php index 41495735..9d9c1051 100644 --- a/src/Dibi/Connection.php +++ b/src/Dibi/Connection.php @@ -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; + } } diff --git a/src/Dibi/Drivers/FirebirdDriver.php b/src/Dibi/Drivers/FirebirdDriver.php index 08150caa..160fb9a5 100644 --- a/src/Dibi/Drivers/FirebirdDriver.php +++ b/src/Dibi/Drivers/FirebirdDriver.php @@ -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'])) { diff --git a/src/Dibi/Drivers/MySqliDriver.php b/src/Dibi/Drivers/MySqliDriver.php index 2d7cb54b..d7d083dd 100644 --- a/src/Dibi/Drivers/MySqliDriver.php +++ b/src/Dibi/Drivers/MySqliDriver.php @@ -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']; diff --git a/src/Dibi/Drivers/OdbcDriver.php b/src/Dibi/Drivers/OdbcDriver.php index 3bc5c969..73204573 100644 --- a/src/Dibi/Drivers/OdbcDriver.php +++ b/src/Dibi/Drivers/OdbcDriver.php @@ -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 { diff --git a/src/Dibi/Drivers/OracleDriver.php b/src/Dibi/Drivers/OracleDriver.php index 42de5bbd..5840697b 100644 --- a/src/Dibi/Drivers/OracleDriver.php +++ b/src/Dibi/Drivers/OracleDriver.php @@ -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'])) { diff --git a/src/Dibi/Drivers/PdoDriver.php b/src/Dibi/Drivers/PdoDriver.php index bc7a67d7..8eed1d82 100644 --- a/src/Dibi/Drivers/PdoDriver.php +++ b/src/Dibi/Drivers/PdoDriver.php @@ -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'); diff --git a/src/Dibi/Drivers/PostgreDriver.php b/src/Dibi/Drivers/PostgreDriver.php index 93e3734b..76de47ec 100644 --- a/src/Dibi/Drivers/PostgreDriver.php +++ b/src/Dibi/Drivers/PostgreDriver.php @@ -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']; diff --git a/src/Dibi/Drivers/Sqlite3Driver.php b/src/Dibi/Drivers/Sqlite3Driver.php index a37b2925..c9de95dd 100644 --- a/src/Dibi/Drivers/Sqlite3Driver.php +++ b/src/Dibi/Drivers/Sqlite3Driver.php @@ -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.'); } diff --git a/src/Dibi/Drivers/SqlsrvDriver.php b/src/Dibi/Drivers/SqlsrvDriver.php index d40ce603..47e45b9c 100644 --- a/src/Dibi/Drivers/SqlsrvDriver.php +++ b/src/Dibi/Drivers/SqlsrvDriver.php @@ -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'); diff --git a/src/Dibi/interfaces.php b/src/Dibi/interfaces.php index 94f0ff39..f04e710d 100644 --- a/src/Dibi/interfaces.php +++ b/src/Dibi/interfaces.php @@ -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