All pastes #403759 Raw Edit

Untitled

public text v1 · immutable
·published 2007-03-20 16:41 UTC
rendered paste body
<?php

/**
 * CyCle IDE, Configuration Class
 *
 * @author Leon Pegg
 * @package CyCleIDE
 */

# Note : all constants should be UPPERCASE and words seperated with _
# Note : all classes should be CamelCaps no _
# Note : methods and class propertys should all have modifyers (public/private/protected)
# Note : static modifyer must precede method and property modifyers (public/private/protected)
# Note : all variables should be lowercase no _
# Note : all methods/functions should be lowercase and words seperated with _
# Note : { should not be on new line

class IdeIni {

	static private $instance = null;
	
	protected $filename = 'cycle.ini';
	protected $results = array();
	protected $loaded = false;
	protected $maxlength = 4096;
	protected $lineseparator = "\n";

	protected function __construct() {
		// singlton
	}

	protected function __clone(){
		// singlton
	}
	
	static public function get_instance() {
		if (self::$instance == null) {
			self::$instance = new self;
			self::$instance->results = self::$instance->parse_ini_file(Ide::CONFIGURE_DIR.DS.self::$instance->filename);
		}
		return self::$instance;
	}
	
	public function get_value($section, $key) {
		if(!($this->loaded)) {
			$this->parse_ini_file(Ide::CONFIGURE_DIR.DS.$this->filename);
		}
		if(isset($this->results[$section])) {
			$sectionValues = $this->results[$section];
			if(is_array($sectionValues)) {
				if(isset($sectionValues[$key])) {
					if (strtolower($sectionValues[$key]) == 'true') {
						return true;
					}
					if (strtolower($sectionValues[$key]) == 'false') {
						return false;
					}
					return $sectionValues[$key];
				} else {
					return null;
				}
			} else {
				return $sectionValues;
			}
		} else {
			return null;
		}
	}
	
	public function set_value($section, $key, $value, $write = true) {
		if($section != "") {
			switch ($value){
				case true: $this->results[$section][$key] = 'true'; break;
				case false: $this->results[$section][$key] = 'false'; break;
				default: $this->results[$section][$key] = $value; break;
			}
		} else {
			switch ($value){
				case true: $this->results[$key] = 'true'; break;
				case false: $this->results[$key] = 'false'; break;
				default: $this->results[$key] = $value; break;
			}
		}
		if($write == true) {
			return $this->write_changes();
		}
		return true;
	}
	
	public function write_changes() {
		$ok = true;
		$fp = fopen(Ide::CONFIGURE_DIR.DS.$this->filename,"wb");
		if(!$fp) {
			$ok = false;
		} else {
			if(isset($this->results)) {
				while(list($section, $values) = each($this->results)) {
					if(is_array($values)) {
						$res = fwrite($fp,"[$section]".$this->lineseparator);
						if($res == -1) {
							$ok = false;
							break;
						}
						while(list($option, $value) = each($values)) {
							$res = fwrite($fp,"$option=$value".$this->lineseparator);
							if($res == -1) {
								$ok = false;
								break;
							}
						}
						$res = fwrite($fp,$this->lineseparator);
						if($res == -1) {
							$ok = false;
							break;
						}
						if($ok == false) break;
					} else {
						$res = fwrite($fp,"$section=$values".$this->lineseparator);
						if($res == -1) {
							$ok = false;
							break;
						}
					}
				}
			}
			if(fclose($fp) == false) {
				$ok = false;
			}
		}
		return $ok;
	}

	public function parse_ini_file($filename) {
		$res = array();
		if(file_exists($filename) && is_readable($filename)) {
			$section = "";
			$fd = fopen($filename,"r");
			while(!feof($fd)) {
				$line = trim(fgets($fd,$this->maxlength));
				$len = strlen($line);
				if($len != 0) {
					if($line[0] != ';') {
						if(($line[0] == '[') && ($line[$len-1] == ']')) {
							$section = substr($line,1,$len-2);
							if(!isset($res[$section])){
								$res[$section] = array();
							}
						}
						$pos = strpos($line,'=');
						if($pos != false) {
							$name = substr($line,0,$pos);
							$value = substr($line,$pos+1,$len-$pos-1);
							if($section != "") {
								$res[$section][$name] = $value;
							} else {
								$res[$name] = $value;
							}
						}
					}
				}
			}
			fclose($fd);
		}
		return $res;
	}
	
}

IdeIni::get_instance();

?>