Files
kiri-core/kiri-engine/Error/ErrorHandler.php
T

144 lines
3.4 KiB
PHP
Raw Normal View History

2022-01-09 03:50:38 +08:00
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/4/26 0026
* Time: 10:00
*/
declare(strict_types=1);
namespace Kiri\Error;
2022-06-22 19:02:30 +08:00
use Closure;
2022-01-09 03:50:38 +08:00
use Exception;
2022-02-14 17:59:56 +08:00
use Kiri;
2022-01-09 03:50:38 +08:00
use Kiri\Abstracts\Component;
2022-06-22 10:53:59 +08:00
use Psr\Container\ContainerExceptionInterface;
2023-07-31 23:09:00 +08:00
use Psr\Container\ContainerInterface;
2022-06-22 10:53:59 +08:00
use Psr\Container\NotFoundExceptionInterface;
use ReflectionException;
2023-07-31 23:09:00 +08:00
use Kiri\Events\OnSystemError;
2022-01-09 03:50:38 +08:00
/**
* Class ErrorHandler
*
2022-01-12 14:10:33 +08:00
* @package Kiri\Base
2022-01-09 03:50:38 +08:00
* @property-read $asError
*/
class ErrorHandler extends Component implements ErrorInterface
{
2023-07-26 17:26:47 +08:00
/**
* @var string
*/
public string $category = 'app';
2023-07-31 23:09:00 +08:00
/**
* @param ContainerInterface $container
*/
public function __construct(public ContainerInterface $container)
{
parent::__construct();
}
2023-07-26 17:26:47 +08:00
/**
* @param array|Closure|null $callback
* @return void
2023-07-31 23:09:00 +08:00
* @throws
2023-07-26 17:26:47 +08:00
*/
public function registerExceptionHandler(null|array|Closure $callback): void
{
if (empty($callback)) {
$callback = [$this, 'exceptionHandler'];
} else if (is_array($callback) && is_string($callback[0])) {
2023-07-31 23:09:00 +08:00
$callback[0] = $this->container->get($callback[0]);
2023-07-06 16:53:53 +08:00
}
2023-07-26 17:26:47 +08:00
set_exception_handler($callback);
}
/**
* @param array|Closure|null $callback
* @return void
2023-07-31 23:09:00 +08:00
* @throws
2023-07-26 17:26:47 +08:00
*/
public function registerErrorHandler(null|array|Closure $callback): void
{
if (empty($callback)) {
$callback = [$this, 'errorHandler'];
} else if (is_array($callback) && is_string($callback[0])) {
2023-07-31 23:09:00 +08:00
$callback[0] = $this->container->get($callback[0]);
2023-07-26 17:26:47 +08:00
}
set_error_handler($callback);
}
/**
* @param array|Closure|null $callback
* @return void
2023-07-31 23:09:00 +08:00
* @throws
2023-07-26 17:26:47 +08:00
*/
public function registerShutdownHandler(null|array|Closure $callback): void
{
if (empty($callback)) {
$callback = [$this, 'shutdown'];
} else if (is_array($callback) && is_string($callback[0])) {
2023-07-31 23:09:00 +08:00
$callback[0] = $this->container->get($callback[0]);
2023-07-26 17:26:47 +08:00
}
register_shutdown_function($callback);
}
/**
* @return void
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws ReflectionException
* @throws Exception
*/
public function shutdown(): void
{
$lastError = error_get_last();
if (empty($lastError) || $lastError['type'] !== E_ERROR) {
return;
}
2023-08-18 20:57:30 +08:00
trigger_print_error($lastError['message']);
2023-07-31 23:09:00 +08:00
event(new OnSystemError());
2023-07-26 17:26:47 +08:00
}
/**
* @param \Throwable $exception
*
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws Exception
*/
public function exceptionHandler(\Throwable $exception): void
{
$this->category = 'exception';
2023-08-11 14:02:51 +08:00
trigger_print_error($exception);
2023-07-26 17:26:47 +08:00
2023-07-31 23:09:00 +08:00
event(new OnSystemError());
2023-07-26 17:26:47 +08:00
}
/**
2023-08-24 12:01:34 +08:00
* @throws \ErrorException
2023-07-26 17:26:47 +08:00
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws ReflectionException
*/
public function errorHandler()
{
2023-08-24 12:01:34 +08:00
$error = func_get_args();
2023-07-26 17:26:47 +08:00
2023-07-31 23:09:00 +08:00
event(new OnSystemError());
2023-07-26 17:26:47 +08:00
2023-08-24 12:01:34 +08:00
throw new \ErrorException($error[1], $error[0], 1, $error[2], $error[3]);
2023-07-26 17:26:47 +08:00
}
2022-01-09 03:50:38 +08:00
}