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

245 lines
5.6 KiB
PHP
Executable File

<?php
/**************************
* DatabaseDriver *
* 08-04-2016 *
***************************
* Designed & Developed by *
* xdrm-brackets *
***************************
* https://xdrm.io/ *
**************************/
namespace database\core;
use database\core\PDOWrapper\PDOWrapper;
use \error\core\Error;
use \error\core\Err;
class DatabaseDriver{
/* STATIC ATTRIBUTES */
private static function conf() : array{
// 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;
}
/** @var DatabaseDriver[] */
private static $instance = []; // Database driver instance list
/** @var Error */
public $error;
/* ATTRIBUTES */
private $host;
private $dbname;
private $username;
private $password;
private $pdo;
/** CONSTRUCTOR OF A DATABASE DRIVER
*
* @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
*
*/
private function __construct(String $host, String $dbname, String $username, String $password, bool $debug = false){
/* (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, [
\PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC,
\PDO::ATTR_TIMEOUT => 5,
\PDO::ERRMODE_EXCEPTION => true
]);
$this->pdo->setAttribute(\PDO::ATTR_STRINGIFY_FETCHES, false);
$this->pdo->setAttribute(\PDO::ATTR_EMULATE_PREPARES, false);
if($debug){
$this->pdo->enableDebug();
}
// On signale que tout s'est bien passe
$this->error = new Error(Err::Success);
}catch(\Exception $e){
// On signale qu'il y a une erreur
$this->error = new Error(Err::PDOConnection);
}
}
/************************************************
**** Multiton Management (static) ****
************************************************/
/** ADDS A NEW CONNECTION
*
* @param String $label [optional] Database Label
*
* @return boolean If added successfully
*
*/
private static function add(String $label='default') : bool{
$conf = self::conf();
/* [1] Default values
=========================================================*/
/* (1) 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') )
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']);
/* (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
*
* @param String $label [optional] Driver's label
* @throws \Exception
* @return DatabaseDriver Multiton
*
*/
public static function get(String $label='default') : DatabaseDriver{
$conf = self::conf();
/* [1] Checks arguments
=========================================================*/
/* (1) If no label, or unknown label */
if( !isset(self::$instance[$label]) ){
/* (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 String|null $label
* @throws \Exception
* @return \PDO
*/
public static function getPDO(?String $label=null) : \PDO{
if(is_string($label)){
$instance = self::get($label);
}else{
$instance = self::get();
}
return $instance->pdo;
}
/**
* @return \PDO
*/
public function pdo(){
return $this->pdo;
}
/**
* @return array
*/
public function getConfig(){
return [
'host' => $this->host,
'dbname' => $this->dbname,
'username' => $this->username
];
}
/**
* enable request stacking
*/
public function enableStacking(){
$this->pdo->enableStacking();
}
/**
* send all the stacked request and flush the stack
*/
public function flushStack(){
$this->pdo->executeStack();
}
/** get all debug data
* @return array
*/
public function getDebug() : array{
return $this->pdo->getDebug();
}
/**
* @return bool
*/
public function isDebugEnabled() : bool {
return $this->pdo->isDebugEnabled();
}
}
?>