From f78ba76767c94b9403578f59631adbfef737ff31 Mon Sep 17 00:00:00 2001 From: yunwuxin <448901948@qq.com> Date: Sun, 15 May 2016 10:14:17 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E5=BC=82=E5=B8=B8=E5=A4=84?= =?UTF-8?q?=E7=90=86=E5=92=8C=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Console.php | 24 ++++++++--------- library/think/Error.php | 10 ++++--- library/think/exception/Handle.php | 14 ++++++++-- mode/console.php | 3 +-- mode/console/Error.php | 26 ------------------- .../thinkphp/library/think/exceptionTest.php | 7 ++--- 6 files changed, 36 insertions(+), 48 deletions(-) delete mode 100644 mode/console/Error.php diff --git a/library/think/Console.php b/library/think/Console.php index 24b85134..884d010a 100644 --- a/library/think/Console.php +++ b/library/think/Console.php @@ -16,7 +16,7 @@ use think\console\helper\Formatter as FormatterHelper; use think\console\helper\Process as ProcessHelper; use think\console\helper\Question as QuestionHelper; use think\console\helper\Set as HelperSet; -use think\console\Input; +use think\console\Input as ConsoleInput; use think\console\input\Argument as InputArgument; use think\console\input\Definition as InputDefinition; use think\console\input\Option as InputOption; @@ -74,7 +74,7 @@ class Console */ public function run() { - $input = new Input(); + $input = new ConsoleInput(); $output = new Output(); $this->configureIO($input, $output); @@ -112,11 +112,11 @@ class Console /** * 执行指令 - * @param Input $input + * @param ConsoleInput $input * @param Output $output * @return int */ - public function doRun(Input $input, Output $output) + public function doRun(ConsoleInput $input, Output $output) { if (true === $input->hasParameterOption(['--version', '-V'])) { $output->writeln($this->getLongVersion()); @@ -129,7 +129,7 @@ class Console if (true === $input->hasParameterOption(['--help', '-h'])) { if (!$name) { $name = 'help'; - $input = new Input(['help']); + $input = new ConsoleInput(['help']); } else { $this->wantHelps = true; } @@ -137,7 +137,7 @@ class Console if (!$name) { $name = $this->defaultCommand; - $input = new Input([$this->defaultCommand]); + $input = new ConsoleInput([$this->defaultCommand]); } $command = $this->find($name); @@ -640,10 +640,10 @@ class Console /** * 配置基于用户的参数和选项的输入和输出实例。 - * @param Input $input 输入实例 + * @param ConsoleInput $input 输入实例 * @param Output $output 输出实例 */ - protected function configureIO(Input $input, Output $output) + protected function configureIO(ConsoleInput $input, Output $output) { if (true === $input->hasParameterOption(['--ansi'])) { $output->setDecorated(true); @@ -683,22 +683,22 @@ class Console /** * 执行指令 * @param Command $command 指令实例 - * @param Input $input 输入实例 + * @param ConsoleInput $input 输入实例 * @param Output $output 输出实例 * @return int * @throws \Exception */ - protected function doRunCommand(Command $command, Input $input, Output $output) + protected function doRunCommand(Command $command, ConsoleInput $input, Output $output) { return $command->run($input, $output); } /** * 获取指令的基础名称 - * @param Input $input + * @param ConsoleInput $input * @return string */ - protected function getCommandName(Input $input) + protected function getCommandName(ConsoleInput $input) { return $input->getFirstArgument(); } diff --git a/library/think/Error.php b/library/think/Error.php index 26fa20c9..08b05b3b 100644 --- a/library/think/Error.php +++ b/library/think/Error.php @@ -14,6 +14,7 @@ namespace think; use think\exception\ErrorException; use think\exception\Handle; use think\exception\ThrowableError; +use think\console\Output as ConsoleOutput; class Error { @@ -35,7 +36,7 @@ class Error /** * Exception Handler - * @param \Exception $e + * @param \Exception|\Throwable $e */ public static function appException($e) { @@ -44,8 +45,11 @@ class Error } self::getExceptionHandler()->report($e); - - self::getExceptionHandler()->render($e)->send(); + if(IS_CLI){ + self::getExceptionHandler()->renderForConsole(new ConsoleOutput, $e); + }else{ + self::getExceptionHandler()->render($e)->send(); + } } /** diff --git a/library/think/exception/Handle.php b/library/think/exception/Handle.php index 890ef93e..a43122b1 100644 --- a/library/think/exception/Handle.php +++ b/library/think/exception/Handle.php @@ -13,6 +13,8 @@ namespace think\exception; use Exception; use think\Config; +use think\Console; +use think\console\Output; use think\Log; use think\Response; @@ -79,6 +81,15 @@ class Handle } } + /** + * @param $output + * @param Exception $e + */ + public function renderForConsole(Output $output, Exception $e) + { + (new Console)->renderException($e, $output); + } + /** * @param HttpException $e * @return \think\Response @@ -110,8 +121,7 @@ class Handle 'code' => $this->getCode($exception), 'source' => $this->getSourceCode($exception), 'datas' => $this->getExtendData($exception), - - 'tables' => [ + 'tables' => [ 'GET Data' => $_GET, 'POST Data' => $_POST, 'Files' => $_FILES, diff --git a/mode/console.php b/mode/console.php index 5f0c488e..1da52480 100644 --- a/mode/console.php +++ b/mode/console.php @@ -23,8 +23,7 @@ return [ ], // 别名定义 'alias' => [ - 'think\App' => MODE_PATH . 'console/App' . EXT, - 'think\Error' => MODE_PATH . 'console/Error' . EXT + 'think\App' => MODE_PATH . 'console/App' . EXT ], // 配置文件 'config' => THINK_PATH . 'convention' . EXT diff --git a/mode/console/Error.php b/mode/console/Error.php deleted file mode 100644 index a0537e10..00000000 --- a/mode/console/Error.php +++ /dev/null @@ -1,26 +0,0 @@ - -// +---------------------------------------------------------------------- - -namespace think; - -class Error -{ - /** - * 注册异常处理 - * @return void - */ - public static function register() - { - ini_set("display_errors","Off"); - //TODO - } - -} \ No newline at end of file diff --git a/tests/thinkphp/library/think/exceptionTest.php b/tests/thinkphp/library/think/exceptionTest.php index e6aa13f8..66957ce6 100644 --- a/tests/thinkphp/library/think/exceptionTest.php +++ b/tests/thinkphp/library/think/exceptionTest.php @@ -18,6 +18,7 @@ namespace tests\thinkphp\library\think; use ReflectionMethod; use think\Exception as ThinkException; +use think\exception\HttpException; class MyException extends ThinkException { @@ -29,9 +30,9 @@ class exceptionTest extends \PHPUnit_Framework_TestCase public function testGetHttpStatus() { try { - throw new ThinkException("Error Processing Request", 1); - } catch (ThinkException $e) { - $this->assertEquals(500, $e->getHttpStatus()); + throw new HttpException(404, "Error Processing Request"); + } catch (HttpException $e) { + $this->assertEquals(404, $e->getStatusCode()); } }