The
Basically meets all configuration-related requirements.
/** * parsing. ini format of the configuration file is a tree structure of the object * configuration file different section through the colon inheritance * default based on hostname to determine the use of the section, if you are not sure to prioritize the use of production * detection environment is always priority to detect P
Roduction, the rest of the section detects * * @author ares@phpdr.net * * * * * * * * * * * * * * * * * @var stdClass * * */**
Private $config; /** * A two-dimensional array, the key is the section * value of the configuration file is an array or callback function * If an array, calculate whether hostname in the array determines whether to use the section * if the callback function determines whether to use the SECTI by returning a value of TRUE or False
On * * @var array */private $map = Array (); The/** * section is parsed, representing the '. ' In the inheritance * configuration item. Used to differentiate the '. ' In a hierarchical relationship * section.
is not parsed and the array in the configuration is unaffected. * * @param string $conf * @throws errorexception * @return stdClass/function __construct ($conf, $map) {$con
Fig = $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 not found in config '); } $sectIon = ' production ';
$hostname = GetHostName (); foreach ($map as $k => $v) {if (Is_array ($v)) {foreach ($v as $v 1) {if ($v 1 = = $hostname) {$sectio
n = $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; /** * Always returns Configuration Object * * @return Mixed/function __get ($key) {if (Isset ($this->config-> $key)) {return
$this->config-> $key; }/** * Split * @param stdClass $v * @param string $k 1 * @param mixed $v 1/private Function S
Plit ($v, $k 1, $v 1) {$keys = explode ('. ', $k 1);
$last = Array_pop ($keys);
$node = $v;
foreach ($keys as $v 2) {if (! isset ($node-> $v 2)) {$node-> $v 2 = new StdClass ();
$node = $node-> $v 2;
$node-> $last = $v 1; if (count ($keys) > 0) {unset ($v-≫ $k 1); }/** * Parseini * * @param object $conf * @return StdClass/Private Function Parseini ($conf) {$confO
BJ = new StdClass ();
foreach ($conf as $k => $v) {//Is section if (Is_array ($v)) {$confObj-> $k = (object) $v;
foreach ($v as $k 1 => $v 1) {Call_user_func (Array ($this, ' split '), $CONFOBJ-> $k, $k 1, $v 1);
} else {Call_user_func (array ($this, ' split '), $CONFOBJ, $k, $v);
} unset ($conf);
Process inherited foreach ($confObj as $k => $v) {if (False!== Strpos ($k, ': ')) {if (0 = = Strpos ($k, ': ')) { throw new errorexception (' config '. $k.
' is invalid, ': ' Can ' t be the ' the ' '; } 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 default is shallow cloning, function implementation deep cloning * * @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; /** * Recursive Merge two objects * * @param unknown $obj 1 * @param unknown $obj 2 */Private function Objectmerger ($obj 1, $obj 2) {foreach ($obj 2 as $k => $v) {if (Is_object ($v) && isset ($obj 1-> $k) && is_obj ECT ($obj 1-> $k)) {$this->objectmerger ($obj 1->$K, $v);
else {$obj 1-> $k = $v; }
}
}
}
Simple to use:
$_env [' config '] = new config (file_get_contents (__dir__). '/config.ini '), Array (
' development ' => array (
' Localhost.localdomain ',
' localhost '
),
' Production ' => Array ())
;
Configuration file Example:
[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