codeigniter源代码分析 - 错误处理类 Exception.php

来源:互联网 发布:图片ps软件 编辑:程序博客网 时间:2024/04/29 19:20

剩下的class 都比较简单但是作用很大 框架必不可少的功能部分

Exception      错误处理 404 php error

Benchmark   检测CI运行加载时间 内存使用情况

Hooks            钩子程序 便于对CI扩展 主要作用是 能通过钩子 在CI的执行流程中添加一些自己的实现

Lang               语言加载


先写Exception 注释源码:

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');class CI_Exceptions {var $action;var $severity;var $message;var $filename;var $line;var $ob_level;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(){// 缓存嵌套级别// Note:  Do not log messages from this constructor.$this->ob_level = ob_get_level();}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);}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);}// 增加404http头信息 调用show_error()echo $this->show_error($heading, $message, 'error_404', 404);exit;}function show_error($heading, $message, $template = 'error_general', $status_code = 500){// 设置http 响应头信息set_status_header($status_code);$message = '<p>'.implode('</p><p>', ( ! is_array($message)) ? array($message) : $message).'</p>';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;}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 path// 解释的挺清除 wo don't show the full path// filepath 变量会在error模版中使用if (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;}}

Code Tips:

show_404 -> show_error

                         show_php_error

主要起作用的方法是 show_error and show_php_error

show_error 用户可以自行调用显示错误 类似exit()

show_php_error 是对php发生用户级别的错误时候进行的处理


需要学习其中用ob进行输出的思想

0 0
原创粉丝点击