<?php
/**
* PHPIniè§£æç±» Beta - PHPä¿®æ¹å é¤å¢å é
ç½®æä»¶
*
* @author Jessica
* @link www.skiyo.cn
* @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 = "<?php \n".$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);
}
}