Chinaunix首页 | 论坛 | 博客
  • 博客访问: 295365
  • 博文数量: 153
  • 博客积分: 3347
  • 博客等级: 中校
  • 技术积分: 1556
  • 用 户 组: 普通用户
  • 注册时间: 2009-12-30 17:50
文章分类

全部博文(153)

文章存档

2013年(7)

2012年(21)

2011年(46)

2010年(16)

2009年(63)

我的朋友

分类:

2009-12-30 19:11:35

/**
 * 配置文件访问类
 * by:李勇
 * at:2009-02-02
 *
 * 用法:
 *    1.
 *      $configArr = yqConfig::read(<配置文件名称>[,<一级分类>[,<二级分类>[,<三级分类>[,<四级分类>]]]]);
 *    2.
 *      $config = new yqConfig(<配置文件名称>);
 *      $configArr = $config->get([<一级分类>[,<二级分类>[,<三级分类>[,<四级分类>]]]]);
 */


class yqConfig{
    private 
$file;      //实例的配置文件名称
    
static private $configs=array();    //所有配置文件的配置内容

    /**
     * @param string $file 配置文件名称
     */
    
public function __construct($file){
        if(!
is_string($file))
            throw new 
ConfigException(ConfigException::CE_FILE_STRING ,array($file));

        if(!
$file)
            throw new 
ConfigException(ConfigException::CE_FILE_NULL );

        
$this->file=$file;

        if(!isset(
self::$configs[$file])) {
            
self::$configs[$file]=self::load($file);
        }
    }

    
/**
     * 首次读取配置文件内容
     *
     * @param string $name  配置文件名称
     * @return array
     */
    
static private function load($filename){

        
$file=rootpath.DIRECTORY_SEPARATOR.'configs'.DIRECTORY_SEPARATOR.$filename.'.conf.php';
        if(!
file_exists($file)){
            throw new 
ConfigException(ConfigException::CE_EXIST ,array($filename));
        }

        try{
            
$config=require($file);
        }catch(
Exception $e){
            throw new 
ConfigException(ConfigException::CE_REQUIRE ,array($filename,$e));
        }

        if(!
$config)$config=array();

        if(!
is_array($config)){
            throw new 
ConfigException(ConfigException::CE_ARRAY ,array($filename,$config));
        }

        return 
$config;
    }

    
/**
     * 实例的获取配置项的方法
     *
     * @param string $key
     * @param string $sub
     * @return array
     */
    
public function get($key=null,$sub=null,$sub2=null,$sub3=null){
        
$config=self::$configs[$this->file];

        if(!
$key) return $config;

        if(!
is_string($key))
            throw new 
ConfigException(ConfigException::CE_KEY_STRING ,array($key));

        if(isset(
$config[$key]))
            
$ret$config[$key];
        else
            throw new 
ConfigException(ConfigException::CE_KEY_NULL ,array($this->file,$key));

        if(!
$sub) return $ret;
        if(!
is_string($sub))
            throw new 
ConfigException(ConfigException::CE_KEY_STRING ,array($sub));

        if(isset(
$ret[$sub]))
            
$ret $ret[$sub];
        else
            throw new 
ConfigException(ConfigException::CE_KEY_NULL ,array($this->file,$sub));

        if(!
$sub2) return $ret;
        if(!
is_string($sub2))
            throw new 
ConfigException(ConfigException::CE_KEY_STRING ,array($sub2));

        if(isset(
$ret[$sub2]))
            
$ret $ret[$sub2];
        else
            throw new 
ConfigException(ConfigException::CE_KEY_NULL ,array($this->file,$sub2));

        if(!
$sub3) return $ret;
        if(!
is_string($sub3))
            throw new 
ConfigException(ConfigException::CE_KEY_STRING ,array($sub3));

        if(isset(
$ret[$sub3]))
            
$ret $ret[$sub3];
        else
            throw new 
ConfigException(ConfigException::CE_KEY_NULL ,array($this->file,$sub3));

        return 
$ret;
    }

    
/**
     * 类静态方法,直接获取指定配置文件的配置项
     *
     * @param string $file 配置文件名称
     * @param string $key   配置项名称
     * @param string $sub   子项名称
     * @return array
     */
    
static public function read($file,$key=null,$sub=null,$sub2=null,$sub3=null){
        
$instance=new yqConfig($file);
        return 
$instance->get($key,$sub,$sub2,$sub3);
    }

    static public function 
has($file,$key=null,$sub=null,$sub2=null,$sub3=null){
        try {
            
$instance=new yqConfig($file);
            return 
$instance->get($key,$sub,$sub2,$sub3);
        }catch (
ConfigException $e){
            return 
false;
        }
    }
}

/**
 * 配置类的异常
 *
 */
class ConfigException extends Exception {
    const 
CE_FILE_STRING    1001;
    const 
CE_FILE_NULL      1002;
    const 
CE_EXIST          1003;
    const 
CE_REQUIRE        1004;
    const 
CE_ARRAY          1005;
    const 
CE_KEY_STRING     1006;
    const 
CE_KEY_NULL       1007;
    const 
CE_UNKNOWN        5000;

    static private 
$msgs=array(
        
self::CE_FILE_STRING    => 'filename must be string:<%s>',
        
self::CE_FILE_NULL      => 'filename need',
        
self::CE_EXIST          => 'file <%s> not exist',
        
self::CE_REQUIRE        => 'file <%s> require error, detail: <%s>',
        
self::CE_ARRAY          => 'file <%s> content wrong , must be array, but now is <%s>',
        
self::CE_KEY_STRING     => 'key must be string, but now is <%s>',
        
self::CE_KEY_NULL       => 'in file <%s> , key <%s> not exist',
    );

    public function 
__construct($code 0,$msg null) {
        if(isset(
self::$msgs[$code])){
            if(
is_string($msg)){
                
$message $msg;
            }else if(
is_array($msg)){
                foreach (
$msg as $k=>$v){
                    if(!
is_string($v)&&!is_numeric($v)){
                        
$msg[$k]=$this->dump($v);
                    }
                }
                
$message vsprintf(self::$msgs[$code],$msg);
            }else{
                
$message self::$msgs[$code];
            }
        }else{
            
$code self::CE_UNKNOWN ;
            if(
is_string($msg)){
                
$message $msg;
            }else{
                
$message '错误代码:'.$code;
            }
        }

        
parent::__construct($message$code);
    }

    private function 
dump($mixed){
        return 
var_export($mixed,true);
    }
}
阅读(760) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~