From d394d29fb0c8abd3b2e480f522f55c6a7bb8bbff Mon Sep 17 00:00:00 2001 From: sunxyw <31698606+sunxyw@users.noreply.github.com> Date: Tue, 27 Dec 2022 18:14:10 +0800 Subject: [PATCH] allow flexible exception handling (#81) --- src/OneBot/Exception/ExceptionHandler.php | 35 ++++++++++++++++------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/src/OneBot/Exception/ExceptionHandler.php b/src/OneBot/Exception/ExceptionHandler.php index 054d198..1b0fdea 100644 --- a/src/OneBot/Exception/ExceptionHandler.php +++ b/src/OneBot/Exception/ExceptionHandler.php @@ -19,16 +19,7 @@ class ExceptionHandler implements ExceptionHandlerInterface protected function __construct() { - $whoops_class = 'Whoops\Run'; - $collision_class = 'NunoMaduro\Collision\Handler'; - if (class_exists($collision_class) && class_exists($whoops_class)) { - /* @phpstan-ignore-next-line */ - $this->whoops = new $whoops_class(); - $this->whoops->allowQuit(false); - $this->whoops->writeToOutput(false); - $this->whoops->pushHandler(new $collision_class()); - $this->whoops->register(); - } + $this->tryEnableCollision(); } public function getWhoops() @@ -64,4 +55,28 @@ protected function handle0(\Throwable $e): void $this->whoops->handleException($e); } + + protected function tryEnableCollision($solution_repo = null): void + { + $whoops_class = 'Whoops\Run'; + $collision_namespace = 'NunoMaduro\Collision'; + $collision_handler = "{$collision_namespace}\\Handler"; + $collision_writer = "{$collision_namespace}\\Writer"; + $collision_repo = "{$collision_namespace}\\Contracts\\SolutionsRepository"; + if (class_exists($collision_handler) && class_exists($whoops_class)) { + if ($solution_repo instanceof $collision_repo) { + // @phpstan-ignore-next-line + $writer = new $collision_writer($solution_repo); + } else { + // @phpstan-ignore-next-line + $writer = new $collision_writer(); + } + + $this->whoops = new $whoops_class(); + $this->whoops->allowQuit(false); + $this->whoops->writeToOutput(false); + $this->whoops->pushHandler(new $collision_handler($writer)); + $this->whoops->register(); + } + } }