Advertisement
Guest User

PHP Config class

a guest
Sep 13th, 2013
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.12 KB | None | 0 0
  1. <?php
  2. class Config {
  3.  
  4.     private $data = Array();
  5.  
  6.     public $dbUser = 'username';
  7.     public $dbPass = 'password';
  8.     public $dsn = 'mysql:dbname=;host=hostname;port=3306;charset=UTF-8';
  9.     public $debug = TRUE;
  10.  
  11.     public function __construct() {
  12.         try {
  13.             $this->pdo = new PDO($this->dsn, $this->dbUser, $this->dbPass, array(PDO::ATTR_PERSISTENT => true));
  14.             $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  15.             $this->pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
  16.         }
  17.         catch (PDOException $e) {
  18.             if ($this->debug) echo $e->getMessage();
  19.         }
  20.         $this->dbCheck();
  21.         if ($this->debug) {
  22.             $this->enableDebug();
  23.         } else {
  24.             $this->disableDebug();
  25.         }
  26.         $this->testObj = new ConfigObject($this->pdo, "testnavn");
  27.     }
  28.  
  29.     function __get($name) {
  30.         if (($name != "pdo")) {
  31.             if (!isset($this->data[$name])) {
  32.                 $this->data[$name]=new ConfigObject($this->pdo, $name);
  33.             }
  34.         }
  35.         return $this->data[$name];
  36.     }
  37.  
  38.     function __set($name, $value) {
  39.         if ($name != "pdo") {
  40.             $value=new ConfigObject($this->pdo, $name);
  41.         }
  42.         $this->data[$name] = $value;
  43.     }
  44.  
  45.     function __isset($name) {
  46.         $query = $this->pdo->prepare("SELECT COUNT (*) FROM `configuration` WHERE `key`=:name");
  47.         $query->bindValue(":name", $name, PDO::PARAM_STR);
  48.         $query->execute();
  49.         $count=$query->fetchColumn();
  50.         $query->closeCursor();
  51.         return ((bool) $count);
  52.     }
  53.  
  54.     function __unset($name) {
  55.         unset($this->data[$name]);
  56.         $query = $this->pdo->prepare("DELETE FROM `configuration` WHERE `key`=:name");
  57.         $query->bindValue(":name", $name, PDO::PARAM_STR);
  58.         $query->execute();
  59.         $numrows=$query->rowCount();
  60.         return ((bool) $numrows);
  61.     }
  62.  
  63.     private function dbCheck() {
  64.         $query=$this->pdo->query("SHOW TABLES LIKE 'configuration'");
  65.         $query->execute();
  66.         if ($query->rowCount() == 0) {
  67.             $query->closeCursor();
  68.             $query=$this->pdo->query("CREATE TABLE IF NOT EXISTS `configuration` (".
  69.                                       "`key` text COLLATE utf8_swedish_ci NOT NULL,".
  70.                                       "`value` text COLLATE utf8_swedish_ci NOT NULL,".
  71.                                       "`group` text COLLATE utf8_swedish_ci,".
  72.                                       "`description` text COLLATE utf8_swedish_ci,".
  73.                                       "UNIQUE KEY `key` (`key`(16))".
  74.                                       ") ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_swedish_ci;");
  75.             $query->execute();
  76.         }
  77.         $query->closeCursor();
  78.     }
  79.  
  80.     public function enableDebug() {
  81.         $this->debug=TRUE;
  82.         ini_set('display_errors', 'On');
  83.         error_reporting(2039);
  84.     }
  85.  
  86.     public function disableDebug() {
  87.         $this->debug=FALSE;
  88.         ini_set('display_errors', 'Off');
  89.     }
  90. }
  91.  
  92. class ConfigObject {
  93.     private $data = Array();
  94.     private $pdo;
  95.     private $name;
  96.  
  97.     public function __construct($pdo, $name) {
  98.         $this->pdo=$pdo;
  99.         $this->name=$name;
  100.     }
  101.  
  102.     function __get($property) {
  103.         if ($property == "pdo") return (true);
  104.         $query = $this->pdo->prepare("SELECT `".$property."` FROM `configuration` WHERE `key`=:name");
  105.         $query->bindValue(":name", $this->name, PDO::PARAM_STR);
  106.         $query->execute();
  107.         $obj = $query->fetchObject();
  108.         $this->data[$property]=$obj->$property;
  109.         $query->closeCursor();
  110.         return $this->data[$property];
  111.     }
  112.  
  113.     function __set($property, $value) {
  114.         $this->data[$property] = $value;
  115.         if (($property == "pdo") || ($property == "name")) return (true);
  116.         $query = $this->pdo->prepare("SELECT COUNT(*) FROM `configuration` WHERE `key`=:name");
  117.         $query->bindValue(":name", $this->name, PDO::PARAM_STR);
  118.         $query->execute();
  119.         $count=$query->fetchColumn();
  120.         $query->closeCursor();
  121.         if ($count == 0) {
  122.             $query = $this->pdo->prepare("INSERT INTO `configuration` (`value`, `key`) VALUES (:value, :name)");
  123.             $query->bindValue(":value", $value, PDO::PARAM_STR);
  124.             $query->bindValue(":name", $this->name, PDO::PARAM_STR);
  125.             $query->execute();
  126.         } else {
  127.             $query = $this->pdo->prepare("UPDATE `configuration` SET `".$property."`=:value WHERE `key`=:name");
  128.             $query->bindValue(":value", $value, PDO::PARAM_STR);
  129.             $query->bindValue(":name", $this->name, PDO::PARAM_STR);
  130.             $query->execute();
  131.         }
  132.     }
  133.  
  134.     public function __tostring() {
  135.         $query = $this->pdo->prepare("SELECT `value` FROM `configuration` WHERE `key`=:name");
  136.         $query->bindValue(":name", $this->name, PDO::PARAM_STR);
  137.         $query->execute();
  138.         $obj = $query->fetchObject();
  139.         $this->data['value']=$obj->value;
  140.         $query->closeCursor();
  141.         return $this->data['value'];
  142.     }
  143. }
  144. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement