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

186 lines
4.1 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;
use Kiri\Core\Json;
use Kiri\Events\EventDispatch;
2022-06-22 10:53:59 +08:00
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use ReflectionException;
2023-04-16 01:45:34 +08:00
use Kiri\Di\Inject\Container;
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-04-01 01:02:14 +08:00
2022-06-22 16:50:37 +08:00
/**
* @var string
*/
2022-01-09 03:50:38 +08:00
public string $category = 'app';
2023-04-17 00:41:30 +08:00
2022-01-09 03:50:38 +08:00
/**
2022-06-22 19:02:30 +08:00
* @param array|Closure|null $callback
2022-06-22 16:57:11 +08:00
* @return void
2023-04-17 00:41:30 +08:00
* @throws ReflectionException
2022-06-22 16:57:11 +08:00
*/
2022-06-22 19:02:30 +08:00
public function registerExceptionHandler(null|array|Closure $callback): void
2022-06-22 16:57:11 +08:00
{
2022-06-22 19:01:44 +08:00
if (empty($callback)) {
$callback = [$this, 'exceptionHandler'];
} else if (is_array($callback) && is_string($callback[0])) {
$callback[0] = Kiri::getDi()->get($callback[0]);
}
set_exception_handler($callback);
2022-06-22 16:57:11 +08:00
}
2023-04-17 00:41:30 +08:00
2022-06-22 16:57:11 +08:00
/**
2022-06-22 19:02:30 +08:00
* @param array|Closure|null $callback
2022-06-22 16:57:11 +08:00
* @return void
2023-04-17 00:41:30 +08:00
* @throws ReflectionException
2022-01-09 03:50:38 +08:00
*/
2022-06-22 19:02:30 +08:00
public function registerErrorHandler(null|array|Closure $callback): void
2022-01-09 03:50:38 +08:00
{
2022-06-22 19:01:44 +08:00
if (empty($callback)) {
$callback = [$this, 'errorHandler'];
} else if (is_array($callback) && is_string($callback[0])) {
$callback[0] = Kiri::getDi()->get($callback[0]);
}
set_error_handler($callback);
2022-01-09 03:50:38 +08:00
}
2023-04-19 10:39:28 +08:00
2022-06-22 16:57:11 +08:00
/**
2022-06-22 19:02:30 +08:00
* @param array|Closure|null $callback
2022-06-22 16:57:11 +08:00
* @return void
2023-04-19 10:39:28 +08:00
* @throws ReflectionException
2022-06-22 16:57:11 +08:00
*/
2022-06-22 19:02:30 +08:00
public function registerShutdownHandler(null|array|Closure $callback): void
2022-06-22 16:57:11 +08:00
{
2022-06-22 19:01:44 +08:00
if (empty($callback)) {
$callback = [$this, 'shutdown'];
} else if (is_array($callback) && is_string($callback[0])) {
$callback[0] = Kiri::getDi()->get($callback[0]);
}
register_shutdown_function($callback);
2022-06-22 16:57:11 +08:00
}
2023-04-01 01:02:14 +08:00
2022-01-09 03:50:38 +08:00
/**
2022-06-22 10:53:59 +08:00
* @return void
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws ReflectionException
2022-01-09 03:50:38 +08:00
* @throws Exception
*/
2022-06-22 10:53:59 +08:00
public function shutdown(): void
2022-01-09 03:50:38 +08:00
{
$lastError = error_get_last();
if (empty($lastError) || $lastError['type'] !== E_ERROR) {
return;
}
2023-04-01 01:02:14 +08:00
2022-01-09 03:50:38 +08:00
$this->category = 'shutdown';
2023-04-01 01:02:14 +08:00
2022-01-09 03:50:38 +08:00
$messages = explode(PHP_EOL, $lastError['message']);
2023-04-01 01:02:14 +08:00
2022-01-09 03:50:38 +08:00
$message = array_shift($messages);
2023-04-01 01:02:14 +08:00
2023-04-19 10:39:28 +08:00
event(new Kiri\Events\OnSystemError());
2023-04-01 01:02:14 +08:00
2022-01-09 03:50:38 +08:00
$this->sendError($message, $lastError['file'], $lastError['line']);
}
2023-04-01 01:02:14 +08:00
2022-01-09 03:50:38 +08:00
/**
* @param \Throwable $exception
*
2022-06-22 10:53:59 +08:00
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
2022-01-09 03:50:38 +08:00
* @throws Exception
*/
2023-04-19 10:39:28 +08:00
public function exceptionHandler(\Throwable $exception): void
2022-01-09 03:50:38 +08:00
{
$this->category = 'exception';
2023-04-17 00:41:30 +08:00
2023-04-19 10:39:28 +08:00
event(new Kiri\Events\OnSystemError());
2023-04-01 01:02:14 +08:00
2022-02-14 17:59:56 +08:00
$this->sendError($exception->getMessage(), $exception->getFile(), $exception->getLine());
2022-01-09 03:50:38 +08:00
}
2023-04-01 01:02:14 +08:00
2022-01-09 03:50:38 +08:00
/**
2022-06-22 10:53:59 +08:00
* @throws \ErrorException
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws ReflectionException
2022-01-09 03:50:38 +08:00
*/
public function errorHandler()
{
$error = func_get_args();
2023-04-01 01:02:14 +08:00
2022-02-14 17:59:56 +08:00
$path = ['file' => $error[2], 'line' => $error[3]];
2023-04-01 01:02:14 +08:00
2022-01-09 03:50:38 +08:00
if ($error[0] === 0) {
$error[0] = 500;
}
2023-04-01 01:02:14 +08:00
$data = Json::jsonFail($error[1], 500, $path);
2023-04-17 00:41:30 +08:00
2023-07-06 16:53:53 +08:00
if (!empty($data)) {
2023-07-26 16:45:28 +08:00
error($data, []);
2023-07-06 16:53:53 +08:00
}
2023-04-19 10:39:28 +08:00
event(new Kiri\Events\OnSystemError());
2023-04-01 01:02:14 +08:00
2022-01-09 03:50:38 +08:00
throw new \ErrorException($error[1], $error[0], 1, $error[2], $error[3]);
}
2023-04-01 01:02:14 +08:00
2022-01-09 03:50:38 +08:00
/**
* @param $message
* @param $file
* @param $line
* @param int $code
* @return false|string
* @throws Exception
*/
2022-06-22 16:29:42 +08:00
public function sendError($message, $file, $line, int $code = 500): bool|string
2022-01-09 03:50:38 +08:00
{
$path = ['file' => $file, 'line' => $line];
2023-04-01 01:02:14 +08:00
$data = Json::jsonFail($this->category . ': ' . $message, $code, $path);
2022-02-14 17:59:56 +08:00
file_put_contents('php://output', $data . PHP_EOL, FILE_APPEND);
2023-04-01 01:02:14 +08:00
2022-01-09 03:50:38 +08:00
return $data;
}
2023-04-16 01:45:34 +08:00
2023-04-01 01:02:14 +08:00
2022-01-09 03:50:38 +08:00
/**
* @param $message
* @param string $category
*
* @throws Exception
*/
public function writer($message, string $category = 'app')
{
2022-06-22 16:29:42 +08:00
Kiri::getLogger()->debug($category, [$message]);
2022-01-09 03:50:38 +08:00
}
}