Database Server's host * @dbname Database name * @username Database username * @password 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 [optional] Database Label * * @return status 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 [optional] Driver's label * * @return driver 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 ]; } } ?>