/** * 自定义异常处理 * * @param mixed $e 异常对象 */ public function appException(&$e) { $error = []; $exceptionClass = new \ReflectionClass($e); $error['exception'] = '\\' . $exceptionClass->name; $error['message'] = $e->getMessage(); $trace = $e->getTrace(); foreach ($trace as $key => $val) { $error['files'][$key] = $val; } if (substr($e->getFile(), -20) !== '\\Tools\\functions.php' || $e->getLine() !== 90) { array_unshift($error['files'], ['file' => $e->getFile(), 'line' => $e->getLine(), 'type' => 'throw']); } if (!Cml::$debug) { //正式环境 只显示‘系统错误’并将错误信息记录到日志 Log::emergency($error['message'], [$error['files'][0]]); $error = []; $error['message'] = Lang::get('_CML_ERROR_'); } if (Request::isCli()) { Output::writeException(sprintf("%s\n[%s]\n%s", isset($error['files']) ? implode($error['files'][0], ':') : '', get_class($e), $error['message'])); } else { header('HTTP/1.1 500 Internal Server Error'); View::getEngine('html')->reset()->assign('error', $error); Cml::showSystemTemplate(Config::get('html_exception')); } }
/** * 致命错误捕获 * * @param array $error 错误信息 */ public function fatalError(&$error) { if (Cml::$debug) { $run = new Run(); $run->pushHandler(Request::isCli() ? new PlainTextHandler() : new PrettyPageHandler()); $run->handleException(new ErrorException($error['message'], $error['type'], $error['type'], $error['file'], $error['line'])); } else { //正式环境 只显示‘系统错误’并将错误信息记录到日志 Log::emergency('fatal_error', [$error]); $error = []; $error['message'] = Lang::get('_CML_ERROR_'); if (Request::isCli()) { Output::writeException(sprintf("[%s]\n%s", 'Fatal Error', $error['message'])); } else { header('HTTP/1.1 500 Internal Server Error'); View::getEngine('html')->reset()->assign('error', $error); Cml::showSystemTemplate(Config::get('html_exception')); } } exit; }
/** * 运行命令 * * @param array|null $argv * * @return mixed */ public function run(array $argv = null) { try { if ($argv === null) { $argv = isset($_SERVER['argv']) ? array_slice($_SERVER['argv'], 1) : []; } list($args, $options) = Input::parse($argv); $command = count($args) ? array_shift($args) : 'help'; if (!isset($this->commands[$command])) { throw new \InvalidArgumentException("Command '{$command}' does not exist"); } isset($options['no-ansi']) && Colour::setNoAnsi(); if (isset($options['h']) || isset($options['help'])) { $help = new Help($this); $help->execute([$command]); exit(0); } $command = explode('::', $this->commands[$command]); return call_user_func_array([new $command[0]($this), isset($command[1]) ? $command[1] : 'execute'], [$args, $options]); } catch (\Exception $e) { Output::writeException($e); exit(1); } }