Advertisement
stixlink

Untitled

Oct 27th, 2016
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.70 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * Created by stixlink.
  5.  * E-mail: stixlink@gmail.com
  6.  * Skype: stixlink
  7.  * Date: 30.12.14
  8.  * Time: 14:48
  9.  */
  10. namespace core\db;
  11.  
  12. use core\exception\DBException;
  13.  
  14. /**
  15.  * Class SDB
  16.  *
  17.  * @property \PDO   $pdo
  18.  * @property array $serverConfig
  19.  */
  20. class SDB
  21. {
  22.  
  23.     private $pdo;
  24.     private $_active = false;
  25.     private $serverConfig;
  26.     private $username = '';
  27.     private $password = '';
  28.     private $dsn = '';
  29.     private $host = 'localhost';
  30.     private $charset = 'utf8';
  31.     private $db = '';
  32.     private $opt;
  33.  
  34.     public function __construct()
  35.     {
  36.         $dbConfigPath = BASE_PATH . DS . APP_DIR . DS . "config" . DS . "db.php";
  37.         if (!file_exists($dbConfigPath)) {
  38.             throw new DBException("Not exist database config file \"$dbConfigPath\"");
  39.         }
  40.         $this->serverConfig = include(BASE_PATH . DS . APP_DIR . DS . "config" . DS . "db.php");
  41.         if (is_array($this->serverConfig)) {
  42.             $reflection = new \ReflectionClass($this);
  43.             $properties = $reflection->getDefaultProperties();
  44.             foreach ($this->serverConfig as $key => $value) {
  45.                 if (isset($properties[$key]) && $properties[$key] !== null) {
  46.                     $this->$key = $value;
  47.                 }
  48.             }
  49.         }
  50.         if (!$this->dsn) {
  51.             $this->dsn = "mysql:host={$this->host};dbname={$this->db};charset={$this->charset}";
  52.         }
  53.  
  54.         $this->opt = array(
  55.             \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
  56.             \PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC
  57.         );
  58.     }
  59.  
  60.     private function initConnection()
  61.     {
  62.         if ($this->charset !== null) {
  63.             $driver = strtolower($this->pdo->getAttribute(\PDO::ATTR_DRIVER_NAME));
  64.             if (in_array($driver, array('pgsql', 'mysql', 'mysqli'))) {
  65.                 $this->pdo->exec('SET NAMES ' . $this->pdo->quote($this->charset));
  66.             }
  67.         }
  68.     }
  69.  
  70.     public function open()
  71.     {
  72.         try {
  73.             $this->pdo = new \PDO($this->dsn, $this->username, $this->password, $this->opt);
  74.             $this->initConnection();
  75.             $this->_active = true;
  76.         } catch (\PDOException $e) {
  77.             throw new DBException($e->getMessage());
  78.         }
  79.     }
  80.  
  81.     public function getInstance()
  82.     {
  83.         if ($this->pdo) {
  84.             return $this->pdo;
  85.         } else {
  86.             $this->open();
  87.  
  88.             return $this->pdo;
  89.         }
  90.     }
  91.  
  92.     public function __sleep()
  93.     {
  94.         $this->close();
  95.  
  96.         return array_keys(get_object_vars($this));
  97.     }
  98.  
  99.     protected function close()
  100.     {
  101.         $this->pdo = null;
  102.         $this->_active = false;
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement