Skip to content

Instantly share code, notes, and snippets.

@jjsty1e
Created June 8, 2016 05:55
Show Gist options
  • Save jjsty1e/ced41adece5cb08ec2a2b79525cbc864 to your computer and use it in GitHub Desktop.
Save jjsty1e/ced41adece5cb08ec2a2b79525cbc864 to your computer and use it in GitHub Desktop.
获取配置
<?php
/**
* Description replace from your description
* Created by Jetbrains PhpStorm.
* User: Jake <singviy@gmail.com>
* Created: 2016/6/7 9:39
*/
class Config
{
private static $configs = array();
public function __construct()
{
//echo "this is config loader";
}
public static function loadGroup($group)
{
return self::$configs[$group];
}
/**
* loadKey获取配置,
* @author Jake
* @param $key
* @param null $group
* @return bool
*/
public static function loadKey($key,$group = null)
{
if($group == null){
if(!self::$configs)
return false;
foreach (self::$configs as $_group => $value){
if(isset(self::$configs[$_group][$key]))
return self::$configs[$_group][$key];
}
return false;
}elseif (isset(self::$configs[$group][$key])){
return self::$configs[$group][$key];
}else
return false;
}
public static function loadAll()
{
return self::$configs;
}
public function registerConfigFile($file){
if(is_array($file)){
}else
{
$config = require_once $file;
self::$configs[$config[0]] = $config[1];
}
}
}
<?php
/**
* Description replace from your description
* Created by Jetbrains PhpStorm.
* User: Jake <singviy@gmail.com>
* Created: 2016/6/7 9:35
*/
/**
* 返回一个两元数组,第一个元素是分组的名字,第二个是配置的值
*/
return array('db',array(
'type' => 'mysql',
'host' => 'localhost',
'port' => 3306,
'name' => 'test',
'user' => 'root',
'password' => 'root'
));
<?php
$configLoader = new Config();
$configLoader->registerConfigFile('配置文件路径'); //配置文件路径
//获取分组下的配置
$grp = Config::loadGroup('db');
//查找键
$name = Config:loadKey('name','group');
//获取所有
$allConfig = Config:loadAll();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment