아주 완벽 한 읽 기와 쓰기 ini 형식의 PHP 설정 클래스 공유
/**
* .ini
* section
* hostname section, production
* production, section
*
* @author [email protected]
*
*/
class Config {
/**
*
*
* @var stdClass
*/
private $config;
/**
* , section
*
* hostname section
* true false section
*
* @var array
*/
private $map = array ();
/**
* section ,:
* '.'
* section '.' , 。
*
* @param string $conf
* @throws ErrorException
* @return stdClass
*/
function __construct($conf, $map) {
$config = $this->parseIni ( ( object ) parse_ini_string ( $conf, true ) );
if (array_key_exists ( 'production', $map )) {
$production = $map ['production'];
unset ( $map ['production'] );
$map = array_merge ( array (
'production' => $production ), $map );
} else {
throw new ErrorException ( 'production section not found in config' );
}
$section = 'production';
$hostname = gethostname ();
foreach ( $map as $k => $v ) {
if (is_array ( $v )) {
foreach ( $v as $v1 ) {
if ($v1 == $hostname) {
$section = $k;
break 2;
}
}
} elseif (is_callable ( $v )) {
if (true == call_user_func ( $v )) {
$section = $k;
break;
}
} else {
throw new ErrorException ( 'Wrong map value in ' . __CLASS__ );
}
}
$this->config = $config->$section;
}
/**
*
*
* @return mixed
*/
function __get($key) {
if (isset ( $this->config->$key )) {
return $this->config->$key;
}
}
/**
*
*
* @param stdClass $v
* @param string $k1
* @param mixed $v1
*/
private function split($v, $k1, $v1) {
$keys = explode ( '.', $k1 );
$last = array_pop ( $keys );
$node = $v;
foreach ( $keys as $v2 ) {
if (! isset ( $node->$v2 )) {
$node->$v2 = new stdClass ();
}
$node = $node->$v2;
}
$node->$last = $v1;
if (count ( $keys ) > 0) {
unset ( $v->$k1 );
}
}
/**
* parseIni
*
* @param object $conf
* @return stdClass
*/
private function parseIni($conf) {
$confObj = new stdClass ();
foreach ( $conf as $k => $v ) {
// section
if (is_array ( $v )) {
$confObj->$k = ( object ) $v;
foreach ( $v as $k1 => $v1 ) {
call_user_func ( array (
$this,
'split' ), $confObj->$k, $k1, $v1 );
}
} else {
call_user_func ( array (
$this,
'split' ), $confObj, $k, $v );
}
}
unset ( $conf );
//
foreach ( $confObj as $k => $v ) {
if (false !== strpos ( $k, ':' )) {
if (0 === strpos ( $k, ':' )) {
throw new ErrorException ( 'config ' . $k . ' is invalid, ':' can't be the first char' );
} elseif (1 < substr_count ( $k, ':' )) {
throw new ErrorException ( 'config ' . $k . ' is invalid, ':' can appear only once' );
} else {
$keys = explode ( ':', $k );
if (! isset ( $confObj->$keys [1] )) {
throw new ErrorException ( 'parent section ' . $keys [1] . ' doesn't exist in config file' );
} else {
if (isset ( $confObj->$keys [0] )) {
throw new ErrorException ( 'config is invalid, ' . $keys [0] . ' and ' . $k . ' conflicts' );
} else {
$confObj->$keys [0] = $this->deepCloneR ( $confObj->$keys [1] );
$this->objectMergeR ( $confObj->$keys [0], $v );
unset ( $confObj->$k );
}
}
}
}
}
return $confObj;
}
/**
* php ,
*
* @param object $obj
* @return object $obj
*/
private function deepCloneR($obj) {
$objClone = clone $obj;
foreach ( $objClone as $k => $v ) {
if (is_object ( $v )) {
$objClone->$k = $this->deepCloneR ( $v );
}
}
return $objClone;
}
/**
*
*
* @param unknown $obj1
* @param unknown $obj2
*/
private function objectMergeR($obj1, $obj2) {
foreach ( $obj2 as $k => $v ) {
if (is_object ( $v ) && isset ( $obj1->$k ) && is_object ( $obj1->$k )) {
$this->objectMergeR ( $obj1->$k, $v );
} else {
$obj1->$k = $v;
}
}
}
}
간단 한 사용:
$_ENV ['config'] = new Config ( file_get_contents ( __DIR__ . '/config.ini' ), array (
'development' => array (
'localhost.localdomain',
'localhost'
),
'production' => array ()
) );
프로필 예제:
[product]
db.default.dsn="mysql:host=127.0.0.1;dbname=default"
db.default.username=root
db.default.password=123456
admin.username=admin
admin.password=123456
php.error_reporting=E_ALL
php.display_errors=no
php.log_errors=yes
php.error_log=APP_PATH'/resource/log/phpError.log'
php.session.save_path=APP_PATH'/resource/data/session'
[development:product]
db.test1.dsn="mysql:host=127.0.0.1;dbname=test1"
db.test1.username=root
db.test1.password=123456
php.display_errors=yes
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
HDU 1017 A Mathematical Curiosity(문제 해결 보고서)바보 B원에서 전재하다 Problem Description Given two integers n and m, count the number of pairs of integers (a,b) such that 0 < a...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.