Skip to content

Commit

Permalink
Merge pull request #162 from kivudesign/version_boss
Browse files Browse the repository at this point in the history
[ENH] improve entity access to manage table inclusion.
  • Loading branch information
bim-g authored Jan 7, 2024
2 parents 19bac2d + dc0c545 commit 2bb274d
Show file tree
Hide file tree
Showing 12 changed files with 186 additions and 92 deletions.
9 changes: 7 additions & 2 deletions controller/exampleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,24 @@
namespace Wepesi\Controller;


use Wepesi\Core\Controller;
use Wepesi\Core\Http\Input;
use Wepesi\Core\Http\Redirect;
use Wepesi\Core\Session;

class exampleController
class exampleController extends Controller
{
public function __construct()
{
parent::__construct();
}

/**
* @return void
*/
function home()
{
Redirect::to("/");
$this->view->display('home');
}

/**
Expand Down
7 changes: 0 additions & 7 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,6 @@
->lang(getenv('LANG'))
->timezone(getenv('TIME_ZONE'));

(new \Wepesi\Core\Orm\DBConfig())
->host($_ENV['DB_HOST'])
->port($_ENV['DB_PORT'])
->db($_ENV['DB_NAME'])
->username($_ENV['DB_USER'])
->password($_ENV['DB_PASSWORD']);

$app = new Application($ROOT_DIR, $configuration);

require_once $app::$ROOT_DIR . '/router/route.php';
Expand Down
2 changes: 1 addition & 1 deletion router/route.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
$router->get('/', function () {
(new View)->display('/home');
});
$router->get('/home', '\Wepesi\Controller\exampleController#home');
$router->get('/home', [\Wepesi\Controller\exampleController::class,'home']);
//
$router->post('/changelang', [exampleController::class, 'changeLang'])
->middleware([exampleValidation::class, 'changeLang']);
Expand Down
13 changes: 13 additions & 0 deletions src/Core/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,20 @@

namespace Wepesi\Core;

/**
*
*/
abstract class Controller
{
/**
* @var View
*/
protected View $view;

/**
*
*/
public function __construct(){
$this->view = new View();
}
}
52 changes: 29 additions & 23 deletions src/Core/Orm/DB.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
<?php
/*
* Copyright (c) 2024. Wepesi Dev Framework
*/

namespace Wepesi\Core\Orm;

use This;
use Closure;
use Exception;
use PDO;
use PDOException;
use Wepesi\Core\Config;
use Wepesi\Core\Orm\Traits\QueryExecuter;

Expand Down Expand Up @@ -36,9 +42,9 @@ class DB extends DBConfig
*/
private int $lastID;
/**
* @var \PDO
* @var PDO
*/
private \PDO $pdoObject;
private PDO $pdoObject;
/**
* @var int
*/
Expand All @@ -56,25 +62,25 @@ private function __construct()
{
try {
if (!Config::get('mysql/usable')) {
throw new \Exception('you should authorized user database on config file.');
throw new Exception('you should authorized user database on config file.');
}
$this->initialisation();
$config = self::getConfig();
$config = $this->getDBConfig();
$this->db_name = $config->db;
$this->pdoObject = new \PDO('mysql:host=' . $config->host . ';port=' . $config->port . ';dbname=' . $this->db_name . ';charset=utf8mb4', $config->username, $config->password);
$this->pdoObject->setAttribute(\PDO::MYSQL_ATTR_INIT_COMMAND, 'SET NAMES utf8');
$this->pdoObject->setAttribute(\PDO::ATTR_EMULATE_PREPARES, false);
$this->pdoObject->setAttribute(\PDO::MYSQL_ATTR_FOUND_ROWS, true);
$this->pdoObject->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
} catch (\PDOException $ex) {
$this->pdoObject = new PDO('mysql:host=' . $config->host . ';port=' . $config->port . ';dbname=' . $this->db_name . ';charset=utf8mb4', $config->username, $config->password);
$this->pdoObject->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, 'SET NAMES utf8');
$this->pdoObject->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$this->pdoObject->setAttribute(PDO::MYSQL_ATTR_FOUND_ROWS, true);
$this->pdoObject->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $ex) {
die($ex->getMessage());
}
}

/**
* @string :$table =>this is the name of the table where to get information
* this method allow to do a select field from a $table with all the conditions defined ;
* @throws \Exception
* @throws Exception
*/
public function get(string $table_name): ?DBSelect
{
Expand All @@ -84,12 +90,12 @@ public function get(string $table_name): ?DBSelect
/**
* @string : $table=> this is the name of the table where to get information
* @string : @action=> this is the type of action tu do while want to do a request
* @throws \Exception
* @throws Exception
*/
private function select_option(string $table_name, string $action = null): DBSelect
{
if (strlen($table_name) < 1) {
throw new \Exception('table name should be a string');
throw new Exception('table name should be a string');
}
$this->queryResult = new DBSelect($this->pdoObject, $table_name, $action);
return $this->queryResult;
Expand Down Expand Up @@ -132,7 +138,7 @@ public function insert(string $table_name): DBInsert
/**
* @param string $table : this is the name of the table where to get information
* @return DBDelete
* @throws \Exception
* @throws Exception
* this method will help delete row data information
*/
public function delete(string $table): DBDelete
Expand All @@ -144,7 +150,7 @@ public function delete(string $table): DBDelete
/**
* @param string $table : this is the name of the table where to get information
* @return DBUpdate
* @throws \Exception
* @throws Exception
* this methode will help update row information of a selected tables
*/
public function update(string $table): DBUpdate
Expand Down Expand Up @@ -183,7 +189,7 @@ public function rowCount(): int
/**
* @string :$table =>this is the name of the table where to get information
* this method allow to do a count the number of field on a $table with all the possible condition
* @throws \Exception
* @throws Exception
*/
public function count(string $table_name): DBSelect
{
Expand All @@ -192,16 +198,16 @@ public function count(string $table_name): DBSelect

/**
*
* @throws \Exception
* @throws Exception
*/
public function transaction(\Closure $callable)
public function transaction(Closure $callable)
{
try {
$this->convertToInnoDB();
$this->pdoObject->beginTransaction();
$callable($this);
$this->pdoObject->commit();
} catch (\Exception $ex) {
} catch (Exception $ex) {
if ($this->pdoObject->inTransaction()) {
$this->pdoObject->rollBack();
}
Expand All @@ -210,7 +216,7 @@ public function transaction(\Closure $callable)
}

/**
* @throws \Exception
* @throws Exception
*/
public function convertToInnoDB()
{
Expand All @@ -228,7 +234,7 @@ public function convertToInnoDB()
/**
* @param string $engine : default "MyISAM"
* @return array
* @throws \Exception
* @throws Exception
*/

protected function get_db_engine_table(string $engine = 'MyISAM'): array
Expand All @@ -237,7 +243,7 @@ protected function get_db_engine_table(string $engine = 'MyISAM'): array
$params = [$this->db_name, $engine];
$sql = 'SELECT TABLE_NAME FROM information_schema.tables WHERE table_schema = ? AND `ENGINE` = ?';
return self::query($sql, $params)->result();
} catch (\Exception $ex) {
} catch (Exception $ex) {
throw $ex;
}
}
Expand Down
43 changes: 33 additions & 10 deletions src/Core/Orm/DBConfig.php
Original file line number Diff line number Diff line change
@@ -1,72 +1,95 @@
<?php
/*
* Copyright (c) 2024. Wepesi Dev Framework
*/

namespace Wepesi\Core\Orm;

/**
*
* Provide DataBase connection connection configurations.
* By default set up from `.env`.
*/
class DBConfig
{
/**
* @var array
*/
private static array $config = [];

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
*/
protected static function getConfig(): object
protected function getDBConfig(): object
{
return (object)self::$config;
return (object) (count($this->dbConfig)>0 ? $this->dbConfig : $this->defaultConfig());
}

/**
* Set database host name
* @param string $host_name
* @return $this
*/
public function host(string $host_name): DBConfig
{
self::$config['host'] = $host_name;
$this->dbConfig['host'] = $host_name;
return $this;
}

/**
* Set database connection user password
* @param string $password
* @return $this
*/
public function password(string $password): DBConfig
{
self::$config['password'] = $password;
$this->dbConfig['password'] = $password;
return $this;
}

/**
* Set database connection username
* @param string $username
* @return $this
*/
public function username(string $username): DBConfig
{
self::$config['username'] = $username;
$this->dbConfig['username'] = $username;
return $this;
}

/**
* set database connection default 3306
* @param string $port
* @return $this
*/
public function port(string $port): DBConfig
{
self::$config['port'] = $port;
$this->dbConfig['port'] = $port;
return $this;
}

/**
* Set database name to be selected
* @param string $db_name
* @return $this
*/
public function db(string $db_name): DBConfig
{
self::$config['db'] = $db_name;
$this->dbConfig['db'] = $db_name;
return $this;
}

Expand Down
14 changes: 12 additions & 2 deletions src/Core/Orm/DBSelect.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,16 @@ public function offset(int $offset): DBSelect
public function result(): array
{
$this->build();
return (!isset($this->result['exception']) && count($this->include_object) > 0 && count($this->result) > 0) ? $this->formatData($this->result) : $this->result;
$operation_result = $this->result;
if (!isset($this->result['exception']) ) {
if ($this->isCount) {
$this->isCount = false;
} else if (count($this->result) > 0) {
$operation_result = $this->formatData($operation_result);
}
}
$this->include_object = [];
return $operation_result;
}

/**
Expand All @@ -227,6 +236,7 @@ private function build()
*/
private function count_all(): void
{
$this->isCount = true;
$WHERE = $this->where['field'] ?? '';
$params = $this->where['value'] ?? [];
$sql = "SELECT COUNT(*) as count FROM {$this->table} " . $WHERE;
Expand Down Expand Up @@ -293,7 +303,7 @@ protected function formatData(array $result): array
break;
}
}
return $this->buildStructure($result, $parent_entity[0], $children_entity, $other_entity);
return $this->buildStructure($result, $parent_entity[0] ?? $this->table , $children_entity, $other_entity);

} catch (Exception $ex) {
return ['exception' => $ex->getMessage()];
Expand Down
Loading

0 comments on commit 2bb274d

Please sign in to comment.