2011年3月17日星期四

PHP下模拟的ASP的Application类

使用读取文件的方式,把每个键值存为一个xml文件,没有放在内存里,效率尚未测试。大概看看代码,可以直接模拟。

  1: <?php
  2: require_once dirname(__FILE__).'/../inc/config.php';
  3: require_once dirname(__FILE__).'/../inc/globe.php';
  4: require_once dirname(__FILE__).'/log.php';
  5: class Application{
  6: 	public static function get($key){
  7: 		$value = NULL;
  8: 		$filename = self::get_filename($key);
  9: 		try{
 10: 			if(is_file($filename)){
 11: 				$dom = new DOMDocument();
 12: 				$dom->load($filename);
 13: 				$node = $dom->firstChild;
 14: 				$value = $node->nodeValue;
 15: 			}
 16: 		}catch(Exception $e){
 17: 			
 18: 		}
 19: 		return $value;
 20: 		
 21: 	}
 22: 	public static function set($key,$value=NULL){
 23: 		if(is_null($value)){
 24: 			self::delete($key);
 25: 		}else{
 26: 			
 27: 			$dom = new DOMDocument('1.0', 'utf-8');
 28: 			add_element($dom, 'value', $value);
 29: 			
 30: 			$filename = self::get_filename($key);
 31: 			file_put_contents($filename, $dom->saveXML(), LOCK_EX);
 32: 		}
 33: 	}
 34: 	public static function delete($key){
 35: 		try{
 36: 			Log::record('delete application data with '.$key, LOG_ANY);
 37: 			@$filename = self::get_filename($key);
 38: 			@unlink($filename);
 39: 		}catch(Exception $e){
 40: 			Log::record('cant delete application data with '.$key, LOG_REMIND);
 41: 		}
 42: 	}
 43: 	//获得文件名
 44: 	public static function get_filename($key){		
 45: 		return self::get_dir().$key.'.xml';
 46: 	}
 47: 	
 48: 	//获得路径
 49: 	public static function get_dir(){
 50: 		$dir = dirname(__FILE__).'/../'.ITEM_APPLICATION_PATH;
 51: 		if(!is_dir($dir)){
 52: 			mkdir($dir,0777,true);
 53: 			Log::record('create application path with '.$dir, LOG_ANY);
 54: 		}
 55: 		return $dir;
 56: 	}
 57: }
 58: ?>

没有评论:

发表评论