diff --git a/README-CN.md b/README-CN.md index 28b0228..2f88e0a 100644 --- a/README-CN.md +++ b/README-CN.md @@ -899,6 +899,10 @@ cp ./.git-hooks/* ./git/hooks # DONE +- v0.8.5(2018/01/06) + - 使用error_report并设置为0,使得错误统一由框架的错误handle处理 + - 修复当__coreError发生时,会把__coreError和正常返回值同事输出的问题 + - v0.8.1(2018/06/24) - 重构日志类 - 增加bin目录统一存放脚本文件 diff --git a/README.md b/README.md index f872b00..225f080 100644 --- a/README.md +++ b/README.md @@ -895,8 +895,8 @@ project address: [https://github.com/TIGERB/easy-php](https://github.com/TIGERB/ - v0.8.5(2018/01/06) - fix error_report - - fix - + - fix when __coreError is occur the response is output 200 but it also out put __coreError + - v0.8.1(2018/06/24) - use easy log - add folder bin diff --git a/framework/App.php b/framework/App.php index 5a3990e..98a20e6 100644 --- a/framework/App.php +++ b/framework/App.php @@ -258,12 +258,29 @@ public function run(Closure $request) * @return json */ public function response(Closure $closure) + { + /** + * 错误处理handle里 fatal error是通过register_shutdown_function注册的函数获取的 + * 防止fatal error时输出两会json 所以response也注册到register_shutdown_function的队列中 + * + * TODO 这个地方要重构 + */ + register_shutdown_function([$this, 'responseShutdownFun'], $closure); + } + + /** + * shutdown response + * + * @param Closure $closure + * @return void + */ + public function responseShutdownFun(Closure $closure) { if ($this->notOutput === true) { return; } if ($this->runningMode === 'cli') { - $closure()->cliModeSuccess($this->responseData); + $closure($this)->cliModeSuccess($this->responseData); return; } @@ -271,9 +288,9 @@ public function response(Closure $closure) ->config['rest_response']; if ($useRest) { - $closure()->restSuccess($this->responseData); + $closure($this)->restSuccess($this->responseData); } - $closure()->response($this->responseData); + $closure($this)->response($this->responseData); } /** diff --git a/framework/Request.php b/framework/Request.php index 4f3e75e..2a80109 100644 --- a/framework/Request.php +++ b/framework/Request.php @@ -11,6 +11,7 @@ namespace Framework; +use Framework\App; use Framework\Exceptions\CoreHttpException; /** diff --git a/framework/Response.php b/framework/Response.php index fbb1cd8..b0c7e9f 100644 --- a/framework/Response.php +++ b/framework/Response.php @@ -11,6 +11,8 @@ namespace Framework; +use Framework\App; + /** * 响应 * @@ -18,12 +20,20 @@ */ class Response { + + /** + * app instance + * + * @var Framework\App + */ + private $app = null; + /** * 构造函数 */ - public function __construct() + public function __construct(App $app) { - #code... + $this->app = $app; } /** diff --git a/framework/exceptions/CoreHttpException.php b/framework/exceptions/CoreHttpException.php index 0e1214b..fc6e222 100644 --- a/framework/exceptions/CoreHttpException.php +++ b/framework/exceptions/CoreHttpException.php @@ -22,6 +22,9 @@ */ class CoreHttpException extends Exception { + + private static $hadException = false; + /** * 响应异常code * @@ -78,9 +81,19 @@ public function reponse() // log Log::error(json_encode($data)); - // response - header('Content-Type:Application/json; Charset=utf-8'); - die(json_encode($data, JSON_UNESCAPED_UNICODE)); + /** + * response + * + * 错误处理handle里 fatal error是通过register_shutdown_function注册的函数获取的 + * 防止fatal error时输出两会json 所以response也注册到register_shutdown_function的队列中 + * + * TODO 这个地方要重构 + */ + register_shutdown_function(function () use ($data) + { + header('Content-Type:Application/json; Charset=utf-8'); + die(json_encode($data, JSON_UNESCAPED_UNICODE)); + }); } /** @@ -120,6 +133,17 @@ public function reponseSwoole() */ public static function reponseErr($e) { + /** + * 防止同时输出多个错误json + */ + if (self::$hadException) { + // log + Log::error(json_encode($data)); + return; + } + + self::$hadException = true; + $data = [ '__coreError' => [ 'code' => 500, @@ -135,7 +159,7 @@ public static function reponseErr($e) Log::error(json_encode($data)); header('Content-Type:Application/json; Charset=utf-8'); - die(json_encode($data)); + die(json_encode($data, JSON_UNESCAPED_UNICODE)); } /** diff --git a/framework/handles/ErrorHandle.php b/framework/handles/ErrorHandle.php index ec9a3e3..6cc1259 100644 --- a/framework/handles/ErrorHandle.php +++ b/framework/handles/ErrorHandle.php @@ -31,6 +31,13 @@ class ErrorHandle implements Handle */ private $mode = 'fmp'; + /** + * app instance + * + * @var Framework\App + */ + private $app = null; + /** * 错误信息 * @@ -56,6 +63,7 @@ public function __construct() public function register(App $app) { $this->mode = $app->runningMode; + $this->app = $app; // do not report the error by php self // E_ERROR | E_WARNING | E_PARSE | E_NOTICE | E_ALL diff --git a/framework/run.php b/framework/run.php index b9ad74c..5655e2e 100644 --- a/framework/run.php +++ b/framework/run.php @@ -52,42 +52,50 @@ * Load all kinds of handles */ $app->load(function () { - // 加载预环境参数机制 Loading env handle + // 加载预环境参数机制 + // Loading env handle return new EnvHandle(); }); $app->load(function () { - // 加载预定义配置机制 Loading config handle + // 加载预定义配置机制 + // Loading config handle return new ConfigHandle(); }); $app->load(function () { - // 加载日志处理机制 Loading log handle + // 加载日志处理机制 + // Loading log handle return new LogHandle(); }); $app->load(function () { - // 加载错误处理机制 Loading error handle + // 加载错误处理机制 + // Loading error handle return new ErrorHandle(); }); $app->load(function () { - // 加载异常处理机制 Loading exception handle. + // 加载异常处理机制 + // Loading exception handle return new ExceptionHandle(); }); $app->load(function () { - // 加载nosql机制 Loading nosql handle + // 加载nosql机制 + // Loading nosql handle return new NosqlHandle(); }); $app->load(function () { - // 加载用户自定义机制 Loading user-defined handle + // 加载用户自定义机制 + // Loading user-defined handle return new UserDefinedHandle(); }); $app->load(function () { - // 加载路由机制 Loading route handle + // 加载路由机制 + // Loading route handle return new RouterHandle(); }); @@ -117,8 +125,8 @@ * * End */ - $app->response(function () { - return new Response(); + $app->response(function ($app) { + return new Response($app); }); } catch (CoreHttpException $e) { /**