1
0
mirror of https://github.com/dg/dibi.git synced 2025-08-03 20:57:36 +02:00

Connection::__construct() parameter config should be array (BC break)

This commit is contained in:
David Grudl
2018-04-17 15:01:30 +02:00
parent 0129d340d3
commit fbdd22de35
3 changed files with 6 additions and 15 deletions

View File

@@ -40,18 +40,7 @@ try {
echo "</p>\n"; echo "</p>\n";
// connects to MySQL using DSN // connects to MySQLi
echo '<p>Connecting to MySQL: ';
try {
dibi::connect('driver=mysql&host=localhost&username=root&password=xxx&database=test&charset=cp1250');
echo 'OK';
} catch (Dibi\Exception $e) {
echo get_class($e), ': ', $e->getMessage(), "\n";
}
echo "</p>\n";
// connects to MySQLi using array
echo '<p>Connecting to MySQLi: '; echo '<p>Connecting to MySQLi: ';
try { try {
dibi::connect([ dibi::connect([

View File

@@ -56,9 +56,11 @@ class Connection implements IConnection
public function __construct($config, string $name = null) public function __construct($config, string $name = null)
{ {
if (is_string($config)) { if (is_string($config)) {
trigger_error(__METHOD__ . '() Configuration should be array.', E_USER_DEPRECATED);
parse_str($config, $config); parse_str($config, $config);
} elseif ($config instanceof Traversable) { } elseif ($config instanceof Traversable) {
trigger_error(__METHOD__ . '() Configuration should be array.', E_USER_DEPRECATED);
$tmp = []; $tmp = [];
foreach ($config as $key => $val) { foreach ($config as $key => $val) {
$tmp[$key] = $val instanceof Traversable ? iterator_to_array($val) : $val; $tmp[$key] = $val instanceof Traversable ? iterator_to_array($val) : $val;
@@ -66,7 +68,7 @@ class Connection implements IConnection
$config = $tmp; $config = $tmp;
} elseif (!is_array($config)) { } elseif (!is_array($config)) {
throw new \InvalidArgumentException('Configuration must be array, string or object.'); throw new \InvalidArgumentException('Configuration must be array.');
} }
Helpers::alias($config, 'username', 'user'); Helpers::alias($config, 'username', 'user');

View File

@@ -30,8 +30,8 @@ test(function () use ($config) { // lazy
}); });
test(function () use ($config) { // query string test(function () use ($config) {
$conn = new Connection(http_build_query($config, '', '&')); $conn = new Connection($config);
Assert::true($conn->isConnected()); Assert::true($conn->isConnected());
Assert::null($conn->getConfig('lazy')); Assert::null($conn->getConfig('lazy'));