您好,登錄后才能下訂單哦!
這篇文章主要講解了“有哪些讀寫ini格式的PHP配置類”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“有哪些讀寫ini格式的PHP配置類”吧!
基本滿足所有配置相關的需求。
/** * 解析.ini格式的配置文件為一個樹形結構的對象 * 配置文件不同section通過冒號繼承 * 默認根據hostname確定使用的section,如果不能確定就優先使用production * 檢測環境的時候總是優先檢測production,其余section按定義順序檢測 * * @author ares@phpdr.net * */ 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
感謝各位的閱讀,以上就是“有哪些讀寫ini格式的PHP配置類”的內容了,經過本文的學習后,相信大家對有哪些讀寫ini格式的PHP配置類這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。