3 * FusionForge configuration functions
5 * Copyright 2009, Roland Mas
7 * This file is part of FusionForge.
9 * FusionForge is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published
11 * by the Free Software Foundation; either version 2 of the License,
12 * or (at your option) any later version.
14 * FusionForge is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with FusionForge; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
25 class FusionForgeConfig {
26 static protected $instance = NULL ;
28 private $bools = array () ;
30 public function get_instance () {
31 if (self::$instance == NULL) {
32 self::$instance = new FusionForgeConfig () ;
34 return self::$instance ;
37 public function get_sections () {
38 return array_keys ($this->settings) ;
41 public function get_variables ($section='core') {
42 if (isset ($this->settings[$section])) {
43 return array_keys ($this->settings[$section]) ;
48 public function get_value ($section, $var) {
49 if (!isset ($this->settings[$section])
50 || !isset ($this->settings[$section][$var])) {
54 $tmp = $this->settings[$section][$var] ;
55 preg_match_all ('/\$[a-z_]+\/[a-z_]+/', $tmp, $matches) ;
57 foreach ($matches[0] as $m) {
58 $c = explode ('/', substr($m,1)) ;
60 if (isset ($this->settings[$c[0]][$c[1]])) {
61 $tmp = str_replace ($m, $this->get_value($c[0],$c[1]), $tmp) ;
64 if ($this->is_bool ($section, $var)) {
65 $tmp = $this->_interpret_as_bool ($tmp) ;
70 public function get_raw_value ($section, $var) {
71 if (!isset ($this->settings[$section])
72 || !isset ($this->settings[$section][$var])) {
75 return $this->settings[$section][$var] ;
78 public function set_value ($section, $var, $value) {
79 if (!isset ($this->settings[$section])) {
80 $this->settings[$section] = array () ;
83 if (!isset ($this->settings[$section][$var])) {
84 $this->settings[$section][$var] = $value ;
88 function read_config_file ($file) {
89 if (file_exists($file) && is_readable($file)) {
90 $sections = parse_ini_file ($file, true) ;
91 if (is_array($sections)) {
92 foreach ($sections as $section => $options) {
93 foreach ($options as $var => $value) {
94 $this->settings[$section][$var] = $value ;
102 function mark_as_bool ($section, $var) {
103 if (!array_key_exists ($section, $this->bools)) {
104 $this->bools[$section] = array () ;
106 $this->bools[$section][$var] = true ;
109 function is_bool ($section, $var) {
110 if (isset ($this->bools[$section])
111 && isset ($this->bools[$section][$var])) {
112 return $this->bools[$section][$var] ;
117 private function _interpret_as_bool ($val) {
118 $val = strtolower ($val) ;
132 if (!isset ($fusionforge_config)) {
133 $fusionforge_config = new FusionForgeConfig () ;
136 function forge_get_config ($var, $section = 'core') {
137 $c = FusionForgeConfig::get_instance () ;
138 return $c->get_value ($section, $var) ;
141 function forge_get_config_array () {
142 $c = FusionForgeConfig::get_instance () ;
146 foreach (func_get_args() as $item) {
147 if (! is_array ($item)) {
148 $item = array ($item) ;
151 if (isset ($item[1])) {
152 $section = $item[1] ;
156 $ret[] = $c->get_value ($section, $var) ;
162 function forge_set_vars_from_config () {
163 $c = FusionForgeConfig::get_instance () ;
165 foreach (func_get_args() as $item) {
166 if (is_array ($item)) {
169 if (isset ($item[1])) {
170 $section = $item[1] ;
171 $x = $section.'__'.$var ;
172 $value = forge_get_config ($var, $section) ;
177 $value = forge_get_config ($var) ;
186 function forge_define_config_item ($var, $section, $default) {
187 $c = FusionForgeConfig::get_instance () ;
189 return $c->set_value ($section, $var, $default) ;
192 function forge_set_config_item_bool ($var, $section) {
193 $c = FusionForgeConfig::get_instance () ;
195 return $c->mark_as_bool ($section, $var) ;
198 function forge_read_config_file ($file) {
199 $c = FusionForgeConfig::get_instance () ;
201 return $c->read_config_file ($file) ;
204 function forge_read_config_dir ($path) {
205 $c = FusionForgeConfig::get_instance () ;
210 if ($handle = opendir($path)) {
211 while (false !== ($file = readdir($handle))) {
214 // Avoid .bak, .old, .dpkg-old and so on, but keep .ini
215 && preg_match ('/^[0-9a-zA-Z_-]+(.ini)?$/', $file)) {
216 $files[] = "$path/$file" ;
224 foreach ($files as $file) {
225 $c->read_config_file ($file) ;
231 // c-file-style: "bsd"