OSDN Git Service

remove unneccesary reference
[ethna/ethna.git] / class / Ethna_Config.php
1 <?php
2 // vim: foldmethod=marker
3 /**
4  *  Ethna_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$
10  */
11
12 // {{{ Ethna_Config
13 /**
14  *  設定クラス
15  *
16  *  @author     Masaki Fujimoto <fujimoto@php.net>
17  *  @access     public
18  *  @package    Ethna
19  */
20 class Ethna_Config
21 {
22     /**#@+
23      *  @access private
24      */
25
26     /** @var    object  Ethna_Controller    controllerオブジェクト */
27     var $controller;
28     
29     /** @var    array   設定内容 */
30     var $config = null;
31
32     /**#@-*/
33
34
35     /**
36      *  Ethna_Configクラスのコンストラクタ
37      *
38      *  @access public
39      *  @param  object  Ethna_Controller    &$controller    controllerオブジェクト
40      */
41     public function __construct(&$controller)
42     {
43         $this->controller = $controller;
44
45         // 設定ファイルの読み込み
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));
51             fclose($fp);
52             $this->controller->fatal();
53         }
54     }
55
56     /**
57      *  設定値へのアクセサ(R)
58      *
59      *  @access public
60      *  @param  string  $key    設定項目名
61      *  @return string  設定値
62      */
63     function get($key = null)
64     {
65         if (is_null($key)) {
66             return $this->config;
67         }
68         if (isset($this->config[$key]) == false) {
69             return null;
70         }
71         return $this->config[$key];
72     }
73
74     /**
75      *  設定値へのアクセサ(W)
76      *
77      *  @access public
78      *  @param  string  $key    設定項目名
79      *  @param  string  $value  設定値
80      */
81     function set($key, $value)
82     {
83         $this->config[$key] = $value;
84     }
85
86     /**
87      *  設定ファイルを更新する
88      *
89      *  @access public
90      *  @return mixed   0:正常終了 Ethna_Error:エラー
91      */
92     function update()
93     {
94         return $this->_setConfig();
95     }
96
97     /**
98      *  設定ファイルを読み込む
99      *
100      *  @access private
101      *  @return mixed   0:正常終了 Ethna_Error:エラー
102      */
103     function _getConfig()
104     {
105         $config = array();
106         $file = $this->_getConfigFile();
107         if (file_exists($file)) {
108             $lh = Ethna_Util::lockFile($file, 'r');
109             if (Ethna::isError($lh)) {
110                 return $lh;
111             }
112
113             include($file);
114
115             Ethna_Util::unlockFile($lh);
116         }
117
118         // デフォルト値設定
119         if (isset($_SERVER['HTTP_HOST']) && isset($config['url']) == false) {
120             $config['url'] = sprintf("http://%s/", $_SERVER['HTTP_HOST']);
121         }
122         if (isset($config['dsn']) == false) {
123             $config['dsn'] = "";
124         }
125         if (isset($config['log_facility']) == false) {
126             $config['log_facility'] = "";
127         }
128         if (isset($config['log_level']) == false) {
129             $config['log_level'] = "";
130         }
131         if (isset($config['log_option']) == false) {
132             $config['log_option'] = "";
133         }
134
135         $this->config = $config;
136
137         return 0;
138     }
139
140     /**
141      *  設定ファイルに書き込む
142      *
143      *  @access private
144      *  @return mixed   0:正常終了 Ethna_Error:エラー
145      */
146     function _setConfig()
147     {
148         $file = $this->_getConfigFile();
149
150         $lh = Ethna_Util::lockFile($file, 'w');
151         if (Ethna::isError($lh)) {
152             return $lh;
153         }
154
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);
160         }
161         fwrite($lh, ");\n");
162
163         Ethna_Util::unlockFile($lh);
164
165         return 0;
166     }
167
168     /**
169      *  設定ファイルに設定値を書き込む
170      *
171      *  @access private
172      */
173     function _setConfigValue($fp, $key, $value, $level)
174     {
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);
180             }
181             fputs($fp, sprintf("%s),\n", str_repeat("    ", $level+1)));
182         } else {
183             fputs($fp, sprintf("'%s',\n", $value));
184         }
185     }
186
187     /**
188      *  設定ファイル名を取得する
189      *
190      *  @access private
191      *  @return string  設定ファイルへのフルパス名
192      */
193     function _getConfigFile()
194     {
195         return $this->controller->getDirectory('etc') . '/' . strtolower($this->controller->getAppId()) . '-ini.php';
196     }
197 }
198 // }}}