ajout mode de configuration mixte, tableau ou variable ENV

This commit is contained in:
2023-11-02 23:04:35 +01:00
parent 84bf765b84
commit d67916e2ff
5 changed files with 41 additions and 16 deletions

View File

@@ -16,7 +16,7 @@
</include> </include>
</coverage> </coverage>
<php> <php>
<env name="DB_TYPE" value="sqlite"/> <env name="DB_DEFAULT_TYPE" value="sqlite"/>
<env name="DB_DATABASE" value=":memory:"/> <env name="DB_DEFAULT_DATABASE" value=":memory:"/>
</php> </php>
</phpunit> </phpunit>

View File

@@ -20,14 +20,35 @@ class Db
private ?string $host; private ?string $host;
private string $error; private string $error;
private function __construct($name, $dir) private function __construct($name, $config)
{ {
$this->error = ""; $this->error = "";
$this->name = $name; $this->name = $name;
$this->loadEnv($dir); if (is_array($config)) {
$this->parseArray($config);
} else {
if (is_dir($config)) {
$this->loadEnv($config);
}
}
$this->initPdo(); $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) private function loadEnv($dir)
{ {
try { try {
@@ -139,23 +160,27 @@ 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(); return self::getInstance();
} }
private static function fillInstances($dir, array $connections) private static function fillInstances($config, array $connections)
{ {
foreach ($connections as $connection) { foreach ($connections as $connection) {
if (!isset(self::$instances[$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])) { if (isset(self::$instances[$connection])) {
$instance = self::$instances[$connection]; $instance = self::$instances[$connection];
if ($instance instanceof Db) { if ($instance instanceof Db) {

View File

@@ -7,7 +7,7 @@ class Model
protected mixed $values; protected mixed $values;
protected array $updated; protected array $updated;
protected string $table; protected string $table;
protected string $connection = "default"; protected string $connection = "";
protected string $pk; protected string $pk;
private ?Db $db; private ?Db $db;
private bool $new; private bool $new;

View File

@@ -17,7 +17,7 @@ class QueryBuilder
protected string $raw_query; protected string $raw_query;
protected bool $distinct; protected bool $distinct;
public function __construct($table, $pk = "id", $connection = "default") public function __construct($table, $pk = "id", $connection = "")
{ {
$this->table = $table; $this->table = $table;
$this->pk = $pk; $this->pk = $pk;

View File

@@ -7,8 +7,8 @@ use Kletellier\PdoWrapper\Model;
use Kletellier\PdoWrapper\QueryBuilder; use Kletellier\PdoWrapper\QueryBuilder;
test("create db",function () { test("create db",function () {
$dir = getcwd(); $config = ["conone" => ["TYPE"=>"sqlite", "DATABASE" => ":memory:" ], "contwo"=>["TYPE"=>"sqlite", "DATABASE" => ":memory:" ]];
$db = Db::init($dir,["default","contwo"]); $db = Db::init($config,["conone","contwo"]);
$ret = $db->executeRawQuery("CREATE TABLE test (id INTEGER PRIMARY KEY AUTOINCREMENT, col1 VARCHAR, date1 DATETIME);"); $ret = $db->executeRawQuery("CREATE TABLE test (id INTEGER PRIMARY KEY AUTOINCREMENT, col1 VARCHAR, date1 DATETIME);");
expect($ret)->toBe(true); expect($ret)->toBe(true);