123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- <?php
- namespace KIF\Core;
- use Exception;
- /**
- *
- * 配置类。作用:
- * 1、作为框架需要配置参数的那些类(如KIF\Cache、KIF\Dao 里的类)的参数获取接口。
- * 2、为KIF框架下的不同APP间 php in-process calls (APP间PHP进程内调用) 提供核心支持
- *
- * @author gaoxiaogang@gmail.com
- *
- */
- class Config {
-
- static private $instance;
-
- /**
- * 所有app的配置集
- * array(
- * md5($app_conf_path) => array(
- *
- * ),
- * ... ,
- * );
- * @var array
- */
- static protected $appConfigs = array();
-
- /**
- *
- * 当前app的配置
- * @var array
- */
- protected $appConfig;
-
- /**
- *
- * 不允许直接 new
- */
- private function __construct() {
-
- }
-
- /**
- *
- * 载入配置,如果成功,返回instance
- * @param string $app_conf_path 配置文件的路径
- * @return KIF\Core\Config
- */
- 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}");
- }
- # 将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;
- }
-
- /**
- *
- * 单例模式
- * @param string $app_conf_path
- * @return KIF\Core\Config
- */
- 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;
- }
-
- /**
- *
- * 获取指定配置项的值
- * @param string $key
- * @return mixed
- */
- 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;
- }
-
- /**
- *
- * 指定配置项是否设置了
- * @param string $key
- * @return boolean
- */
- public function exists($key) {
- return isset($this->appConfig[$key]);
- }
-
- /**
- *
- * 获取当前的配置信息
- * @return array
- */
- public function current() {
- return $this->appConfig;
- }
-
- /**
- *
- * 获取所有app的配置集
- * @return array
- */
- static public function all() {
- return self::$appConfigs;
- }
- }
|