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 $sections = parse_ini_file ($file, true) ;
56 foreach ($sections as $section => $options) {
57 if (!isset (self::$instance->settings[$section]))
59 foreach ($options as $var => $value) {
60 if (!isset (self::$instance->settings[$section][$var]))
62 self::$instance->settings[$section][$var] = $value ;
70 if (!isset ($fusionforge_config)) {
71 $fusionforge_config = new FusionForgeConfig () ;
74 function forge_get_config ($var, $section = 'core') {
75 $c = FusionForgeConfig::get_instance () ;
76 return $c->get_value ($section, $var) ;
79 function forge_get_config_array () {
80 $c = FusionForgeConfig::get_instance () ;
84 foreach (func_get_args() as $item) {
85 if (! is_array ($item)) {
86 $item = array ($item) ;
89 if (isset ($item[1])) {
94 $ret[] = $c->get_value ($section, $var) ;
100 function forge_set_vars_from_config () {
101 $c = FusionForgeConfig::get_instance () ;
103 foreach (func_get_args() as $item) {
105 if (is_array ($item)) {
108 if (isset ($item[1])) {
109 $section = $item[1] ;
110 $x = $section.'__'.$var ;
118 $$x = forge_get_config ($var, $section) ;
123 function forge_define_config_item ($var, $section, $default) {
124 $c = FusionForgeConfig::get_instance () ;
126 return $c->set_value ($section, $var, $default) ;
129 function forge_read_config_file ($file) {
130 $c = FusionForgeConfig::get_instance () ;
132 return $c->read_config_file ($file) ;
137 // c-file-style: "bsd"