/** * 系统初始化 * * @param boolean $auto_execute 是否自动运行 */ public static function setup($auto_execute = true) { static $run = null; if (!$run) { $run = true; # 注册自动加载类 spl_autoload_register(array('Bootstrap', 'auto_load')); # 读取配置 if (!is_file(DIR_SYSTEM . 'config' . EXT)) { self::show_error('Please rename the file config.new.php to config.php'); } $include_config_file = function (&$config, $file) { include $file; }; self::$config = array('core' => array()); # 读取主配置 $include_config_file(self::$config['core'], DIR_SYSTEM . 'config' . EXT); # 读Debug配置 if (isset(self::$config['core']['debug_config']) && self::$config['core']['debug_config'] && is_file(DIR_SYSTEM . 'debug.config' . EXT)) { $include_config_file(self::$config['core'], DIR_SYSTEM . 'debug.config' . EXT); } # DEBUG配置 if (isset(self::$config['core']['local_debug_cfg']) && self::$config['core']['local_debug_cfg']) { if (function_exists('get_cfg_var')) { $open_debug = get_cfg_var(self::$config['core']['local_debug_cfg']) ? 1 : 0; } else { $open_debug = 0; } } else { $open_debug = 0; } /** * 判断是否开启了在线调试 * * @return boolean */ $is_online_debug = function () { if (IS_SYSTEM_MODE) { if (isset($_SERVER['HTTP_X_MYQEE_SYSTEM_DEBUG']) && $_SERVER['HTTP_X_MYQEE_SYSTEM_DEBUG'] == '1') { return true; } else { return false; } } if (!isset($_COOKIE['_debug_open'])) { return false; } if (!isset(Bootstrap::$config['core']['debug_open_password'])) { return false; } if (!is_array(Bootstrap::$config['core']['debug_open_password'])) { return false; } foreach (Bootstrap::$config['core']['debug_open_password'] as $user => $pass) { if ($_COOKIE['_debug_open'] == Bootstrap::get_debug_hash($user, $pass)) { return true; } } return false; }; if ($is_online_debug()) { $open_debug == 1 << 1 | $open_debug; } unset($is_online_debug); /** * 是否开启DEBUG模式 * * if (IS_DEBUG>>1) * { * //开启了在线调试 * } * * if (IS_DEBUG & 1) * { * //本地调试打开 * } * * if (IS_DEBUG) * { * // 开启了调试 * } * * @var int */ define('IS_DEBUG', $open_debug); # 请求模式 $request_mode = ''; if (IS_CLI) { if (!isset($_SERVER["argv"])) { exit('Err Argv'); } $argv = $_SERVER["argv"]; //$argv[0]为文件名 if (isset($argv[1]) && $argv[1] && isset(self::$config['core']['projects'][$argv[1]])) { self::$project = $argv[1]; } array_shift($argv); //将文件名移除 array_shift($argv); //将项目名移除 self::$path_info = trim(implode('/', $argv)); unset($argv); } else { self::setup_by_url($request_mode); if (isset(self::$config['core']['charset'])) { # 输出文件头 header('Content-Type: text/html;charset=' . self::$config['core']['charset']); } } # 设置页面错误等级 if (isset(self::$config['core']['error_reporting'])) { error_reporting(self::$config['core']['error_reporting']); } # 设置时区 if (isset(self::$config['core']['timezone']) && self::$config['core']['timezone']) { date_default_timezone_set(self::$config['core']['timezone']); } /** * 加载类库 * @var array $arr */ $load_library = function ($arr) { # 逆向排序 rsort($arr); foreach ($arr as $library_name) { if (!$library_name) { continue; } try { Bootstrap::import_library($library_name); } catch (Exception $e) { Bootstrap::show_error($e->getMessage()); } } }; /** * 是否后台模式 * * @var boolean */ define('IS_ADMIN_MODE', !IS_CLI && $request_mode == 'admin' ? true : false); if (IS_SYSTEM_MODE) { # 设置控制器在[system]目录下 self::$dir_setting['controller'][0] .= DS . '[system]'; } # 如果有autoload目录,则设置加载目录 if (isset(self::$config['core']['libraries']['autoload']) && is_array(self::$config['core']['libraries']['autoload']) && self::$config['core']['libraries']['autoload']) { $load_library(self::$config['core']['libraries']['autoload']); } if (IS_CLI) { # cli模式 if (isset(self::$config['core']['libraries']['cli']) && is_array(self::$config['core']['libraries']['cli']) && self::$config['core']['libraries']['cli']) { $load_library(self::$config['core']['libraries']['cli']); } if (!IS_SYSTEM_MODE) { # 控制器在[shell]目录下 self::$dir_setting['controller'][0] .= DS . '[shell]'; } } elseif (IS_ADMIN_MODE) { # 后台模式 if (isset(self::$config['core']['libraries']['admin']) && is_array(self::$config['core']['libraries']['admin']) && self::$config['core']['libraries']['admin']) { $load_library(self::$config['core']['libraries']['admin']); } if (!IS_SYSTEM_MODE) { # 控制器在[admin]目录下 self::$dir_setting['controller'][0] .= DS . '[admin]'; } } elseif ($request_mode == 'app') { # APP模式 if (isset(self::$config['core']['libraries']['app']) && is_array(self::$config['core']['libraries']['app']) && self::$config['core']['libraries']['app']) { $load_library(self::$config['core']['libraries']['app']); } } if (IS_DEBUG) { # 加载debug类库 if (isset(self::$config['core']['libraries']['debug']) && is_array(self::$config['core']['libraries']['debug']) && self::$config['core']['libraries']['debug']) { $load_library(self::$config['core']['libraries']['debug']); } # 输出一些系统信息 Core::debug()->group('include path'); foreach (self::$include_path as $value) { Core::debug()->log(Core::debug_path($value)); } Core::debug()->groupEnd(); if (self::$project) { Core::debug()->info('project: ' . self::$project); } elseif ($request_mode == 'app') { Core::debug()->info('app mode'); } else { Core::debug()->info('default application'); } if (IS_ADMIN_MODE) { Core::debug()->info('admin mode'); } } unset($load_library); Core::setup(); } # 直接执行 if ($auto_execute) { if (IS_CLI || IS_SYSTEM_MODE) { self::execute(self::$path_info); } else { ob_start(); try { self::execute(self::$path_info); } catch (Exception $e) { $code = $e->getCode(); if (404 === $code || E_PAGE_NOT_FOUND === $code) { Core::show_404($e->getMessage()); } elseif (500 === $code) { Core::show_500($e->getMessage()); } else { Core::show_500($e->getMessage(), $code); } } HttpIO::$body = ob_get_clean(); } # 全部全部连接 Core::close_all_connect(); } }
/** * Catches errors that are not caught by the error handler, such as E_PARSE. * * @uses Kohana::exception_handler * @return void */ public static function shutdown_handler() { $error = error_get_last(); if ($error) { static $run = null; if (true === $run) { return; } $run = true; if (((\E_ERROR | \E_PARSE | \E_CORE_ERROR | \E_COMPILE_ERROR | \E_USER_ERROR | \E_RECOVERABLE_ERROR) & $error['type']) !== 0) { $run = true; static::exception_handler(new \ErrorException($error['message'], $error['type'], 0, $error['file'], $error['line'])); exit(1); } } if (isset($_REQUEST['debug'])) { if (\Core::debug()->profiler('output')->is_open()) { \Core::debug()->profiler('output')->start('Views', 'Global Data'); \Core::debug()->profiler('output')->stop(array('Global Data' => \View::get_global_data())); } // 输出debug信息 $file = \Core::find_view('debug/profiler'); if ($file) { if (\IS_CLI) { include $file; } else { \ob_start(); include $file; $out = \ob_get_clean(); if (\stripos(\HttpIO::$body, '</body>') !== false) { \HttpIO::$body = \str_ireplace('</body>', $out . '</body>', \HttpIO::$body); } else { \HttpIO::$body .= $out; } } } } }