2 // vim: foldmethod=marker
6 * @author Masaki Fujimoto <fujimoto@php.net>
7 * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
16 * @author Masaki Fujimoto <fujimoto@php.net>
26 /** @var object Ethna_Controller controllerオブジェクト */
29 /** @var array 設定内容 */
36 * Ethna_Configクラスのコンストラクタ
39 * @param object Ethna_Controller &$controller controllerオブジェクト
41 public function __construct(&$controller)
43 $this->controller = $controller;
46 $r = $this->_getConfig();
47 if (Ethna::isError($r)) {
48 // この時点ではlogging等は出来ない(Loggerオブジェクトが生成されていない)
49 $fp = fopen("php://stderr", "r");
50 fputs($fp, sprintf("error occured while reading config file(s) [%s]\n"), $r->getInfo(0));
52 $this->controller->fatal();
60 * @param string $key 設定項目名
63 function get($key = null)
68 if (isset($this->config[$key]) == false) {
71 return $this->config[$key];
78 * @param string $key 設定項目名
79 * @param string $value 設定値
81 function set($key, $value)
83 $this->config[$key] = $value;
90 * @return mixed 0:正常終了 Ethna_Error:エラー
94 return $this->_setConfig();
101 * @return mixed 0:正常終了 Ethna_Error:エラー
103 function _getConfig()
106 $file = $this->_getConfigFile();
107 if (file_exists($file)) {
108 $lh = Ethna_Util::lockFile($file, 'r');
109 if (Ethna::isError($lh)) {
115 Ethna_Util::unlockFile($lh);
119 if (isset($_SERVER['HTTP_HOST']) && isset($config['url']) == false) {
120 $config['url'] = sprintf("http://%s/", $_SERVER['HTTP_HOST']);
122 if (isset($config['dsn']) == false) {
125 if (isset($config['log_facility']) == false) {
126 $config['log_facility'] = "";
128 if (isset($config['log_level']) == false) {
129 $config['log_level'] = "";
131 if (isset($config['log_option']) == false) {
132 $config['log_option'] = "";
135 $this->config = $config;
144 * @return mixed 0:正常終了 Ethna_Error:エラー
146 function _setConfig()
148 $file = $this->_getConfigFile();
150 $lh = Ethna_Util::lockFile($file, 'w');
151 if (Ethna::isError($lh)) {
155 fwrite($lh, "<?php\n");
156 fwrite($lh, sprintf("/*\n * %s\n *\n * update: %s\n */\n", basename($file), strftime('%Y/%m/%d %H:%M:%S')));
157 fwrite($lh, "\$config = array(\n");
158 foreach ($this->config as $key => $value) {
159 $this->_setConfigValue($lh, $key, $value, 0);
163 Ethna_Util::unlockFile($lh);
173 function _setConfigValue($fp, $key, $value, $level)
175 fputs($fp, sprintf("%s'%s' => ", str_repeat(" ", $level+1), $key));
176 if (is_array($value)) {
177 fputs($fp, sprintf("array(\n"));
178 foreach ($value as $k => $v) {
179 $this->_setConfigValue($fp, $k, $v, $level+1);
181 fputs($fp, sprintf("%s),\n", str_repeat(" ", $level+1)));
183 fputs($fp, sprintf("'%s',\n", $value));
191 * @return string 設定ファイルへのフルパス名
193 function _getConfigFile()
195 return $this->controller->getDirectory('etc') . '/' . strtolower($this->controller->getAppId()) . '-ini.php';