您的位置:首页 > 编程语言 > PHP开发

zhphp framework (七) 配置文件加载与读取以及动态写、读配置文件

2015-12-11 13:29 751 查看
<?php
/**
* Created by JetBrains PhpStorm.
* User: 张华
* Date: 14-3-8
* Time: 下午12:21
* QQ: 746502560@qq.com
* To change this template use File | Settings | File Templates.
*/
defined('IN_ZHPHP')?NULL:die('attempts to hack the system');
final class config{
public static $confVal=array();
public static $zhConf=array();
private function __construct() {}#防止被实例化
/**
* 预加载配置文件
*/
public static function loadConfig(){
$configFile1=read_folder_directory(ROOT_PATH.'config/');
$configFile2=read_folder_directory(APP_PATH.'config/');
$c1=array();$c2=array();$c3=array();
foreach ($configFile2 as $file){
$c1[]= include_once APP_PATH.'config/'.$file;
}
foreach ($configFile1 as $file){
$c2[]= include_once ROOT_PATH.'config/'.$file;
}
$c3=array_merge_recursive($c2,$c1);
self::$confVal=array3DToArray2D($c3);
unset($configFile1,$configFile2,$c1,$c2,$c3);
}
/**
* 写配置文件
* @param type $key
* @param type $value
*/
public  static function writeConfig($key,$value){
if(array_key_exists($key, self::$confVal) === false){
self::$confVal[$key]=$value;
}
}
/**
*读取配置文件
* @param type $key
* @param type $twoKey
* @return type
*/
public static function readConfig($key,$twoKey=null){
if(self::isConfig($key,$twoKey)){
if(array_key_exists($key, self::$confVal)){
return (is_null($twoKey))?self::$confVal[$key]:self::$confVal[$key][$twoKey];
}
foreach (self::$confVal as $array){
if(array_key_exists($key,$array)){
return (is_null($twoKey))?$array[$key]:$array[$key][$twoKey];
break;
}
}
}
return false;
}
/**
* 判断判断项目是否存在
*/
public  static  function isConfig($key,$twoKey){
if(array_key_exists($key, self::$confVal) && is_null($twoKey)){
return true;
}else if(array_key_exists($key, self::$confVal) &&  ! is_null($twoKey)){
if(array_key_exists($twoKey,self::$confVal[$key])){
return true;
}
}else{
return false;
}
}

/**
* 查看配置文件项目
*/
public static  function   debugConfig(){
echo '<pre>';
print_r(self::$confVal);
echo '</pre>';
die();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: