Skip to content

Commit 8a9c020

Browse files
authored
Optimized DataFormatterInterface which uses object instead of array as inputs. (#4367)
* Optimized code for `DataFormatterInterface`. * Update CHANGELOG-3.0.md
1 parent f14ad5f commit 8a9c020

File tree

2 files changed

+27
-23
lines changed

2 files changed

+27
-23
lines changed

src/CoreMiddleware.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
use Hyperf\HttpMessage\Base\Response;
1616
use Hyperf\HttpServer\Router\Dispatched;
1717
use Hyperf\Rpc\Contract\DataFormatterInterface;
18+
use Hyperf\Rpc\ErrorResponse;
1819
use Hyperf\Rpc\Protocol;
20+
use Hyperf\Rpc\Response as RPCResponse;
1921
use Hyperf\RpcMultiplex\Contract\HttpMessageBuilderInterface;
2022
use Psr\Container\ContainerInterface;
2123
use Psr\Http\Message\ResponseInterface;
@@ -89,13 +91,15 @@ protected function buildErrorData(ServerRequestInterface $request, int $code, st
8991
{
9092
$id = $request->getAttribute(Constant::REQUEST_ID);
9193

92-
return $this->dataFormatter->formatErrorResponse([$id, $code, $message ?? Response::getReasonPhraseByCode($code), $throwable]);
94+
return $this->dataFormatter->formatErrorResponse(
95+
new ErrorResponse($id, $code, $message ?? Response::getReasonPhraseByCode($code), $throwable)
96+
);
9397
}
9498

9599
protected function buildData(ServerRequestInterface $request, $response): array
96100
{
97101
$id = $request->getAttribute(Constant::REQUEST_ID);
98102

99-
return $this->dataFormatter->formatResponse([$id, $response]);
103+
return $this->dataFormatter->formatResponse(new RPCResponse($id, $response));
100104
}
101105
}

src/DataFormatter.php

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313

1414
use Hyperf\Rpc\Context;
1515
use Hyperf\Rpc\Contract\DataFormatterInterface;
16+
use Hyperf\Rpc\ErrorResponse;
17+
use Hyperf\Rpc\Request;
18+
use Hyperf\Rpc\Response;
1619
use Hyperf\RpcClient\Exception\RequestException;
1720
use Hyperf\RpcMultiplex\Contract\DataFetcherInterface;
1821
use Hyperf\Utils\Codec\Json;
@@ -26,44 +29,41 @@ public function __construct(Context $context)
2629
$this->context = $context;
2730
}
2831

29-
public function formatRequest(array $data): array
32+
public function formatRequest(Request $request): array
3033
{
31-
[$path, $params, $id] = $data;
3234
return [
33-
Constant::ID => $id,
34-
Constant::PATH => $path,
35-
Constant::DATA => $params,
35+
Constant::ID => $request->getId(),
36+
Constant::PATH => $request->getPath(),
37+
Constant::DATA => $request->getParams(),
3638
Constant::CONTEXT => $this->context->getData(),
3739
];
3840
}
3941

40-
public function formatResponse(array $data): array
42+
public function formatResponse(Response $response): array
4143
{
42-
[$id, $result] = $data;
4344
return [
44-
Constant::ID => $id,
45-
Constant::RESULT => $result,
45+
Constant::ID => $response->getId(),
46+
Constant::RESULT => $response->getResult(),
4647
Constant::CONTEXT => $this->context->getData(),
4748
];
4849
}
4950

50-
public function formatErrorResponse(array $data): array
51+
public function formatErrorResponse(ErrorResponse $response): array
5152
{
52-
[$id, $code, $message, $data] = $data;
53-
54-
if (isset($data) && $data instanceof \Throwable) {
55-
$data = [
56-
'class' => get_class($data),
57-
'code' => $data->getCode(),
58-
'message' => $data->getMessage(),
53+
$exception = $response->getException();
54+
if ($exception instanceof \Throwable) {
55+
$exception = [
56+
'class' => get_class($exception),
57+
'code' => $exception->getCode(),
58+
'message' => $exception->getMessage(),
5959
];
6060
}
6161
return [
62-
Constant::ID => $id ?? null,
62+
Constant::ID => $response->getId(),
6363
Constant::ERROR => [
64-
Constant::CODE => $code,
65-
Constant::MESSAGE => $message,
66-
Constant::DATA => $data,
64+
Constant::CODE => $response->getCode(),
65+
Constant::MESSAGE => $response->getMessage(),
66+
Constant::DATA => $exception,
6767
],
6868
Constant::CONTEXT => $this->context->getData(),
6969
];

0 commit comments

Comments
 (0)