<?php
/*
 +----------------------------------------------------------------------
 | TEMMOKUMVC [ NO BEST, ONLY BETTER ]
 +----------------------------------------------------------------------
 | Copyright (c) 2018~2019 https://www.temmoku.cn All rights reserved.
 +----------------------------------------------------------------------
 | Author: 张宗强 Email:webmaster@temmoku.cn QQ:158726877 516669373 TEL:17895221001 微信:temmokumvc
 +---------------------------------------------------------------------- 
*/
namespace admin\public_class;
use \temmoku\db;
class cache {
	
	public static function all(){
		//权限缓存
		self::User_group();
		//核心缓存
		self::setting_cache();
		//导航缓存
		self::Home_Menu();
		self::Make_Module_Route();
		self::plugin_route();
		self::hook();
	}
	//钩子
	public static function hook(){
		$data=db::select('*')->from(jab.'hook_list')->where('state=1')->orderByDESC(['reorder'])->query();
		$datedb=[];
		foreach($data AS $key=>$row){
			$datedb[$row['hook_name']][]=['class'=>$row['class'],'method'=>$row['method']];
		}
		
		$array=['hook'=>$datedb];
		createconf($array,'hook');
	}
	//用户权限缓存
	public static function User_group(){
		$data=(new \admin\model\group)->get_all();
		$datadb=[];
		foreach($data AS $key=>$row){
			$datadb[$row['id']]=[
				'name'=>$row['name'],
				'type'=>$row['type'],
				'if_login_user'=>$row['if_login_user'],
				'if_login_admin'=>$row['if_login_admin'],
				'setting'=>$row['setting']
			];
		}
		$array=[
			'user_group'=>$datadb
		];
		createconf($array,'user_group');
	}
	//核心缓存
	public static function setting_cache(){
		
		$data = (new \admin\model\setting)->setting();
		$file = APP_PATH."conf/setting.php";
		$text="<?php \r\nreturn [\r\n";
		foreach($data AS $key=>$value){
			if($key=='caching'){
				$caching_file = APP_PATH."caching.php";
				$_caching=unserialize($value);
				$caching= $_caching['state'] ?  addslashes(addslashes($value)) : '';
				write_file($caching_file,"<?php \r\nreturn [\r\n\t\t'caching'=> \"$caching\"\r\n\t]\r\n?>");
				continue;
			}elseif($key=='trigger_state_words'){
				$array=[
					'trigger_state_words'=>$value
				];
				createconf($array,"/{$key}/{$key}");
				continue;
			}
			$text .="\t'$key'=>'$value',\r\n";
		}
		$text.="]\r\n?>";
		
		write_file($file,$text);
		self::caching();
	}
	

	//导航缓存
	public static function Home_Menu(){
		$data=(new \admin\model\homemenu)->all();
		$array=[
			'home_menu'=>$data
		];
		createconf($array,'home_menu');
	}
	
	public static function Make_Module_Route($del_vlue=''){
		$del_vlue=strtolower($del_vlue);
		$file=APP_PATH."module_route.php";
		$Module_Route= include $file;
		$data=db::select('*')->from(jab.'modular')->query();
		$ModuleDB=[];
		$ROUTE=[];
		foreach($data AS $R){
			$R['dir']=strtolower($R['dir']);
			$R['domain']= $R['domain'] ? $R['domain'] : C('WEBURL')."/".$R['dir'];
			$R['domain']=strtolower($R['domain']);
			//生成一个初始文件
			//if(!is_file(APP_PATH.$R['dir']."/init.php")){
				self::make_init_class(['name'=>$R['name'],'table'=>$R['table'],'dir'=>$R['dir'],'initial_name'=>$R['initial_name'],'domain'=>$R['domain']],APP_PATH.$R['dir'],true);
			//}
			$ModuleDB[]=$R['dir'];
			$ROUTE[]=['dir'=>$R['dir'],'domain'=>$R['domain']];
		}
		$route_file=APP_PATH."/conf/route/route.php";
		$_ROUTE=[];
		foreach($ROUTE AS $row){
			if(!$row['domain']){
				continue;
			}
			$_ROUTE[]="\r\n\t\t'$row[domain]'=>['dir'=>'$row[dir]']";
		}
		
		$_ROUTE_html= "<?php \nreturn [\r\n\t'route'=>[".implode(',',$_ROUTE)."\r\t]\n]\n?>";
		write_file($route_file,$_ROUTE_html);
		$Module_Route['MODULE_ROTUE']=array_unique(array_merge($Module_Route['MODULE_ROTUE'],$ModuleDB));
		unset($Module_Route['ROUTE']);
		foreach($Module_Route AS $K=>$R){
			$_v=[];
			foreach($R as $v){
				if($v==$del_vlue){
					continue;
				}
				$v=strtolower($v);
				$_v[]="'$v'";
			}
			$Module[]= "\n\t'$K'=>[".implode(',',$_v)."]";
		}
		$text= "<?php \n return [".implode(',',$Module)."\n]\n?>";
		write_file($file,$text);
	}

	
	public static function plugin_route(){
		$file=APP_PATH."plugin_route.php";
		$Plugin = db::select('*')->from(jab.'plugin')->where('state=1')->query();
		$set=[];
		foreach($Plugin as $row){
			$_filse=APP_PATH.'plugin/'.$row['dir'].'/home';
			if(file_exists($_filse)){
				$set[]="\n\t\t'".strtolower($row['dir'])."'";
			}
			//if(!is_file(APP_PATH.'plugin/'.$row['dir']."/init.php")){
    			self::make_init_class([
    			'name'=>$row['name'],
    			'table'=>$row['table'],
    			'dir'=>$row['dir'],
    			'initial_name'=>$row['initial_name']
    			],APP_PATH.'plugin/'.$row['dir']);
    		//}
		}
		$_set="<?php \nreturn [\n\t'Plugin_Route'=>[".implode(',',$set)."\n\t]\n]?>";
		write_file($file,$_set);
	}

	//如果启用了高速缓存,通知更新
	public static function caching (){
		if(CACHING==true){
			define('CACHING_ALLCACHE', true);
			\temmoku\vendor\think\cache::rm('conf_file');
			\temmoku\vendor\think\cache::rm('_config');
		}
	}
	
	//给每个模块或者插件生成一个初始配置文件
	private static function make_init_class($array,$dir,$m=false){
		if($m==true){
			$namespace="namespace $array[dir];";
			$table=jab.$array['table'];
		}else{
			$namespace="namespace plugin\\\\$array[dir];";
			$table=jab.'plugin_'.$array['table'];
		}
$html='<?php
'.$namespace.'
abstract class init{
	public static $table="'.$table.'";
	public static $dir="'.$array['dir'].'";
	public static $name="'.$array['name'].'";
	public static $initial_name="'.$array['initial_name'].'";
	public static $domain="'.$array['domain'].'";
}
?>';
		write_file($dir."/init.php", $html);
	}
}
?>
