From 464f43e4a431aef8de136285ec7a27a3288a583c Mon Sep 17 00:00:00 2001 From: bim-g Date: Sun, 22 Sep 2024 16:39:22 +0200 Subject: [PATCH] [ENH] Setup db configuration to setup on application module --- src/Core/AppConfiguration.php | 14 +++++++------- src/Core/Application.php | 16 +++++++++++++++- src/Core/Orm/DBConfig.php | 31 +++++++++++-------------------- 3 files changed, 33 insertions(+), 28 deletions(-) diff --git a/src/Core/AppConfiguration.php b/src/Core/AppConfiguration.php index e774092..f3af68b 100644 --- a/src/Core/AppConfiguration.php +++ b/src/Core/AppConfiguration.php @@ -23,8 +23,7 @@ public function __construct() */ public function lang(string $lang = 'fr'): AppConfiguration { - $this->params['lang'] = $lang; - return $this; + return $this->setParams('lang', $lang); } /** @@ -33,20 +32,21 @@ public function lang(string $lang = 'fr'): AppConfiguration */ public function timezone(string $timezone = 'Africa/Kigali'): AppConfiguration { - $this->params['timezone'] = $timezone; - return $this; + return $this->setParams('timezone', $timezone); } /** * @param string $path * @return $this */ - public function setNotFound(string $path = '404.php'): AppConfiguration + public function setNotFound(string $path = '/views/404.php'): AppConfiguration { - $this->params['not_found'] = $path; + return $this->setParams('not_found', $path); + } + private function setParams(string $key, $value): AppConfiguration { + $this->params[$key] = $value; return $this; } - /** * @return array */ diff --git a/src/Core/Application.php b/src/Core/Application.php index 4dedffd..fc6f1e4 100644 --- a/src/Core/Application.php +++ b/src/Core/Application.php @@ -5,6 +5,7 @@ namespace Wepesi\Core; +use Wepesi\Core\Orm\DBConfig; use Wepesi\Core\Routing\Router; /** @@ -208,13 +209,26 @@ public function basePath(string $path): string { return self::$root_dir . '/' . trim($path,'/'); } - + /** + * Initialise the database configuration + * @return void + */ + private function initDB(): void + { + (new DBConfig()) + ->host($_ENV['DB_HOST']) + ->port($_ENV['DB_PORT']) + ->db($_ENV['DB_NAME']) + ->username($_ENV['DB_USER']) + ->password($_ENV['DB_PASSWORD']); + } /** * @return void * @throws \Exception */ public function run(): void { + $this->initDB(); $this->routeProvider(); $this->router->run(); } diff --git a/src/Core/Orm/DBConfig.php b/src/Core/Orm/DBConfig.php index ad1cd39..4462b08 100644 --- a/src/Core/Orm/DBConfig.php +++ b/src/Core/Orm/DBConfig.php @@ -15,32 +15,23 @@ class DBConfig * @var array */ protected array $dbConfig = []; - /* - * Get Default configuration - * @return array - */ - private function defaultConfig (): array - { - return [ - 'host' => $_ENV['DB_HOST'], - 'port' => $_ENV['DB_PORT'], - 'db' => $_ENV['DB_NAME'], - 'password' => $_ENV['DB_USER'], - 'username' => $_ENV['DB_PASSWORD'] - ]; - } + /** * Get database connection information's * @return object + * @throws \Exception */ protected function getDBConfig(): object { - return (object) (count($this->dbConfig)>0 ? $this->dbConfig : $this->defaultConfig()); + if (count($this->dbConfig) > 0) { + return (object)$this->dbConfig ; + } + throw new \Exception('database connection information is not defined'); } /** * Set database host name - * @param string $host_name + * @param string $host_name database host name default 127.0.0.1 * @return $this */ public function host(string $host_name): DBConfig @@ -51,7 +42,7 @@ public function host(string $host_name): DBConfig /** * Set database connection user password - * @param string $password + * @param string $password database password * @return $this */ public function password(string $password): DBConfig @@ -62,7 +53,7 @@ public function password(string $password): DBConfig /** * Set database connection username - * @param string $username + * @param string $username database username * @return $this */ public function username(string $username): DBConfig @@ -73,7 +64,7 @@ public function username(string $username): DBConfig /** * set database connection default 3306 - * @param string $port + * @param string $port database port default 3306 * @return $this */ public function port(string $port): DBConfig @@ -84,7 +75,7 @@ public function port(string $port): DBConfig /** * Set database name to be selected - * @param string $db_name + * @param string $db_name database name * @return $this */ public function db(string $db_name): DBConfig