<?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();
		//信息缓存
		//模型缓存
		self::Module_Cache();

		//主分类缓存
		$categorydb=db::select('*')->from(jab."category")->query();
		self::category_setting($categorydb);
		
		//以下是专题缓存
		$special_list_setting=db::select('*')->from(jab.'special_list')->where("1")->query();
		self::special_list_setting($special_list_setting);
		
		$special_category_setting=db::select('*')->from(jab."special_category")->where('1')->query();
		self::special_category_setting($special_category_setting);
		
	}
	
	//钩子缓存
	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 Module_Cache(){
		$data=db::select('*')->from(jab."module")->orderByDESC(['reorder'])->query();
		$datadb=[];
		foreach((array)$data AS $row){
			//获取本模型下的所有字段
			$field=db::select('*')->from(jab."field")->where("mid='$row[id]'")->orderByDESC(['reorder'])->query();
			$datadb[$row['id']]=[
				'name'=>$row['name'],
				'field'=>$field
			];
		}
		$array=[
			'home_modeldb'=>$datadb
		];
		createconf($array,'custom_model_cache','home');
	}
	

	
	public static function category_setting($data){
    	//兼容全局缓存
		self::home_route($data);
		$datadb=[];
		foreach((array)$data AS $key=>$row){
			$son= self::get_son($row['cid']);
			$datadb[$row['cid']]=[
				'name'=>$row['name'],
				'seo_name'=>$row['seo_name'],
				'seo_keywords'=>$row['seo_keywords'],
				'dir_name'=>$row['dir_name'],
				'seo_description'=>$row['seo_description'],
				'setting'=>$row['setting'],
				'son'=>trim($son,','),
				'upcid'=>$row['upcid']
			];
		}
		
		$array=['category_setting'=>$datadb];
		createconf($array,'category_setting','home');
		self::Crumbs_Cache($data);
	}
	 //获取子孙
	private static function get_son($cid){
		$data=db::select('*')->from(jab."category")->where("upcid=$cid")->query();
		$echo='';
		foreach($data AS $key=>$row){
		    $echo.=$row['cid'].",";
		    if($row['type']==1){
		    	$echo.=self::get_son($row['cid']);
		    }
		}
	    return $echo;
	}
	//ROUTE
	private static function home_route($data){
		//兼容全局缓存

		$Home_Route =APP_PATH."home/conf/route.php";
		$set="<?php \r\n return [\r\n\t'home_route' =>[";
		$datadb=[];
		foreach($data AS $key=>$row){
			if(!$row['dir_name']){
				continue;
			}
			$datadb[strtolower($row['dir_name'])]=$row['cid'];
		}
		$array=['home_route'=>$datadb];
		createconf($array,'route','home');
	}
	//面包屑缓存
	private static function Crumbs_Cache($data){
		//兼容全局缓存
		$Crumbs_file = APP_PATH."home/conf/crumbs.php";
		$datadb=[];
		foreach((array)$data AS $key=>$row){
    		$echo=self::get_father($row['cid']);
			$datadb[$row['cid']]="<a href='/'>首页</a>".$echo;
		}
		$array=['home_crumbs'=>$datadb];
		createconf($array,'crumbs','home');
	}
	private static function get_father($cid){
		//兼容全局缓存
	    $Crumbs_DB=db::select('*')->from(jab."category")->where("cid=$cid")->query();
		foreach($Crumbs_DB AS $key=>$row){
		   	$echo.= '-> <a href="cid='.$cid.'">'.$row['name'].'</a>';
		   	if($row['upcid']){
		   		$echo=self::get_father($row['upcid']).$echo;
		    }
		}
	    return $echo;
    }
    
	//以下是专题
	public static function special_list_setting($data){
    	//兼容全局缓存

		$datadb=[];
		foreach((array)$data AS $key=>$row){
			$datadb[$row['id']]=[
				'name'=>$row['name'],
				'seo_name'=>$row['seo_name'],
				'seo_keywords'=>$row['seo_keywords'],
				'dir_name'=>$row['dir_name'],
				'seo_description'=>$row['seo_description'],
				'setting'=>$row['setting'],
				'sid'=>$row['sid'],
				'thumb'=>$row['thumb'],
				'is_comment'=>$row['is_comment']
			];
		}
		
		
		$array=['Special_list_setting'=>$datadb];
		createconf($array,'special_list_setting','home');
		
		
		self::special_list_route($data);
	}
	//分类ROUTE
	private static function special_list_route($data){
		//兼容全局缓存
		$datadb=[];
		foreach($data AS $key=>$row){
			if(!$row['dir_name']){
				continue;
			}
			$datadb[strtolower($row['dir_name'])]=$row['id'];
		}
		$array=['special_list_route'=>$datadb];
		createconf($array,'special_list_route','home');
		
	}
	
	
    
    public static function special_category_setting($data){
    	//兼容全局缓存
		$datadb=[];
		foreach($data AS $key=>$row){
			$datadb[$row['sid']]=[
				'name'=>$row['name'],
				'seo_name'=>$row['seo_name'],
				'seo_keywords'=>$row['seo_keywords'],
				'dir_name'=>$row['dir_name'],
				'seo_description'=>$row['seo_description'],
				'setting'=>$row['setting'],
				'upsid'=>$row['upsid']
			];
		}
		$array=['Special_category_setting'=>$datadb];
		createconf($array,'special_category_setting','home');
		
		
		
		
		
		self::special_category_crumbs_cache($data,$M_MODULE,$M_jab);
		self::special_category_route($data,$M_MODULE,$M_jab);
	}
	//如果启用了高速缓存,通知更新
	public static function caching (){
		if(CACHING==true){
			define('CACHING_ALLCACHE', true);
			\temmoku\vendor\think\cache::rm('conf_file');
			\temmoku\vendor\think\cache::rm('_config');
		}
	}
	//分类ROUTE
	private static function special_category_route($data){
		$datadb=[];
		foreach($data AS $key=>$row){
			if(!$row['dir_name']){
				continue;
			}
			$datadb[strtolower($row['dir_name'])]=$row['sid'];
		}
		$array=['Special_Route'=>$datadb];
		createconf($array,'special_route','home');
		
	}
	//面包屑缓存
	private static function special_category_crumbs_cache($data){
		$datadb=[];
		foreach($data AS $key=>$row){
    		$echo=self::get_up($row['sid']);
			$datadb[$row['sid']]="<a href='/'>首页</a>".$echo;
		}
		
		$array=['Special_Crumbs'=>$datadb];
		createconf($array,'special_crumbs','home');
	}
	private static function get_up($sid){
		//兼容全局缓存
	    $Crumbs_DB=db::select('*')->from(jab."special_category")->where("sid=$sid")->query();
		foreach($Crumbs_DB AS $key=>$row){
		    $echo.=" -> <a href=\"sid=$sid\">$row[name]</a>";
		    if($row['upsid']){
		    	$echo=self::get_up($row['upsid']).$echo;
		    }
		}
	    return $echo;
   	}
	//给每个模块或者插件生成一个初始配置文件
	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);
	}
}
?>
