Revision
106
Author
emsmith
Date
2007-01-08 08:19:22 -0800 (Mon, 08 Jan 2007)

Log Message

Driver style configuration, extend CC_Config and set CC_Config::$class to the name of the extended class before calling CC_Config::instance() to use

Modified Paths

Added Paths

Diff

Modified: trunk/lib/config.class.php (105 => 106)


--- trunk/lib/config.class.php	2007-01-08 13:54:29 UTC (rev 105)
+++ trunk/lib/config.class.php	2007-01-08 16:19:22 UTC (rev 106)
@@ -1,8 +1,8 @@
 <?php
 /**
- * config.class.php - Callicore configuration and preferences
+ * config.class.php - Callicore configuration abstract class
  *
- * user configuration for each individual program is stored in $appdata/program.config.ini
+ * shoves information into a big array using spl's arrayAccess
  *
  * This is released under the GPL, see docs/gpl.txt for details
  *
@@ -19,70 +19,48 @@
  */
 
 /**
- * CC_Config - reads in an ini file to get configuration settings
- *
- * Writes out to file on destruct
+ * CC_Config - individual drivers should READ in data during construct and
+ * WRITE out data during destruct
  */
-class CC_Config
+abstract class CC_Config implements ArrayAccess
 {
 
 	/**
-	 * singleton instance for this class
+	 * singleton instance for class
 	 * @var $singleton instanceof config
 	 */
 	static protected $singleton;
 
 	/**
+	 * name of config class to use
+	 * @var $class string
+	 */
+	static public $class;
+
+	/**
 	 * multi-dim array of current configuration information
 	 * @var $data array
 	 */
 	protected $data;
 
 	/**
-	 * location of config file
-	 * @var $file string
-	 */
-	protected $file;
-
-	/**
-	 * name of program we're writing for
-	 * @var $program string
-	 */
-	protected $program;
-
-	/**
 	 * public function __construct
 	 *
-	 * Will load in a current config file and optionally create a new one
+	 * should load in stored options
 	 *
 	 * @return void
 	 */
-	protected function __construct()
-	{
+	abstract protected function __construct();
 
-		$this->program = CC::$program;
-		$folders = CC_Os::instance();
-		$this->file = $folders->get_appdata() . strtolower($this->program) . '.config.ini';
-		if (file_exists($this->file))
-		{
-			$this->data = parse_ini_file($this->file);
-		}
-		else
-		{
-			$this->data = array();
-		}
-		return;
-	}
-
 	/**
-	 * public function __get
+	 * public function offsetGet
 	 *
 	 * gets item from data store
 	 *
 	 * @param string $name
 	 * @return mixed
 	 */
-	protected function __get($name)
+	public function offsetGet($name)
 	{
 		if (isset($this->data[$name]))
 		{
@@ -92,20 +70,20 @@
 	}
 
 	/**
-	 * public function __isset
+	 * public function offsetExists
 	 *
 	 * checks if item is set in data store
 	 *
 	 * @param string $name
 	 * @return mixed
 	 */
-	protected function __isset($name)
+	public function offsetExists($name)
 	{
 		return isset($this->data[$name]);
 	}
 
 	/**
-	 * public function __set
+	 * public function offsetSet
 	 *
 	 * sets an item in the data store and writes out the file
 	 *
@@ -113,61 +91,34 @@
 	 * @param mixed $value
 	 * @return void
 	 */
-	protected function __set($name, $value)
+	public function offsetSet($name, $value)
 	{
 		$this->data[$name] = $value;
 		return;
 	}
 
 	/**
-	 * public function __unset
+	 * public function offsetUnset
 	 *
 	 * removes item from the data store and writes out the file
 	 *
 	 * @param string $name
 	 * @return void
 	 */
-	protected function __unset($name)
+	public function offsetUnset($name)
 	{
 		unset ($this->data[$name]);
 		return;
 	}
 
 	/**
-	 * public function __destruct
+	 * protected function __destruct
 	 *
-	 * actually writes the file out
+	 * should actually save the data
 	 *
 	 * @return void
 	 */
-	public function __destruct()
-	{
-		$string = CC::i18n(';Preferences and Configuration for Callicore ') . $this->program
-			. EOL . CC::i18n('; Saved ') . date('Y-m-d H:i:s') . EOL . EOL;
-		foreach ($this->data as $key => $value)
-		{
-			if (is_array($value))
-			{
-				$key = preg_replace('/[' . preg_quote('{}|&~![()"') . ']/', '', $key) . '[]';
-				foreach ($value as $subvalue)
-				{
-					$string .= $key . ' = "' . str_replace('"', '', $subvalue) . '"' . EOL;
-				}
-			}
-			elseif (is_bool($value))
-			{
-				$string .= preg_replace('/[' . preg_quote('{}|&~![()"') . ']/', '', $key)
-					. ' = ' . (($value == true) ? 'TRUE' : 'FALSE') . EOL;
-			}
-			else
-			{
-				$string .= preg_replace('/[' . preg_quote('{}|&~![()"') . ']/', '', $key)
-					. ' = "' . str_replace('"', '', $value) . '"' . EOL;
-			}
-		}
-		file_put_contents($this->file, $string);
-		return;
-	}
+	abstract public function __destruct();
 
 	/**
 	 * static public function instance
@@ -178,9 +129,14 @@
 	 */
 	static public function instance()
 	{
+		if (is_null(self::$class))
+		{
+			throw new CC_Exception('You must set a class name for CC_Config before using');
+		}
+		$class = self::$class;
 		if (is_null(self::$singleton))
 		{
-			self::$singleton = new CC_Config();
+			self::$singleton = new $class();
 		}
 		return self::$singleton;
 	}

Added: trunk/lib/ini.class.php (105 => 106)


--- trunk/lib/ini.class.php	2007-01-08 13:54:29 UTC (rev 105)
+++ trunk/lib/ini.class.php	2007-01-08 16:19:22 UTC (rev 106)
@@ -0,0 +1,94 @@
+<?php
+/**
+ * ini.class.php - ini file backend for callicore storage
+ *
+ * writes out stored user configuration 
+ *
+ * This is released under the GPL, see docs/gpl.txt for details
+ *
+ * @author       Elizabeth Smith <emsmith@callicore.net>
+ * @copyright    Elizabeth Smith (c)2006
+ * @link         http://callicore.net/desktop
+ * @license      http://www.opensource.org/licenses/gpl-license.php GPL
+ * @version      $Id$
+ * @since        Php 5.1.0
+ * @package      callicore
+ * @subpackage   desktop
+ * @category     lib
+ * @filesource
+ */
+
+/**
+ * CC_Ini - reads in an ini file to get configuration settings
+ *
+ * Writes out to file on destruct - each individual program is stored in
+ * $appdata/{$program}.config.ini
+ */
+class CC_Ini extends CC_Config
+{
+
+	/**
+	 * location of config file
+	 * @var $file string
+	 */
+	protected $file;
+
+	/**
+	 * public function __construct
+	 *
+	 * Will load in a current config file and optionally create a new one
+	 *
+	 * @return void
+	 */
+	protected function __construct()
+	{
+
+		$folders = CC_Os::instance();
+		$this->file = $folders->get_appdata() . strtolower(CC::$program) . '.config.ini';
+		if (file_exists($this->file))
+		{
+			$this->data = parse_ini_file($this->file);
+		}
+		else
+		{
+			$this->data = array();
+		}
+		return;
+	}
+
+	/**
+	 * public function __destruct
+	 *
+	 * actually writes the file out
+	 *
+	 * @return void
+	 */
+	public function __destruct()
+	{
+		$string = CC::i18n(';Preferences and Configuration for Callicore ') . CC::$program
+			. EOL . CC::i18n('; Saved ') . date('Y-m-d H:i:s') . EOL . EOL;
+		foreach ($this->data as $key => $value)
+		{
+			if (is_array($value))
+			{
+				$key = preg_replace('/[' . preg_quote('{}|&~![()"') . ']/', '', $key) . '[]';
+				foreach ($value as $subvalue)
+				{
+					$string .= $key . ' = "' . str_replace('"', '', $subvalue) . '"' . EOL;
+				}
+			}
+			elseif (is_bool($value))
+			{
+				$string .= preg_replace('/[' . preg_quote('{}|&~![()"') . ']/', '', $key)
+					. ' = ' . (($value == true) ? 'TRUE' : 'FALSE') . EOL;
+			}
+			else
+			{
+				$string .= preg_replace('/[' . preg_quote('{}|&~![()"') . ']/', '', $key)
+					. ' = "' . str_replace('"', '', $value) . '"' . EOL;
+			}
+		}
+		file_put_contents($this->file, $string);
+		return;
+	}
+}
\ No newline at end of file
Property changes on: trunk/lib/ini.class.php
___________________________________________________________________
Name: tsvn:logminsize
   + 15
Name: svn:keywords
   + Id
Name: svn:eol-style
   + LF

Modified: trunk/lib/window.class.php (105 => 106)


--- trunk/lib/window.class.php	2007-01-08 13:54:29 UTC (rev 105)
+++ trunk/lib/window.class.php	2007-01-08 16:19:22 UTC (rev 106)
@@ -81,6 +81,10 @@
 
 		// size and config restoration cannot be done during show signal
 		$name = $this->name;
+		if (is_null(CC_Config::$class))
+		{
+			CC_Config::$class = 'CC_Ini';
+		}
 		$config = CC_Config::instance();
 
 		// default size and location