2018-02-17 17:18:58 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**************************
|
|
|
|
* DatabaseDriver *
|
|
|
|
* 08-04-2016 *
|
|
|
|
***************************
|
|
|
|
* Designed & Developed by *
|
|
|
|
* xdrm-brackets *
|
|
|
|
***************************
|
|
|
|
* https://xdrm.io/ *
|
|
|
|
**************************/
|
|
|
|
|
|
|
|
namespace database\core;
|
2018-03-08 19:09:02 +00:00
|
|
|
use database\core\PDOWrapper\PDOWrapper;
|
2018-02-17 17:18:58 +00:00
|
|
|
use \error\core\Error;
|
|
|
|
use \error\core\Err;
|
|
|
|
|
|
|
|
|
|
|
|
class DatabaseDriver{
|
|
|
|
|
|
|
|
/* STATIC ATTRIBUTES */
|
|
|
|
private static function conf(){
|
|
|
|
// YOUR CONFIGURATION BEHIND
|
|
|
|
$path = __CONFIG__.'/database-driver.json';
|
|
|
|
|
|
|
|
/* (1) Checks the file */
|
|
|
|
if( !is_file($path) )
|
|
|
|
return [];
|
|
|
|
|
|
|
|
/* (2) Checks json */
|
|
|
|
$parsed = json_decode( file_get_contents($path), true );
|
|
|
|
|
|
|
|
if( !is_array($parsed) )
|
|
|
|
return [];
|
|
|
|
|
|
|
|
/* (3) Returns configuration */
|
|
|
|
return $parsed;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static $path; // Databases configurations files
|
|
|
|
private static $config; // PDO configurations
|
|
|
|
private static $instance = []; // Database driver instance list
|
|
|
|
|
|
|
|
public $error;
|
|
|
|
|
|
|
|
/* ATTRIBUTES */
|
|
|
|
private $host;
|
|
|
|
private $dbname;
|
|
|
|
private $username;
|
|
|
|
private $password;
|
|
|
|
private $pdo;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* CONSTRUCTOR OF A DATABASE DRIVER
|
|
|
|
*
|
|
|
|
* @host<String> Database Server's host
|
|
|
|
* @dbname<String> Database name
|
|
|
|
* @username<String> Database username
|
|
|
|
* @password<String> Database password
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
private function __construct($host, $dbname, $username, $password){
|
|
|
|
/* (2) Stores configuration */
|
|
|
|
$this->host = $host;
|
|
|
|
$this->dbname = $dbname;
|
|
|
|
$this->username = $username;
|
|
|
|
$this->password = $password;
|
|
|
|
|
|
|
|
try{
|
|
|
|
|
2018-03-08 19:09:02 +00:00
|
|
|
$this->pdo = new PDOWrapper('mysql:host='.$this->host.';dbname='.$this->dbname.';charset=utf8', $this->username, $this->password, [
|
2018-02-17 17:18:58 +00:00
|
|
|
\PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC,
|
2018-03-08 19:09:02 +00:00
|
|
|
\PDO::ATTR_TIMEOUT => 5,
|
|
|
|
\PDO::ERRMODE_EXCEPTION => true
|
2018-02-17 17:18:58 +00:00
|
|
|
]);
|
|
|
|
|
2018-03-03 13:56:19 +00:00
|
|
|
$this->pdo->setAttribute(\PDO::ATTR_STRINGIFY_FETCHES, false);
|
|
|
|
$this->pdo->setAttribute(\PDO::ATTR_EMULATE_PREPARES, false);
|
|
|
|
|
2018-02-17 17:18:58 +00:00
|
|
|
// On signale que tout s'est bien passe
|
|
|
|
$this->error = new Error(Err::Success);
|
|
|
|
|
2018-02-27 13:48:07 +00:00
|
|
|
}catch(\Exception $e){
|
2018-02-17 17:18:58 +00:00
|
|
|
// On signale qu'il y a une erreur
|
|
|
|
$this->error = new Error(Err::PDOConnection);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/************************************************
|
|
|
|
**** Multiton Management (static) ****
|
|
|
|
************************************************/
|
|
|
|
|
|
|
|
/* ADDS A NEW CONNECTION
|
|
|
|
*
|
|
|
|
* @label<String> [optional] Database Label
|
|
|
|
*
|
|
|
|
* @return status<Boolean> If added successfully
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
private static function add($label=null){
|
|
|
|
$conf = self::conf();
|
|
|
|
|
|
|
|
/* [1] Default values
|
|
|
|
=========================================================*/
|
|
|
|
/* (1) If label isn't given */
|
|
|
|
is_null($label) && ($label = 'default');
|
|
|
|
|
|
|
|
/* (2) If label and no path */
|
|
|
|
if( $label !== 'default' && !isset($conf[$label]) )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
|
|
/* [3] Instanciates the driver
|
|
|
|
=========================================================*/
|
|
|
|
try{
|
|
|
|
|
|
|
|
/* (1) If local -> instanciates with local configuration */
|
|
|
|
// if( !checkdnsrr($_SERVER['SERVER_NAME'], 'NS') )
|
|
|
|
self::$instance[$label] = new DatabaseDriver($conf[$label]['local']['host'], $conf[$label]['local']['dbname'], $conf[$label]['local']['user'], $conf[$label]['local']['password']);
|
|
|
|
/* (2) If Remote -> instanciates with Remote configuration */
|
|
|
|
// else
|
|
|
|
// self::$instance[$label] = new DatabaseDriver($conf[$label]['remote']['host'], $conf[$label]['remote']['dbname'], $conf[$label]['remote']['user'], $conf[$label]['remote']['password']);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}catch(\Exception $e){
|
|
|
|
|
|
|
|
/* (3) If fails */
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* GET A DATABASE DRIVER INSTANCE
|
|
|
|
*
|
|
|
|
* @label<String> [optional] Driver's label
|
|
|
|
*
|
|
|
|
* @return driver<Database> Multiton
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static function get($label=null){
|
|
|
|
$conf = self::conf();
|
|
|
|
|
|
|
|
/* [1] Checks arguments
|
|
|
|
=========================================================*/
|
|
|
|
/* (1) Label default value */
|
|
|
|
is_null($label) && ($label = 'default');
|
|
|
|
|
|
|
|
/* (2) If no label, or unknown label */
|
2018-02-27 13:48:07 +00:00
|
|
|
if( !isset(self::$instance[$label]) ){
|
2018-02-17 17:18:58 +00:00
|
|
|
|
|
|
|
/* (2.1) Try to add the configuration if exists */
|
|
|
|
if( isset($conf[$label]) ){
|
|
|
|
self::add($label);
|
|
|
|
return self::get($label);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
throw new \Exception('Database @label is incorrect.');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* [2] Returns instance
|
|
|
|
=========================================================*/
|
|
|
|
return self::$instance[$label];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** retourne la connection statique
|
|
|
|
* @param null $label
|
|
|
|
* @return \PDO
|
|
|
|
*/
|
|
|
|
public static function getPDO($label=null){
|
|
|
|
$instance = self::get($label);
|
|
|
|
|
|
|
|
return $instance->pdo;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function pdo(){
|
|
|
|
return $this->pdo;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function getConfig(){
|
|
|
|
return [
|
|
|
|
'host' => $this->host,
|
|
|
|
'dbname' => $this->dbname,
|
|
|
|
'username' => $this->username
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2018-03-08 19:09:02 +00:00
|
|
|
public function enableStacking(){
|
|
|
|
$this->pdo->enableStacking();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function flushStack(){
|
|
|
|
$this->pdo->executeStack();
|
|
|
|
}
|
|
|
|
|
2018-02-17 17:18:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
?>
|