CI框架源码阅读---------Exceptions

来源:互联网 发布:手套 牌子 知乎 编辑:程序博客网 时间:2024/05/21 20:52
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');/** * CodeIgniter * * An open source application development framework for PHP 5.1.6 or newer * * @packageCodeIgniter * @authorExpressionEngine Dev Team * @copyrightCopyright (c) 2008 - 2011, EllisLab, Inc. * @licensehttp://codeigniter.com/user_guide/license.html * @linkhttp://codeigniter.com * @sinceVersion 1.0 * @filesource */// ------------------------------------------------------------------------/** * Exceptions Class * 异常处理类 * @packageCodeIgniter * @subpackageLibraries * @categoryExceptions * @authorExpressionEngine Dev Team * @linkhttp://codeigniter.com/user_guide/libraries/exceptions.html */class CI_Exceptions {var $action;var $severity;var $message;var $filename;var $line;/** * Nesting level of the output buffering mechanism * 输出缓存机制的嵌套级别 * @var int * @access public */var $ob_level;/** * List if available error levels * 有效地错误级别列表 * @var array * @access public */var $levels = array(E_ERROR=>'Error',E_WARNING=>'Warning',E_PARSE=>'Parsing Error',E_NOTICE=>'Notice',E_CORE_ERROR=>'Core Error',E_CORE_WARNING=>'Core Warning',E_COMPILE_ERROR=>'Compile Error',E_COMPILE_WARNING=>'Compile Warning',E_USER_ERROR=>'User Error',E_USER_WARNING=>'User Warning',E_USER_NOTICE=>'User Notice',E_STRICT=>'Runtime Notice');/** * Constructor */public function __construct(){$this->ob_level = ob_get_level();// Note:  Do not log messages from this constructor.}// --------------------------------------------------------------------/** * Exception Logger * 异常日志的记录 * This function logs PHP generated error messages * 这个函数将记录php产生的错误消息 * @accessprivate * @paramstringthe error severity * @paramstringthe error string * @paramstringthe error filepath * @paramstringthe error line number * @returnstring */function log_exception($severity, $message, $filepath, $line){// 根据错误级别设置错误严重性$severity = ( ! isset($this->levels[$severity])) ? $severity : $this->levels[$severity];log_message('error', 'Severity: '.$severity.'  --> '.$message. ' '.$filepath.' '.$line, TRUE);}// --------------------------------------------------------------------/** * 404 Page Not Found Handler * 404 页面 * @accessprivate * @paramstringthe page * @param boollog error yes/no * @returnstring */function show_404($page = '', $log_error = TRUE){$heading = "404 Page Not Found";$message = "The page you requested was not found.";// By default we log this, but allow a dev to skip itif ($log_error){log_message('error', '404 Page Not Found --> '.$page);}echo $this->show_error($heading, $message, 'error_404', 404);exit;}// --------------------------------------------------------------------/** * General Error Page * 一般的错误页面 * This function takes an error message as input * (either as a string or an array) and displays * it using the specified template. * 这个函数将使用指定的模板显示一个错误消息 * @accessprivate * @paramstringthe heading * @paramstringthe message * @paramstringthe template name * @param intthe status code * @returnstring */function show_error($heading, $message, $template = 'error_general', $status_code = 500){// 默认是500set_status_header($status_code);// 组合错误消息$message = '<p>'.implode('</p><p>', ( ! is_array($message)) ? array($message) : $message).'</p>';/** 如果还没看过core/Loader.php,下面这个判断可能让人有点迷惑。* ob_get_level()是取得当前缓冲机制的嵌套级别。(缓冲是可以一层嵌一层的。)* 右边的$this->ob_level是在__construct()里面同样通过ob_get_level()被赋值的。* 也就是说,有可能出现:Exception组件被加载时(也就是应用刚开始运行时)* 的缓冲级别(其实也就是程序最开始的时候的缓冲级别,那时* 候是还没有ob_start()过的),与发生错误的时候的缓冲级别相差1。* 在控制器执行$this->load->view("xxx");的时候,实质,* Loader引入并执行这个视图文件的时候,是先把缓冲打开,即* 先ob_start(),所有输出放到缓冲区(详见:core/Loader.php中的_ci_load()),然后再由Output处理输出。* 因此,如果是在视图文件发生错误,则就会出现缓冲级别相差1的情况* 这就会导致输出的内容不仅仅只是这个的错误信息* 此时先把输出的内容给flush出来,然后再把错误信息输出。* */if (ob_get_level() > $this->ob_level + 1){ob_end_flush();}ob_start();include(APPPATH.'errors/'.$template.'.php');$buffer = ob_get_contents();ob_end_clean();return $buffer;}// --------------------------------------------------------------------/** * Native PHP error handler * 本地php错误处理 * @accessprivate * @paramstringthe error severity * @paramstringthe error string * @paramstringthe error filepath * @paramstringthe error line number * @returnstring */function show_php_error($severity, $message, $filepath, $line){$severity = ( ! isset($this->levels[$severity])) ? $severity : $this->levels[$severity];$filepath = str_replace("\\", "/", $filepath);// For safety reasons we do not show the full file pathif (FALSE !== strpos($filepath, '/')){$x = explode('/', $filepath);$filepath = $x[count($x)-2].'/'.end($x);}if (ob_get_level() > $this->ob_level + 1){ob_end_flush();}ob_start();include(APPPATH.'errors/error_php.php');$buffer = ob_get_contents();ob_end_clean();echo $buffer;}}// END Exceptions Class/* End of file Exceptions.php *//* Location: ./system/core/Exceptions.php */