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 class FusionForgeConfig {
24 static protected $instance = NULL ;
26 private $bools = array () ;
28 static public function get_instance () {
29 if (self::$instance == NULL) {
30 self::$instance = new FusionForgeConfig () ;
32 return self::$instance ;
35 public function get_sections () {
36 return array_keys ($this->settings) ;
39 public function get_variables ($section='core') {
40 if (isset ($this->settings[$section])) {
41 return array_keys ($this->settings[$section]) ;
46 public function get_value ($section, $var) {
47 if (!isset ($this->settings[$section])
48 || !isset ($this->settings[$section][$var])) {
52 $tmp = $this->settings[$section][$var] ;
53 preg_match_all ('/\$[a-z_]+\/[a-z_]+/', $tmp, $matches) ;
55 foreach ($matches[0] as $m) {
56 $c = explode ('/', substr($m,1)) ;
58 if (isset ($this->settings[$c[0]][$c[1]])) {
59 $tmp = str_replace ($m, $this->get_value($c[0],$c[1]), $tmp) ;
62 if ($this->is_bool ($section, $var)) {
63 $tmp = $this->_interpret_as_bool ($tmp) ;
68 public function get_raw_value ($section, $var) {
69 if (!isset ($this->settings[$section])
70 || !isset ($this->settings[$section][$var])) {
73 return $this->settings[$section][$var] ;
76 public function set_value ($section, $var, $value) {
77 if (!isset ($this->settings[$section])) {
78 $this->settings[$section] = array () ;
81 if (!isset ($this->settings[$section][$var])) {
82 $this->settings[$section][$var] = $value ;
86 public function reset_value ($section, $var, $value) {
87 if (!isset ($this->settings[$section])) {
88 $this->settings[$section] = array () ;
91 $this->settings[$section][$var] = $value ;
94 function read_config_file ($file) {
95 if (getenv ('FUSIONFORGE_USE_PRE_51_CONFIG') == 'true') {
96 $fallback_only = true;
98 $fallback_only = false;
101 if (file_exists($file) && is_readable($file)) {
102 $sections = parse_ini_file ($file, true) ;
103 if (is_array($sections)) {
104 foreach ($sections as $section => $options) {
105 foreach ($options as $var => $value) {
106 if ($fallback_only) {
107 $this->set_value($section,$var,$value);
109 $this->reset_value($section,$var,$value);
118 function mark_as_bool ($section, $var) {
119 if (!array_key_exists ($section, $this->bools)) {
120 $this->bools[$section] = array () ;
122 $this->bools[$section][$var] = true ;
125 function is_bool ($section, $var) {
126 if (isset ($this->bools[$section])
127 && isset ($this->bools[$section][$var])) {
128 return $this->bools[$section][$var] ;
133 private function _interpret_as_bool ($val) {
134 $val = strtolower ($val) ;
148 if (!isset ($fusionforge_config)) {
149 $fusionforge_config = new FusionForgeConfig () ;
152 function forge_get_config ($var, $section = 'core') {
153 $c = FusionForgeConfig::get_instance () ;
154 return $c->get_value ($section, $var) ;
157 function forge_get_config_array () {
158 $c = FusionForgeConfig::get_instance () ;
162 foreach (func_get_args() as $item) {
163 if (! is_array ($item)) {
164 $item = array ($item) ;
167 if (isset ($item[1])) {
168 $section = $item[1] ;
172 $ret[] = $c->get_value ($section, $var) ;
178 function forge_set_vars_from_config () {
179 $c = FusionForgeConfig::get_instance () ;
181 foreach (func_get_args() as $item) {
182 if (is_array ($item)) {
185 if (isset ($item[1])) {
186 $section = $item[1] ;
187 $x = $section.'__'.$var ;
188 $value = forge_get_config ($var, $section) ;
193 $value = forge_get_config ($var) ;
202 function forge_define_config_item ($var, $section, $default) {
203 $c = FusionForgeConfig::get_instance () ;
205 return $c->set_value ($section, $var, $default) ;
208 function forge_set_config_item_bool ($var, $section) {
209 $c = FusionForgeConfig::get_instance () ;
211 return $c->mark_as_bool ($section, $var) ;
214 function forge_read_config_file ($file) {
215 $c = FusionForgeConfig::get_instance () ;
217 return $c->read_config_file ($file) ;
220 function forge_read_config_dir ($path) {
221 $c = FusionForgeConfig::get_instance () ;
226 if ($handle = opendir($path)) {
227 while (false !== ($file = readdir($handle))) {
230 // Avoid .bak, .old, .dpkg-old and so on, but keep .ini
231 && preg_match ('/^[0-9a-zA-Z_-]+(.ini)?$/', $file)) {
232 $files[] = "$path/$file" ;
240 foreach ($files as $file) {
241 $c->read_config_file ($file) ;
245 function forge_reset_config_item ($var, $section, $default) {
246 $c = FusionForgeConfig::get_instance () ;
248 return $c->reset_value ($section, $var, $default) ;
253 // c-file-style: "bsd"