feat: 增加了inArray、hasExtends方法

This commit is contained in:
李东云
2023-03-14 18:29:05 +08:00
parent 7c1efe1a1f
commit de96e288c3
11 changed files with 139 additions and 78 deletions

View File

@@ -29,4 +29,4 @@ class ClassicStyleMiddleware implements MiddlewareInterface
$this->apiStyleService->set(ApiStyleService::CLASSIC);
return $handler->handle($request);
}
}
}

View File

@@ -39,13 +39,59 @@ class CommonCoreMiddleware extends CoreMiddleware
private ApiStyleService $apiStyleService;
/**
* Transfer the non-standard response content to a standard response object.
* @template TKey of array-key
* @template TValue
* @param null|array<TKey, TValue>|Arrayable<TKey, TValue>|Jsonable|string $response
* @inheritDoc
*/
protected function transferToClassicResponse($response, ServerRequestInterface $request): ResponseInterface
protected function transferToResponse($response, ServerRequestInterface $request): ResponseInterface
{
$style = $this->apiStyleService->get();
if ($style === ApiStyleService::RESTFUL) {
return $this->transferToRestfulResponse($response, $request);
} else {
return $this->transferToClassicResponse($response, $request);
}
}
/**
* @param array|string|Arrayable|Jsonable|null $response
* @param ServerRequestInterface $request
* @return ResponseInterface
*/
protected function transferToRestfulResponse(Arrayable|Jsonable|array|string|null $response, ServerRequestInterface $request): ResponseInterface
{
// 分页数据
if ($response instanceof LengthAwarePaginatorInterface) {
$fact_response = $this->response()
->withHeader('Per-Page', (string)$response->perPage())
->withHeader('Total', (string)$response->total())
->withHeader('Current-Page', (string)$response->currentPage())
->withHeader('Total-Pages', (string)$response->hasPages());
$fact_response = $this->utilsService->extendLinkToHeader($fact_response, $response->nextPageUrl(), 'next');
$fact_response = $this->utilsService->extendLinkToHeader(
$fact_response,
$response->url($response->lastPage()),
'last'
);
$fact_response = $this->utilsService->extendLinkToHeader($fact_response, $response->url(1), 'first');
return $this->utilsService->extendLinkToHeader(
$fact_response,
$response->previousPageUrl(),
'prev'
);
}
return parent::transferToResponse($response, $request);
}
/**
* @param array|string|Arrayable|Jsonable|null $response
* @param ServerRequestInterface $request
* @return ResponseInterface
*/
protected function transferToClassicResponse(
Arrayable|Jsonable|array|string|null $response,
ServerRequestInterface $request
): ResponseInterface {
$code_name = config('common.response.code_name');
$message_name = config('common.response.message_name');
$data_name = config('common.response.data_name');
@@ -108,49 +154,4 @@ class CommonCoreMiddleware extends CoreMiddleware
new SwooleStream((string)$response)
);
}
protected function transferToResponse($response, ServerRequestInterface $request): ResponseInterface
{
$style = $this->apiStyleService->get();
if ($style === ApiStyleService::RESTFUL) {
return $this->transferToRestfulResponse($response, $request);
} else {
return $this->transferToClassicResponse($response, $request);
}
}
/**
* @template TKey of array-key
* @template TValue
* @param null|array<TKey, TValue>|Arrayable<TKey, TValue>|Jsonable|string $response
* @param ServerRequestInterface $request
* @return ResponseInterface
*/
protected function transferToRestfulResponse($response, ServerRequestInterface $request): ResponseInterface
{
// 分页数据
if ($response instanceof LengthAwarePaginatorInterface) {
$fact_response = $this->response()
->withHeader('Per-Page', (string)$response->perPage())
->withHeader('Total', (string)$response->total())
->withHeader('Current-Page', (string)$response->currentPage())
->withHeader('Total-Pages', (string)$response->hasPages());
$fact_response = $this->utilsService->extendLinkToHeader($fact_response, $response->nextPageUrl(), 'next');
$fact_response = $this->utilsService->extendLinkToHeader(
$fact_response,
$response->url($response->lastPage()),
'last'
);
$fact_response = $this->utilsService->extendLinkToHeader($fact_response, $response->url(1), 'first');
return $this->utilsService->extendLinkToHeader(
$fact_response,
$response->previousPageUrl(),
'prev'
);
}
return parent::transferToResponse($response, $request);
}
}

View File

@@ -29,4 +29,4 @@ class RestStyleMiddleware implements MiddlewareInterface
$this->apiStyleService->set(ApiStyleService::RESTFUL);
return $handler->handle($request);
}
}
}

View File

@@ -40,7 +40,6 @@ class SessionMiddleware extends \Hyperf\Session\Middleware\SessionMiddleware
ResponseInterface $response,
SessionInterface $session
): ResponseInterface {
$cookie = new Cookie(
name: $session->getName(),
value: $session->getId(),
@@ -71,5 +70,4 @@ class SessionMiddleware extends \Hyperf\Session\Middleware\SessionMiddleware
}
return $expirationDate;
}
}
}

View File

@@ -21,7 +21,11 @@ class ApiStyleService
Context::set(self::class, config('common.response.restful') ? self::RESTFUL : self::CLASSIC);
}
public function set($style): static
/**
* @param literal-string $style
* @return $this
*/
public function set(string $style): static
{
Context::set(self::class, $style === self::RESTFUL ? self::RESTFUL : self::CLASSIC);
return $this;
@@ -33,4 +37,4 @@ class ApiStyleService
? ApiStyleService::RESTFUL
: ApiStyleService::CLASSIC);
}
}
}

View File

@@ -10,6 +10,10 @@ use Psr\Http\Message\ServerRequestInterface;
*/
class ExtendService
{
public function __construct(private UtilsService $utils)
{
}
/**
* @param ServerRequestInterface|null $request
* @param array<string, string>|null $params
@@ -29,6 +33,7 @@ class ExtendService
return [];
}
/**
* @return array<string, string>
*/
@@ -37,6 +42,12 @@ class ExtendService
return Context::get(self::class) ?? [];
}
public function hasExtends(): bool
{
$extends = Context::get(self::class);
return is_array($extends) && count($extends) > 0;
}
/**
* 判断是否传入了此扩展
*
@@ -44,8 +55,8 @@ class ExtendService
*
* @return bool
*/
public function hasExtends(string $field): bool
public function hasExtend(string $field): bool
{
return Context::has(self::class) && isset(array_flip(Context::get(self::class))[$field]);
return $this->utils->inArray($field, Context::get(self::class));
}
}

View File

@@ -37,8 +37,10 @@ class HttpRequestService
private array $options = [];
/**
* @param string $url
* @param array<string, string|int> $params
* @return ResponseInterface
* @throws GuzzleException
* @throws ClientExceptionInterface
*/
public function requestGet(string $url, array $params = []): ResponseInterface
{
@@ -55,14 +57,15 @@ class HttpRequestService
/**
* @param string $url
* @param array $data
* @param array $params
* @param array<string, mixed> $data
* @param array<string, string|int> $params
* @return ResponseInterface
* @throws GuzzleException
*/
public function requestPost(string $url, string $data = '', array $params = []): ResponseInterface
public function requestPost(string $url, array $data = [], array $params = []): ResponseInterface
{
$request = new Request('post', $url, [], $data);
$data = Json::encode($data);
$request = new Request('post', $url, ['Content-Type' => 'application/json'], Utils::streamFor($data));
return $this->getClient()->send($request, [
'params' => $params,
]);
@@ -71,8 +74,8 @@ class HttpRequestService
/**
* @param string $url
* @param array $data
* @param array $params
* @param array<string, mixed> $data
* @param array<string, string|int> $params
* @return ResponseInterface
* @throws GuzzleException
*/
@@ -92,7 +95,7 @@ class HttpRequestService
/**
* 定制 options
* @param array $options
* @param array<string, mixed> $options
* @return $this
*/
public function setOptions(array $options): HttpRequestService
@@ -116,4 +119,4 @@ class HttpRequestService
throw new BadResponseException($response->getReasonPhrase(), $request, $response);
}
}
}
}

View File

@@ -328,4 +328,17 @@ class UtilsService
'unit' => $unitToUpper ? strtoupper($unit[$i]) : $unit[$i],
];
}
/**
* 更快的判断数组元素方法
* @param float|int|string $needle
* @param array<int, mixed> $haystack
* @return bool
*/
public function inArray(float|int|string $needle, array $haystack): bool
{
$list = array_flip($haystack);
return isset($list[$needle]);
}
}