diff --git a/.phpstorm.meta.php b/.phpstorm.meta.php index 3872ac74..4fcce2d1 100644 --- a/.phpstorm.meta.php +++ b/.phpstorm.meta.php @@ -5,13 +5,15 @@ namespace PHPSTORM_META { // Reflect use Kiri\Di\Container; use Kiri\Di\ContainerInterface; + use Psr\Container\ContainerInterface as SC; override(ContainerInterface::get(0), map('@')); + override(SC::get(0), map('@')); override(Container::get(0), map('@')); override(Container::make(0), map('@')); override(Container::create(0), map('@')); // override(\Hyperf\Utils\Context::get(0), map('@')); -// override(\make(0), map('@')); + override(\make(0), map('@')); override(\di(0), map('@')); override(\duplicate(0), map('@')); diff --git a/kiri-engine/Error/ErrorHandler.php b/kiri-engine/Error/ErrorHandler.php index baa60f2a..d59cfda6 100644 --- a/kiri-engine/Error/ErrorHandler.php +++ b/kiri-engine/Error/ErrorHandler.php @@ -13,9 +13,14 @@ use Exception; use Kiri; use Kiri\Abstracts\Component; use Kiri\Core\Json; +use Kiri\Annotation\Inject; use Kiri\Events\EventDispatch; use Kiri\Message\Events\OnAfterRequest; use Kiri\Message\Handler\Formatter\IFormatter; +use Psr\Container\ContainerExceptionInterface; +use Psr\Container\NotFoundExceptionInterface; +use Kiri\Server\Events\OnShutdown; +use ReflectionException; /** * Class ErrorHandler @@ -31,6 +36,14 @@ class ErrorHandler extends Component implements ErrorInterface public string $category = 'app'; + + /** + * @var EventDispatch + */ + #[Inject(EventDispatch::class)] + public EventDispatch $dispatch; + + /** * 错误处理注册 */ @@ -42,9 +55,13 @@ class ErrorHandler extends Component implements ErrorInterface } /** + * @return void + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + * @throws ReflectionException * @throws Exception */ - public function shutdown() + public function shutdown(): void { $lastError = error_get_last(); if (empty($lastError) || $lastError['type'] !== E_ERROR) { @@ -57,6 +74,8 @@ class ErrorHandler extends Component implements ErrorInterface $message = array_shift($messages); + $this->dispatch->dispatch(new OnShutdown()); + $this->sendError($message, $lastError['file'], $lastError['line']); } @@ -64,22 +83,26 @@ class ErrorHandler extends Component implements ErrorInterface /** * @param \Throwable $exception * + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + * @throws ReflectionException * @throws Exception */ public function exceptionHandler(\Throwable $exception) { $this->category = 'exception'; - di(EventDispatch::class)->dispatch(new OnAfterRequest()); + $this->dispatch->dispatch(new OnAfterRequest()); $this->sendError($exception->getMessage(), $exception->getFile(), $exception->getLine()); } /** - * @throws Exception - * - * 以异常形式抛出错误,防止执行后续程序 + * @throws \ErrorException + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + * @throws ReflectionException */ public function errorHandler() { @@ -95,7 +118,7 @@ class ErrorHandler extends Component implements ErrorInterface $this->logger->error('On error handler', [$data]); - di(EventDispatch::class)->dispatch(new OnAfterRequest()); + $this->dispatch->dispatch(new OnAfterRequest()); throw new \ErrorException($error[1], $error[0], 1, $error[2], $error[3]); } @@ -116,8 +139,6 @@ class ErrorHandler extends Component implements ErrorInterface file_put_contents('php://output', $data . PHP_EOL, FILE_APPEND); - write($data, $this->category); - return $data; }