<?php

/**
 *--------------------------------------
 * 框架部署
 *--------------------------------------
 * @project		: pfa
 * @author		: cblee
 * @created		: 2012-9-23
 * @copyright	: (c)2012 AsThis
 *--------------------------------------
 */

if(!defined('PFA_PATH')) exit();

/* 检查应用目录 */
if(!is_dir(RUNTIME_PATH))
{
	/* 部署应用目录 */
	build_appDir();
}
else
{
	/* 检查缓存目录 */
	check_runtimeDir();
}

/* 部署应用目录 */
function build_appDir()
{
	if(!is_dir(APP_PATH))
	{
		mk_dir(APP_PATH, 0777);
	}
	if(!is_writeable(APP_PATH))
	{
		halt(L('_DIR_READONLY_').': '.APP_PATH);
	}
	$dirs = array(
		CFG_PATH,
		LIB_PATH,
		LIB_CTRLR_PATH,
		LIB_MODL_PATH,
		LIB_COMM_PATH,
		LANG_PATH,
		TPL_PATH,
		TPL_PATH.D_S.C('TE.TPL_THEME'),
		RUNTIME_PATH,
		CACHE_PATH,
		DATA_PATH,
		TEMP_PATH,
		LOG_PATH);
	mkdirs($dirs);

	/* 目录安全 */
	if(C('DIR_INDEX.SWITCH'))
	{
		$content = C('DIR_INDEX.CONTENT');
		foreach($dirs as $dir)
		{
			file_put_contents($dir.C('DIR_INDEX.FILENAME'), $content);
		}
	}

	/* 创建默认常量定义 */
	if(!is_file(CFG_PATH.'/define.php'))
	{
		copy(PFA_PATH.'/tpl/cfg/define.php', CFG_PATH.'/define.php');
	}
	/* 创建默认公共配置 */
	if(!is_file(CFG_PATH.'/comm.php'))
	{
		copy(PFA_PATH.'/tpl/cfg/comm.php', CFG_PATH.'/comm.php');
	}
	/* 创建默认路由规则 */
	if(!is_file(CFG_PATH.'/route.php'))
	{
		copy(PFA_PATH.'/tpl/cfg/route.php', CFG_PATH.'/route.php');
	}
	/* 创建默认控制器 */
	if(!is_file(LIB_CTRLR_PATH.D_S.C('APP.CTRLR').'.class.php'))
	{
		$defaultCtrlr = ucwords(strtolower(C('APP.CTRLR')))."Ctrlr";
		$content = file_get_contents(PFA_PATH.'/tpl/IndexCtrlr.class.php');
			$_t_from = array('IndexCtrlr','index','created-time');
			$_t_to = array($defaultCtrlr, C('APP.ACTN'), date('Y-m-d'));
		$content = str_replace($_t_from, $_t_to, $content);
		file_put_contents(LIB_CTRLR_PATH.D_S.$defaultCtrlr.'.class.php', $content);

		$content = file_get_contents(PFA_PATH.'/tpl/index.tpl.php');
		file_put_contents(TPL_PATH.D_S.C('TE.TPL_THEME').'/'.C('APP.ACTN').C('TE.TPL_SUFFIX'), $content);
	}
}

/* 检查缓存目录 */
function check_runtimeDir()
{
	if(!is_writeable(RUNTIME_PATH))
	{
		halt(L('_DIR_READONLY_').': '.RUNTIME_PATH);
	}
	if(!is_dir(CACHE_PATH))
	{
		mk_dir(CACHE_PATH);
	}
	if(!is_dir(DATA_PATH))
	{
		mk_dir(DATA_PATH);
	}
	if(!is_dir(TEMP_PATH))
	{
		mk_dir(TEMP_PATH);
	}
	if(!is_dir(LOG_PATH))
	{
		mk_dir(LOG_PATH);
	}
}

/* 创建编译缓存 */
function build_runtimeCache($append = '')
{
	/* 读取框架核心文件列表 */
	$list = include PFA_PATH.'/comm/core.php';
	/* 加载框架函数库 */
	$list[] = PFA_PATH.'/comm/function.php';
	/* 生成编译文件 */
	$defs = get_defined_constants(true);
	$content = array_define($defs['user']);
	foreach($list as $file)
	{
		$content .= compile($file);
	}
	$content .= $append."\r\nC(".var_export(C(), true).');';
	$runtime = '~runtime.php';
	file_put_contents(RUNTIME_PATH.D_S.$runtime, strip_whitespace('<?php '.$content));
}

?>