分类:
2009-06-03 17:51:04
/**
* PHPIni解析类详细例子
*/
require_once('PHPIni.class.php');
//------------基础用法(不带Section)--------------
$ini = new PHPIni('config1.ini.php', false); //文件必须为PHP结尾.保证安全性 如果文件不存在就创建
//以非Section方式打开.也就是忽略Section的存在
$ini->setKey('key1', 'value1'); //设置key
$ini->setKey('key2', 'value2');
$ini->saveIniFile(); //设置完要保存ini文件.这步很重要
print_r($ini->getIniArr()); //打印出解析ini文件得到的数组
//这时候你可以打开config1.ini.php看下设置是什么样的,然后执行下面的语句
$ini->setKey('key1', '我是可以key1'); //更改key1的值
$ini->delKey('key2'); //删除key2
$ini->saveIniFile(); //保存文件
print_r($ini->getIniArr()); //打印出解析ini文件得到的数组
unset($ini);
//------------end 基础用法(不带Section)--------------
//------------中级用法(带Section)------------------
$ini = new PHPIni('config2.ini.php'); //使用默认方式(Section方式)打开文件
//注:在Section模式下.每个键值都必须依附于一个Section.
$ini->addSection('section1'); //如果一个Section都没有就必须设置一个
$ini->setKey('key1', 'value1', 'section1'); //为Section1设置一个值
$ini->addSection('section2', array('key1' => 'value1')); //直接为Section设置值 和上面的效果相同
$ini->setKey('key2', 'value2'); //Section为空.就在最后一个Section后增加值.当前就是在Section2后加入
$ini->saveIniFile(); //保存文件
print_r($ini->getIniArr()); //打印出解析ini文件得到的数组
$ini->delSection('section1'); //删除Section 注意下面的所有键值都会被删除
$ini->saveIniFile(); //保存文件
print_r($ini->getIniArr()); //打印出解析ini文件得到的数组
unset($ini);
//------------end 中级用法(带Section)------------------
//------------高级用法(通过数组设置ini)------------------
$ini = new PHPIni('config3.ini.php');
$iniArr = array(
'section1' => array(
'key1' => 'value1',
'key2' => 'value2',
),
'section2' => array(
'key1' => 'value1',
'key2' => 'value2',
),
);
$ini->setIniArr($iniArr);
$ini->saveIniFile(); //把数组保存到Ini文件
//下面的方法也可以
$ini->setIniFile($iniArr); //这个方法也可以.跟上面两句话等价.
//------------end 高级用法(通过数组设置ini)------------------
/**
* PHPIni解析类 Beta - PHP修改删除增加配置文件
*
* @author Jessica
* @link [url][/url]
* @version 1.0.0
*
*/
class PHPIni {
/**
* Ini文件的数组形式
*
* @var array
*/
private $iniArr = array();
/**
* Ini文件路径
*
* @var string
*/
private $iniFile = '';
/**
* 是否以Section方式打开ini文件
*
* @var boolean
*/
private $isSection = true;
/**
* 构造函数
* $iniFile表示文件路径.若不存在就自动创建
* $isSection表示是否以Section方式打开ini文件
*
* @param string $iniFile
* @param boolean $isSection
* @access public
*/
public function __construct($iniFile, $isSection = true) {
$this->iniFile = $iniFile;
$this->isSection = $isSection;
if (empty($this->iniArr)) {
$this->parseIni();
}
}
/**
* 解析ini文件到数组
*
* @access public
* @return void
*/
private function parseIni() {
if (empty($this->iniFile) || !is_file($this->iniFile)) {
if (!$this->createIniFile()) {
$this->throwException('无法创建Ini文件!');
}
}
$this->iniArr = parse_ini_file($this->iniFile, $this->isSection);
}
/**
* 获取解析到的数组
*
* @return array
* @access public
*/
public function getIniArr() {
return $this->iniArr;
}
/**
* 创建Ini文件
*
* @return boolean
* @access private
*/
private function createIniFile() {
if (touch($this->iniFile)) {
return true;
} else {
return false;
}
}
/**
* 添加或者修改一个config
* 如果存在就更改以前的设置.如果不存在就添加ini设置
* 如果$section为空就在最后添加设置,如果不为空就在指定的$section添加设置
*
* @param string $key
* @param mixed $value
* @param string $section
* @access public
* @return void
*/
public function setKey($key, $value, $section = '') {
//如果$section为空就在最后添加设置
if (empty($section)) {
if ($this->isSection) {
//将数组指针移动到最后
end($this->iniArr);
//在数组的最后插入元素
if (empty($this->iniArr[key($this->iniArr)])) {
$this->throwException('没有任何Section,请先添加Section');
}
$this->iniArr[key($this->iniArr)][$key] = $value;
} else {
$this->iniArr[$key] = $value;
}
} else {
//$section就按照$section添加设置
if ($this->isSection) {
$this->iniArr[$section][$key] = $value;
} else {
$this->iniArr[$key] = $value;
}
}
}
/**
* 增加一个Section,其内容为数组
*
* @param string $section
* @param array $value
*/
public function addSection($section, $value = array()) {
if (!$this->isSection) {
$this->throwException('无法在非Section模式下增加Section!');
} else {
if (array_key_exists($section, $this->iniArr)) {
$this->throwException('无法增加Section,已经存在相同的Section!');
} else {
if (is_array($value)) {
$this->iniArr[$section] = $value;
} else {
$this->throwException('Section的值必须是数组!');
}
}
}
}
/**
* 手工设置ini的参数.
*
* @param array $iniArr
* @access public
*/
public function setIniArr($iniArr) {
$this->iniArr = $iniArr;
}
/**
* 删除一个Section
*
* @param string $section
*/
public function delSection($section) {
if ($this->isSection) {
if (array_key_exists($section, $this->iniArr)) {
unset($this->iniArr[$section]);
return true;
} else {
return false;
}
} else {
return false;
}
}
/**
* 删除一个config
* 如果$section为空就在最后一个Section删除对应的Key
*
* @param string $key
* @param string $section
*/
public function delKey($key, $section = '') {
if (empty($section)) {
if ($this->isSection) {
end($this->iniArr);
if (array_key_exists($key, $this->iniArr[key($this->iniArr)])) {
unset($this->iniArr[key($this->iniArr)][$key]);
} else {
return false;
}
} else {
if (array_key_exists($key, $this->iniArr)) {
unset($this->iniArr[$key]);
} else {
return false;
}
}
} else {
//$section就按照$section添加设置
if ($this->isSection) {
if (array_key_exists($key, $this->iniArr[$section])) {
unset($this->iniArr[$section][$key]);
} else {
return false;
}
} else {
if (array_key_exists($key, $this->iniArr)) {
unset($this->iniArr[$key]);
} else {
return false;
}
}
}
return true;
}
/**
* 删除数组中相同的元素.(只搜索第一层键值)
*
* @param array $array
* @return array
* @access private
*/
private function removeSameArrKey($array) {
foreach ($array as $key => $value) {
$keyArr[] = $key;
}
$keyArr = array_unique($keyArr);
foreach ($array as $key1 => $value) {
foreach ($keyArr as $key2) {
if ($key2 == $key1) {
$return[$key2] = $value;
}
}
}
return $return;
}
/**
* 根据数组保存ini文件
* 注:如果$iniArr为空默认使用事先读到的数组
*
* @param array $iniArr
* @return boolean
* @access public
*/
public function setIniFile($iniArr = array()) {
if (empty($this->iniFile) || !is_file($this->iniFile)) {
$this->throwException('没有找到ini文件!');
} else {
//判断参数
if (empty($iniArr)) {
if (empty($this->iniArr)) {
$this->throwException('没有任何可以读取的设置!请先解析Ini');
} else {
$iniArr = $this->iniArr;
}
}
//生成Ini
$iniArr = $this->removeSameArrKey($iniArr);
foreach($iniArr as $key => $item) {
//由于只会有一层Section.所以不必递归
//Section
if(is_array($item)) {
$item = array_unique($item);
$content .= "\n[{$key}]\n";
foreach ($item as $key2 => $item2) {
if(is_numeric($item2) || is_bool($item2)) {
$content .= "{$key2} = {$item2}\n";
} else{
$content .= "{$key2} = \"{$item2}\"\n";
}
}
} else {
if(is_numeric($item) || is_bool($item)) {
$content .= "{$key} = {$item}\n";
} else {
$content .= "{$key} = \"{$item}\"\n";
}
}
}
//保证安全性
$content = ".$content."?>";
//写入文件
if(!$handle = fopen($this->iniFile, 'w')) {
return false;
}
if (flock($handle, LOCK_EX)) {
if(!fwrite($handle, $content)) {
return false;
}
flock($handle, LOCK_UN); // 释放锁定
fclose($handle);
return true;
} else {
$this->throwException('无法锁定ini文件'.$this->iniFile);
}
}
}
/**
* 保存ini文件
*
* @return boolean
* @access public
*/
public function saveIniFile() {
if ($this->setIniFile()) {
return true;
} else {
return false;
}
}
/**
* 抛出一异常信息
*
* @param string $message
* @return void
* @access protected
*/
protected function throwException($message) {
throw new Exception($message);
}
}