Barebone setup@5 > add: database.{core.Repo,core.Repo_i,repo.user} (Repo dispatcher + user.getAll,getById,getByMail) | dup:config.database-driver done | upd: view.homepage loads a repo (call example)

This commit is contained in:
xdrm-brackets 2017-11-24 16:00:12 +01:00
parent bd1248b8da
commit 6cd9baf19c
7 changed files with 174 additions and 4 deletions

View File

@ -176,6 +176,11 @@
} }
public function pdo(){
return $this->pdo;
}
public function getConfig(){ public function getConfig(){
return [ return [
'host' => $this->host, 'host' => $this->host,

View File

@ -0,0 +1,79 @@
<?php
/**************************
* Repo *
* 24-11-2017 *
***************************
* Designed & Developed by *
* xdrm-brackets *
***************************
* https://xdrm.io/ *
**************************/
namespace database\core;
use \error\core\Error;
use \error\core\Err;
class Repo{
/* (1) Driver
---------------------------------------------------------*/
private static $driver = null;
public static function setDriver(DatabaseDriver $driver){ self::$driver = $driver; }
public static function request(Array $path=null){
/* (1) Check arguments
---------------------------------------------------------*/
/* (1) Check @path */
if( !is_array($path) || count($path) != 2 )
throw new \Exception("@path is not an array with 2 elements");
/* (2) Deep check @path */
if( !is_string($path[0]) || !is_string($path[1]) )
throw new \Exception("@path elements must be 2 strings");
/* (3) Check class path */
$class_path = "\\database\\repo\\".$path[0];
if( !\class_exists($class_path) )
throw new \Exception("Repo class '$class_path' cannot be found");
/* (2) Call the method
---------------------------------------------------------*/
/* (1) Create the instance (pre-script) */
$instance = new $class_path();
/* (2) Check extends Repo_i */
if( !( $instance instanceof Repo_i) )
throw new \Exception("Repo class '$class_path' must extends Repo_i");
/* (3) Bind pdo instance */
\call_user_func([$instance, 'setPDO'], self::$driver->pdo());
/* (3) Check if the method exists */
if( !\method_exists($instance, $path[1]) )
throw new \Exception("Repo '${path[0]}' has no public method '{$path[1]}'");
/* (4) Fetch response (send arguments as well) */
$response = call_user_func_array([$instance, $path[1]], array_slice(func_get_args(), 1));
/* (5) Call post-script */
$instance = null;
/* (6) Dispatch response */
return $response;
}
}

View File

@ -0,0 +1,15 @@
<?php
namespace database\core;
class Repo_i{
protected $pdo = null;
public function setPDO($pdo){
$this->pdo = $pdo;
}
}

View File

@ -0,0 +1,51 @@
<?php
namespace database\repo;
use \database\core\Repo_i;
class user extends Repo_i{
public function getAll(){
/* (1) Statement */
$st = $this->pdo->query("SELECT * FROM user ORDER BY username ASC");
/* (2) Fetched data */
return $st->fetchAll();
}
public function getById(int $id_user){
/* (1) Prepare Statement */
$pst = $this->pdo->prepare("SELECT * FROM user WHERE id_user = :id_user");
/* (2) Bind variables */
$pst->bindParam(':id_user', $id_user, \PDO::PARAM_INT);
/* (3) Execute */
if( !$pst->execute() ) return false; // if error -> send FALSE
/* (4) Fetched data */
return $pst->fetchAll();
}
public function getByMail(String $mail){
/* (1) Prepare Statement */
$pst = $this->pdo->prepare("SELECT * FROM user WHERE mail = :mail");
/* (2) Bind variables */
$pst->bindParam(':mail', $mail, \PDO::PARAM_STR, 50);
/* (3) Execute */
if( !$pst->execute() ) return false; // if error -> send FALSE
/* (4) Fetched data */
return $pst->fetchAll();
}
}

View File

@ -1,10 +1,10 @@
{ {
"default": { "default": {
"local": { "local": {
"host" : "db_local_host", "host" : "localhost",
"dbname" : "db_local_name", "dbname" : "ndli1718",
"user" : "db_local_user", "user" : "ndli1718-php",
"password" : "db_local_password" "password" : "4JB1dtbrIC8pT935"
}, },
"remote": { "remote": {
"host" : "db_remote_host", "host" : "db_remote_host",

View File

@ -1,8 +1,19 @@
<?php require_once '../autoloader.php'; <?php require_once '../autoloader.php';
use \router\core\Router; use \router\core\Router;
use \database\core\Repo;
use \database\core\DatabaseDriver;
debug();
/* (1) Start session */
session_start(); session_start();
$_SESSION['PERM'] = ['journalist']; $_SESSION['PERM'] = ['journalist'];
/* (2) Set default Driver for Repos */
Repo::setDriver(DatabaseDriver::get());
/* (3) launch router */
echo "<pre>";
$MainRouter = Router::launch($_GET['url']); $MainRouter = Router::launch($_GET['url']);

View File

@ -16,6 +16,15 @@
</head> </head>
<body> <body>
<pre><?php
use \database\core\Repo;
print_r( Repo::request(['user', 'getByMail'], "lucas.mascaro@yahoo.fr") );
?></pre>
<!-- Main loop --> <!-- Main loop -->
<script type='text/javascript' src='/js/action-script.js'></script> <script type='text/javascript' src='/js/action-script.js'></script>
</body> </body>