3 * FusionForge configuration functions
5 * Copyright 2009, Roland Mas
7 * This file is part of FusionForge. FusionForge is free software;
8 * you can redistribute it and/or modify it under the terms of the
9 * GNU General Public License as published by the Free Software
10 * Foundation; either version 2 of the Licence, or (at your option)
13 * FusionForge is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with FusionForge; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 // See docs in http://fusionforge.org/plugins/mediawiki/wiki/fusionforge/index.php/Configuration
27 * Singleton FusionForge configuration database manager TODO : Enter better description here ...
30 class FusionForgeConfig {
31 static protected $instance = NULL ;
33 private $bools = array () ;
36 * Singleton accessor to the configuration database
37 * @return FusionForgeConfig instance
39 static public function get_instance () {
40 if (self::$instance == NULL) {
41 self::$instance = new FusionForgeConfig () ;
43 return self::$instance ;
46 // TODO: add a constructor that initializes self::$instance to self ?
48 public function get_sections () {
49 return array_keys ($this->settings) ;
52 public function get_variables ($section='core') {
53 if (isset ($this->settings[$section])) {
54 return array_keys ($this->settings[$section]) ;
59 public function get_value ($section, $var) {
60 if (!isset ($this->settings[$section])
61 || !isset ($this->settings[$section][$var])) {
65 $tmp = $this->settings[$section][$var] ;
66 preg_match_all ('/\$[a-z_]+\/[a-z_]+/', $tmp, $matches) ;
68 foreach ($matches[0] as $m) {
69 $c = explode ('/', substr($m,1)) ;
71 if (isset ($this->settings[$c[0]][$c[1]])) {
72 $tmp = str_replace ($m, $this->get_value($c[0],$c[1]), $tmp) ;
75 if ($this->is_bool ($section, $var)) {
76 $tmp = $this->_interpret_as_bool ($tmp) ;
81 public function get_raw_value ($section, $var) {
82 if (!isset ($this->settings[$section])
83 || !isset ($this->settings[$section][$var])) {
86 return $this->settings[$section][$var] ;
89 public function set_value ($section, $var, $value) {
90 if (!isset ($this->settings[$section])) {
91 $this->settings[$section] = array () ;
94 if (!isset ($this->settings[$section][$var])) {
95 $this->settings[$section][$var] = $value ;
99 function read_config_file ($filename) {
100 if (file_exists($filename) && is_readable($filename)) {
101 $sections = parse_ini_file ($filename, true) ;
102 if (is_array($sections)) {
103 foreach ($sections as $section => $options) {
104 foreach ($options as $var => $value) {
105 $this->settings[$section][$var] = $value ;
113 function mark_as_bool ($section, $var) {
114 if (!array_key_exists ($section, $this->bools)) {
115 $this->bools[$section] = array () ;
117 $this->bools[$section][$var] = true ;
120 function is_bool ($section, $var) {
121 if (isset ($this->bools[$section])
122 && isset ($this->bools[$section][$var])) {
123 return $this->bools[$section][$var] ;
128 private function _interpret_as_bool ($val) {
129 $val = strtolower ($val) ;
143 if (!isset ($fusionforge_config)) {
144 $fusionforge_config = new FusionForgeConfig () ;
148 * Get value of variable "name" in section "section"
150 * @param string $section defaults to "core" if missing
151 * @return Ambigous <NULL, boolean>
153 function forge_get_config ($var, $section = 'core') {
154 $c = FusionForgeConfig::get_instance () ;
155 return $c->get_value ($section, $var) ;
158 function forge_get_config_array () {
159 $c = FusionForgeConfig::get_instance () ;
163 foreach (func_get_args() as $item) {
164 if (! is_array ($item)) {
165 $item = array ($item) ;
168 if (isset ($item[1])) {
169 $section = $item[1] ;
173 $ret[] = $c->get_value ($section, $var) ;
179 function forge_set_vars_from_config () {
180 $c = FusionForgeConfig::get_instance () ;
182 foreach (func_get_args() as $item) {
183 if (is_array ($item)) {
186 if (isset ($item[1])) {
187 $section = $item[1] ;
188 $x = $section.'__'.$var ;
189 $value = forge_get_config ($var, $section) ;
194 $value = forge_get_config ($var) ;
204 * Define a new configuration item with given name/section and default value
205 * @param string $var name
206 * @param string $section
207 * @param any $default
209 function forge_define_config_item ($var, $section, $default) {
210 $c = FusionForgeConfig::get_instance () ;
212 return $c->set_value ($section, $var, $default) ;
216 * Tag the variable as boolean, which allows human-readable values in the configuration files (such as yes, true, on and 1; anything else is mapped to false)
217 * @param string $var name
218 * @param string $section
220 function forge_set_config_item_bool ($var, $section) {
221 $c = FusionForgeConfig::get_instance () ;
223 return $c->mark_as_bool ($section, $var) ;
227 * Read a *.ini file and inject its contents into the configuration database
228 * @param string $file
230 function forge_read_config_file ($filename) {
231 $c = FusionForgeConfig::get_instance () ;
233 return $c->read_config_file ($filename) ;
237 * Read all configuration files in a directory
238 * @param string $path
240 function forge_read_config_dir ($path) {
241 $c = FusionForgeConfig::get_instance () ;
246 if ($handle = opendir($path)) {
247 while (false !== ($file = readdir($handle))) {
250 // Avoid .bak, .old, .dpkg-old and so on, but keep .ini
251 && preg_match ('/^[0-9a-zA-Z_-]+(.ini)?$/', $file)) {
252 $files[] = "$path/$file" ;
260 foreach ($files as $file) {
261 $c->read_config_file ($file) ;
267 // c-file-style: "bsd"