123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- <?php
- namespace KIF\Core;
- use Exception;
- class Config {
-
- static private $instance;
-
-
- static protected $appConfigs = array();
-
-
- protected $appConfig;
-
-
- private function __construct() {
-
- }
-
-
- static public function load($app_conf_path) {
- $app_conf_path = realpath($app_conf_path);
- $app_conf_key = md5($app_conf_path);
- if (!isset(self::$appConfigs[$app_conf_key])) {
- $config = include($app_conf_path);
- if (!is_array($config)) {
- throw new Exception("load app_conf_path fail: {$app_conf_path}");
- }
-
- $config['conf_path'] = $app_conf_path;
-
- self::$appConfigs[$app_conf_key] = $config;
- }
-
- if (is_null(self::$instance)) {
- self::$instance = new self();
- }
-
- self::$instance->appConfig = self::$appConfigs[$app_conf_key];
-
- return self::$instance;
- }
-
-
- static public function getInstance() {
- if (is_null(self::$instance)) {
- self::$instance = new self();
- }
-
- if (is_null(self::$instance->appConfig)) {
- throw new Exception('instance appconfig is null, pleace run KIF\Core\Config::load !');
- }
-
- return self::$instance;
- }
-
-
- public function get($key) {
- if (empty($key)) {
- return false;
- }
-
- $keys = explode('.', $key);
-
- $value = $this->appConfig;
-
- foreach ($keys as $tmpKey) {
- if (!isset($value[$tmpKey])) {
- return false;
- }
- $value = $value[$tmpKey];
- }
- return $value;
- }
-
-
- public function exists($key) {
- return isset($this->appConfig[$key]);
- }
-
-
- public function current() {
- return $this->appConfig;
- }
-
-
- static public function all() {
- return self::$appConfigs;
- }
- }
|