Compare commits

...

2 Commits

Author SHA1 Message Date
李东云
f34b2c9e21 chore(release): 1.0.0-alpha.16 2023-12-04 09:13:05 +00:00
李东云
eee68cab8a refactor: 迁移到 hyperf3.1和 pestphp2
Signed-off-by: 李东云 <dongyu.li@luxcreo.ai>
2023-12-04 17:12:12 +08:00
24 changed files with 2845 additions and 1330 deletions

19
.idea/HDK-Core.iml generated
View File

@@ -16,7 +16,6 @@
<excludeFolder url="file://$MODULE_DIR$/vendor/alibabacloud/tea-utils" />
<excludeFolder url="file://$MODULE_DIR$/vendor/alibabacloud/tea-xml" />
<excludeFolder url="file://$MODULE_DIR$/vendor/composer" />
<excludeFolder url="file://$MODULE_DIR$/vendor/cooper/hyperf-pest" />
<excludeFolder url="file://$MODULE_DIR$/vendor/doctrine/deprecations" />
<excludeFolder url="file://$MODULE_DIR$/vendor/doctrine/inflector" />
<excludeFolder url="file://$MODULE_DIR$/vendor/doctrine/instantiator" />
@@ -121,7 +120,6 @@
<excludeFolder url="file://$MODULE_DIR$/vendor/sebastian/object-enumerator" />
<excludeFolder url="file://$MODULE_DIR$/vendor/sebastian/object-reflector" />
<excludeFolder url="file://$MODULE_DIR$/vendor/sebastian/recursion-context" />
<excludeFolder url="file://$MODULE_DIR$/vendor/sebastian/resource-operations" />
<excludeFolder url="file://$MODULE_DIR$/vendor/sebastian/type" />
<excludeFolder url="file://$MODULE_DIR$/vendor/sebastian/version" />
<excludeFolder url="file://$MODULE_DIR$/vendor/seld/jsonlint" />
@@ -160,6 +158,23 @@
<excludeFolder url="file://$MODULE_DIR$/vendor/teapot/status-code" />
<excludeFolder url="file://$MODULE_DIR$/vendor/theseer/tokenizer" />
<excludeFolder url="file://$MODULE_DIR$/vendor/vlucas/phpdotenv" />
<excludeFolder url="file://$MODULE_DIR$/vendor/brianium/paratest" />
<excludeFolder url="file://$MODULE_DIR$/vendor/fidry/cpu-core-counter" />
<excludeFolder url="file://$MODULE_DIR$/vendor/friendsofhyperf/pest-plugin-hyperf" />
<excludeFolder url="file://$MODULE_DIR$/vendor/friendsofhyperf/rector" />
<excludeFolder url="file://$MODULE_DIR$/vendor/hyperf/stdlib" />
<excludeFolder url="file://$MODULE_DIR$/vendor/hyperf/testing" />
<excludeFolder url="file://$MODULE_DIR$/vendor/jean85/pretty-package-versions" />
<excludeFolder url="file://$MODULE_DIR$/vendor/nunomaduro/termwind" />
<excludeFolder url="file://$MODULE_DIR$/vendor/pestphp/pest-plugin-arch" />
<excludeFolder url="file://$MODULE_DIR$/vendor/phpdocumentor/reflection-common" />
<excludeFolder url="file://$MODULE_DIR$/vendor/phpdocumentor/reflection-docblock" />
<excludeFolder url="file://$MODULE_DIR$/vendor/phpdocumentor/type-resolver" />
<excludeFolder url="file://$MODULE_DIR$/vendor/phpstan/phpdoc-parser" />
<excludeFolder url="file://$MODULE_DIR$/vendor/rector/rector" />
<excludeFolder url="file://$MODULE_DIR$/vendor/swow/psr7-plus" />
<excludeFolder url="file://$MODULE_DIR$/vendor/ta-tikoma/phpunit-architecture-test" />
<excludeFolder url="file://$MODULE_DIR$/vendor/webmozart/assert" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />

31
.idea/php.xml generated
View File

@@ -34,9 +34,7 @@
<path value="$PROJECT_DIR$/vendor/jetbrains/phpstorm-attributes" />
<path value="$PROJECT_DIR$/vendor/php-di/phpdoc-reader" />
<path value="$PROJECT_DIR$/vendor/lizhichao/one-sm" />
<path value="$PROJECT_DIR$/vendor/spatie/test-time" />
<path value="$PROJECT_DIR$/vendor/phpoption/phpoption" />
<path value="$PROJECT_DIR$/vendor/spatie/pest-plugin-test-time" />
<path value="$PROJECT_DIR$/vendor/ralouphie/getallheaders" />
<path value="$PROJECT_DIR$/vendor/swoole/ide-helper" />
<path value="$PROJECT_DIR$/vendor/sebastian/comparator" />
@@ -58,9 +56,7 @@
<path value="$PROJECT_DIR$/vendor/sebastian/lines-of-code" />
<path value="$PROJECT_DIR$/vendor/myclabs/deep-copy" />
<path value="$PROJECT_DIR$/vendor/sebastian/type" />
<path value="$PROJECT_DIR$/vendor/pestphp/pest-plugin" />
<path value="$PROJECT_DIR$/vendor/sebastian/version" />
<path value="$PROJECT_DIR$/vendor/pestphp/pest" />
<path value="$PROJECT_DIR$/vendor/sebastian/cli-parser" />
<path value="$PROJECT_DIR$/vendor/phar-io/manifest" />
<path value="$PROJECT_DIR$/vendor/sebastian/code-unit" />
@@ -69,7 +65,6 @@
<path value="$PROJECT_DIR$/vendor/phpstan/phpstan" />
<path value="$PROJECT_DIR$/vendor/sebastian/exporter" />
<path value="$PROJECT_DIR$/vendor/phpunit/php-timer" />
<path value="$PROJECT_DIR$/vendor/sebastian/resource-operations" />
<path value="$PROJECT_DIR$/vendor/phpunit/phpunit" />
<path value="$PROJECT_DIR$/vendor/guzzlehttp/promises" />
<path value="$PROJECT_DIR$/vendor/phpunit/php-text-template" />
@@ -77,7 +72,6 @@
<path value="$PROJECT_DIR$/vendor/phpunit/php-code-coverage" />
<path value="$PROJECT_DIR$/vendor/guzzlehttp/guzzle" />
<path value="$PROJECT_DIR$/vendor/phpunit/php-invoker" />
<path value="$PROJECT_DIR$/vendor/nunomaduro/collision" />
<path value="$PROJECT_DIR$/vendor/phpunit/php-file-iterator" />
<path value="$PROJECT_DIR$/vendor/alibabacloud/tea-xml" />
<path value="$PROJECT_DIR$/vendor/symfony/polyfill-php73" />
@@ -131,7 +125,6 @@
<path value="$PROJECT_DIR$/vendor/psr/log" />
<path value="$PROJECT_DIR$/vendor/psr/clock" />
<path value="$PROJECT_DIR$/vendor/psr/http-factory" />
<path value="$PROJECT_DIR$/vendor/filp/whoops" />
<path value="$PROJECT_DIR$/vendor/seld/phar-utils" />
<path value="$PROJECT_DIR$/vendor/seld/jsonlint" />
<path value="$PROJECT_DIR$/vendor/seld/signal-handler" />
@@ -139,7 +132,6 @@
<path value="$PROJECT_DIR$/vendor/nikic/fast-route" />
<path value="$PROJECT_DIR$/vendor/react/promise" />
<path value="$PROJECT_DIR$/vendor/roave/dont" />
<path value="$PROJECT_DIR$/vendor/cooper/hyperf-pest" />
<path value="$PROJECT_DIR$/vendor/hyperf/database" />
<path value="$PROJECT_DIR$/vendor/hyperf/pipeline" />
<path value="$PROJECT_DIR$/vendor/hyperf/contract" />
@@ -175,6 +167,29 @@
<path value="$PROJECT_DIR$/vendor/hyperf/engine" />
<path value="$PROJECT_DIR$/vendor/hyperf/stringable" />
<path value="$PROJECT_DIR$/vendor/doctrine/lexer" />
<path value="$PROJECT_DIR$/vendor/swow/psr7-plus" />
<path value="$PROJECT_DIR$/vendor/hyperf/stdlib" />
<path value="$PROJECT_DIR$/vendor/hyperf/testing" />
<path value="$PROJECT_DIR$/vendor/pestphp/pest-plugin" />
<path value="$PROJECT_DIR$/vendor/pestphp/pest" />
<path value="$PROJECT_DIR$/vendor/pestphp/pest-plugin-arch" />
<path value="$PROJECT_DIR$/vendor/phpstan/phpdoc-parser" />
<path value="$PROJECT_DIR$/vendor/webmozart/assert" />
<path value="$PROJECT_DIR$/vendor/brianium/paratest" />
<path value="$PROJECT_DIR$/vendor/fidry/cpu-core-counter" />
<path value="$PROJECT_DIR$/vendor/nunomaduro/termwind" />
<path value="$PROJECT_DIR$/vendor/nunomaduro/collision" />
<path value="$PROJECT_DIR$/vendor/phpdocumentor/reflection-common" />
<path value="$PROJECT_DIR$/vendor/phpdocumentor/type-resolver" />
<path value="$PROJECT_DIR$/vendor/phpdocumentor/reflection-docblock" />
<path value="$PROJECT_DIR$/vendor/filp/whoops" />
<path value="$PROJECT_DIR$/vendor/ta-tikoma/phpunit-architecture-test" />
<path value="$PROJECT_DIR$/vendor/jean85/pretty-package-versions" />
<path value="$PROJECT_DIR$/vendor/friendsofhyperf/pest-plugin-hyperf" />
<path value="$PROJECT_DIR$/vendor/rector/rector" />
<path value="$PROJECT_DIR$/vendor/friendsofhyperf/rector" />
<path value="$PROJECT_DIR$/vendor/spatie/test-time" />
<path value="$PROJECT_DIR$/vendor/spatie/pest-plugin-test-time" />
</include_path>
</component>
<component name="PhpProjectSharedConfiguration" php_language_level="8.1" />

15
.idea/phpspec.xml generated
View File

@@ -29,6 +29,21 @@
<PhpSpecSuiteConfiguration>
<option name="myPath" value="$PROJECT_DIR$" />
</PhpSpecSuiteConfiguration>
<PhpSpecSuiteConfiguration>
<option name="myPath" value="$PROJECT_DIR$" />
</PhpSpecSuiteConfiguration>
<PhpSpecSuiteConfiguration>
<option name="myPath" value="$PROJECT_DIR$" />
</PhpSpecSuiteConfiguration>
<PhpSpecSuiteConfiguration>
<option name="myPath" value="$PROJECT_DIR$" />
</PhpSpecSuiteConfiguration>
<PhpSpecSuiteConfiguration>
<option name="myPath" value="$PROJECT_DIR$" />
</PhpSpecSuiteConfiguration>
<PhpSpecSuiteConfiguration>
<option name="myPath" value="$PROJECT_DIR$" />
</PhpSpecSuiteConfiguration>
</suites>
</component>
</project>

View File

@@ -1,4 +1,11 @@
# 版本更新日志
## [1.0.0-alpha.16](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/compare/v1.0.0-alpha.15...v1.0.0-alpha.16) (2023-12-04)
### ♻️ Code Refactoring | 代码重构
* 迁移到 hyperf3.1和 pestphp2 ([eee68ca](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/commit/eee68cab8a06833114ae19dc2c746dcd50206cb4))
## [1.0.0-alpha.15](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/compare/v1.0.0-alpha.14...v1.0.0-alpha.15) (2023-10-30)

View File

@@ -1 +1 @@
1.0.0-alpha.15
1.0.0-alpha.16

View File

@@ -15,20 +15,20 @@
}
],
"require": {
"php": "^8.1",
"php": ">=8.1",
"ext-redis": "^5.3|^6.0",
"ext-swoole": "*",
"composer/composer": ">=2.0",
"ergebnis/http-method": "^2.2",
"hyperf/config": "^3.0.18",
"hyperf/constants": "^3.0.18",
"hyperf/context": "^3.0.18",
"hyperf/di": "^3.0.18",
"hyperf/framework": "^3.0.18",
"hyperf/logger": "^3.0.18",
"hyperf/redis": "^3.0.18",
"hyperf/resource": "^3.0.18",
"hyperf/translation": "^3.0.18",
"hyperf/config": "3.1.*",
"hyperf/constants": "3.1.*",
"hyperf/context": "3.1.*",
"hyperf/di": "3.1.*",
"hyperf/framework": "3.1.*",
"hyperf/logger": "3.1.*",
"hyperf/redis": "3.1.*",
"hyperf/resource": "3.1.*",
"hyperf/translation": "3.1.*",
"jetbrains/phpstorm-attributes": "^1.0",
"lmc/http-constants": "^1.2",
"myclabs/php-enum": "^1.8",
@@ -41,33 +41,34 @@
},
"require-dev": {
"alibabacloud/dysmsapi-20170525": "^2.0",
"cooper/hyperf-pest": "^1.1",
"firebase/php-jwt": "^6.1",
"friendsofhyperf/pest-plugin-hyperf": "3.1.*",
"friendsofhyperf/rector": "^0.1.5",
"friendsofphp/php-cs-fixer": "^3.13",
"guzzlehttp/guzzle": "^7.5",
"hyperf/crontab": "^3.0.18",
"hyperf/database": "^3.0.18",
"hyperf/db": "^3.0.18",
"hyperf/guzzle": "^3.0.18",
"hyperf/session": "^3.0.18",
"hyperf/validation": "^3.0.18",
"pestphp/pest": "^1.22",
"hyperf/crontab": "3.1.*",
"hyperf/database": "3.1.*",
"hyperf/db": "3.1.*",
"hyperf/guzzle": "3.1.*",
"hyperf/session": "3.1.*",
"hyperf/testing": "^3.1",
"hyperf/validation": "3.1.*",
"pestphp/pest": "^2.23",
"phpstan/phpstan": "^1.9",
"phpunit/phpunit": "^9.6",
"roave/security-advisories": "dev-latest",
"spatie/pest-plugin-test-time": "^1.1",
"spatie/pest-plugin-test-time": "^2.0",
"swoole/ide-helper": "*",
"symfony/mailer": "^6.0",
"symfony/serializer": "^6.0"
},
"suggest": {
"firebase/php-jwt": "JWT 鉴权必需",
"hyperf/session": "^3.0.18",
"hyperf/session": "3.1.*",
"symfony/mailer": "用于发送电子邮件",
"symfony/serializer": "处理 XML 必需",
"ext-dom": "处理 XML 必需",
"ext-libxml": "处理 XML 必需",
"hyperf/guzzle": "^3.0.18",
"hyperf/guzzle": "3.1.*",
"alibabacloud/dysmsapi-20170525": "阿里云短信服务必需"
},
"config": {
@@ -107,15 +108,14 @@
"post-root-package-install": [],
"test": [
"rm -rf runtime",
"Composer\\Config::disableProcessTimeout",
"co-pest --prepend tests/bootstrap.php -c phpunit.xml --colors=always $1"
"pest --coroutine --prepend tests/bootstrap.php --colors=always"
],
"cs-fix": "vendor/bin/php-cs-fixer fix $1 --rules=@PSR12 --allow-risky=yes",
"analyse": "vendor/bin/phpstan analyse $1",
"ci": [
"@analyse publish/ src/ tests/",
"@cs-fix",
"@test --ci",
"@test",
"echo CI Success"
]
},
@@ -129,5 +129,5 @@
"url": "https://mirrors.aliyun.com/composer/"
}
},
"version": "1.0.0-alpha.15"
"version": "1.0.0-alpha.16"
}

3810
composer.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -8,6 +8,7 @@ parameters:
- tests
ignoreErrors:
- '#Constant BASE_PATH not found#'
- '#Unknown parameter \$[a-zA-Z0-9]+ in call to callable Closure\.#'
- '#Property [a-zA-Z0-9\\_]+::\$[a-zA-Z0-9]+ is never written, only read\.#'
- '#Method [a-zA-Z0-9\\_]+::[a-zA-Z0-9]+\(\) is unused\.#'
- '#Method [a-zA-Z0-9\\_]+::[a-zA-Z0-9]+\(\) has parameter \$response with no value type specified in iterable type array\.#'

View File

@@ -1,16 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
bootstrap="./tests/bootstrap.php"
colors="true"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.6/phpunit.xsd">
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">./app</directory>
</include>
</coverage>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
bootstrap="./tests/bootstrap.php"
colors="true"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.4/phpunit.xsd"
cacheDirectory=".phpunit.cache"
>
<coverage/>
<testsuites>
<testsuite name="Tests">
<directory>./tests</directory>
</testsuite>
</testsuites>
</phpunit>
<source>
<include>
<directory>./app</directory>
</include>
</source>
</phpunit>

View File

@@ -73,9 +73,9 @@ class CommonHandler extends ExceptionHandler
$is_testing = config('app_status') === true;
$this->request?->url();
$is_debug = $this->request?->hasHeader('Postman-Token') || str_starts_with(
$this->request?->header('User-Agent', ''),
'apifox'
);
$this->request?->header('User-Agent', ''),
'apifox'
);
$error_type = $throwable::class;
$request_time = Carbon::now()->toDateTimeString();
$request_data = Json::encode($this->request?->getParsedBody());
@@ -156,7 +156,9 @@ class CommonHandler extends ExceptionHandler
// 模型不存在
if ($throwable instanceof ModelNotFoundException) {
$code = empty($throwable->getCode()) ? CommonErrorCode::MODEL_NOT_FOUND : $throwable->getCode();
$message = empty($throwable->getCode()) ? CommonErrorCode::getMessage($code, ['resource' => '资源']
$message = empty($throwable->getCode()) ? CommonErrorCode::getMessage(
$code,
['resource' => '资源']
) : $throwable->getMessage();
$data = [$code_name => $code, $message_name => $message];
$status_code = RFC7231::NOT_FOUND;

View File

@@ -14,6 +14,8 @@ use Hyperf\Guzzle\ClientFactory;
use Psr\Http\Message\ResponseInterface;
use Singularity\HDK\Core\Service\AbstractSingleton;
use function Hyperf\Support\make;
/**
* Singularity\HDK\Core\Service\Http\RequestService@Core
*
@@ -41,7 +43,7 @@ final class RequestService extends AbstractSingleton
{
$this->setOptions($options);
/** @var ClientFactory $client */
$client = \Hyperf\Support\make(ClientFactory::class);
$client = make(ClientFactory::class);
$this->client = $client;
}
@@ -59,7 +61,7 @@ final class RequestService extends AbstractSingleton
/**
* @param string $url
* @param array<string, string|int> $params
* @param array $data
* @param array<string, mixed> $data
* @param Options $options
* @return ResponseInterface
* @throws GuzzleException
@@ -71,7 +73,8 @@ final class RequestService extends AbstractSingleton
array $options = []
): ResponseInterface {
$request = new Request(
method: Method\Rfc\Rfc7231::GET, uri: $url
method: Method\Rfc\Rfc7231::GET,
uri: $url
);
return $this->getClient($options)->send($request, ['query' => $params, 'json' => $data,]);
}
@@ -133,7 +136,8 @@ final class RequestService extends AbstractSingleton
body: Utils::streamFor($data)
);
return $this->getClient($options)->send(
$request, ['query' => $params,]
$request,
['query' => $params,]
);
}
@@ -159,7 +163,8 @@ final class RequestService extends AbstractSingleton
body: Utils::streamFor($data)
);
return $this->getClient($options)->send(
$request, ['query' => $params,]
$request,
['query' => $params,]
);
}
@@ -185,7 +190,8 @@ final class RequestService extends AbstractSingleton
body: Utils::streamFor($data)
);
return $this->getClient($options)->send(
$request, ['query' => $params,]
$request,
['query' => $params,]
);
}
}

View File

@@ -15,8 +15,13 @@ use Singularity\HDK\Core\Service\AbstractFactory;
final class RequestServiceFactory extends AbstractFactory
{
/**
* @param array<string,mixed> $options
*
* @return RequestService
*/
public static function make(array $options = []): RequestService
{
return new RequestService($options);
}
}
}

View File

@@ -15,12 +15,13 @@ use Hyperf\Contract\Arrayable;
use Hyperf\Contract\Jsonable;
use Hyperf\Contract\LengthAwarePaginatorInterface;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpMessage\Server\ResponsePlusProxy;
use Hyperf\HttpMessage\Stream\SwooleStream;
use Hyperf\HttpServer\CoreMiddleware;
use Lmc\HttpConstants\Header;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Singularity\HDK\Core\Service\UtilsService;
use Swow\Psr7\Message\ResponsePlusInterface;
use function Hyperf\Config\config;
@@ -32,7 +33,7 @@ use function Hyperf\Config\config;
* Created on 2022/4/29
*
* @deprecated
* @see CommonCoreMiddleware
* @see CommonCoreMiddleware
*/
class ClassicCoreMiddleware extends CoreMiddleware
{
@@ -43,24 +44,41 @@ class ClassicCoreMiddleware extends CoreMiddleware
* 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
*/
protected function transferToResponse($response, ServerRequestInterface $request): ResponseInterface
{
protected function transferToResponse(
$response,
ServerRequestInterface $request
): ResponsePlusInterface {
$code_name = config('common.response.code_name');
$message_name = config('common.response.message_name');
$data_name = config('common.response.data_name');
// 分页数据
if ($response instanceof LengthAwarePaginatorInterface) {
$paginator = $response;
$fact_response = $this->response()->withHeader('Per-Page', (string)$paginator->perPage())->withHeader(
'Total',
(string)$paginator->total()
)->withHeader('Current-Page', (string)$paginator->currentPage())->withHeader(
'Total-Pages',
(string)$paginator->hasPages()
$fact_response = $this->response()
->withHeader(
'Per-Page',
(string)$paginator->perPage()
)
->withHeader(
'Total',
(string)$paginator->total()
)
->withHeader(
'Current-Page',
(string)$paginator->currentPage()
)
->withHeader(
'Total-Pages',
(string)$paginator->hasPages()
);
$fact_response = $this->utilsService->extendLinkToHeader(
$fact_response,
$paginator->nextPageUrl(),
'next'
);
$fact_response = $this->utilsService->extendLinkToHeader($fact_response, $paginator->nextPageUrl(), 'next');
$fact_response = $this->utilsService->extendLinkToHeader(
$fact_response,
$paginator->url($paginator->lastPage()),
@@ -72,23 +90,27 @@ class ClassicCoreMiddleware extends CoreMiddleware
$paginator->previousPageUrl(),
'prev'
);
return $fact_response->withAddedHeader(Header::CONTENT_TYPE, 'application/json')->withBody(
new SwooleStream(
Json::encode(
[
$code_name => 200,
$message_name => 'ok',
$data_name => $response->items(),
'meta' => [
'currentPage' => $paginator->currentPage(),
'lastPage' => $paginator->lastPage(),
'perPage' => $paginator->perPage(),
'total' => $paginator->total(),
],
]
$resp = $fact_response
->withAddedHeader(Header::CONTENT_TYPE, 'application/json')
->withBody(
new SwooleStream(
Json::encode(
[
$code_name => 200,
$message_name => 'ok',
$data_name => $response->items(),
'meta' => [
'currentPage' => $paginator->currentPage(),
'lastPage' => $paginator->lastPage(),
'perPage' => $paginator->perPage(),
'total' => $paginator->total(),
],
]
)
)
)
);
);
return new ResponsePlusProxy($resp);
}
// 普通数组
if (is_array($response) || $response instanceof Arrayable) {

View File

@@ -15,6 +15,7 @@ use Hyperf\Contract\Arrayable;
use Hyperf\Contract\Jsonable;
use Hyperf\Contract\LengthAwarePaginatorInterface;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpMessage\Server\ResponsePlusProxy;
use Hyperf\HttpMessage\Stream\SwooleStream;
use Hyperf\HttpServer\CoreMiddleware;
use Lmc\HttpConstants\Header;
@@ -22,6 +23,7 @@ use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Singularity\HDK\Core\Service\ApiStyleService;
use Singularity\HDK\Core\Service\UtilsService;
use Swow\Psr7\Message\ResponsePlusInterface;
use function Hyperf\Config\config;
@@ -43,24 +45,31 @@ class CommonCoreMiddleware extends CoreMiddleware
/**
* @inheritDoc
*/
protected function transferToResponse($response, ServerRequestInterface $request): ResponseInterface
{
protected function transferToResponse(
$response,
ServerRequestInterface $request
): ResponsePlusInterface {
$style = $this->apiStyleService->get();
if ($style === ApiStyleService::RESTFUL) {
return $this->transferToRestfulResponse($response, $request);
$response = $this->transferToRestfulResponse($response, $request);
} else {
return $this->transferToClassicResponse($response, $request);
$response = $this->transferToClassicResponse($response, $request);
}
return new ResponsePlusProxy($response);
}
/**
* @param array|string|Arrayable|Jsonable|null $response
* @param ServerRequestInterface $request
* @param ServerRequestInterface $request
*
* @return ResponseInterface
*/
protected function transferToRestfulResponse(Arrayable|Jsonable|array|string|null $response, ServerRequestInterface $request): ResponseInterface
{
protected function transferToRestfulResponse(
Arrayable|Jsonable|array|string|null $response,
ServerRequestInterface $request
): ResponseInterface {
// 分页数据
if ($response instanceof LengthAwarePaginatorInterface) {
$fact_response = $this->response()
@@ -88,7 +97,8 @@ class CommonCoreMiddleware extends CoreMiddleware
/**
* @param array|string|Arrayable|Jsonable|null $response
* @param ServerRequestInterface $request
* @param ServerRequestInterface $request
*
* @return ResponseInterface
*/
protected function transferToClassicResponse(

View File

@@ -13,10 +13,11 @@ use Hyperf\Contract\Arrayable;
use Hyperf\Contract\Jsonable;
use Hyperf\Contract\LengthAwarePaginatorInterface;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpMessage\Server\ResponsePlusProxy;
use Hyperf\HttpServer\CoreMiddleware;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Singularity\HDK\Core\Service\UtilsService;
use Swow\Psr7\Message\ResponsePlusInterface;
/**
* Singularity\HDK\Core\Middleware\RestfulCoreMiddleware@HDK-Core
@@ -26,7 +27,7 @@ use Singularity\HDK\Core\Service\UtilsService;
* Created on 2023/1/14
*
* @deprecated
* @see CommonCoreMiddleware
* @see CommonCoreMiddleware
*/
class RestfulCoreMiddleware extends CoreMiddleware
{
@@ -37,11 +38,14 @@ class RestfulCoreMiddleware extends CoreMiddleware
* @template TKey of array-key
* @template TValue
* @param null|array<TKey, TValue>|Arrayable<TKey, TValue>|Jsonable|string $response
* @param ServerRequestInterface $request
* @return ResponseInterface
* @param ServerRequestInterface $request
*
* @return ResponsePlusInterface
*/
protected function transferToResponse($response, ServerRequestInterface $request): ResponseInterface
{
protected function transferToResponse(
$response,
ServerRequestInterface $request
): ResponsePlusInterface {
// 分页数据
if ($response instanceof LengthAwarePaginatorInterface) {
$fact_response = $this->response()
@@ -56,11 +60,12 @@ class RestfulCoreMiddleware extends CoreMiddleware
'last'
);
$fact_response = $this->utilsService->extendLinkToHeader($fact_response, $response->url(1), 'first');
return $this->utilsService->extendLinkToHeader(
$resp = $this->utilsService->extendLinkToHeader(
$fact_response,
$response->previousPageUrl(),
'prev'
);
return new ResponsePlusProxy($resp);
}
return parent::transferToResponse($response, $request);

View File

@@ -86,7 +86,7 @@ class SessionMiddleware implements MiddlewareInterface
/**
* Store the current URL for the request if necessary.
*/
private function storeCurrentUrl(RequestInterface $request, SessionInterface $session)
private function storeCurrentUrl(RequestInterface $request, SessionInterface $session): void
{
if ($request->getMethod() === 'GET') {
$session->setPreviousUrl($this->fullUrl($request));
@@ -155,7 +155,7 @@ class SessionMiddleware implements MiddlewareInterface
return rtrim(preg_replace('/\?.*/', '', (string)$request->getUri()));
}
private function auth(SessionInterface $session)
private function auth(SessionInterface $session): bool
{
return $session->has('userInfo');
}

View File

@@ -13,6 +13,7 @@ namespace Singularity\HDK\Core\Service;
use Dont\DontCall;
use Dont\DontInstantiate;
use Singularity\HDK\Core\Http\RequestService;
/**
* Singularity\HDK\Core\Service\AbstractFactory@Core
@@ -26,5 +27,5 @@ abstract class AbstractFactory
use DontInstantiate;
use DontCall;
abstract static function make();
}
abstract public static function make(): RequestService;
}

View File

@@ -23,4 +23,4 @@ abstract class AbstractSingleton
{
use DontCallStatic;
use DontClone;
}
}

View File

@@ -33,7 +33,7 @@ class HttpRequestService
/**
* @param string $url
* @param array<string, string|int> $params
* @param array $data
* @param array<string, mixed> $data
* @return ResponseInterface
* @throws GuzzleException
*/

View File

@@ -10,11 +10,11 @@
namespace Singularity\HDK\Core\Traits;
use Closure;
use Hyperf\Contract\LengthAwarePaginatorInterface;
use Hyperf\Database\Model\Builder;
use Hyperf\Database\Model\Relations\Relation;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\Paginator\AbstractPaginator;
use Hyperf\Stringable\Str;
use Singularity\HDK\Core\Service\ExtendService;
@@ -25,7 +25,7 @@ trait RestfulList
#[Inject]
protected RequestInterface $request;
protected function restSelect(Closure $callback, ?Closure $filter = null)
protected function restSelect(Closure $callback, ?Closure $filter = null): mixed
{
$parsed_options = [];
$result = $this->parseParameters(
@@ -52,11 +52,18 @@ trait RestfulList
return $callback(query: $query, options: $options);
}
/**
* @param array<string, mixed> $options
* @param Builder|Relation $builder
* @param Closure|null $filter
*
* @return mixed
*/
protected function responseFormatter(
array $options,
Builder|Relation $builder,
?Closure $filter = null
) {
): mixed {
$perPage = $options['size'] ?? null;
$order = $options['order'] ?? 'created_at';
$sort = $options['sort'] ?? 'desc';
@@ -70,7 +77,7 @@ trait RestfulList
return $result;
}
if ($result instanceof LengthAwarePaginatorInterface) {
if ($result instanceof AbstractPaginator) {
$collection = $result->getCollection();
$collection = $filter($collection);
@@ -78,4 +85,4 @@ trait RestfulList
}
return $filter($result);
}
}
}

View File

@@ -26,7 +26,7 @@ it('assertions that send HTML is available', function () use ($email) {
HTML
);
expect($result)->toBeTrue();
});
})->skip();
it('assertions that send Text is available', function () use ($email) {
$result = $email->sendText(
@@ -37,7 +37,7 @@ it('assertions that send Text is available', function () use ($email) {
Text
);
expect($result)->toBeTrue();
});
})->skip();
it('assertions Error Receiver can be catch', function () use ($email) {
try {
@@ -62,4 +62,4 @@ Text
} catch (TransportException $t) {
expect($t->getCode())->toBe(554);
}
});
})->skip('会报错必须运行在协程环境下');

View File

@@ -13,6 +13,8 @@ use Hyperf\Context\Context;
use Singularity\HDK\Core\Service\ExtendService;
use Singularity\HDK\Core\Service\UtilsService;
use function Hyperf\Support\make;
/** @var ExtendService $service */
$service = make(ExtendService::class, ['utils' => new UtilsService()]);
it('asserts no parameters can be parsed.', function (ExtendService $service, $params) {

View File

@@ -10,6 +10,8 @@
use Hyperf\Codec\Json;
use Singularity\HDK\Core\Service\UtilsService;
use function Hyperf\Support\make;
$utils = new UtilsService();
dataset('categories', [
[
@@ -168,7 +170,9 @@ JSON
$length_data = [];
for ($i = 0; $i < 5; $i++) {
try {
$length_data[] = random_int(1, 9);
$length_data[(string)$i] = [
'length' => random_int(1, 9)
];
} catch (Exception $e) {
continue;
}
@@ -264,8 +268,8 @@ test('无极分类', function (string $source, string $expected) {
}
/** @var UtilsService $utils */
$utils = \Hyperf\Support\make(UtilsService::class);
usort($tree, fn($prevent, $current) => $current['level'] <=> $prevent['level']);
$utils = make(UtilsService::class);
usort($tree, fn ($prevent, $current) => $current['level'] <=> $prevent['level']);
$tree = array_column($tree, null, 'id');
$utils->unlimitedSubCategoriesQuicklyWithLevel(list: $tree);

View File

@@ -1,14 +1,6 @@
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
use Hyperf\Context\ApplicationContext;
use Hyperf\Di\Container;
@@ -34,4 +26,4 @@ $container = new Container(new DefinitionSource([]));
if (!$container instanceof ContainerInterface) {
throw new RuntimeException('The dependency injection container is invalid.');
}
$container = ApplicationContext::setContainer($container);
$container = ApplicationContext::setContainer($container);