ptut-vhost/build/database/core/DatabaseDriver.php

245 lines
5.6 KiB
PHP
Raw Normal View History

2018-02-17 17:18:58 +00:00
<?php
/**************************
* DatabaseDriver *
* 08-04-2016 *
***************************
* Designed & Developed by *
* xdrm-brackets *
***************************
* https://xdrm.io/ *
**************************/
namespace database\core;
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 */
2018-03-11 15:14:12 +00:00
private static function conf() : array{
2018-02-17 17:18:58 +00:00
// 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;
}
2018-03-11 15:14:12 +00:00
/** @var DatabaseDriver[] */
2018-02-17 17:18:58 +00:00
private static $instance = []; // Database driver instance list
2018-03-11 15:14:12 +00:00
/** @var Error */
2018-02-17 17:18:58 +00:00
public $error;
/* ATTRIBUTES */
private $host;
private $dbname;
private $username;
private $password;
private $pdo;
2018-03-11 15:14:12 +00:00
/** CONSTRUCTOR OF A DATABASE DRIVER
2018-02-17 17:18:58 +00:00
*
2018-03-11 15:14:12 +00:00
* @param String $host Database Server's host
* @param String $dbname Database name
* @param String $username Database username
* @param String $password Database password
* @param bool $debug
2018-02-17 17:18:58 +00:00
*
*/
2018-03-11 15:14:12 +00:00
private function __construct(String $host, String $dbname, String $username, String $password, bool $debug = false){
2018-02-17 17:18:58 +00:00
/* (2) Stores configuration */
$this->host = $host;
$this->dbname = $dbname;
$this->username = $username;
$this->password = $password;
try{
$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,
\PDO::ATTR_TIMEOUT => 5,
\PDO::ERRMODE_EXCEPTION => true
2018-02-17 17:18:58 +00:00
]);
$this->pdo->setAttribute(\PDO::ATTR_STRINGIFY_FETCHES, false);
$this->pdo->setAttribute(\PDO::ATTR_EMULATE_PREPARES, false);
2018-03-08 19:53:40 +00:00
if($debug){
$this->pdo->enableDebug();
}
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) ****
************************************************/
2018-03-11 15:14:12 +00:00
/** ADDS A NEW CONNECTION
2018-02-17 17:18:58 +00:00
*
2018-03-11 15:14:12 +00:00
* @param String $label [optional] Database Label
2018-02-17 17:18:58 +00:00
*
2018-03-11 15:14:12 +00:00
* @return boolean If added successfully
2018-02-17 17:18:58 +00:00
*
*/
2018-03-11 15:14:12 +00:00
private static function add(String $label='default') : bool{
2018-02-17 17:18:58 +00:00
$conf = self::conf();
/* [1] Default values
=========================================================*/
2018-03-11 15:14:12 +00:00
/* (1) If label and no path */
2018-02-17 17:18:58 +00:00
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') )
2018-03-08 19:55:00 +00:00
if(!isset($conf[$label]['local']['debug'])){
$conf[$label]['local']['debug'] = false;
}
self::$instance[$label] = new DatabaseDriver($conf[$label]['local']['host'], $conf[$label]['local']['dbname'], $conf[$label]['local']['user'], $conf[$label]['local']['password'],$conf[$label]['local']['debug']);
2018-02-17 17:18:58 +00:00
/* (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;
}
}
2018-03-11 15:14:12 +00:00
/** GET A DATABASE DRIVER INSTANCE
2018-02-17 17:18:58 +00:00
*
2018-03-11 15:14:12 +00:00
* @param String $label [optional] Driver's label
* @throws \Exception
* @return DatabaseDriver Multiton
2018-02-17 17:18:58 +00:00
*
*/
2018-03-11 15:14:12 +00:00
public static function get(String $label='default') : DatabaseDriver{
2018-02-17 17:18:58 +00:00
$conf = self::conf();
/* [1] Checks arguments
=========================================================*/
2018-03-11 15:14:12 +00:00
/* (1) 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
2018-03-11 15:14:12 +00:00
* @param String|null $label
* @throws \Exception
2018-02-17 17:18:58 +00:00
* @return \PDO
*/
2018-03-11 15:14:12 +00:00
public static function getPDO(?String $label=null) : \PDO{
if(is_string($label)){
$instance = self::get($label);
}else{
$instance = self::get();
}
2018-02-17 17:18:58 +00:00
return $instance->pdo;
}
2018-03-11 15:14:12 +00:00
/**
* @return \PDO
*/
2018-02-17 17:18:58 +00:00
public function pdo(){
return $this->pdo;
}
2018-03-11 15:14:12 +00:00
/**
* @return array
*/
2018-02-17 17:18:58 +00:00
public function getConfig(){
return [
'host' => $this->host,
'dbname' => $this->dbname,
'username' => $this->username
];
}
2018-03-11 15:14:12 +00:00
/**
* enable request stacking
*/
public function enableStacking(){
$this->pdo->enableStacking();
}
2018-03-11 15:14:12 +00:00
/**
* send all the stacked request and flush the stack
*/
public function flushStack(){
$this->pdo->executeStack();
}
2018-03-11 15:14:12 +00:00
/** get all debug data
* @return array
*/
2018-03-08 19:53:40 +00:00
public function getDebug() : array{
return $this->pdo->getDebug();
}
2018-03-11 15:14:12 +00:00
/**
* @return bool
*/
2018-03-08 19:53:40 +00:00
public function isDebugEnabled() : bool {
return $this->pdo->isDebugEnabled();
}
2018-02-17 17:18:58 +00:00
}
?>