| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Config files loader |
| 5 |
*/ |
| 6 |
|
| 7 |
class VP_Util_Config |
| 8 |
{ |
| 9 |
|
| 10 |
private static $_instance; |
| 11 |
|
| 12 |
private $_configs; |
| 13 |
|
| 14 |
private function __construct() |
| 15 |
{ |
| 16 |
$this->_configs = array(); |
| 17 |
} |
| 18 |
|
| 19 |
public static function instance() |
| 20 |
{ |
| 21 |
if (is_null(self::$_instance)) |
| 22 |
{ |
| 23 |
self::$_instance = new self(); |
| 24 |
} |
| 25 |
return self::$_instance; |
| 26 |
} |
| 27 |
|
| 28 |
|
| 29 |
public function load($config_name, $key = '') |
| 30 |
{ |
| 31 |
// get the config, try to get in memory cache |
| 32 |
if (array_key_exists($config_name, $this->_configs)) |
| 33 |
{ |
| 34 |
$config = $this->_configs[$config_name]; |
| 35 |
} |
| 36 |
else |
| 37 |
{ |
| 38 |
if(is_file(VP_CONFIG_DIR . '/'. $config_name . '.php')) |
| 39 |
{ |
| 40 |
$config = require VP_CONFIG_DIR . '/'. $config_name . '.php'; |
| 41 |
} |
| 42 |
else |
| 43 |
{ |
| 44 |
throw new Exception("$config_name file not found.\n", 1); |
| 45 |
} |
| 46 |
// cache 'em |
| 47 |
$this->_configs[$config_name] = $config; |
| 48 |
} |
| 49 |
|
| 50 |
// if key supplied, get the specific index of config array |
| 51 |
$temp = $config; |
| 52 |
if($key !== '') |
| 53 |
{ |
| 54 |
$keys = explode('.', $key); |
| 55 |
foreach ($keys as $key) |
| 56 |
{ |
| 57 |
$temp = $temp[$key]; |
| 58 |
} |
| 59 |
} |
| 60 |
return $temp; |
| 61 |
} |
| 62 |
|
| 63 |
} |