initial setup of framework code
[cacert-mgr.git] / manager / library / config / Config.php
1 <?php
2
3 require_once('config/Config_Db.php');
4
5 class Config {
6 /**
7 * static pointer to instances
8 * @var array(Config)
9 */
10 private static $instances = array();
11
12 /**
13 * can handle several instances, distinct by instance name string
14 * @var string
15 */
16 private $instanceName = '';
17
18 /**
19 * config object
20 * @var Config_Db
21 */
22 private $config = null;
23
24 /**
25 * make a new Config_Db
26 *
27 * by using the $where statement you can limit the data that is fetched from db, i.e. only get config for zone $id
28 *
29 * @param string $instanceName
30 * @param Zend_Db_Adapter $db
31 * @param string $where
32 */
33 protected function __construct($instanceName = null, $db = null, $where = null) {
34 if ($instanceName === null)
35 throw new Exception(__METHOD__ . ': expected an instance name, got none');
36
37 $config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/application.ini', APPLICATION_ENV);
38 $this->instanceName = $instanceName;
39
40 if ($db === null)
41 $db = Zend_Db::factory($config->dnssecme->db->config->pdo, $config->dnssecme->db->config);
42
43 $this->config = new Config_Db($db, $instanceName, $where, true);
44 }
45
46 /**
47 * get already existing instance, make new instance or throw an exception
48 * @param string $instanceName
49 * @param Zend_Db_Adapter $db
50 * @param string $where
51 */
52 public static function getInstance($instanceName, $db = null, $where = null) {
53 if ($instanceName === null)
54 throw new Exception(__METHOD__ . ': expected an instance name, got none');
55
56 // no caching if presumeably volatile data is requested
57 if ($db !== null && $where !== null) {
58 return new Config($instanceName, $db, $where);
59 }
60
61 if (!array_key_exists($instanceName, self::$instances)) {
62 self::$instances[$instanceName] = new Config($instanceName, $db, $where);
63 }
64
65 return self::$instances[$instanceName];
66 }
67
68 /**
69 * magic method that dispatches all unrecognized method calls to the config object
70 *
71 * @param string $param
72 */
73 public function __get($param) {
74 return $this->config->$param;
75 }
76
77 /**
78 * magic method that handles isset inquiries to attributes
79 *
80 * @param string $param
81 */
82 public function __isset($param) {
83 return isset($this->config->$param);
84 }
85
86 /**
87 * magic method that dispatches all unrecognized method calls to the config object
88 *
89 * @param string $param
90 * @param string $value
91 */
92 public function __set($param, $value) {
93 $this->config->$param = $value;
94 }
95
96 /**
97 * get the config object
98 * @return Zend_Config_*
99 */
100 public function getConfig() {
101 return $this->config;
102 }
103 }
104 ?>