This commit is contained in:
2023-04-01 01:02:14 +08:00
parent a659375e1f
commit 779c13f25a
2 changed files with 78 additions and 55 deletions
+39 -16
View File
@@ -20,8 +20,8 @@ use Throwable;
*/ */
class Json class Json
{ {
/** /**
* @param $data * @param $data
* @return false|string * @return false|string
@@ -36,8 +36,8 @@ class Json
} }
return $data; return $data;
} }
/** /**
* @param $data * @param $data
* @param bool $asArray * @param bool $asArray
@@ -51,8 +51,33 @@ class Json
if (!is_string($data)) return null; if (!is_string($data)) return null;
return json_decode($data, $asArray); return json_decode($data, $asArray);
} }
/**
* @param string $message
* @param int $code
* @param array|string $data
* @param int $count
* @return string
*/
public static function jsonFail(string $message, int $code = 500, array|string $data = [], int $count = 0): string
{
return json_encode(['code' => $code, 'param' => $data, 'message' => $message, 'count' => $count], JSON_UNESCAPED_UNICODE);
}
/**
* @param string $message
* @param array|string $data
* @param int $count
* @return string
*/
public static function jsonSuccess(array|string $data = [], string $message = "ok", int $count = 0): string
{
return json_encode(['code' => 0, 'param' => $data, 'message' => $message, 'count' => $count], JSON_UNESCAPED_UNICODE);
}
/** /**
* @param $code * @param $code
* @param string|array $message * @param string|array $message
@@ -65,11 +90,9 @@ class Json
{ {
$params['code'] = $code; $params['code'] = $code;
if (!is_string($message)) { if (!is_string($message)) {
$params['message'] = 'System success.'; $params['message'] = 'System success.';
$params['param'] = $message; $params['param'] = $message;
if (!empty($data)) { $params['exPageInfo'] = $data;
$params['exPageInfo'] = $data;
}
} else { } else {
$params['message'] = $message; $params['message'] = $message;
$params['param'] = $data; $params['param'] = $data;
@@ -87,8 +110,8 @@ class Json
} }
return static::encode($params); return static::encode($params);
} }
/** /**
* @param Throwable|Error $throwable * @param Throwable|Error $throwable
* @return bool|string * @return bool|string
@@ -103,8 +126,8 @@ class Json
]; ];
return Json::encode($array); return Json::encode($array);
} }
/** /**
* @param $state * @param $state
* @param $body * @param $body
@@ -115,7 +138,7 @@ class Json
{ {
$params['state'] = $state; $params['state'] = $state;
$params['body'] = ArrayAccess::toArray($body); $params['body'] = ArrayAccess::toArray($body);
return static::encode($params); return static::encode($params);
} }
} }
+39 -39
View File
@@ -31,24 +31,24 @@ use ReflectionException;
*/ */
class ErrorHandler extends Component implements ErrorInterface class ErrorHandler extends Component implements ErrorInterface
{ {
/** @var ?IFormatter $message */ /** @var ?IFormatter $message */
private ?IFormatter $message = NULL; private ?IFormatter $message = NULL;
/** /**
* @var string * @var string
*/ */
public string $category = 'app'; public string $category = 'app';
/** /**
* @var EventDispatch * @var EventDispatch
*/ */
#[Inject(EventDispatch::class)] #[Inject(EventDispatch::class)]
public EventDispatch $dispatch; public EventDispatch $dispatch;
/** /**
* @param array|Closure|null $callback * @param array|Closure|null $callback
* @return void * @return void
@@ -62,8 +62,8 @@ class ErrorHandler extends Component implements ErrorInterface
} }
set_exception_handler($callback); set_exception_handler($callback);
} }
/** /**
* @param array|Closure|null $callback * @param array|Closure|null $callback
* @return void * @return void
@@ -77,8 +77,8 @@ class ErrorHandler extends Component implements ErrorInterface
} }
set_error_handler($callback); set_error_handler($callback);
} }
/** /**
* @param array|Closure|null $callback * @param array|Closure|null $callback
* @return void * @return void
@@ -92,8 +92,8 @@ class ErrorHandler extends Component implements ErrorInterface
} }
register_shutdown_function($callback); register_shutdown_function($callback);
} }
/** /**
* @return void * @return void
* @throws ContainerExceptionInterface * @throws ContainerExceptionInterface
@@ -107,19 +107,19 @@ class ErrorHandler extends Component implements ErrorInterface
if (empty($lastError) || $lastError['type'] !== E_ERROR) { if (empty($lastError) || $lastError['type'] !== E_ERROR) {
return; return;
} }
$this->category = 'shutdown'; $this->category = 'shutdown';
$messages = explode(PHP_EOL, $lastError['message']); $messages = explode(PHP_EOL, $lastError['message']);
$message = array_shift($messages); $message = array_shift($messages);
$this->dispatch->dispatch(new OnShutdown()); $this->dispatch->dispatch(new OnShutdown());
$this->sendError($message, $lastError['file'], $lastError['line']); $this->sendError($message, $lastError['file'], $lastError['line']);
} }
/** /**
* @param \Throwable $exception * @param \Throwable $exception
* *
@@ -131,13 +131,13 @@ class ErrorHandler extends Component implements ErrorInterface
public function exceptionHandler(\Throwable $exception) public function exceptionHandler(\Throwable $exception)
{ {
$this->category = 'exception'; $this->category = 'exception';
$this->dispatch->dispatch(new OnAfterRequest()); $this->dispatch->dispatch(new OnAfterRequest());
$this->sendError($exception->getMessage(), $exception->getFile(), $exception->getLine()); $this->sendError($exception->getMessage(), $exception->getFile(), $exception->getLine());
} }
/** /**
* @throws \ErrorException * @throws \ErrorException
* @throws ContainerExceptionInterface * @throws ContainerExceptionInterface
@@ -147,22 +147,22 @@ class ErrorHandler extends Component implements ErrorInterface
public function errorHandler() public function errorHandler()
{ {
$error = func_get_args(); $error = func_get_args();
$path = ['file' => $error[2], 'line' => $error[3]]; $path = ['file' => $error[2], 'line' => $error[3]];
if ($error[0] === 0) { if ($error[0] === 0) {
$error[0] = 500; $error[0] = 500;
} }
$data = Json::to(500, $error[1], $path); $data = Json::jsonFail($error[1], 500, $path);
$this->logger->error('On error handler', [$data]); $this->logger->error('On error handler', [$data]);
$this->dispatch->dispatch(new OnAfterRequest()); $this->dispatch->dispatch(new OnAfterRequest());
throw new \ErrorException($error[1], $error[0], 1, $error[2], $error[3]); throw new \ErrorException($error[1], $error[0], 1, $error[2], $error[3]);
} }
/** /**
* @param $message * @param $message
* @param $file * @param $file
@@ -174,14 +174,14 @@ class ErrorHandler extends Component implements ErrorInterface
public function sendError($message, $file, $line, int $code = 500): bool|string public function sendError($message, $file, $line, int $code = 500): bool|string
{ {
$path = ['file' => $file, 'line' => $line]; $path = ['file' => $file, 'line' => $line];
$data = Json::to($code, $this->category . ': ' . $message, $path); $data = Json::jsonFail($this->category . ': ' . $message, $code, $path);
file_put_contents('php://output', $data . PHP_EOL, FILE_APPEND); file_put_contents('php://output', $data . PHP_EOL, FILE_APPEND);
return $data; return $data;
} }
/** /**
* @return mixed * @return mixed
*/ */
@@ -191,7 +191,7 @@ class ErrorHandler extends Component implements ErrorInterface
$this->message = NULL; $this->message = NULL;
return $message->getData(); return $message->getData();
} }
/** /**
* @return bool * @return bool
*/ */
@@ -199,7 +199,7 @@ class ErrorHandler extends Component implements ErrorInterface
{ {
return $this->message !== NULL; return $this->message !== NULL;
} }
/** /**
* @param $message * @param $message
* @param string $category * @param string $category