Revision
153
Author
emsmith
Date
2007-07-04 15:44:54 -0700 (Wed, 04 Jul 2007)

Log Message

Part one of great rearrange

Modified Paths

Added Paths

Diff

Copied: trunk/callicore/lib (from rev 152, trunk/lib) ( => )


Modified: trunk/callicore/lib/cc.class.php
===================================================================
--- trunk/lib/cc.class.php	2007-06-13 13:31:27 UTC (rev 152)
+++ trunk/callicore/lib/cc.class.php	2007-07-04 22:44:54 UTC (rev 153)
@@ -2,7 +2,7 @@
 /**
  * cc.class.php - callicore's base class
  *
- * base class to set up framework and additional items
+ * base class for callicore, acts as controller for entire program
  *
  * This is released under the GPL, see docs/gpl.txt for details
  *
@@ -13,23 +13,23 @@
  * @version      $Id$
  * @since        Php 5.2.0
  * @package      callicore
- * @subpackage   core
+ * @subpackage   callicore
  * @category     lib
  * @filesource
  */
 
 /**
- * CC - checks settings and manages common properties
+ * CC - checks settings and manages common properties, runs program
  *
- * Basically a wrapper for important callicore functions
- * STATIC ONLY
+ * Includes several important static functions, and sets up the base callicore
+ * program information before running it
  */
 class CC
 {
 	/**
-	 * @const VERSION app version number
+	 * @const API_VERSION callicore app version number
 	 */
-	const VERSION = '1.0.0-alpha';
+	const API_VERSION = '1.0.0-alpha';
 
 	/**
 	 * Icon sizes - these SHOULD be constants - but
@@ -50,60 +50,56 @@
 	static public $IMAGE;
 
 	/**
+	 * @var string charset for program
+	 */
+	protected $charset;
+
+	/**
+	 * @var string locale to use for program
+	 */
+	protected $locale;
+
+	/**
 	 * @var string program to run
 	 */
-	static public $program;
+	protected $program;
 
 	/**
-	 * @var string root callicore dir
+	 * @var string program dir
 	 */
-	static public $dir;
+	protected $dir;
 
 	/**
-	 * public function __construct
-	 *
-	 * forces only static calls
-	 *
-	 * @return void
+	 * @var string callicore dir
 	 */
-	public function __construct()
-	{
-		throw new CC_Exception('CC contains only static methods and cannot be constructed');
-	}
+	protected $cc;
 
 	/**
-	 * static public function run
+	 * @var string configuration class to use
+	 */
+	protected $config = 'CC_Config_Ini';
+
+	/**
+	 * @var bool is this in production mode
+	 */
+	protected $production = true;
+
+	/**
+	 * public function __construct
 	 *
-	 * checks for requirements, figures out the right program to run, registers
-	 * base autoload, and does basic theme setup
+	 * sets up a specific program in order to run it
 	 *
-	 * @param string $program 
 	 * @return void
 	 */
-	static public function run($program)
+	public function __construct($program)
 	{
-		// global defines for the duration of the program
-		// I got tired of typing DIRECTORY_SEPARATOR all the time
-		define('DS', DIRECTORY_SEPARATOR, true);
-		define('EOL', PHP_EOL, true);
-
-		// CC root - whack off one dir
-		self::$dir = dirname((dirname(__FILE__))) . DS;
-		self::$program = ucfirst(strtolower($program));
-
-		// set up translation
-		if (extension_loaded('gettext'))
-		{
-			// we use system locale
-			bindtextdomain('Callicore', self::$dir . 'locale');
-			textdomain('Callicore');
-		}
 		if (version_compare(PHP_VERSION, '5.2.0', '<'))
 		{
-			throw new CC_Exception('You must use php 5.2.0 or higher');
+			throw new Exception('You must use php 5.2.0 or higher');
 		}
+
 		$have = get_loaded_extensions();
-		$needed = array('standard', 'pcre', 'date', 'Reflection', 'tokenizer', 'SPL', 'php-gtk');
+		$needed = array('standard', 'pcre', 'date', 'Reflection', 'tokenizer', 'SPL', 'php-gtk', 'gettext', 'iconv');
 		if (stristr(PHP_OS, 'win32'))
 		{
 			$needed[] = 'com';
@@ -111,100 +107,87 @@
 		$diff = array_diff($needed, $have);
 		if (!empty($diff))
 		{
-			throw new CC_Exception('%s : %s', 'The following extensions must be present - either built into to php or loaded via your php.ini - for Callicore to function', implode(', ', $diff));
+			throw new Exception('The following extensions must be present - either built into to php or loaded via your php.ini - for Callicore to function: ' . implode(', ', $diff));
 		}
 
+		error_reporting(E_ALL | E_STRICT);
+		define('DS', DIRECTORY_SEPARATOR, true);
+		define('EOL', PHP_EOL, true);
+
 		self::$LARGE = Gtk::icon_size_register('gtk-large',64, 64);
 		self::$IMAGE = Gtk::icon_size_register('gtk-image',128, 128);
-		Gtk::rc_parse(self::$dir . 'share' . DS . 'images' . DS . 'stock.rc');
-		Gtk::rc_parse(self::$dir . 'share' . DS . 'images' . DS . 'callicore.rc');
 
-		spl_autoload_register(array(__CLASS__, 'autoload'));
+		$this->cc = realpath(dirname(__FILE__) . '/../') . DS;
+		$this->program = strtolower($program);
+		$this->dir = realpath(dirname(__FILE__) . '/../../') . DS . $this->program . DS;
+		ini_set('display_errors', false);
+		ini_set('log_errors', true);
 
-		$class = 'CC_' . self::$program;
-		new $class();
-		return;
+		spl_autoload_register(array($this, 'autoload'));
+		$this->charset = ini_get('php-gtk.codepage');
+		$this->locale = setlocale(LC_ALL, null);
+
+		// load in images
+		Gtk::rc_parse($this->cc . 'share' . DS . 'stock.rc');
+		Gtk::rc_parse($this->cc . 'share' . DS. 'callicore.rc');
 	}
 
 	/**
-	 * static public function ext
+	 * public function __set
 	 *
-	 * checks for and attempts to load a php extension
-	 * if require is true die with an error message
+	 * interceptor for protected itesm
 	 *
-	 * @param string $ext extension to load
+	 * @param string $name name of variable to set
+	 * @param mixed $value value to set it to
 	 * @return void
 	 */
-	static public function ext($ext)
+	public function __set($name, $value)
 	{
-		// is the extension loaded?
-		if(extension_loaded($ext))
+		switch($name)
 		{
-			return true;
+			case 'production':
+				$this->production = (bool) $value;
+				if ($this->production == true)
+				{
+					ini_set('display_errors', false);
+					ini_set('log_errors', true);
+				}
+				else
+				{
+					ini_set('display_errors', true);
+				}
+				break;
+			case 'program':
+				$this->program = strtolower($value);
+				break;
+			case 'dir':
+				if (file_exists($value) && is_dir($value))
+				{
+					$this->dir = realpath($value) . DS;
+				}
+				break;
+			case 'charset':
+				$this->charset = $value;
+				ini_set('php-gtk.codepage', $value);
+				break;
+			case 'locale':
+				$this->locale = setlocale('LC_ALL', $value);
+				break;
+			case 'icon':
+				$this->icon($value);
+				break;
 		}
-		// let's try to dl it
-		if((bool)ini_get('enable_dl') && !(bool)ini_get('safe_mode'))
-		{
-			// get absolute path to dl
-			$path = realpath(ini_get('extension_dir'));
-			// we can't rely on PHP_SHLIB_SUFFIX because it screws up on MAC
-			if(stristr(PHP_OS, 'win') && file_exists($path . DS . 'php_' . $ext . '.dll')
-				&& dl($path . DS . 'php_' . $ext . '.dll'))
-			{
-				return true;
-			}
-			elseif(file_exists($path . DS . $ext . '.so') && dl($path . DS . $ext . '.so'))
-			{
-				return true;
-			}
-		}
-		return false;
 	}
 
-	//----------------------------------------------------------------
-	//             static helper functions
-	//----------------------------------------------------------------
-
 	/**
-	 * public static function i18n
+	 * protected function icon
 	 *
-	 * wrapper for gettext + sprintf/vsprintf
-	 *
-	 * @param string $string string to translate
-	 * @return string translated string
-	 */
-	public static function i18n($string)
-	{
-		$args = func_get_args();
-		array_shift($args);
-		if (!empty($args) && count($args) == 1 && is_array($args[0]))
-		{
-			$args = $args[0];
-		}
-		if (function_exists('gettext'))
-		{
-			// if we have args, the first item is format only and not translated
-			if (is_array($args) && !empty($args))
-			{
-				$args = array_map('gettext', $args);
-			}
-			else
-			{
-				$string = gettext($string);
-			}
-		}
-		return vsprintf($string, $args);
-	}
-
-	/**
-	 * public static function icon
-	 *
 	 * uses add_builtin_icon and render_icon to make set_default_icon_name work
 	 *
 	 * @param string $icon icon name to use as default
 	 * @return void
 	 */
-	public static function icon($icon)
+	protected function icon($icon)
 	{
 		$theme = GtkIconTheme::get_for_screen(GdkScreen::get_default());
 		$window = new GtkWindow();
@@ -226,12 +209,14 @@
 		$theme->add_builtin_icon($icon, self::$IMAGE,
 			$window->render_icon($icon, self::$IMAGE));
 
+		unset($theme, $window);
+
 		GtkWindow::set_default_icon_name($icon);
 		return;
 	}
 
 	/**
-	 * static public function autoload
+	 * public function autoload
 	 *
 	 * if you have a program class the same name as a base class the program
 	 * one will always be loaded first (as long as it wasn't previously loaded)
@@ -240,36 +225,140 @@
 	 * @param string $class class to include
 	 * @return bool
 	 */
-	static public function autoload($class)
+	public function autoload($class)
 	{
-		preg_match_all('/[A-Z][a-z0-9_]*/', str_replace('CC_', '', $class), $matches);
-		$array = array_map('strtolower', $matches[0]);
+		$array = explode('_', strtolower($class));
+		if (array_shift($array) !== 'cc')
+		{
+			return false;
+		}
+		// create filename
 		$file = array_pop($array) . '.class.php';
-		if (!empty($array))
+		$path = implode(DS, $array) . DS;
+		if (file_exists($this->dir . 'lib' . DS . $path . $file))
 		{
-			$array[] = '';
+			include $this->dir . 'lib' . DS . $path . $file;
+			return true;
 		}
+		elseif (file_exists($this->cc . 'lib' . DS . $path . $file))
+		{
+			include $this->cc . 'lib' . DS . $path . $file;
+			return true;
+		}
+		else
+		{
+			echo $this->dir . $path . $file . ' AND ' . $this->cc . 'lib' . DS . $path . $file;
+			return false;
+		}
+	}
 
-		$program = self::$dir . 'programs' . DS . strtolower(self::$program) . DS . 'lib' . DS . implode(DS, $array) . $file;
-		$lib = self::$dir . 'lib' . DS . implode(DS, $array) . $file;
+	/**
+	 * public function run
+	 *
+	 * checks for requirements, figures out the right program to run, registers
+	 * base autoload, and does basic theme setup
+	 *
+	 * @param string $program 
+	 * @return void
+	 */
+	public function run()
+	{
+		// setup locale
+		bindtextdomain('cc_' . $this->program, $this->dir . 'locale');
+		textdomain('cc_' . $this->program);
 
-		if (file_exists($program))
+		// load program configuration
+		$config = CC_Config::instance($this->config);
+
+		// start program
+		$program = 'CC_' . ucfirst($this->program);
+		$program = new $program;
+
+		// check for autoupdate
+		if (isset($config['autoupdate']))
 		{
-			include $program;
-			$return = true;
+			throw new Exception('Autoupdate not yet implemented');
 		}
-		elseif (file_exists($lib))
+
+		// load plugins
+		if (isset($config['plugins']))
 		{
-			include $lib;
-			$return = true;
+			throw new Exception('Plugin system not yet implemented');
 		}
-		else
+
+		$program->start();
+		return;
+	}
+
+	//----------------------------------------------------------------
+	//             static helper functions
+	//----------------------------------------------------------------
+
+	/**
+	 * static public function ext
+	 *
+	 * checks for and attempts to load a php extension
+	 *
+	 * @param string $ext extension to load
+	 * @return bool
+	 */
+	static public function ext($ext)
+	{
+		// is the extension loaded?
+		if(extension_loaded($ext))
 		{
-			echo $program;
-			$return = false;
+			return true;
 		}
-		return $return;
+		// let's try to dl it
+		if((bool)ini_get('enable_dl') && !(bool)ini_get('safe_mode'))
+		{
+			// get absolute path to dl
+			$path = realpath(ini_get('extension_dir'));
+			// we can't rely on PHP_SHLIB_SUFFIX because it screws up on MAC
+			if(stristr(PHP_OS, 'win') && file_exists($path . DS . 'php_' . $ext . '.dll')
+				&& dl($path . DS . 'php_' . $ext . '.dll'))
+			{
+				return true;
+			}
+			elseif(file_exists($path . DS . $ext . '.so') && dl($path . DS . $ext . '.so'))
+			{
+				return true;
+			}
+		}
+		return false;
 	}
+
+	/**
+	 * public static function i18n
+	 *
+	 * wrapper for gettext + sprintf/vsprintf
+	 *
+	 * @param string $string string to translate
+	 * @return string translated string
+	 */
+	public static function i18n($string)
+	{
+		$args = func_get_args();
+		array_shift($args);
+		if (!empty($args) && count($args) == 1 && is_array($args[0]))
+		{
+			$args = $args[0];
+		}
+		return vsprintf(gettext($string), $args);
+	}
+
+	/**
+	 * public static function convert
+	 *
+	 * uses iconv to convert a string to the program charset
+	 *
+	 * @param string $string string to translate
+	 * @return string translated string
+	 */
+	public static function convert($string, $charset)
+	{
+		return iconv($charset, $this->charset.'//TRANSLIT', $string);
+	}
 }
 
 /**

Modified: trunk/callicore/lib/config.class.php (152 => 153)


--- trunk/lib/config.class.php	2007-06-13 13:31:27 UTC (rev 152)
+++ trunk/callicore/lib/config.class.php	2007-07-04 22:44:54 UTC (rev 153)
@@ -22,7 +22,7 @@
  * CC_Config - individual drivers should READ in data during construct and
  * WRITE out data during destruct
  */
-abstract class CC_Config
+abstract class CC_Config extends ArrayObject
 {
 
 	/**
@@ -32,90 +32,36 @@
 	static protected $singleton;
 
 	/**
-	 * name of config class to use
-	 * @var $class string
+	 * make sure constructor can ONLY be called by instance
+	 * @var $check bool
 	 */
-	static public $class;
+	static protected $check;
 
 	/**
 	 * multi-dim object of current configuration information
 	 * @var $data object
 	 */
-	protected $data;
+	protected $data = array();
 
 	/**
 	 * public function __construct
 	 *
-	 * should load in stored options
+	 * extending class should load in stored options
 	 *
 	 * @return void
 	 */
-	abstract protected function __construct();
-
-	/**
-	 * public function __get
-	 *
-	 * gets item from data store
-	 *
-	 * @param string $name
-	 * @return mixed
-	 */
-	public function __get($name)
+	public function __construct()
 	{
-		if (isset($this->data->$name))
+		// pretend this is a protected method - throw the same error php does
+		if (self::$check == false)
 		{
-			return $this->data->$name;
+			trigger_error('Call to protected ' . __METHOD__ . ' from invalid context', E_USER_ERROR);
 		}
-		return;
-	}
 
-	/**
-	 * public function __isset
-	 *
-	 * checks if item is set in data store
-	 *
-	 * @param string $name
-	 * @return mixed
-	 */
-	public function __isset($name)
-	{
-		return isset($this->data->$name);
+		parent::__construct($this->data);
 	}
 
 	/**
-	 * public function __set
-	 *
-	 * sets an item in the data store and writes out the file
-	 *
-	 * @param string $name
-	 * @param mixed $value
-	 * @return void
-	 */
-	public function __set($name, $value)
-	{
-		if (is_array($value))
-		{
-			$value = (object) $value;
-		}
-		$this->data->$name = $value;
-		return;
-	}
-
-	/**
-	 * public function __unset
-	 *
-	 * removes item from the data store and writes out the file
-	 *
-	 * @param string $name
-	 * @return void
-	 */
-	public function __unset($name)
-	{
-		unset ($this->data->$name);
-		return;
-	}
-
-	/**
 	 * protected function __destruct
 	 *
 	 * should actually save the data
@@ -131,16 +77,17 @@
 	 *
 	 * @return object instanceof CC_Config
 	 */
-	static public function instance()
+	static public function instance($class = null)
 	{
-		if (is_null(self::$class))
+		if (is_null(self::$singleton) && is_null($class))
 		{
-			throw new CC_Exception('You must set a class name for CC_Config before using');
+			throw new CC_Exception('You must send a class name for CC_Config on first creation');
 		}
-		$class = self::$class;
 		if (is_null(self::$singleton))
 		{
+			self::$check = true;
 			self::$singleton = new $class();
+			self::$check = false;
 		}
 		return self::$singleton;
 	}