1
0
mirror of https://github.com/dg/dibi.git synced 2025-08-05 05:37:39 +02:00

DibiConnection: option 'driver' can contain driver instance or class name [Closes #153]

This commit is contained in:
Pavel Zelezny
2015-10-26 16:11:42 +01:00
committed by David Grudl
parent e339eff00f
commit 9100f94b8f

View File

@@ -75,19 +75,26 @@ class DibiConnection extends DibiObject
$config['driver'] = dibi::$defaultDriver;
}
$class = preg_replace(array('#\W#', '#sql#'), array('_', 'Sql'), ucfirst(strtolower($config['driver'])));
$class = "Dibi{$class}Driver";
if (!class_exists($class)) {
include_once dirname(__FILE__) . "/../drivers/$class.php";
if ($config['driver'] instanceof IDibiDriver) {
$this->driver = $config['driver'];
$config['driver'] = get_class($this->driver);
} elseif (PHP_VERSION_ID >= 50307 && is_subclass_of($config['driver'], 'IDibiDriver')) {
$this->driver = new $config['driver'];
} else {
$class = preg_replace(array('#\W#', '#sql#'), array('_', 'Sql'), ucfirst(strtolower($config['driver'])));
$class = "Dibi{$class}Driver";
if (!class_exists($class)) {
include_once dirname(__FILE__) . "/../drivers/$class.php";
if (!class_exists($class, FALSE)) {
throw new DibiException("Unable to create instance of dibi driver '$class'.");
if (!class_exists($class, FALSE)) {
throw new DibiException("Unable to create instance of dibi driver '$class'.");
}
}
$this->driver = new $class;
}
$config['name'] = $name;
$this->config = $config;
$this->driver = new $class;
$this->translator = new DibiTranslator($this);
// profiler