改进Handle类支持异常的多语言 Lang类增加has方法 增加核心中文语言包

This commit is contained in:
thinkphp
2016-06-16 16:07:44 +08:00
parent d8fc0bf7aa
commit 50d1798e92
35 changed files with 151 additions and 59 deletions

2
lang/.gitignore vendored
View File

@@ -1,2 +0,0 @@
*
!.gitignore

58
lang/zh-cn.php Normal file
View File

@@ -0,0 +1,58 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
// 核心中文语言包
return [
// 系统错误提示
'Undefined variable' => '未定义变量',
'Undefined index' => '未定义索引',
'Parse error' => '语法解析错误',
'Type error' => '类型错误',
'Fatal error' => '致命错误',
// 框架核心错误提示
'dispatch type not support' => '不支持的调度类型',
'method param miss' => '方法参数错误',
'method not exists' => '方法不存在',
'module not exists' => '模块不存在',
'class not exists' => '类不存在',
'template not exists' => '模板文件不存在',
'illegal controller name' => '非法的控制器名称',
'illegal action name' => '非法的操作名称',
'url suffix deny' => '禁止的URL后缀访问',
'Route Not Found' => '当前访问路由未定义',
'Underfined db type' => '未定义数据库类型',
'variable type error' => '变量类型错误',
'PSR-4 error' => 'PSR-4 规范错误',
'not support total' => '简洁模式下不能获取数据总数',
'not support last' => '简洁模式下不能获取最后一页',
'error session handler' => '错误的SESSION处理器类',
'not allow php tag' => '模板不允许使用PHP语法',
'not support' => '不支持',
'redisd master' => 'Redisd 主服务器错误',
'redisd slave' => 'Redisd 从服务器错误',
'must run at sae' => '必须在SAE运行',
'memcache init error' => '未开通Memcache服务请在SAE管理平台初始化Memcache服务',
'KVDB init error' => '没有初始化KVDB请在SAE管理平台初始化KVDB服务',
'fields not exists' => '数据表字段不存在',
'where express error' => '查询表达式错误',
'no data to update' => '没有任何数据需要更新',
'miss complex primary data' => '缺少复合主键数据',
'miss update condition' => '缺少更新条件',
'model data Not Found' => '模型数据不存在',
'table data not Found' => '表数据不存在',
'delete without condition' => '没有条件不会执行删除操作',
'miss relation data' => '缺少关联表数据',
'tag attr must' => '模板标签属性必须',
'tag error' => '模板标签错误',
'cache write error' => '缓存写入失败',
'sae mc write error' => 'SAE mc 写入错误',
];

View File

@@ -249,7 +249,7 @@ class App
// 初始化模块
$config = self::init($module);
} else {
throw new HttpException(404, 'module [ ' . $module . ' ] not exists ');
throw new HttpException(404, 'module not exists:' . $module);
}
} else {
// 单一模块部署
@@ -286,7 +286,7 @@ class App
$action = $actionName . $config['action_suffix'];
if (!preg_match('/^[A-Za-z](\w)*$/', $action)) {
// 非法操作
throw new \ReflectionException('illegal action name :' . $actionName);
throw new \ReflectionException('illegal action name:' . $actionName);
}
// 执行操作方法
@@ -301,7 +301,7 @@ class App
$data = $method->invokeArgs($instance, [$action, '']);
self::$debug && Log::record('[ RUN ] ' . $method->getFileName(), 'info');
} else {
throw new HttpException(404, 'method [ ' . (new \ReflectionClass($instance))->getName() . '->' . $action . ' ] not exists ');
throw new HttpException(404, 'method not exists:' . (new \ReflectionClass($instance))->getName() . '->' . $action);
}
}
return $data;
@@ -419,7 +419,7 @@ class App
{
// 检测URL禁用后缀
if ($config['url_deny_suffix'] && preg_match('/\.(' . $config['url_deny_suffix'] . ')$/i', $request->pathinfo())) {
throw new Exception('url suffix deny');
throw new Exception('url suffix deny:'.$request->ext());
}
$path = $request->path();

View File

@@ -65,7 +65,7 @@ class Db
// 解析连接参数 支持数组和字符串
$options = self::parseConfig($config);
if (empty($options['type'])) {
throw new \InvalidArgumentException('db type error');
throw new \InvalidArgumentException('Underfined db type');
}
$class = (!empty($options['namespace']) ? $options['namespace'] : '\\think\\db\\connector\\') . ucwords($options['type']);
// 记录初始化信息

View File

@@ -474,7 +474,7 @@ class Input
if (is_scalar($data)) {
$data = (string) $data;
} else {
throw new \InvalidArgumentException('变量类型不允许' . gettype($data));
throw new \InvalidArgumentException('variable type error' . gettype($data));
}
}
}

View File

@@ -92,6 +92,19 @@ class Lang
return self::$lang[$range];
}
/**
* 获取语言定义(不区分大小写)
* @param string|null $name 语言变量
* @param array $vars 变量替换
* @param string $range 语言作用域
* @return mixed
*/
public static function has($name, $range = '')
{
$range = $range ?: self::$range;
return isset(self::$lang[$range][strtolower($name)]);
}
/**
* 获取语言定义(不区分大小写)
* @param string|null $name 语言变量

View File

@@ -147,7 +147,7 @@ class Loader
foreach ($map as $namespace => $path) {
$length = strlen($namespace);
if ('\\' !== $namespace[$length - 1]) {
throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator.");
throw new \InvalidArgumentException("PSR-4 error: A non-empty PSR-4 prefix must end with a namespace separator.");
}
self::$prefixLengthsPsr4[$namespace[0]][$namespace] = $length;
self::$prefixDirsPsr4[$namespace] = (array) $path;
@@ -292,7 +292,7 @@ class Loader
if (class_exists($class)) {
$model = new $class();
} else {
throw new ClassNotFoundException('class [ ' . $class . ' ] not exists', $class);
throw new ClassNotFoundException('class not exists:' . $class, $class);
}
}
self::$instance[$name . $layer] = $model;
@@ -321,7 +321,7 @@ class Loader
} elseif ($empty && class_exists($emptyClass = self::parseClass($module, $layer, $empty, $appendSuffix))) {
return new $emptyClass(Request::instance());
} else {
throw new ClassNotFoundException('class [ ' . $class . ' ] not exists', $class);
throw new ClassNotFoundException('class not exists:' . $class, $class);
}
}
@@ -357,7 +357,7 @@ class Loader
if (class_exists($class)) {
$validate = new $class;
} else {
throw new ClassNotFoundException('class [ ' . $class . ' ] not exists', $class);
throw new ClassNotFoundException('class not exists:' . $class, $class);
}
}
self::$instance[$name . $layer] = $validate;

View File

@@ -143,7 +143,7 @@ abstract class Paginator
public function total()
{
if ($this->simple) {
throw new \DomainException('简洁模式下不能获取数据总数');
throw new \DomainException('not support total');
}
return $this->total;
}
@@ -161,7 +161,7 @@ abstract class Paginator
public function lastPage()
{
if ($this->simple) {
throw new \DomainException('简洁模式下不能获取最后一页');
throw new \DomainException('not support last');
}
return $this->lastPage;
}

View File

@@ -123,7 +123,7 @@ class Response
if (is_scalar($data)) {
echo $data;
} elseif (!is_null($data)) {
throw new \InvalidArgumentException('不支持的数据类型输出' . gettype($data));
throw new \InvalidArgumentException('variable type error' . gettype($data));
}
if (function_exists('fastcgi_finish_request')) {

View File

@@ -95,7 +95,7 @@ class Session
// 检查驱动类
if (!class_exists($class) || !session_set_save_handler(new $class($config))) {
throw new ClassNotFoundException('error session handler', $class);
throw new ClassNotFoundException('error session handler:' . $class, $class);
}
}
if ($isDoStart) {

View File

@@ -1068,7 +1068,7 @@ class Template
$this->includeFile[$template] = filemtime($template);
return $template;
} else {
throw new TemplateNotFoundException('template not exist:' . $template, $template);
throw new TemplateNotFoundException('template not exists:' . $template, $template);
}
}

View File

@@ -1115,7 +1115,7 @@ class Validate
if (method_exists($class, $method)) {
return call_user_func_array([$class, $method], $params);
} else {
throw new \BadMethodCallException(__CLASS__ . ':' . $method . ' method not exist');
throw new \BadMethodCallException('method not exists:' . __CLASS__ . '->' . $method);
}
}
}

View File

@@ -36,7 +36,7 @@ class Apc
public function __construct($options = [])
{
if (!function_exists('apc_cache_info')) {
throw new \BadFunctionCallException('not support Apc');
throw new \BadFunctionCallException('not support: Apc');
}
if (!empty($options)) {
$this->options = array_merge($this->options, $options);

View File

@@ -35,7 +35,7 @@ class Memcache
public function __construct($options = [])
{
if (!extension_loaded('memcache')) {
throw new \BadFunctionCallException('not support memcache');
throw new \BadFunctionCallException('not support: memcache');
}
if (!empty($options)) {
$this->options = array_merge($this->options, $options);

View File

@@ -33,7 +33,7 @@ class Memcached
public function __construct($options = [])
{
if (!extension_loaded('memcached')) {
throw new \BadFunctionCallException('not support memcached');
throw new \BadFunctionCallException('not support: memcached');
}
if (!empty($options)) {
$this->options = array_merge($this->options, $options);

View File

@@ -42,7 +42,7 @@ class Redis
public function __construct($options = [])
{
if (!extension_loaded('redis')) {
throw new \BadFunctionCallException('not support redis');
throw new \BadFunctionCallException('not support: redis');
}
if (!empty($options)) {
$this->options = array_merge($this->options, $options);

View File

@@ -84,7 +84,7 @@ class Redisd
public function __construct($options = [])
{
if (!extension_loaded('redis')) {
throw new \BadFunctionCallException('not support redis');
throw new \BadFunctionCallException('not support: redis');
}
$this->options = $options = array_merge($this->options, $options);

View File

@@ -38,11 +38,11 @@ class Sae
public function __construct($options = [])
{
if (!function_exists('memcache_init')) {
throw new \BadFunctionCallException('请在SAE平台上运行代码。');
throw new \BadFunctionCallException('must run at sae');
}
$this->handler = memcache_init();
if (!$this->handler) {
throw new Exception('您未开通Memcache服务请在SAE管理平台初始化Memcache服务');
throw new Exception('memcache init error');
}
if (!empty($options)) {
$this->options = array_merge($this->options, $options);
@@ -113,7 +113,7 @@ class Sae
if (!$kv) {
$kv = new \SaeKV();
if (!$kv->init()) {
throw new Exception('您没有初始化KVDB请在SAE管理平台初始化KVDB服务');
throw new Exception('KVDB init error');
}
}
return $kv;

View File

@@ -38,7 +38,7 @@ class Sqlite implements CacheInterface
public function __construct($options = [])
{
if (!extension_loaded('sqlite')) {
throw new \BadFunctionCallException('not support sqlite');
throw new \BadFunctionCallException('not support: sqlite');
}
if (!empty($options)) {
$this->options = array_merge($this->options, $options);

View File

@@ -34,7 +34,7 @@ class Wincache
public function __construct($options = [])
{
if (!function_exists('wincache_ucache_info')) {
throw new \BadFunctionCallException('not support WinCache');
throw new \BadFunctionCallException('not support: WinCache');
}
if (!empty($options)) {
$this->options = array_merge($this->options, $options);

View File

@@ -34,7 +34,7 @@ class Xcache
public function __construct($options = [])
{
if (!function_exists('xcache_info')) {
throw new \BadFunctionCallException('not support Xcache');
throw new \BadFunctionCallException('not support: Xcache');
}
if (!empty($options)) {
$this->options = array_merge($this->options, $options);

View File

@@ -94,7 +94,7 @@ abstract class Builder
foreach ($data as $key => $val) {
if (!in_array($key, $fields, true)) {
if ($options['strict']) {
throw new Exception(' fields not exists :[' . $key . ']');
throw new Exception('fields not exists:[' . $key . ']');
}
} else {
$item = $this->parseKey($key);
@@ -611,7 +611,7 @@ abstract class Builder
foreach ($data as $key => $val) {
if (!in_array($key, $fields, true)) {
if ($options['strict']) {
throw new Exception(' fields not exists :[' . $key . ']');
throw new Exception('fields not exists:[' . $key . ']');
}
unset($data[$key]);
} elseif (is_scalar($val)) {

View File

@@ -84,7 +84,7 @@ class Query
$where[$name] = $args[0];
return $this->where($where)->value($args[1]);
} else {
throw new Exception(__CLASS__ . ':' . $method . ' method not exist');
throw new Exception('method not exist:' . __CLASS__ . '->' . $method);
}
}
@@ -1672,7 +1672,7 @@ class Query
$where[$field] = $data[$field];
} else {
// 如果缺少复合主键数据则不执行
throw new Exception('miss pk data');
throw new Exception('miss complex primary data');
}
unset($data[$field]);
}
@@ -1775,10 +1775,10 @@ class Query
}
} elseif (!empty($options['fail'])) {
if(!empty($this->model)){
throw new ModelNotFoundException('Data not Found', $this->model, $options);
throw new ModelNotFoundException('model data Not Found:' . $this->model , $this->model, $options);
}else{
throw new DataNotFoundException('Data not Found', $options['table'], $options);
}
throw new DataNotFoundException('table data not Found:' . $options['table'], $options['table'], $options);
}
}
return $resultSet;
}
@@ -1856,10 +1856,10 @@ class Query
}
} elseif (!empty($options['fail'])) {
if(!empty($this->model)){
throw new ModelNotFoundException('Data not Found', $this->model, $options);
throw new ModelNotFoundException('model data Not Found:' . $this->model , $this->model, $options);
}else{
throw new DataNotFoundException('Data not Found', $options['table'], $options);
}
throw new DataNotFoundException('table data not Found:' . $options['table'], $options['table'], $options);
}
} else {
$data = false;
}
@@ -1969,7 +1969,7 @@ class Query
if (empty($options['where'])) {
// 如果条件为空 不进行删除操作 除非设置 1=1
throw new Exception('no data to delete without where');
throw new Exception('delete without condition');
}
// 生成删除SQL语句
$sql = $this->builder()->delete($options);

View File

@@ -16,6 +16,7 @@ use think\App;
use think\Config;
use think\Console;
use think\console\Output;
use think\Lang;
use think\Log;
use think\Response;
@@ -40,14 +41,14 @@ class Handle
$data = [
'file' => $exception->getFile(),
'line' => $exception->getLine(),
'message' => $exception->getMessage(),
'message' => $this->getMessage($exception),
'code' => $this->getCode($exception),
];
$log = "[{$data['code']}]{$data['message']}[{$data['file']}:{$data['line']}]";
} else {
$data = [
'code' => $exception->getCode(),
'message' => $exception->getMessage(),
'code' => $this->getCode($exception),
'message' => $this->getMessage($exception),
];
$log = "[{$data['code']}]{$data['message']}";
}
@@ -121,7 +122,7 @@ class Handle
'name' => get_class($exception),
'file' => $exception->getFile(),
'line' => $exception->getLine(),
'message' => $exception->getMessage(),
'message' => $this->getMessage($exception),
'trace' => $exception->getTrace(),
'code' => $this->getCode($exception),
'source' => $this->getSourceCode($exception),
@@ -140,8 +141,8 @@ class Handle
} else {
// 部署模式仅显示 Code 和 Message
$data = [
'code' => $exception->getCode(),
'message' => $exception->getMessage(),
'code' => $this->getCode($exception),
'message' => $this->getMessage($exception),
];
if (!Config::get('show_error_msg')) {
@@ -189,6 +190,28 @@ class Handle
return $code;
}
/**
* 获取错误信息
* ErrorException则使用错误级别作为错误编码
* @param \Exception $exception
* @return string 错误信息
*/
protected function getMessage(Exception $exception)
{
$message = $exception->getMessage();
if (IS_CLI) {
return $message;
}
// 导入语言包
Lang::load(THINK_PATH . 'lang' . DS . Lang::detect() . EXT);
if (strpos($message,':')) {
$name = strstr($message, ':', true);
return Lang::has($name) ? Lang::get($name) . ' ' . strstr($message, ':') : $message;
} else {
return Lang::has($message) ? Lang::get($message) : $message;
}
}
/**
* 获取出错文件内容
* 获取错误的前9行和后9行

View File

@@ -615,7 +615,7 @@ class Relation
$query = clone $this->parent->db();
return $query->table($this->middle)->insert($pivot);
} else {
throw new Exception(' miss relation data');
throw new Exception('miss relation data');
}
}
@@ -680,7 +680,7 @@ class Relation
}
return call_user_func_array([$db, $method], $args);
} else {
throw new Exception(__CLASS__ . ':' . $method . ' method not exist');
throw new Exception('method not exists:' . __CLASS__ . '->' . $method);
}
}

View File

@@ -76,7 +76,7 @@ class Collection extends \think\Collection
if ($this->paginator && method_exists($this->paginator, $method)) {
return call_user_func_array([$this->paginator, $method], $args);
} else {
throw new Exception(__CLASS__ . ':' . $method . ' method not exist');
throw new Exception('method not exists:' . __CLASS__ . '->' . $method);
}
}
}

View File

@@ -41,7 +41,7 @@ class Memcache extends SessionHandler
{
// 检测php环境
if (!extension_loaded('memcache')) {
throw new Exception('_NOT_SUPPERT_:memcache');
throw new Exception('not support:memcache');
}
$this->handler = new \Memcache;
// 支持集群

View File

@@ -42,7 +42,7 @@ class Memcached extends SessionHandler
{
// 检测php环境
if (!extension_loaded('memcached')) {
throw new Exception('_NOT_SUPPERT_:memcached');
throw new Exception('not support:memcached');
}
$this->handler = new \Memcached;
// 设置连接超时时间(单位:毫秒)

View File

@@ -42,7 +42,7 @@ class Redis extends SessionHandler
{
// 检测php环境
if (!extension_loaded('redis')) {
throw new Exception('_NOT_SUPPERT_:redis');
throw new Exception('not support:redis');
}
$this->handler = new \Redis;

View File

@@ -255,7 +255,7 @@ class TagLib
$must = explode(',', $tag['must']);
foreach ($must as $name) {
if (!isset($result[$name])) {
throw new Exception('_PARAM_ERROR_:' . $name);
throw new Exception('tag attr must:' . $name);
}
}
}
@@ -272,7 +272,7 @@ class TagLib
$result['expression'] = rtrim($result['expression'], '/');
$result['expression'] = trim($result['expression']);
} elseif (empty($this->tags[$name]) || !empty($this->tags[$name]['attr'])) {
throw new Exception('_XML_TAG_ERROR_:' . $name);
throw new Exception('tag error:' . $name);
}
}
return $result;

View File

@@ -30,7 +30,7 @@ class File
}
// 生成模板缓存文件
if (false === file_put_contents($cacheFile, $content)) {
throw new Exception('cache write error :' . $cacheFile, 11602);
throw new Exception('cache write error:' . $cacheFile, 11602);
}
}

View File

@@ -46,7 +46,7 @@ class Sae
// 添加写入时间
$content = time() . $content;
if (!$this->mc->set($cacheFile, $content, MEMCACHE_COMPRESSED, 0)) {
throw new Exception('sae mc write error :' . $cacheFile);
throw new Exception('sae mc write error:' . $cacheFile);
} else {
$this->contents[$cacheFile] = $content;
return true;

View File

@@ -63,7 +63,7 @@ class Php
}
// 模板不存在 抛出异常
if (!is_file($template)) {
throw new TemplateNotFoundException('template file not exists:' . $template, $template);
throw new TemplateNotFoundException('template not exists:' . $template, $template);
}
// 记录视图信息
App::$debug && Log::record('[ VIEW ] ' . $template . ' [ ' . var_export(array_keys($data), true) . ' ]', 'info');

View File

@@ -73,7 +73,7 @@ class Think
}
// 模板不存在 抛出异常
if (!is_file($template)) {
throw new TemplateNotFoundException('template file not exists:' . $template, $template);
throw new TemplateNotFoundException('template not exists:' . $template, $template);
}
// 记录视图信息
App::$debug && Log::record('[ VIEW ] ' . $template . ' [ ' . var_export(array_keys($data), true) . ' ]', 'info');

View File

@@ -39,7 +39,7 @@ trait Instance
if (0 === strpos($method, '_') && is_callable([self::$instance, $call])) {
return call_user_func_array([self::$instance, $call], $params);
} else {
throw new Exception("not exists method:" . $method);
throw new Exception("method not exists:" . $method);
}
}
}