genera.php 3.19 KB
<meta charset='utf-8'>
<?php
class Genera {
	
	private $_search = array('%FRAMEWORK_DIR%');
	
	private $_replace = array();
	
	private $create_info = '' ;
	
	/**
	 * 初始化
	 *
	 * @param string $_project 项目名称
	 * @param string $_object_dir 应用创建路径
	 * @param string $show_info 是否 显示创建信息
	 */
	function __construct($_project,$_object_dir,$show_info=true){
		$_project = strtolower(preg_replace('/[^a-z0-9_]+/i', '', $_project));
		if (empty($_project)){
			throw new Exception('请填写有效的项目名称(字母、数字或下划线)');
		}
		if (!is_dir($_object_dir)){
			throw new Exception('你想创建的目标路径不存在');
		}
		$from_source = dirname(__FILE__).'/app' ;
		$target_source = $this->formatDir($_object_dir).'/'.$_project ;

		if (!is_dir($from_source)){
			throw new Exception($from_source.':框架模板不存在,缺少文件');
		}
		if (is_dir($target_source)) {
			throw new Exception($target_source.':已存在,不可以重新生成');
		}
		if (!mkdir($target_source,0777)){
			throw new Exception($target_source.':创建目录失败,请检查是否拥有权限');
		}
		$framework_dir = str_replace('\\','/',realpath(dirname(dirname(__FILE__))));
		//设置替换值
		$this->_replace = array($framework_dir);
		if ($show_info == false){
			ob_start();
		}
		$this->_copy($from_source,$target_source);
		if ($show_info  == false){
			$create_info = ob_get_contents();
			ob_clean();
			$this->create_info = $create_info ;
		}
	}
	
	/**
	 * 获取创建信息
	 *
	 * @return unknown
	 */
	public function getInfo(){
		return $this->create_info ;
	}
	
	function formatDir($dir){
		$dir = str_replace('\\','/',$dir);
		if (substr($dir,-1,1) == '/'){
			return substr($dir,0,-1);
		}
		return $dir ;
	}
	
	/**
	 * 复制内容
	 *
	 * @param unknown_type $_project
	 * @param unknown_type $_object_dir
	 */
	private function _copy($source,$target){

		$source = rtrim($source, '/\\') . '/';
		$target = rtrim($target, '/\\') . '/';
		$h = opendir($source);
		$skip = array('.', '..', '.svn', '.cvs');
		while (($file = readdir($h)) !== false)
		{
		    if (in_array($file, $skip)) continue;
			$path = $source . $file;
			echo '  create ', $target . $file;
			echo "<br>";
			if (is_dir($path))
            {
				mkdir($target . $file, 0777);
				$this->_copy($path, $target . $file);
			}
            else
            {
                $filesize = filesize($path);
                if ($filesize)
                {
                    $fp = fopen($path, 'rb');
                    $content = fread($fp, $filesize);
                    fclose($fp);
                }
                else
                {
                    $content = '';
                }

                $extname = strtolower(pathinfo($path, PATHINFO_EXTENSION));
                if ($extname == 'php')
                {
				    $content = str_replace($this->_search, $this->_replace, $content);
				    //$content = str_replace(array("\n\r", "\r\n", "\r"), "\n", $content);
                }

                $fp = fopen($target . $file, 'wb');
                fwrite($fp, $content);
                fclose($fp);
                chmod($target . $file, 0666);

				unset($content);
			}
		}
		closedir($h);
		
		
	}
}
?>