dmBridge PHP API
DMConfigIni.php
00001 <?php
00002 #
00003 # dmBridge: a data access framework for CONTENTdm(R)
00004 #
00005 # Copyright © 2009, 2010, 2011 Board of Regents of the Nevada System of Higher
00006 # Education, on behalf of the University of Nevada, Las Vegas
00007 #
00008 
00015 class DMConfigIni {
00016 
00020    private static $instances = array();
00021 
00023    private $config_ini_data;
00025    private $config_ini_pathname;
00026 
00030    private static function getDefaultIniPathname() {
00031       return dirname(__FILE__) . "/../../config.ini";
00032    }
00033 
00038    public static function getInstance($ini_file_pathname = null) {
00039       if (!$ini_file_pathname) {
00040          $ini_file_pathname = self::getDefaultIniPathname();
00041       }
00042       if (!array_key_exists($ini_file_pathname, self::$instances)) {
00043          self::$instances[$ini_file_pathname] = new DMConfigIni(
00044                $ini_file_pathname);
00045       }
00046       return self::$instances[$ini_file_pathname];
00047    }
00048 
00053    public static function destroyInstance(DMConfigIni $instance) {
00054       foreach (self::$instances as $pathname => $inst) {
00055          if ($inst == $instance) {
00056             unset(self::$instances[$pathname]);
00057             break;
00058          }
00059       }
00060    }
00061 
00065    protected function __construct($ini_file_pathname) {
00066       $this->config_ini_pathname = $ini_file_pathname;
00067    }
00068 
00069    public function __clone() {}
00070    public function __wakeup() {}
00071 
00077    public function getBoolean($key) {
00078       if (!$this->config_ini_data) {
00079          $this->initConfigIniData();
00080       }
00081       if (array_key_exists($key, $this->config_ini_data)) {
00082          if (in_array($this->config_ini_data[$key], array("1", "true"))) {
00083             return true;
00084          } else if (in_array($this->config_ini_data[$key], array("0", "false"))) {
00085             return false;
00086          }
00087       }
00088       return null;
00089    }
00090 
00097    public function setBoolean($key, $value) {
00098       if (!$this->config_ini_data) {
00099          $this->initConfigIniData();
00100       }
00101       $this->config_ini_data[$key] = $value ? "true" : "false";
00102    }
00103 
00109    public function unsetKey($key) {
00110       if (!$this->config_ini_data) {
00111          $this->initConfigIniData();
00112       }
00113       if (array_key_exists($key, $this->config_ini_data)) {
00114          unset($this->config_ini_data[$key]);
00115       }
00116    }
00117 
00125    public function getString($key, $default_value = null) {
00126       if (!$this->config_ini_data) {
00127          $this->initConfigIniData();
00128       }
00129       return array_key_exists($key, $this->config_ini_data)
00130             ? $this->config_ini_data[$key] : $default_value;
00131    }
00132 
00139    public function setString($key, $value) {
00140       if (!$this->config_ini_data) {
00141          $this->initConfigIniData();
00142       }
00143       $this->config_ini_data[$key] = $value;
00144    }
00145 
00146    private function initConfigIniData() {
00147       if (!$this->config_ini_data) {
00148          $this->config_ini_data = parse_ini_file($this->config_ini_pathname);
00149       }
00150       return $this->config_ini_data;
00151    }
00152 
00153 }
 All Data Structures Functions Variables