2023-03-15 20:40:49 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace SPC\exception;
|
|
|
|
|
|
|
|
|
|
class ExceptionHandler
|
|
|
|
|
{
|
|
|
|
|
protected $whoops;
|
|
|
|
|
|
|
|
|
|
private static $obj;
|
|
|
|
|
|
|
|
|
|
private 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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function getInstance(): ExceptionHandler
|
|
|
|
|
{
|
|
|
|
|
if (self::$obj === null) {
|
|
|
|
|
self::$obj = new self();
|
|
|
|
|
}
|
|
|
|
|
return self::$obj;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getWhoops()
|
|
|
|
|
{
|
|
|
|
|
return $this->whoops;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 处理异常
|
|
|
|
|
*/
|
|
|
|
|
public function handle(\Throwable $e): void
|
|
|
|
|
{
|
|
|
|
|
if (is_null($this->whoops)) {
|
|
|
|
|
logger()->error('Uncaught ' . get_class($e) . ': ' . $e->getMessage() . ' at ' . $e->getFile() . '(' . $e->getLine() . ')');
|
|
|
|
|
logger()->error($e->getTraceAsString());
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
$this->whoops->handleException($e);
|
2023-08-08 20:23:38 +08:00
|
|
|
|
|
|
|
|
logger()->critical('You can report this exception to static-php-cli GitHub repo.');
|
2023-03-15 20:40:49 +08:00
|
|
|
}
|
|
|
|
|
}
|