<?php

/**
 *--------------------------------------
 * 缓存
 *--------------------------------------
 * @project		: uwa
 * @author		: cblee
 * @created		: 2012-10-22
 * @copyright	: (c)2012 AsThis
 *--------------------------------------
 */

class BuildModl extends Modl
{
	public function clear_cache($type)
	{
		$dir = '';
		switch($type)
		{
			case 'runtime':
				$dir = RUNTIME_PATH;
				self::clear_dir($dir);
				break;
			case 'cache':
				$dir = CACHE_PATH;
				self::clear_dir($dir, true);
				break;
			case 'data':
				$dir = DATA_PATH;
				self::clear_dir($dir, true);
				break;
			case 'temp':
				$dir = TEMP_PATH;
				self::clear_dir($dir, true);
				break;
			case 'js':
				$dir = RUNTIME_PATH.'/js';
				self::clear_dir($dir);
				break;
			default:
				break;
		}
	}
	/* 删除目录中的文件 $self:是否删除目录 */
	private static function clear_dir($dir, $sub = false)
	{
		if(is_dir($dir) == false)
		{
			exit("The Directory Is Not Exist!");
		}
		$handle = opendir($dir);
		while(($file = readdir($handle)) !== false)
		{
			if($file != "." && $file != "..")
			{
				if(is_dir("$dir/$file") && $sub)
				{
					self::clear_dir("$dir/$file", true);
				}
				elseif('~' == substr($file, 0, 1))
				{
					@unlink(realpath("$dir/$file"));
				}
			}
		}
		if(readdir($handle) == false)
		{
			closedir($handle);
		}
	}

	/* 输出进度 $barLength: % */
	public function show_progress($msg, $barLength = '50')
	{
		echo '<script type="text/javascript">show_progress("'.$msg.'", "'.$barLength.'%");</script>';
		@ob_flush();
		@flush();
		sleep(1);
	}

	/* 输出跳转 */
	public function show_direction($nextUrl, $show = false)
	{

		if($show)
		{
			echo '<script type="text/javascript">show_direction("'.$nextUrl.'")</script>';
		}
		echo '<script type="text/javascript">location.href = "'.$nextUrl.'";</script>';
		@ob_flush();
		@flush();
		sleep(1);
	}
}

?>