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:
parent
bd1248b8da
commit
6cd9baf19c
|
@ -176,6 +176,11 @@
|
|||
}
|
||||
|
||||
|
||||
public function pdo(){
|
||||
return $this->pdo;
|
||||
}
|
||||
|
||||
|
||||
public function getConfig(){
|
||||
return [
|
||||
'host' => $this->host,
|
||||
|
|
|
@ -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;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
<?php
|
||||
|
||||
namespace database\core;
|
||||
|
||||
class Repo_i{
|
||||
|
||||
protected $pdo = null;
|
||||
|
||||
public function setPDO($pdo){
|
||||
|
||||
$this->pdo = $pdo;
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -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();
|
||||
|
||||
}
|
||||
}
|
|
@ -1,10 +1,10 @@
|
|||
{
|
||||
"default": {
|
||||
"local": {
|
||||
"host" : "db_local_host",
|
||||
"dbname" : "db_local_name",
|
||||
"user" : "db_local_user",
|
||||
"password" : "db_local_password"
|
||||
"host" : "localhost",
|
||||
"dbname" : "ndli1718",
|
||||
"user" : "ndli1718-php",
|
||||
"password" : "4JB1dtbrIC8pT935"
|
||||
},
|
||||
"remote": {
|
||||
"host" : "db_remote_host",
|
||||
|
|
|
@ -1,8 +1,19 @@
|
|||
<?php require_once '../autoloader.php';
|
||||
|
||||
use \router\core\Router;
|
||||
use \database\core\Repo;
|
||||
use \database\core\DatabaseDriver;
|
||||
|
||||
debug();
|
||||
|
||||
/* (1) Start session */
|
||||
session_start();
|
||||
$_SESSION['PERM'] = ['journalist'];
|
||||
|
||||
/* (2) Set default Driver for Repos */
|
||||
Repo::setDriver(DatabaseDriver::get());
|
||||
|
||||
/* (3) launch router */
|
||||
echo "<pre>";
|
||||
$MainRouter = Router::launch($_GET['url']);
|
||||
|
||||
|
|
|
@ -16,6 +16,15 @@
|
|||
</head>
|
||||
<body>
|
||||
|
||||
|
||||
<pre><?php
|
||||
|
||||
use \database\core\Repo;
|
||||
|
||||
print_r( Repo::request(['user', 'getByMail'], "lucas.mascaro@yahoo.fr") );
|
||||
?></pre>
|
||||
|
||||
|
||||
<!-- Main loop -->
|
||||
<script type='text/javascript' src='/js/action-script.js'></script>
|
||||
</body>
|
||||
|
|
Loading…
Reference in New Issue