throw_exception方法简化为E方法

删除redirect方法和N方法
This commit is contained in:
thinkphp
2013-03-31 10:15:32 +08:00
parent 0a0a8d0dce
commit 7890bca611
27 changed files with 85 additions and 217 deletions

View File

@@ -44,7 +44,7 @@ class LocationTemplate {
}
$templateFile = MODULE_PATH.'view/'.$template.'.html';
if(!is_file($templateFile))
throw_exception(L('_TEMPLATE_NOT_EXIST_').'['.$templateFile.']');
E(L('_TEMPLATE_NOT_EXIST_').'['.$templateFile.']');
return $templateFile;
}
}

View File

@@ -24,7 +24,7 @@ class Config {
if(class_exists($class)) {
self::set((new $class())->parse($config),'',$range);
}else{
throw_exception('class not exists: ' . $class);
E('class not exists: ' . $class);
}
}

View File

@@ -85,7 +85,7 @@ abstract class Driver {
}
$this->linkID[$linkNum] = new PDO( $config['dsn'], $config['username'], $config['password'],$config['params']);
}catch (\PDOException $e) {
throw_exception($e->getMessage());
E($e->getMessage());
}
if(!empty($config['charset'])) {
$this->linkID[$linkNum]->exec('SET NAMES '.$config['charset']);
@@ -121,7 +121,7 @@ abstract class Driver {
$this->debug(true);
$this->PDOStatement = $this->_linkID->prepare($str);
if(false === $this->PDOStatement)
throw_exception($this->error());
E($this->error());
$result = $this->PDOStatement->execute($bind);
// 调试结束
$this->debug(false);
@@ -150,7 +150,7 @@ abstract class Driver {
$this->debug(true);
$this->PDOStatement = $this->_linkID->prepare($str);
if(false === $this->PDOStatement) {
throw_exception($this->error());
E($this->error());
}
$result = $this->PDOStatement->execute($bind);
$this->debug(false);
@@ -421,7 +421,7 @@ abstract class Driver {
}else{
// 查询字段的安全过滤
if(!preg_match('/^[A-Z_\|\&\-.a-z0-9\(\)\,]+$/',trim($key))){
throw_exception(L('_EXPRESS_ERROR_').':'.$key);
E(L('_EXPRESS_ERROR_').':'.$key);
}
// 多条件支持
$multi = is_array($val) && isset($val['_multi']);
@@ -490,7 +490,7 @@ abstract class Driver {
$data = is_string($val[1])? explode(',',$val[1]):$val[1];
$whereStr .= ' ('.$key.' '.strtoupper($val[0]).' '.$this->parseValue($data[0]).' AND '.$this->parseValue($data[1]).' )';
}else{
throw_exception(L('_EXPRESS_ERROR_').':'.$val[0]);
E(L('_EXPRESS_ERROR_').':'.$val[0]);
}
}else {
$count = count($val);

View File

@@ -41,13 +41,13 @@ class Oracle extends Driver{
if ( !empty($this->PDOStatement) ) $this->free();
$this->executeTimes++;
// 记录开始执行时间
Debug::remark('queryStartTime','time');
$this->debug(true);
$this->PDOStatement = $this->_linkID->prepare($str);
if(false === $this->PDOStatement) {
throw_exception($this->error());
E($this->error());
}
$result = $this->PDOStatement->execute($bind);
$this->debug();
$this->debug(false);
if ( false === $result) {
$this->error();
return false;

View File

@@ -82,7 +82,7 @@ class Lite {
}
$this->linkID[$linkNum] = new PDO( $config['dsn'], $config['username'], $config['password'],$config['params']);
}catch (\PDOException $e) {
throw_exception($e->getMessage());
E($e->getMessage());
}
if(!empty($config['charset'])) {
$this->linkID[$linkNum]->exec('SET NAMES '.$config['charset']);
@@ -117,7 +117,7 @@ class Lite {
$this->debug(true);
$this->PDOStatement = $this->_linkID->prepare($str);
if(false === $this->PDOStatement)
throw_exception($this->error());
E($this->error());
$result = $this->PDOStatement->execute($bind);
$this->debug(false);
if ( false === $result ) {
@@ -145,7 +145,7 @@ class Lite {
$this->debug(true);
$this->PDOStatement = $this->_linkID->prepare($str);
if(false === $this->PDOStatement) {
throw_exception($this->error());
E($this->error());
}
$result = $this->PDOStatement->execute($bind);
$this->debug(false);

View File

@@ -11,70 +11,4 @@
// $Id$
namespace Think;
class Exception extends \Exception {
/**
* 异常类型
* @var string
* @access private
*/
private $type;
// 是否存在多余调试信息
private $extra;
/**
* 架构函数
* @access public
* @param string $message 异常信息
*/
public function __construct($message,$code=0,$extra=false) {
parent::__construct($message,$code);
$this->type = get_class($this);
$this->extra = $extra;
}
/**
* 异常输出 所有异常处理类均通过__toString方法输出错误
* 每次异常都会写入系统日志
* 该方法可以被子类重载
* @access public
* @return array
*/
public function __toString() {
$trace = $this->getTrace();
if($this->extra)
// 通过throw_exception抛出的异常要去掉多余的调试信息
array_shift($trace);
$this->class = isset($trace[0]['class'])?$trace[0]['class']:'';
$this->function = isset($trace[0]['function'])?$trace[0]['function']:'';
$this->file = $trace[0]['file'];
$this->line = $trace[0]['line'];
$file = file($this->file);
$traceInfo = '';
$time = date('y-m-d H:i:m');
foreach($trace as $t) {
$traceInfo .= '['.$time.'] '.$t['file'].' ('.$t['line'].') ';
$traceInfo .= $t['class'].$t['type'].$t['function'].'(';
$traceInfo .= implode(', ', $t['args']);
$traceInfo .=")\n";
}
$error['message'] = $this->message;
$error['type'] = $this->type;
$error['detail'] = ($this->line-2).': '.$file[$this->line-3];
$error['detail'] .= ($this->line-1).': '.$file[$this->line-2];
$error['detail'] .= '<font color="#FF6600" >'.($this->line).': <strong>'.$file[$this->line-1].'</strong></font>';
$error['detail'] .= ($this->line+1).': '.$file[$this->line];
$error['detail'] .= ($this->line+2).': '.$file[$this->line+1];
$error['class'] = $this->class;
$error['function'] = $this->function;
$error['file'] = $this->file;
$error['line'] = $this->line;
$error['trace'] = $traceInfo;
// 记录 Exception 日志
if(C('LOG_EXCEPTION_RECORD')) {
ThinkLog::Write('('.$this->type.') '.$this->message);
}
return $error ;
}
}

View File

@@ -30,7 +30,7 @@ class Imagick{
*/
public function __construct($imgname = null) {
if ( !extension_loaded('Imagick') ) {
throw_exception(__('_NOT_SUPPERT_').':Imagick');
E(L('_NOT_SUPPERT_').':Imagick');
}
$imgname && $this->open($imgname);
}

View File

@@ -25,7 +25,7 @@ class Log {
return self::$handler;
}else {
// 类没有定义
throw_exception(Lang::get('_CLASS_NOT_EXIST_').': ' . $class);
E(L('_CLASS_NOT_EXIST_').': ' . $class);
}
}
}

View File

@@ -960,7 +960,7 @@ class Model {
}elseif(is_string($data)){
parse_str($data,$data);
}elseif(!is_array($data)){
throw_exception(L('_DATA_TYPE_INVALID_'));
E(L('_DATA_TYPE_INVALID_'));
}
$this->data = $data;
return $this;
@@ -1007,7 +1007,7 @@ class Model {
$options = $union;
}
}else{
throw_exception(L('_DATA_TYPE_INVALID_'));
E(L('_DATA_TYPE_INVALID_'));
}
$this->options['union'][] = $options;
return $this;

View File

@@ -85,7 +85,7 @@ class Diandian extends Driver{
$userInfo['avatar'] = "https://api.diandian.com/v1/blog/{$data['response']['blogs'][0]['blogUuid']}/avatar/144";
return $userInfo;
} else {
throw_exception("获取点点用户信息失败:{$data}");
E("获取点点用户信息失败:{$data}");
}
}

View File

@@ -83,7 +83,7 @@ class Douban extends Driver{
$userInfo['avatar'] = $data['avatar'];
return $userInfo;
} else {
throw_exception("获取豆瓣用户信息失败:{$data['msg']}");
E("获取豆瓣用户信息失败:{$data['msg']}");
}
}

View File

@@ -85,7 +85,7 @@ class Github extends Driver{
$userInfo['avatar'] = $data['avatar_url'];
return $userInfo;
} else {
throw_exception("获取Github用户信息失败{$data}");
E("获取Github用户信息失败{$data}");
}
}

View File

@@ -91,7 +91,7 @@ class Google extends Driver{
$userInfo['avatar'] = $data['picture'];
return $userInfo;
} else {
throw_exception("获取Google用户信息失败{$data}");
E("获取Google用户信息失败{$data}");
}
}

View File

@@ -86,7 +86,7 @@ class Kaixin extends Driver{
$userInfo['avatar'] = $data['logo50'];
return $userInfo;
} else {
throw_exception("获取开心网用户信息失败:{$data['error']}");
E("获取开心网用户信息失败:{$data['error']}");
}
}

View File

@@ -92,7 +92,7 @@ class Msn extends Driver{
$userInfo['avatar'] = '微软暂未提供头像URL请通过 me/picture 接口下载';
return $userInfo;
} else {
throw_exception("获取msn用户信息失败{$data}");
E("获取msn用户信息失败{$data}");
}
}

View File

@@ -95,7 +95,7 @@ class Qq extends Driver{
$userInfo['avatar'] = $data['figureurl_2'];
return $userInfo;
} else {
throw_exception("获取腾讯QQ用户信息失败{$data['msg']}");
E("获取腾讯QQ用户信息失败{$data['msg']}");
}
}
}

View File

@@ -109,7 +109,7 @@ class Renren extends Driver{
$userInfo['avatar'] = $data[0]['headurl'];
return $userInfo;
} else {
throw_exception("获取人人网用户信息失败:{$data['error_msg']}");
E("获取人人网用户信息失败:{$data['error_msg']}");
}
}
}

View File

@@ -85,7 +85,7 @@ class Sina extends Driver{
$userInfo['avatar'] = $data[0]['headurl'];
return $userInfo;
} else {
throw_exception("获取人人网用户信息失败:{$data['error_msg']}");
E("获取人人网用户信息失败:{$data['error_msg']}");
}
}

View File

@@ -85,7 +85,7 @@ class Sohu extends Driver{
$userInfo['avatar'] = $data['data']['icon'];
return $userInfo;
} else {
throw_exception("获取搜狐用户信息失败:{$data['message']}");
E("获取搜狐用户信息失败:{$data['message']}");
}
}

View File

@@ -87,7 +87,7 @@ class T163 extends Driver{
$userInfo['avatar'] = str_replace('w=48&h=48', 'w=180&h=180', $data['profile_image_url']);
return $userInfo;
} else {
throw_exception("获取网易微博用户信息失败:{$data['error']}");
E("获取网易微博用户信息失败:{$data['error']}");
}
}

View File

@@ -89,7 +89,7 @@ class Taobao extends Driver{
$userInfo['avatar'] = $user['avatar'];
return $userInfo;
} else {
throw_exception("获取淘宝网用户信息失败:{$data['error_response']['msg']}");
E("获取淘宝网用户信息失败:{$data['error_response']['msg']}");
}
}

View File

@@ -90,7 +90,7 @@ class Tencent extends Driver{
$userInfo['avatar'] = $data[0]['headurl'];
return $userInfo;
} else {
throw_exception("获取人人网用户信息失败:{$data['error_msg']}");
E("获取人人网用户信息失败:{$data['error_msg']}");
}
}

View File

@@ -85,7 +85,7 @@ class X360 extends Driver{
$userInfo['avatar'] = $data['avatar'];
return $userInfo;
} else {
throw_exception("获取360用户信息失败{$data['error']}");
E("获取360用户信息失败{$data['error']}");
}
}

View File

@@ -67,7 +67,7 @@ abstract class Rest {
$this->$fun();
}else{
// 抛出异常
throw_exception(L('_ERROR_ACTION_:').ACTION_NAME);
E(L('_ERROR_ACTION_:').ACTION_NAME);
}
}

View File

@@ -56,7 +56,7 @@ class Session {
$hander->execute();
}else {
// 类没有定义
throw_exception(Lang::get('_CLASS_NOT_EXIST_').': ' . $class);
E(L('_CLASS_NOT_EXIST_').': ' . $class);
}
}
// 启动session

View File

@@ -20,7 +20,7 @@ class File {
mkdir($dir,0755,true);
// 生成模板缓存文件
if( false === file_put_contents($cacheFile,$content))
throw_exception('_CACHE_WRITE_ERROR_:'.$cacheFile);
E('_CACHE_WRITE_ERROR_:'.$cacheFile);
}
// 读取编译编译

168
base.php
View File

@@ -40,71 +40,35 @@ define('NOW_TIME', $_SERVER['REQUEST_TIME']);
// 获取多语言变量
function L($name){
return \Think\Lang::get($name);
return Think\Lang::get($name);
}
// 获取配置参数
function C($name='',$range='') {
return \Think\Config::get($name,$range);
return Think\Config::get($name,$range);
}
// 获取输入数据
function I($key,$default,$filter) {
// 获取输入数据 支持默认值和过滤
function I($key,$default='',$filter='') {
if(strpos($key,'.')) { // 指定参数来源
list($method,$key) = explode('.',$key);
}else{ // 默认为自动判断
$method = 'param';
}
return \Think\Input::$method($key,$filter,$default);
return Think\Input::$method($key,$filter,$default);
}
/**
* 记录和统计时间(微秒)和内存使用情况
* 使用方法:
* <code>
* G('begin'); // 记录开始标记位
* // ... 区间运行代码
* G('end'); // 记录结束标签位
* echo G('begin','end',6); // 统计区间运行时间 精确到小数后6位
* echo G('begin','end','m'); // 统计区间内存使用情况
* 如果end标记位没有定义则会自动以当前作为标记位
* 其中统计内存使用需要 MEMORY_LIMIT_ON 常量为true才有效
* </code>
* @param string $start 开始标签
* @param string $end 结束标签
* @param integer|string $dec 小数位或者m
* @return mixed
* 记录时间(微秒)和内存使用情况
* @param string $label 记录标签
* @return void
*/
function G($name) {
Think\Debug::remark($name);
function G($label) {
Think\Debug::remark($label);
}
/**
* 设置和获取统计数据
* 使用方法:
* <code>
* N('db',1); // 记录数据库操作次数
* N('read',1); // 记录读取次数
* echo N('db'); // 获取当前页面数据库的所有操作次数
* echo N('read'); // 获取当前页面读取次数
* </code>
* @param string $key 标识位置
* @param integer $step 步进值
* @return mixed
*/
function N($key, $step=0) {
static $_num = array();
if (!isset($_num[$key])) {
$_num[$key] = 0;
}
if (empty($step))
return $_num[$key];
else
$_num[$key] = $_num[$key] + (int) $step;
}
/**
* M函数用于实例化一个没有模型文件的Model
* 实例化一个没有模型文件的Model
* @param string $name Model名称 支持指定基础模型 例如 MongoModel:User
* @param string $tablePrefix 表前缀
* @param mixed $connection 数据库连接信息
@@ -115,27 +79,27 @@ function M($name='', $tablePrefix='',$connection='') {
}
/**
* D函数用于实例化Model
* 实例化Model
* @param string $name Model名称
* @param string $layer 业务层名称
* @return ThinkModel
* @return object
*/
function D($name='',$layer='model') {
return Think\Loader::model($name,$layer);
}
/**
* A函数用于实例化控制器 格式:[分组/]模块
* 实例化控制器 格式:[分组/]模块
* @param string $name 资源地址
* @param string $layer 控制层名称
* @return Action|false
* @return object
*/
function A($name,$layer='') {
return Think\Loader::controll($name,$layer);
}
/**
* 远程调用模块的操作方法 参数格式 [模块/控制器/]操作
* 调用模块的操作方法 参数格式 [模块/控制器/]操作
* @param string $url 调用地址
* @param string|array $vars 调用参数 支持字符串和数组
* @param string $layer 要调用的控制层名称
@@ -145,21 +109,6 @@ function R($url,$vars=array(),$layer='') {
return Think\Loader::action($url,$vars,$layer);
}
/**
* 字符串命名风格转换
* type 0 将Java风格转换为C的风格 1 将C风格转换为Java的风格
* @param string $name 字符串
* @param integer $type 转换类型
* @return string
*/
function parse_name($name, $type=0) {
if ($type) {
return ucfirst(preg_replace("/_([a-zA-Z])/e", "strtoupper('\\1')", $name));
} else {
return strtolower(trim(preg_replace("/[A-Z]/", "_\\0", $name), "_"));
}
}
/**
* 导入所需的类库 同java的Import 本函数有缓存功能
* @param string $class 类库命名空间字符串
@@ -172,17 +121,13 @@ function import($class, $baseUrl = '', $ext= EXT ) {
}
/**
* 自定义异常处理
* 抛出异常处理
* @param string $msg 异常消息
* @param string $type 异常类型 默认为ThinkException
* @param integer $code 异常代码 默认为0
* @return void
*/
function throw_exception($msg, $type='Think\Exception', $code=0) {
if (class_exists($type))
throw new $type($msg, $code, true);
else
Think\Error::halt($msg); // 异常类型不存在则输出错误信息字串
function E($msg, $code=0) {
throw new Think\Exception($msg, $code);
}
/**
@@ -205,17 +150,17 @@ function dump($var, $echo=true, $label=null) {
* @return void
*/
function _404($msg='',$url='') {
Think\Config::get('app_debug') && throw_exception($msg);
Think\Config::get('app_debug') && E($msg);
if($msg) Think\Log::record($msg,'ERR');
$url = $url?$url:Think\Config::get('url_404_redirect');
if($url) {
redirect($url);
header('Location: ' . $url);
}else{
header('HTTP/1.1 404 Not Found');
// 确保FastCGI模式下正常
header('Status:404 Not Found');
exit;
}
exit;
}
/**
@@ -228,35 +173,6 @@ function W($name, $data=array()) {
echo R($name,$data,'Widget');
}
/**
* URL重定向
* @param string $url 重定向的URL地址
* @param integer $time 重定向的等待时间(秒)
* @param string $msg 重定向前的提示信息
* @return void
*/
function redirect($url, $time=0, $msg='') {
//多行URL地址支持
$url = str_replace(array("\n", "\r"), '', $url);
if (empty($msg))
$msg = "系统将在{$time}秒之后自动跳转到{$url}";
if (!headers_sent()) {
// redirect
if (0 === $time) {
header('Location: ' . $url);
} else {
header("refresh:{$time};url={$url}");
echo($msg);
}
exit();
} else {
$str = "<meta http-equiv='Refresh' content='{$time};URL={$url}'>";
if ($time != 0)
$str .= $msg;
exit($str);
}
}
/**
* 缓存管理
* @param mixed $name 缓存名称,如果为数组表示进行缓存设置
@@ -266,21 +182,39 @@ function redirect($url, $time=0, $msg='') {
*/
function S($name,$value='',$options=null) {
static $cache = null;
if(is_array($options)){
// 缓存操作的同时初始化
Think\Cache::connect($options);
if(is_array($options)){// 缓存操作的同时初始化
$cache = Think\Cache::connect($options);
}elseif(is_array($name)) { // 缓存初始化
Think\Cache::connect($name);
}elseif(is_null($cache)) { // 自动初始化
Think\Cache::connect();
$cache = true;
$cache = Think\Cache::connect($name);
return $cache;
}elseif(is_null($cache)) {// 自动初始化
$cache = Think\Cache::connect();
}
if(''=== $value){ // 获取缓存
return Think\Cache::get($name);
return $cache->get($name);
}elseif(is_null($value)) { // 删除缓存
return Think\Cache::rm($name);
return $cache->rm($name);
}else { // 缓存数据
$expire = is_numeric($options)?$options:NULL;
return Think\Cache::set($name, $value, $expire);
if(is_array($options)) {
$expire = is_numeric($options['expire'])?$options['expire']:NULL; //修复查询缓存无法设置过期时间
}else{
$expire = is_numeric($options)?$options:NULL; //默认快捷缓存设置过期时间
}
return $cache->set($name, $value, $expire);
}
}
/**
* 字符串命名风格转换
* type 0 将Java风格转换为C的风格 1 将C风格转换为Java的风格
* @param string $name 字符串
* @param integer $type 转换类型
* @return string
*/
function parse_name($name, $type=0) {
if ($type) {
return ucfirst(preg_replace("/_([a-zA-Z])/e", "strtoupper('\\1')", $name));
} else {
return strtolower(trim(preg_replace("/[A-Z]/", "_\\0", $name), "_"));
}
}