Skip to content

Commit

Permalink
Merge pull request #204 from kivudesign/refactor_db_config
Browse files Browse the repository at this point in the history
[ENH] Setup db configuration to setup on application module
  • Loading branch information
bim-g authored Sep 22, 2024
2 parents 82e31b1 + 464f43e commit eb7af87
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 28 deletions.
14 changes: 7 additions & 7 deletions src/Core/AppConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand All @@ -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
*/
Expand Down
16 changes: 15 additions & 1 deletion src/Core/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

namespace Wepesi\Core;

use Wepesi\Core\Orm\DBConfig;
use Wepesi\Core\Routing\Router;

/**
Expand Down Expand Up @@ -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();
}
Expand Down
31 changes: 11 additions & 20 deletions src/Core/Orm/DBConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit eb7af87

Please sign in to comment.