<?php
define('CONFIG_PARSER_LF', "\n"); //如果为windows,请设置为\r\n
class ConfigParser
{
private static $_instance = null;
private $_config;
private $_filePath;
final private function __construct() {}
final private function __clone() {}
public static function instance($filePath)
{
if (empty(self::$_instance)) {
self::$_instance = new ConfigParser();
}
self::$_instance->_filePath = $filePath;
self::$_instance->_config = @parse_ini_file($filePath, true);
if (empty(self::$_instance->_config)) {
self::$_instance->_config = array();
}
return self::$_instance;
}
public function get()
{
//获取所有
if (func_num_args() == 0) {
return $this->_config;
}
//获取section
if (func_num_args() == 1) {
if (!isset($this->_config[func_get_arg(0)])) {
return null;
}
return $this->_config[func_get_arg(0)];
}
//获取option
if (func_num_args() == 2) {
if (!isset($this->_config[func_get_arg(0)])) {
return null;
}
$selection = $this->_config[func_get_arg(0)];
if (!is_array($selection) || !isset($selection[func_get_arg(1)])) {
return null;
}
return $selection[func_get_arg(1)];
}
}
public function addSection($section)
{
if (isset($this->_config[$section])) {
return false;
}
$this->_config[$section] = array();
return true;
}
public function set()
{
if (in_array(func_num_args(), array(2, 3))) {
return false;
}
//设置section
if (func_num_args() == 2) {
$this->_config[func_get_arg(0)] = func_get_arg(1);
return true;
}
//设置option
if (func_num_args() == 3) {
if (!isset($this->_config[func_get_arg(0)])) {
$this->_config[func_get_arg(0)] = array();
}
if (!is_array($this->_config[func_get_arg(0)])) {
return false;
}
$this->_config[func_get_arg(0)][func_get_arg(1)] = func_get_arg(2);
return true;
}
}
public function del()
{
//删除所有
if (func_num_args() == 0) {
$this->_config = array();
return true;
}
//删除section
if (func_num_args() == 1) {
if (isset($this->_config[func_get_arg(0)])) {
unset($this->_config[func_get_arg(0)]);
return true;
}
}
//删除option
if (func_num_args() == 2) {
if (!isset($this->_config[func_get_arg(0)])
|| !is_array($this->_config[func_get_arg(0)])
) {
return false;
}
if (!isset($this->_config[func_get_arg(0)][func_get_arg(1)])) {
return false;
}
unset($this->_config[func_get_arg(0)][func_get_arg(1)]);
return true;
}
return false;
}
public function write()
{
if (empty($this->_fileName)) {
$this->_config = array();
return false;
}
$contents = array();
$contentsNotInSection = array();
if (!empty($this->_config)) {
foreach ($this->_config as $section => $sectionItem) {
if (is_array($sectionItem)) {
array_push($contents, "[{$section}]");
if (!empty($sectionItem)) {
foreach ($sectionItem as $key => $value) {
array_push($contents, "{$key} = {$value}");
}
}
continue;
}
array_push(
$contentsNotInSection, "{$section} = {$sectionItem}"
);
}
$contents = array_unique($contents);
$contentsNotInSection = array_unique($contentsNotInSection);
$contents = array_merge($contentsNotInSection, $contents);
}
return file_put_contents(
$this->_filePath, implode(CONFIG_PARSER_LF, $contents)
);
}
}
//使用说明
//config.ini
//gmail = hzlhu.dargon@gmail.com
//[section]
//name = value
$config = ConfigParser::instance('config.ini');
//读取
$config->get();
$config->get('gmail');
$config->get('section', 'name');
//添加section
$config->addSection('test');
//设置
$config->set('gmail', 'hzlhu.dargon@gmail.com');
$config->set('section', array('name' => 'value'));
$config->set('section', 'name', 'value');
//删除
$config->del();
$config->del('gmail');
$config->del('section');
$config->del('section', 'name');
//将修改后的值保存到文件中
$config->write();