ajout mode de configuration mixte, tableau ou variable ENV
This commit is contained in:
@@ -16,7 +16,7 @@
|
||||
</include>
|
||||
</coverage>
|
||||
<php>
|
||||
<env name="DB_TYPE" value="sqlite"/>
|
||||
<env name="DB_DATABASE" value=":memory:"/>
|
||||
<env name="DB_DEFAULT_TYPE" value="sqlite"/>
|
||||
<env name="DB_DEFAULT_DATABASE" value=":memory:"/>
|
||||
</php>
|
||||
</phpunit>
|
||||
|
||||
45
src/Db.php
45
src/Db.php
@@ -20,14 +20,35 @@ class Db
|
||||
private ?string $host;
|
||||
private string $error;
|
||||
|
||||
private function __construct($name, $dir)
|
||||
private function __construct($name, $config)
|
||||
{
|
||||
$this->error = "";
|
||||
$this->name = $name;
|
||||
$this->loadEnv($dir);
|
||||
if (is_array($config)) {
|
||||
$this->parseArray($config);
|
||||
} else {
|
||||
if (is_dir($config)) {
|
||||
$this->loadEnv($config);
|
||||
}
|
||||
}
|
||||
$this->initPdo();
|
||||
}
|
||||
|
||||
private function parseArray($config)
|
||||
{
|
||||
try {
|
||||
$key = (strtolower(trim($this->name)));
|
||||
$this->type = isset($config[$key]["TYPE"]) ? $config[$key]["TYPE"] : null;
|
||||
$this->user = isset($config[$key]["USER"]) ? $config[$key]["USER"] : null;
|
||||
$this->host = isset($config[$key]["HOST"]) ? $config[$config[$key]["HOST"]] : null;
|
||||
$this->password = isset($config[$key]["PASSWORD"]) ? $config[$key]["PASSWORD"] : null;
|
||||
$this->database = isset($config[$key]["DATABASE"]) ? $config[$key]["DATABASE"] : null;
|
||||
$this->port = isset($config[$key]["PORT"]) ? $config[$key]["PORT"] : null;
|
||||
} catch (\Throwable $th) {
|
||||
$this->error = $th->getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
private function loadEnv($dir)
|
||||
{
|
||||
try {
|
||||
@@ -139,29 +160,33 @@ class Db
|
||||
}
|
||||
}
|
||||
|
||||
public static function init($dir, array $connections = ["default"])
|
||||
public static function init($config, array $connections = ["default"])
|
||||
{
|
||||
self::fillInstances($dir, $connections);
|
||||
self::fillInstances($config, $connections);
|
||||
return self::getInstance();
|
||||
}
|
||||
|
||||
private static function fillInstances($dir, array $connections)
|
||||
private static function fillInstances($config, array $connections)
|
||||
{
|
||||
foreach ($connections as $connection) {
|
||||
if (!isset(self::$instances[$connection])) {
|
||||
self::$instances[$connection] = new Db($connection, $dir);
|
||||
self::$instances[$connection] = new Db($connection, $config);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static function getInstance($connection = "default")
|
||||
{
|
||||
public static function getInstance($connection="")
|
||||
{
|
||||
if($connection=="" && count(self::$instances)>0)
|
||||
{
|
||||
$connection = array_keys(self::$instances)[0];
|
||||
}
|
||||
if (isset(self::$instances[$connection])) {
|
||||
$instance = self::$instances[$connection];
|
||||
if ($instance instanceof Db) {
|
||||
return self::$instances[$connection];
|
||||
}
|
||||
}
|
||||
}
|
||||
throw new \Exception("Unknown connection");
|
||||
}
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@ class Model
|
||||
protected mixed $values;
|
||||
protected array $updated;
|
||||
protected string $table;
|
||||
protected string $connection = "default";
|
||||
protected string $connection = "";
|
||||
protected string $pk;
|
||||
private ?Db $db;
|
||||
private bool $new;
|
||||
|
||||
@@ -17,7 +17,7 @@ class QueryBuilder
|
||||
protected string $raw_query;
|
||||
protected bool $distinct;
|
||||
|
||||
public function __construct($table, $pk = "id", $connection = "default")
|
||||
public function __construct($table, $pk = "id", $connection = "")
|
||||
{
|
||||
$this->table = $table;
|
||||
$this->pk = $pk;
|
||||
|
||||
@@ -7,8 +7,8 @@ use Kletellier\PdoWrapper\Model;
|
||||
use Kletellier\PdoWrapper\QueryBuilder;
|
||||
|
||||
test("create db",function () {
|
||||
$dir = getcwd();
|
||||
$db = Db::init($dir,["default","contwo"]);
|
||||
$config = ["conone" => ["TYPE"=>"sqlite", "DATABASE" => ":memory:" ], "contwo"=>["TYPE"=>"sqlite", "DATABASE" => ":memory:" ]];
|
||||
$db = Db::init($config,["conone","contwo"]);
|
||||
$ret = $db->executeRawQuery("CREATE TABLE test (id INTEGER PRIMARY KEY AUTOINCREMENT, col1 VARCHAR, date1 DATETIME);");
|
||||
expect($ret)->toBe(true);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user