/class/Config.php

https://github.com/bobpp/ethna · PHP · 198 lines · 99 code · 23 blank · 76 comment · 21 complexity · 6b34a0c94b49c27ff5b92f82e8ce484a MD5 · raw file

  1. <?php
  2. // vim: foldmethod=marker
  3. /**
  4. * Config.php
  5. *
  6. * @author Masaki Fujimoto <fujimoto@php.net>
  7. * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
  8. * @package Ethna
  9. * @version $Id: 0ebeba1b7190c9f5f30b6c09704ddb167214b675 $
  10. */
  11. // {{{ Ethna_Config
  12. /**
  13. * 設定クラス
  14. *
  15. * @author Masaki Fujimoto <fujimoto@php.net>
  16. * @access public
  17. * @package Ethna
  18. */
  19. class Ethna_Config
  20. {
  21. /**#@+
  22. * @access private
  23. */
  24. /** @var object Ethna_Controller controllerオブジェクト */
  25. var $controller;
  26. /** @var array 設定内容 */
  27. var $config = null;
  28. /**#@-*/
  29. /**
  30. * Ethna_Configクラスのコンストラクタ
  31. *
  32. * @access public
  33. * @param object Ethna_Controller &$controller controllerオブジェクト
  34. */
  35. public function __construct(&$controller)
  36. {
  37. $this->controller = $controller;
  38. // 設定ファイルの読み込み
  39. $r = $this->_getConfig();
  40. if (Ethna::isError($r)) {
  41. // この時点ではlogging等は出来ない(Loggerオブジェクトが生成されていない)
  42. $fp = fopen("php://stderr", "r");
  43. fputs($fp, sprintf("error occured while reading config file(s) [%s]\n"), $r->getInfo(0));
  44. fclose($fp);
  45. $this->controller->fatal();
  46. }
  47. }
  48. /**
  49. * 設定値へのアクセサ(R)
  50. *
  51. * @access public
  52. * @param string $key 設定項目名
  53. * @return string 設定値
  54. */
  55. function get($key = null)
  56. {
  57. if (is_null($key)) {
  58. return $this->config;
  59. }
  60. if (isset($this->config[$key]) == false) {
  61. return null;
  62. }
  63. return $this->config[$key];
  64. }
  65. /**
  66. * 設定値へのアクセサ(W)
  67. *
  68. * @access public
  69. * @param string $key 設定項目名
  70. * @param string $value 設定値
  71. */
  72. function set($key, $value)
  73. {
  74. $this->config[$key] = $value;
  75. }
  76. /**
  77. * 設定ファイルを更新する
  78. *
  79. * @access public
  80. * @return mixed 0:正常終了 Ethna_Error:エラー
  81. */
  82. function update()
  83. {
  84. return $this->_setConfig();
  85. }
  86. /**
  87. * 設定ファイルを読み込む
  88. *
  89. * @access private
  90. * @return mixed 0:正常終了 Ethna_Error:エラー
  91. */
  92. function _getConfig()
  93. {
  94. $config = array();
  95. $file = $this->_getConfigFile();
  96. if (file_exists($file)) {
  97. $lh = Ethna_Util::lockFile($file, 'r');
  98. if (Ethna::isError($lh)) {
  99. return $lh;
  100. }
  101. include($file);
  102. Ethna_Util::unlockFile($lh);
  103. }
  104. // デフォルト値設定
  105. if (isset($_SERVER['HTTP_HOST']) && isset($config['url']) == false) {
  106. $config['url'] = sprintf("http://%s/", $_SERVER['HTTP_HOST']);
  107. }
  108. if (isset($config['dsn']) == false) {
  109. $config['dsn'] = "";
  110. }
  111. if (isset($config['log_facility']) == false) {
  112. $config['log_facility'] = "";
  113. }
  114. if (isset($config['log_level']) == false) {
  115. $config['log_level'] = "";
  116. }
  117. if (isset($config['log_option']) == false) {
  118. $config['log_option'] = "";
  119. }
  120. $this->config = $config;
  121. return 0;
  122. }
  123. /**
  124. * 設定ファイルに書き込む
  125. *
  126. * @access private
  127. * @return mixed 0:正常終了 Ethna_Error:エラー
  128. */
  129. function _setConfig()
  130. {
  131. $file = $this->_getConfigFile();
  132. $lh = Ethna_Util::lockFile($file, 'w');
  133. if (Ethna::isError($lh)) {
  134. return $lh;
  135. }
  136. fwrite($lh, "<?php\n");
  137. fwrite($lh, sprintf("/*\n * %s\n *\n * update: %s\n */\n", basename($file), strftime('%Y/%m/%d %H:%M:%S')));
  138. fwrite($lh, "\$config = array(\n");
  139. foreach ($this->config as $key => $value) {
  140. $this->_setConfigValue($lh, $key, $value, 0);
  141. }
  142. fwrite($lh, ");\n");
  143. Ethna_Util::unlockFile($lh);
  144. return 0;
  145. }
  146. /**
  147. * 設定ファイルに設定値を書き込む
  148. *
  149. * @access private
  150. */
  151. function _setConfigValue($fp, $key, $value, $level)
  152. {
  153. fputs($fp, sprintf("%s'%s' => ", str_repeat(" ", $level+1), $key));
  154. if (is_array($value)) {
  155. fputs($fp, sprintf("array(\n"));
  156. foreach ($value as $k => $v) {
  157. $this->_setConfigValue($fp, $k, $v, $level+1);
  158. }
  159. fputs($fp, sprintf("%s),\n", str_repeat(" ", $level+1)));
  160. } else {
  161. fputs($fp, sprintf("'%s',\n", $value));
  162. }
  163. }
  164. /**
  165. * 設定ファイル名を取得する
  166. *
  167. * @access private
  168. * @return string 設定ファイルへのフルパス名
  169. */
  170. function _getConfigFile()
  171. {
  172. return $this->controller->getDirectory('etc') . '/' . strtolower($this->controller->getAppId()) . '-ini.php';
  173. }
  174. }
  175. // }}}