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

198 lines
4.6 KiB
PHP
Executable File

<?php
/**************************
* DatabaseDriver *
* 08-04-2016 *
***************************
* Designed & Developed by *
* xdrm-brackets *
***************************
* https://xdrm.io/ *
**************************/
namespace database\core;
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{
$this->pdo = new \PDO('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
]);
// 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
*
* @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 */
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 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
];
}
}
?>