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 ;
29 public function get_instance () {
30 if (self::$instance == NULL) {
31 self::$instance = new FusionForgeConfig () ;
33 return self::$instance ;
36 public function get_value ($section, $var) {
37 if (!isset (self::$instance->settings[$section])
38 || !isset (self::$instance->settings[$section][$var])) {
41 return self::$instance->settings[$section][$var] ;
44 public function set_value ($section, $var, $value) {
45 if (!isset (self::$instance->settings[$section])) {
46 self::$instance->settings[$section] = array () ;
49 if (!isset (self::$instance->settings[$section][$var])) {
50 self::$instance->settings[$section][$var] = $value ;
54 function read_config_file ($file) {
55 if (file_exists($file)) {
56 $sections = parse_ini_file ($file, true) ;
57 if(is_array($sections)) {
58 foreach ($sections as $section => $options) {
59 if (!isset (self::$instance->settings[$section]))
61 foreach ($options as $var => $value) {
62 if (!isset (self::$instance->settings[$section][$var]))
64 self::$instance->settings[$section][$var] = $value ;
74 if (!isset ($fusionforge_config)) {
75 $fusionforge_config = new FusionForgeConfig () ;
78 function forge_get_config ($var, $section = 'core') {
79 $c = FusionForgeConfig::get_instance () ;
80 return $c->get_value ($section, $var) ;
83 function forge_get_config_array () {
84 $c = FusionForgeConfig::get_instance () ;
88 foreach (func_get_args() as $item) {
89 if (! is_array ($item)) {
90 $item = array ($item) ;
93 if (isset ($item[1])) {
98 $ret[] = $c->get_value ($section, $var) ;
104 function forge_set_vars_from_config () {
105 $c = FusionForgeConfig::get_instance () ;
107 foreach (func_get_args() as $item) {
108 if (is_array ($item)) {
111 if (isset ($item[1])) {
112 $section = $item[1] ;
113 $x = $section.'__'.$var ;
114 $value = forge_get_config ($var, $section) ;
119 $value = forge_get_config ($var) ;
128 function forge_define_config_item ($var, $section, $default) {
129 $c = FusionForgeConfig::get_instance () ;
131 return $c->set_value ($section, $var, $default) ;
134 function forge_read_config_file ($file) {
135 $c = FusionForgeConfig::get_instance () ;
137 return $c->read_config_file ($file) ;
142 // c-file-style: "bsd"