<?php
final class mvc{
    public static $cfg = [];
    public static $dbList = [];
	public static $action 	= '';
	public static $method     = '';
    public static $params     = [];
    public static $req     = [];

    public static function init(){
        self::$cfg = require(DIR_LIB.'/config.php'); //加载配置文件
        self::$cfg['database'] = require(DIR_LIB.'/config_database.php');  //加载数据库配置文件
        define('DIR_LOG',DIR.'/logs');
        define('DIR_RUNTIME',DIR.'/runtime');
        if(DEBUG && !is_dir(DIR_RUNTIME)) mkdir(DIR_RUNTIME);
        require(DIR_LIB.'/lib/func.php'); //加载主函数
        spl_autoload_register ('\mvc::autoload');   //注册__autoload()函数
        require DIR_COMPOSER.'/vendor/autoload.php';
        if(DEBUG){
            file_put_contents(DIR_RUNTIME.'/dump.html','');
            file_put_contents(DIR_RUNTIME.'/error.html','');
        }
        //错误处理
        error_reporting(0);
        ini_set('xdebug.remote_enable',0);
        if(DEBUG){
            //bebug 模式，禁用opcache
            if(function_exists("opcache_reset")){
                opcache_reset();
            }
            register_shutdown_function('\mvc::rare_shutdown_catch_error_debug');
        }else{
            register_shutdown_function('\mvc::rare_shutdown_catch_error');
        }
    }

    /**
     * 自动加载类
     * @return void
     */
    public static function autoload($className){
        if(in_array($className,['db','sqler','pdoHelper','SqlFormatter','LibLRTree','LibDb'])){
            $classFile = DIR_LIB.'/lib/'.str_replace('\\','/',$className).'.php';
            include_once($classFile);
        }else{
            $classFile = DIR.'/mod/'.str_replace('\\','/',$className).'.php';
            include_once($classFile);
        }
    }
    /**
     * 生产一个错误
     * @param $type = debug,cli,product 分别为  调试模式,控制台,正式环境
     * @return void
     * @example $error  [
            'type'=> E_ERROR,
            'file'=> $caller['file'],
            'line'=> $caller['line'],
            'message' => $message,
            'backtrace' => $backtrace,
            'sql' => $this->lastSql,
        ];
     */
    public static function errorMake($error)
    {
        if (empty($error['type'])) $error['type'] = E_ERROR;
        if(is_null($error)){
            return;
        }
        if(DEBUG){
            $content = \mvc::errorReport('debug',$error) ?: '';
            $f = fopen(DIR_RUNTIME.'/error.html', 'a');
            fwrite($f, $content);
            fclose($f);
            echo "\r\n==============================================\r\n";
            echo "FILE:".$error['file']."\r\n";
            echo "LINE:".$error['line']."\r\n";
            echo $error['message'];
            echo "\r\n----------------------------------------------\r\n";
            exit();
        }else{
            self::rare_shutdown_catch_error($error);
            exit();
        }
    } 
    
    /**
     * 格式化错误信息
     * @param $type = debug,cli,product 分别为  调试模式,控制台,正式环境
     * @return void
     */
    public static function errorReport($type,$error):string
    {
        return require(DIR_LIB."/rare_error/".$type.".php");
    }   
    
    public static function rare_shutdown_catch_error_debug($error=null){
        $error = $error ?: error_get_last();
        self::errorMake($error);
    }   
    
    /**
     * 非Debug错误处理
     * @return void
     */
    public static function rare_shutdown_catch_error($error=null){
        $error = $error ?: error_get_last();
        return(DIR_LIB."/rare_error/product.php");
    }

}