一个非常完美的读写ini格式的PHP配置类分享

这篇文章主要介绍了一个非常完美的读写ini格式的PHP配置类分享,本文给出类代码、使用例子和配置文件例子,需要的朋友可以参考下

基本满足所有配置相关的需求。

  1. /**
  2. * 解析.ini格式的配置文件为一个树形结构的对象
  3. * 配置文件不同section通过冒号继承
  4. * 默认根据hostname确定使用的section,如果不能确定就优先使用production
  5. * 检测环境的时候总是优先检测production,其余section按定义顺序检测
  6. *
  7. * @author ares@phpdr.net
  8. *
  9. */
  10. class Config {
  11. /**
  12. * 解析后的配置文件
  13. *
  14. * @var stdClass
  15. */
  16. private $config;
  17. /**
  18. * 一个二维数组,键是配置文件的section
  19. * 值是一个数组或回调函数
  20. * 如果是数组则计算hostname是否在数组中决定是否使用该section
  21. * 如果是回调函数通过返回值true或false来确定是否使用该section
  22. *
  23. * @var array
  24. */
  25. private $map = array ();
  26. /**
  27. * section会被解析,:表示继承
  28. * 配置项中的'.'用来区分层级关系
  29. * section中的'.'不会被解析,配置中的数组不受影响。
  30. *
  31. * @param string $conf
  32. * @throws ErrorException
  33. * @return stdClass
  34. */
  35. function __construct($conf, $map) {
  36. $config = $this->parseIni ( ( object ) parse_ini_string ( $conf, true ) );
  37. if (array_key_exists ( 'production', $map )) {
  38. $production = $map ['production'];
  39. unset ( $map ['production'] );
  40. $map = array_merge ( array (
  41. 'production' => $production ), $map );
  42. } else {
  43. throw new ErrorException ( 'production section not found in config' );
  44. }
  45. $section = 'production';
  46. $hostname = gethostname ();
  47. foreach ( $map as $k => $v ) {
  48. if (is_array ( $v )) {
  49. foreach ( $v as $v1 ) {
  50. if ($v1 == $hostname) {
  51. $section = $k;
  52. break 2;
  53. }
  54. }
  55. } elseif (is_callable ( $v )) {
  56. if (true == call_user_func ( $v )) {
  57. $section = $k;
  58. break;
  59. }
  60. } else {
  61. throw new ErrorException ( 'Wrong map value in ' . __CLASS__ );
  62. }
  63. }
  64. $this->config = $config->$section;
  65. }
  66. /**
  67. * 总是返回配置对象
  68. *
  69. * @return mixed
  70. */
  71. function __get($key) {
  72. if (isset ( $this->config->$key )) {
  73. return $this->config->$key;
  74. }
  75. }
  76. /**
  77. * 切分
  78. *
  79. * @param stdClass $v
  80. * @param string $k1
  81. * @param mixed $v1
  82. */
  83. private function split($v, $k1, $v1) {
  84. $keys = explode ( '.', $k1 );
  85. $last = array_pop ( $keys );
  86. $node = $v;
  87. foreach ( $keys as $v2 ) {
  88. if (! isset ( $node->$v2 )) {
  89. $node->$v2 = new stdClass ();
  90. }
  91. $node = $node->$v2;
  92. }
  93. $node->$last = $v1;
  94. if (count ( $keys ) > 0) {
  95. unset ( $v->$k1 );
  96. }
  97. }
  98. /**
  99. * parseIni
  100. *
  101. * @param object $conf
  102. * @return stdClass
  103. */
  104. private function parseIni($conf) {
  105. $confObj = new stdClass ();
  106. foreach ( $conf as $k => $v ) {
  107. // 是section
  108. if (is_array ( $v )) {
  109. $confObj->$k = ( object ) $v;
  110. foreach ( $v as $k1 => $v1 ) {
  111. call_user_func ( array (
  112. $this,
  113. 'split' ), $confObj->$k, $k1, $v1 );
  114. }
  115. } else {
  116. call_user_func ( array (
  117. $this,
  118. 'split' ), $confObj, $k, $v );
  119. }
  120. }
  121. unset ( $conf );
  122. // 处理继承
  123. foreach ( $confObj as $k => $v ) {
  124. if (false !== strpos ( $k, ':' )) {
  125. if (0 === strpos ( $k, ':' )) {
  126. throw new ErrorException ( 'config ' . $k . ' is invalid, ':' can't be the first char' );
  127. } elseif (1 < substr_count ( $k, ':' )) {
  128. throw new ErrorException ( 'config ' . $k . ' is invalid, ':' can appear only once' );
  129. } else {
  130. $keys = explode ( ':', $k );
  131. if (! isset ( $confObj->$keys [1] )) {
  132. throw new ErrorException ( 'parent section ' . $keys [1] . ' doesn't exist in config file' );
  133. } else {
  134. if (isset ( $confObj->$keys [0] )) {
  135. throw new ErrorException ( 'config is invalid, ' . $keys [0] . ' and ' . $k . ' conflicts' );
  136. } else {
  137. $confObj->$keys [0] = $this->deepCloneR ( $confObj->$keys [1] );
  138. $this->objectMergeR ( $confObj->$keys [0], $v );
  139. unset ( $confObj->$k );
  140. }
  141. }
  142. }
  143. }
  144. }
  145. return $confObj;
  146. }
  147. /**
  148. * php默认是浅克隆,函数实现深克隆
  149. *
  150. * @param object $obj
  151. * @return object $obj
  152. */
  153. private function deepCloneR($obj) {
  154. $objClone = clone $obj;
  155. foreach ( $objClone as $k => $v ) {
  156. if (is_object ( $v )) {
  157. $objClone->$k = $this->deepCloneR ( $v );
  158. }
  159. }
  160. return $objClone;
  161. }
  162. /**
  163. * 递归的合并两个对象
  164. *
  165. * @param unknown $obj1
  166. * @param unknown $obj2
  167. */
  168. private function objectMergeR($obj1, $obj2) {
  169. foreach ( $obj2 as $k => $v ) {
  170. if (is_object ( $v ) && isset ( $obj1->$k ) && is_object ( $obj1->$k )) {
  171. $this->objectMergeR ( $obj1->$k, $v );
  172. } else {
  173. $obj1->$k = $v;
  174. }
  175. }
  176. }
  177. }

简单使用:

  1. $_ENV ['config'] = new Config ( file_get_contents ( __DIR__ . '/config.ini' ), array (
  2. 'development' => array (
  3. 'localhost.localdomain',
  4. 'localhost'
  5. ),
  6. 'production' => array ()
  7. ) );

配置文件示例:

  1. [product]
  2. db.default.dsn="mysql:host=127.0.0.1;dbname=default"
  3. db.default.username=root
  4. db.default.password=123456
  5. admin.username=admin
  6. admin.password=123456
  7. php.error_reporting=E_ALL
  8. php.display_errors=no
  9. php.log_errors=yes
  10. php.error_log=APP_PATH'/resource/log/phpError.log'
  11. php.session.save_path=APP_PATH'/resource/data/session'
  12. [development:product]
  13. db.test1.dsn="mysql:host=127.0.0.1;dbname=test1"
  14. db.test1.username=root
  15. db.test1.password=123456
  16. php.display_errors=yes