From d67916e2ff0da1f5aa70c90e8cf9c6652ff639b6 Mon Sep 17 00:00:00 2001 From: Kregs Date: Thu, 2 Nov 2023 23:04:35 +0100 Subject: [PATCH] ajout mode de configuration mixte, tableau ou variable ENV --- phpunit.xml | 4 ++-- src/Db.php | 45 ++++++++++++++++++++++++++++++++++---------- src/Model.php | 2 +- src/QueryBuilder.php | 2 +- tests/DbTest.php | 4 ++-- 5 files changed, 41 insertions(+), 16 deletions(-) diff --git a/phpunit.xml b/phpunit.xml index 8787c26..51ab231 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -16,7 +16,7 @@ - - + + diff --git a/src/Db.php b/src/Db.php index c6833b3..a599e98 100644 --- a/src/Db.php +++ b/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"); } diff --git a/src/Model.php b/src/Model.php index fd6874c..682cb01 100644 --- a/src/Model.php +++ b/src/Model.php @@ -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; diff --git a/src/QueryBuilder.php b/src/QueryBuilder.php index c521785..5ff6b49 100644 --- a/src/QueryBuilder.php +++ b/src/QueryBuilder.php @@ -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; diff --git a/tests/DbTest.php b/tests/DbTest.php index 727beaa..a53d001 100644 --- a/tests/DbTest.php +++ b/tests/DbTest.php @@ -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);