Init Repo

This commit is contained in:
root
2019-09-06 23:53:10 +08:00
commit f0ef89dfbb
7905 changed files with 914138 additions and 0 deletions

View File

@ -0,0 +1,32 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: yunwuxin <448901948@qq.com>
// +----------------------------------------------------------------------
namespace think\exception;
class ClassNotFoundException extends \RuntimeException
{
protected $class;
public function __construct($message, $class = '')
{
$this->message = $message;
$this->class = $class;
}
/**
* 获取类名
* @access public
* @return string
*/
public function getClass()
{
return $this->class;
}
}

View File

@ -0,0 +1,43 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://zjzit.cn>
// +----------------------------------------------------------------------
namespace think\exception;
use think\Exception;
/**
* Database相关异常处理类
*/
class DbException extends Exception
{
/**
* DbException constructor.
* @param string $message
* @param array $config
* @param string $sql
* @param int $code
*/
public function __construct($message, array $config, $sql, $code = 10500)
{
$this->message = $message;
$this->code = $code;
$this->setData('Database Status', [
'Error Code' => $code,
'Error Message' => $message,
'Error SQL' => $sql,
]);
unset($config['username'], $config['password']);
$this->setData('Database Config', $config);
}
}

View File

@ -0,0 +1,57 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://zjzit.cn>
// +----------------------------------------------------------------------
namespace think\exception;
use think\Exception;
/**
* ThinkPHP错误异常
* 主要用于封装 set_error_handler 和 register_shutdown_function 得到的错误
* 除开从 think\Exception 继承的功能
* 其他和PHP系统\ErrorException功能基本一样
*/
class ErrorException extends Exception
{
/**
* 用于保存错误级别
* @var integer
*/
protected $severity;
/**
* 错误异常构造函数
* @param integer $severity 错误级别
* @param string $message 错误详细信息
* @param string $file 出错文件路径
* @param integer $line 出错行号
* @param array $context 错误上下文,会包含错误触发处作用域内所有变量的数组
*/
public function __construct($severity, $message, $file, $line, array $context = [])
{
$this->severity = $severity;
$this->message = $message;
$this->file = $file;
$this->line = $line;
$this->code = 0;
empty($context) || $this->setData('Error Context', $context);
}
/**
* 获取错误级别
* @return integer 错误级别
*/
final public function getSeverity()
{
return $this->severity;
}
}

View File

@ -0,0 +1,282 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: yunwuxin <448901948@qq.com>
// +----------------------------------------------------------------------
namespace think\exception;
use Exception;
use think\App;
use think\Config;
use think\console\Output;
use think\Lang;
use think\Log;
use think\Response;
class Handle
{
protected $render;
protected $ignoreReport = [
'\\think\\exception\\HttpException',
];
public function setRender($render)
{
$this->render = $render;
}
/**
* Report or log an exception.
*
* @param \Exception $exception
* @return void
*/
public function report(Exception $exception)
{
if (!$this->isIgnoreReport($exception)) {
// 收集异常数据
if (App::$debug) {
$data = [
'file' => $exception->getFile(),
'line' => $exception->getLine(),
'message' => $this->getMessage($exception),
'code' => $this->getCode($exception),
];
$log = "[{$data['code']}]{$data['message']}[{$data['file']}:{$data['line']}]";
} else {
$data = [
'code' => $this->getCode($exception),
'message' => $this->getMessage($exception),
];
$log = "[{$data['code']}]{$data['message']}";
}
if (Config::get('record_trace')) {
$log .= "\r\n" . $exception->getTraceAsString();
}
Log::record($log, 'error');
}
}
protected function isIgnoreReport(Exception $exception)
{
foreach ($this->ignoreReport as $class) {
if ($exception instanceof $class) {
return true;
}
}
return false;
}
/**
* Render an exception into an HTTP response.
*
* @param \Exception $e
* @return Response
*/
public function render(Exception $e)
{
if ($this->render && $this->render instanceof \Closure) {
$result = call_user_func_array($this->render, [$e]);
if ($result) {
return $result;
}
}
if ($e instanceof HttpException) {
return $this->renderHttpException($e);
} else {
return $this->convertExceptionToResponse($e);
}
}
/**
* @param Output $output
* @param Exception $e
*/
public function renderForConsole(Output $output, Exception $e)
{
if (App::$debug) {
$output->setVerbosity(Output::VERBOSITY_DEBUG);
}
$output->renderException($e);
}
/**
* @param HttpException $e
* @return Response
*/
protected function renderHttpException(HttpException $e)
{
$status = $e->getStatusCode();
$template = Config::get('http_exception_template');
if (!App::$debug && !empty($template[$status])) {
return Response::create($template[$status], 'view', $status)->assign(['e' => $e]);
} else {
return $this->convertExceptionToResponse($e);
}
}
/**
* @param Exception $exception
* @return Response
*/
protected function convertExceptionToResponse(Exception $exception)
{
// 收集异常数据
if (App::$debug) {
// 调试模式,获取详细的错误信息
$data = [
'name' => get_class($exception),
'file' => $exception->getFile(),
'line' => $exception->getLine(),
'message' => $this->getMessage($exception),
'trace' => $exception->getTrace(),
'code' => $this->getCode($exception),
'source' => $this->getSourceCode($exception),
'datas' => $this->getExtendData($exception),
'tables' => [
'GET Data' => $_GET,
'POST Data' => $_POST,
'Files' => $_FILES,
'Cookies' => $_COOKIE,
'Session' => isset($_SESSION) ? $_SESSION : [],
'Server/Request Data' => $_SERVER,
'Environment Variables' => $_ENV,
'ThinkPHP Constants' => $this->getConst(),
],
];
} else {
// 部署模式仅显示 Code 和 Message
$data = [
'code' => $this->getCode($exception),
'message' => $this->getMessage($exception),
];
if (!Config::get('show_error_msg')) {
// 不显示详细错误信息
$data['message'] = Config::get('error_message');
}
}
//保留一层
while (ob_get_level() > 1) {
ob_end_clean();
}
$data['echo'] = ob_get_clean();
ob_start();
extract($data);
include Config::get('exception_tmpl');
// 获取并清空缓存
$content = ob_get_clean();
$response = new Response($content, 'html');
if ($exception instanceof HttpException) {
$statusCode = $exception->getStatusCode();
$response->header($exception->getHeaders());
}
if (!isset($statusCode)) {
$statusCode = 500;
}
$response->code($statusCode);
return $response;
}
/**
* 获取错误编码
* ErrorException则使用错误级别作为错误编码
* @param \Exception $exception
* @return integer 错误编码
*/
protected function getCode(Exception $exception)
{
$code = $exception->getCode();
if (!$code && $exception instanceof ErrorException) {
$code = $exception->getSeverity();
}
return $code;
}
/**
* 获取错误信息
* ErrorException则使用错误级别作为错误编码
* @param \Exception $exception
* @return string 错误信息
*/
protected function getMessage(Exception $exception)
{
$message = $exception->getMessage();
if (IS_CLI) {
return $message;
}
if (strpos($message, ':')) {
$name = strstr($message, ':', true);
$message = Lang::has($name) ? Lang::get($name) . strstr($message, ':') : $message;
} elseif (strpos($message, ',')) {
$name = strstr($message, ',', true);
$message = Lang::has($name) ? Lang::get($name) . ':' . substr(strstr($message, ','), 1) : $message;
} elseif (Lang::has($message)) {
$message = Lang::get($message);
}
return $message;
}
/**
* 获取出错文件内容
* 获取错误的前9行和后9行
* @param \Exception $exception
* @return array 错误文件内容
*/
protected function getSourceCode(Exception $exception)
{
// 读取前9行和后9行
$line = $exception->getLine();
$first = ($line - 9 > 0) ? $line - 9 : 1;
try {
$contents = file($exception->getFile());
$source = [
'first' => $first,
'source' => array_slice($contents, $first - 1, 19),
];
} catch (Exception $e) {
$source = [];
}
return $source;
}
/**
* 获取异常扩展信息
* 用于非调试模式html返回类型显示
* @param \Exception $exception
* @return array 异常类定义的扩展数据
*/
protected function getExtendData(Exception $exception)
{
$data = [];
if ($exception instanceof \think\Exception) {
$data = $exception->getData();
}
return $data;
}
/**
* 获取常量列表
* @return array 常量列表
*/
private static function getConst()
{
return get_defined_constants(true)['user'];
}
}

View File

@ -0,0 +1,36 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: yunwuxin <448901948@qq.com>
// +----------------------------------------------------------------------
namespace think\exception;
class HttpException extends \RuntimeException
{
private $statusCode;
private $headers;
public function __construct($statusCode, $message = null, \Exception $previous = null, array $headers = [], $code = 0)
{
$this->statusCode = $statusCode;
$this->headers = $headers;
parent::__construct($message, $code, $previous);
}
public function getStatusCode()
{
return $this->statusCode;
}
public function getHeaders()
{
return $this->headers;
}
}

View File

@ -0,0 +1,33 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: yunwuxin <448901948@qq.com>
// +----------------------------------------------------------------------
namespace think\exception;
use think\Response;
class HttpResponseException extends \RuntimeException
{
/**
* @var Response
*/
protected $response;
public function __construct(Response $response)
{
$this->response = $response;
}
public function getResponse()
{
return $this->response;
}
}

View File

@ -0,0 +1,39 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://zjzit.cn>
// +----------------------------------------------------------------------
namespace think\exception;
/**
* PDO异常处理类
* 重新封装了系统的\PDOException类
*/
class PDOException extends DbException
{
/**
* PDOException constructor.
* @param \PDOException $exception
* @param array $config
* @param string $sql
* @param int $code
*/
public function __construct(\PDOException $exception, array $config, $sql, $code = 10501)
{
$error = $exception->errorInfo;
$this->setData('PDO Error Info', [
'SQLSTATE' => $error[0],
'Driver Error Code' => isset($error[1]) ? $error[1] : 0,
'Driver Error Message' => isset($error[2]) ? $error[2] : '',
]);
parent::__construct($exception->getMessage(), $config, $sql, $code);
}
}

View File

@ -0,0 +1,22 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: yunwuxin <448901948@qq.com>
// +----------------------------------------------------------------------
namespace think\exception;
class RouteNotFoundException extends HttpException
{
public function __construct()
{
parent::__construct(404, 'Route Not Found');
}
}

View File

@ -0,0 +1,33 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: yunwuxin <448901948@qq.com>
// +----------------------------------------------------------------------
namespace think\exception;
class TemplateNotFoundException extends \RuntimeException
{
protected $template;
public function __construct($message, $template = '')
{
$this->message = $message;
$this->template = $template;
}
/**
* 获取模板文件
* @access public
* @return string
*/
public function getTemplate()
{
return $this->template;
}
}

View File

@ -0,0 +1,47 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: yunwuxin <448901948@qq.com>
// +----------------------------------------------------------------------
namespace think\exception;
class ThrowableError extends \ErrorException
{
public function __construct(\Throwable $e)
{
if ($e instanceof \ParseError) {
$message = 'Parse error: ' . $e->getMessage();
$severity = E_PARSE;
} elseif ($e instanceof \TypeError) {
$message = 'Type error: ' . $e->getMessage();
$severity = E_RECOVERABLE_ERROR;
} else {
$message = 'Fatal error: ' . $e->getMessage();
$severity = E_ERROR;
}
parent::__construct(
$message,
$e->getCode(),
$severity,
$e->getFile(),
$e->getLine()
);
$this->setTrace($e->getTrace());
}
protected function setTrace($trace)
{
$traceReflector = new \ReflectionProperty('Exception', 'trace');
$traceReflector->setAccessible(true);
$traceReflector->setValue($this, $trace);
}
}

View File

@ -0,0 +1,33 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: yunwuxin <448901948@qq.com>
// +----------------------------------------------------------------------
namespace think\exception;
class ValidateException extends \RuntimeException
{
protected $error;
public function __construct($error)
{
$this->error = $error;
$this->message = is_array($error) ? implode("\n\r", $error) : $error;
}
/**
* 获取验证错误信息
* @access public
* @return array|string
*/
public function getError()
{
return $this->error;
}
}