[Fusionforge-commits] r9066 - trunk/gforge/common/include

Roland Mas lolando at libremir.placard.fr.eu.org
Mon Mar 15 14:03:35 CET 2010


Author: lolando
Date: 2010-03-15 14:03:35 +0100 (Mon, 15 Mar 2010)
New Revision: 9066

Modified:
   trunk/gforge/common/include/config.php
Log:
Moved code to a singleton class to ease unit testing

Modified: trunk/gforge/common/include/config.php
===================================================================
--- trunk/gforge/common/include/config.php	2010-03-15 13:03:25 UTC (rev 9065)
+++ trunk/gforge/common/include/config.php	2010-03-15 13:03:35 UTC (rev 9066)
@@ -22,48 +22,71 @@
  * USA
  */
 
+class FusionForgeConfig {
+	static private $instance = NULL ;
+	private $settings ;
+    
+	public function get_instance () {
+		if (self::$instance == NULL) {
+			self::$instance = new FusionForgeConfig () ;
+		}
+		return self::$instance ;
+	}
+  
+	public function get_value ($section, $var) {
+		if (!isset ($settings[$section])
+		    || !isset ($settings[$section][$var])) {
+			return NULL ;
+		}
+		return $settings[$section][$var] ;
+	}
+
+	public function set_value ($section, $var, $value) {
+		if (!isset ($settings[$section])) {
+			$settings[$section] = array () ;
+		}
+
+		if (!isset ($settings[$section][$var])) {
+			$settings[$section][$var] = $value ;
+		}
+	}
+
+	function fusionforge_read_config_file ($file) {
+		$sections = parse_ini_file ($file, true) ;
+		foreach ($sections as $sectname => $options) {
+			if (!isset ($settings[$section]))
+				continue ;
+			foreach ($options as $key => $value) {
+				if (!isset ($settings[$section][$var]))
+					continue ;
+				$settings[$sectname][$key] = $value ;
+			}
+		}
+		return ;
+	}
+
+  }
+
 if (!isset ($fusionforge_config)) {
-	$fusionforge_config = array () ;
+	$fusionforge_config = new FusionForgeConfig () ;
 }
 
 function fusionforge_get_config ($section, $var) {
-	global $fusionforge_config ;
+	$c = FusionForgeConfig::get_instance () ;
 
-	if (!isset ($fusionforge_config[$section])
-	    || !isset ($fusionforge_config[$section][$var])) {
-		return false ;
-	}
-	
-	return $fusionforge_config[$section][$var] ;
+	return $c->get_value ($section, $var) ;
 }
 
 function fusionforge_define_config_item ($section, $var, $default) {
-	global $fusionforge_config ;
+	$c = FusionForgeConfig::get_instance () ;
 
-	if (!isset ($fusionforge_config[$section])) {
-		$fusionforge_config[$section] = array () ;
-	}
-
-	if (!isset ($fusionforge_config[$section][$var])) {
-		$fusionforge_config[$section][$var] = $default ;
-	}
+	return $c->set_value ($section, $var, $default) ;
 }
 
 function fusionforge_read_config_file ($file) {
-	global $fusionforge_config ;
+	$c = FusionForgeConfig::get_instance () ;
 
-	$sections = parse_ini_file ($file, true) ;
-	foreach ($sections as $sectname => $options) {
-		if (!isset ($fusionforge_config[$section]))
-			continue ;
-		foreach ($options as $key => $value) {
-			if (!isset ($fusionforge_config[$section][$var]))
-				continue ;
-			$fusionforge_config[$sectname][$key] = $value ;
-		}
-	}
-
-	return ;
+	return $c->read_config_file ($file) ;
 }
 
 // Local Variables:




More information about the Fusionforge-commits mailing list