mirror of
http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore.git
synced 2026-01-15 05:35:09 +08:00
build(ci): 完善 style-fix/syntax-check/unit-test
This commit is contained in:
6
.idea/HDK-Core.iml
generated
6
.idea/HDK-Core.iml
generated
@@ -136,6 +136,12 @@
|
|||||||
<excludeFolder url="file://$MODULE_DIR$/vendor/filp/whoops" />
|
<excludeFolder url="file://$MODULE_DIR$/vendor/filp/whoops" />
|
||||||
<excludeFolder url="file://$MODULE_DIR$/vendor/nunomaduro/collision" />
|
<excludeFolder url="file://$MODULE_DIR$/vendor/nunomaduro/collision" />
|
||||||
<excludeFolder url="file://$MODULE_DIR$/vendor/facade/ignition-contracts" />
|
<excludeFolder url="file://$MODULE_DIR$/vendor/facade/ignition-contracts" />
|
||||||
|
<excludeFolder url="file://$MODULE_DIR$/vendor/spatie/test-time" />
|
||||||
|
<excludeFolder url="file://$MODULE_DIR$/vendor/spatie/pest-plugin-test-time" />
|
||||||
|
<excludeFolder url="file://$MODULE_DIR$/vendor/friendsofphp/php-cs-fixer" />
|
||||||
|
<excludeFolder url="file://$MODULE_DIR$/vendor/symfony/options-resolver" />
|
||||||
|
<excludeFolder url="file://$MODULE_DIR$/vendor/symfony/stopwatch" />
|
||||||
|
<excludeFolder url="file://$MODULE_DIR$/vendor/phpstan/phpstan" />
|
||||||
</content>
|
</content>
|
||||||
<orderEntry type="inheritedJdk" />
|
<orderEntry type="inheritedJdk" />
|
||||||
<orderEntry type="sourceFolder" forTests="false" />
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
|
|||||||
1
.idea/inspectionProfiles/Project_Default.xml
generated
1
.idea/inspectionProfiles/Project_Default.xml
generated
@@ -2,5 +2,6 @@
|
|||||||
<profile version="1.0">
|
<profile version="1.0">
|
||||||
<option name="myName" value="Project Default" />
|
<option name="myName" value="Project Default" />
|
||||||
<inspection_tool class="PhpPropertyOnlyWrittenInspection" enabled="false" level="WARNING" enabled_by_default="false" />
|
<inspection_tool class="PhpPropertyOnlyWrittenInspection" enabled="false" level="WARNING" enabled_by_default="false" />
|
||||||
|
<inspection_tool class="PhpStanGlobal" enabled="true" level="WEAK WARNING" enabled_by_default="true" />
|
||||||
</profile>
|
</profile>
|
||||||
</component>
|
</component>
|
||||||
14
.idea/php-test-framework.xml
generated
Normal file
14
.idea/php-test-framework.xml
generated
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="PhpTestFrameworkSettings">
|
||||||
|
<test_tools>
|
||||||
|
<tool tool_name="Pest">
|
||||||
|
<settings>
|
||||||
|
<configurations>
|
||||||
|
<local_configuration configuration_file_path="$PROJECT_DIR$/phpunit.xml" executable_path="$PROJECT_DIR$/vendor/pestphp/pest/bin/pest" use_configuration_file="true" />
|
||||||
|
</configurations>
|
||||||
|
</settings>
|
||||||
|
</tool>
|
||||||
|
</test_tools>
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
21
.idea/php.xml
generated
21
.idea/php.xml
generated
@@ -4,11 +4,20 @@
|
|||||||
<option name="transferred" value="true" />
|
<option name="transferred" value="true" />
|
||||||
</component>
|
</component>
|
||||||
<component name="PHPCSFixerOptionsConfiguration">
|
<component name="PHPCSFixerOptionsConfiguration">
|
||||||
|
<option name="allowRiskyRules" value="true" />
|
||||||
<option name="transferred" value="true" />
|
<option name="transferred" value="true" />
|
||||||
</component>
|
</component>
|
||||||
<component name="PHPCodeSnifferOptionsConfiguration">
|
<component name="PHPCodeSnifferOptionsConfiguration">
|
||||||
<option name="transferred" value="true" />
|
<option name="transferred" value="true" />
|
||||||
</component>
|
</component>
|
||||||
|
<component name="PhpCSFixer">
|
||||||
|
<phpcsfixer_settings>
|
||||||
|
<PhpCSFixerConfiguration standards="PSR1;PSR2;Symfony;DoctrineAnnotation;PHP70Migration;PHP71Migration" tool_path="$PROJECT_DIR$/vendor/friendsofphp/php-cs-fixer/php-cs-fixer" />
|
||||||
|
</phpcsfixer_settings>
|
||||||
|
</component>
|
||||||
|
<component name="PhpExternalFormatter">
|
||||||
|
<option name="externalFormatter" value="PHP_CBF" />
|
||||||
|
</component>
|
||||||
<component name="PhpIncludePathManager">
|
<component name="PhpIncludePathManager">
|
||||||
<include_path>
|
<include_path>
|
||||||
<path value="$PROJECT_DIR$/vendor/sebastian/recursion-context" />
|
<path value="$PROJECT_DIR$/vendor/sebastian/recursion-context" />
|
||||||
@@ -142,10 +151,22 @@
|
|||||||
<path value="$PROJECT_DIR$/vendor/filp/whoops" />
|
<path value="$PROJECT_DIR$/vendor/filp/whoops" />
|
||||||
<path value="$PROJECT_DIR$/vendor/nunomaduro/collision" />
|
<path value="$PROJECT_DIR$/vendor/nunomaduro/collision" />
|
||||||
<path value="$PROJECT_DIR$/vendor/facade/ignition-contracts" />
|
<path value="$PROJECT_DIR$/vendor/facade/ignition-contracts" />
|
||||||
|
<path value="$PROJECT_DIR$/vendor/spatie/test-time" />
|
||||||
|
<path value="$PROJECT_DIR$/vendor/spatie/pest-plugin-test-time" />
|
||||||
|
<path value="$PROJECT_DIR$/vendor/friendsofphp/php-cs-fixer" />
|
||||||
|
<path value="$PROJECT_DIR$/vendor/symfony/options-resolver" />
|
||||||
|
<path value="$PROJECT_DIR$/vendor/symfony/stopwatch" />
|
||||||
|
<path value="$PROJECT_DIR$/vendor/phpstan/phpstan" />
|
||||||
</include_path>
|
</include_path>
|
||||||
</component>
|
</component>
|
||||||
<component name="PhpProjectSharedConfiguration" php_language_level="8.0" />
|
<component name="PhpProjectSharedConfiguration" php_language_level="8.0" />
|
||||||
|
<component name="PhpStan">
|
||||||
|
<PhpStan_settings>
|
||||||
|
<PhpStanConfiguration tool_path="$PROJECT_DIR$/vendor/bin/phpstan" />
|
||||||
|
</PhpStan_settings>
|
||||||
|
</component>
|
||||||
<component name="PhpStanOptionsConfiguration">
|
<component name="PhpStanOptionsConfiguration">
|
||||||
|
<option name="memoryLimit" value="300M" />
|
||||||
<option name="transferred" value="true" />
|
<option name="transferred" value="true" />
|
||||||
</component>
|
</component>
|
||||||
<component name="PhpUnit">
|
<component name="PhpUnit">
|
||||||
|
|||||||
23
.php-cs-fixer.dist.php
Normal file
23
.php-cs-fixer.dist.php
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* .php-cs-fixer.dist.php@HDK-Core
|
||||||
|
*
|
||||||
|
* @author 李东云 <Dongyun.Li@LuxCreo.Ai>
|
||||||
|
* Powered by PhpStorm
|
||||||
|
* Created on 2023/1/9
|
||||||
|
*/
|
||||||
|
|
||||||
|
$finder = PhpCsFixer\Finder::create()->in([
|
||||||
|
__DIR__ . '/publish',
|
||||||
|
__DIR__ . '/src',
|
||||||
|
__DIR__ . '/tests',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$config = new PhpCsFixer\Config();
|
||||||
|
return $config->setRules([
|
||||||
|
'@PSR12' => true,
|
||||||
|
'strict_param' => true,
|
||||||
|
'array_syntax' => ['syntax' => 'short'],
|
||||||
|
])
|
||||||
|
->setUsingCache(false)
|
||||||
|
->setFinder($finder);
|
||||||
@@ -79,13 +79,13 @@
|
|||||||
"prefer-stable": true,
|
"prefer-stable": true,
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"post-root-package-install": [],
|
"post-root-package-install": [],
|
||||||
"test": "vendor/bin/pest",
|
"test": "vendor/bin/pest $1",
|
||||||
"cs-fix": "vendor/bin/php-cs-fixer fix $1",
|
"cs-fix": "vendor/bin/php-cs-fixer fix $1 --rules=@PSR12 --allow-risky=yes",
|
||||||
"analyse": "vendor/bin/phpstan analyse -l 0 $1",
|
"analyse": "vendor/bin/phpstan analyse $1",
|
||||||
"ci": [
|
"ci": [
|
||||||
"vendor/bin/pest --ci",
|
|
||||||
"@cs-fix .",
|
|
||||||
"@analyse publish/ src/ tests/",
|
"@analyse publish/ src/ tests/",
|
||||||
|
"@cs-fix",
|
||||||
|
"@test --ci",
|
||||||
"echo CI Success"
|
"echo CI Success"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|||||||
9
phpstan.dist.neon
Normal file
9
phpstan.dist.neon
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
parameters:
|
||||||
|
level: 6
|
||||||
|
paths:
|
||||||
|
- publish
|
||||||
|
- src
|
||||||
|
- tests
|
||||||
|
ignoreErrors:
|
||||||
|
- '#Constant BASE_PATH not found#'
|
||||||
|
- '#Property [a-zA-Z0-9\\_]+::\$[a-zA-Z0-9]+ is never written, only read.#'
|
||||||
@@ -17,8 +17,10 @@ use Hyperf\Framework\Logger\StdoutLogger;
|
|||||||
|
|
||||||
class ConfigProvider
|
class ConfigProvider
|
||||||
{
|
{
|
||||||
|
/** @phpstan-ignore-next-line */
|
||||||
public function __invoke(): array
|
public function __invoke(): array
|
||||||
{
|
{
|
||||||
|
/** @noinspection PhpUndefinedConstantInspection */
|
||||||
return [
|
return [
|
||||||
// 合并到 config/autoload/dependencies.php 文件
|
// 合并到 config/autoload/dependencies.php 文件
|
||||||
'dependencies' => [
|
'dependencies' => [
|
||||||
|
|||||||
@@ -65,5 +65,4 @@ abstract class AbstractController
|
|||||||
* @var \Hyperf\Contract\StdoutLoggerInterface
|
* @var \Hyperf\Contract\StdoutLoggerInterface
|
||||||
*/
|
*/
|
||||||
protected StdoutLoggerInterface $stdoutLogger;
|
protected StdoutLoggerInterface $stdoutLogger;
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -22,5 +22,4 @@ interface RFC4559
|
|||||||
* @link https://www.ietf.org/rfc/rfc4559.txt RFC 4559
|
* @link https://www.ietf.org/rfc/rfc4559.txt RFC 4559
|
||||||
*/
|
*/
|
||||||
public const NEGOTIATE_NTLM = 'Negotiate';
|
public const NEGOTIATE_NTLM = 'Negotiate';
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -31,5 +31,4 @@ interface RFC7616
|
|||||||
* @codingStandardsIgnoreStart
|
* @codingStandardsIgnoreStart
|
||||||
*/
|
*/
|
||||||
public const DIGEST = Header::DIGEST;
|
public const DIGEST = Header::DIGEST;
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -137,7 +137,7 @@ class CommonHandler extends ExceptionHandler
|
|||||||
|
|
||||||
if (is_numeric($data)) {
|
if (is_numeric($data)) {
|
||||||
$code = (int)$data;
|
$code = (int)$data;
|
||||||
$data = CommonErrorCode::getMessage($data);
|
$data = CommonErrorCode::getMessage($code);
|
||||||
}
|
}
|
||||||
$data = [
|
$data = [
|
||||||
$code_name => $code ?? CommonErrorCode::REQUEST_PARAMS_ERROR,
|
$code_name => $code ?? CommonErrorCode::REQUEST_PARAMS_ERROR,
|
||||||
@@ -149,9 +149,9 @@ class CommonHandler extends ExceptionHandler
|
|||||||
$message = $throwable->getMessage();
|
$message = $throwable->getMessage();
|
||||||
$data = [
|
$data = [
|
||||||
$code_name => $code,
|
$code_name => $code,
|
||||||
$message_name => $message ?? CommonErrorCode::getMessage($code, [
|
$message_name => empty($message) ? CommonErrorCode::getMessage($code, [
|
||||||
'param' => $throwable->getFieldName(),
|
'param' => $throwable->getFieldName(),
|
||||||
]),
|
]) : $message,
|
||||||
];
|
];
|
||||||
if ($is_debug) {
|
if ($is_debug) {
|
||||||
$data['currentValue'] = $throwable->getCurrentValue();
|
$data['currentValue'] = $throwable->getCurrentValue();
|
||||||
@@ -231,7 +231,7 @@ class CommonHandler extends ExceptionHandler
|
|||||||
// 其他错误
|
// 其他错误
|
||||||
if ($throwable instanceof HttpException) {
|
if ($throwable instanceof HttpException) {
|
||||||
$data = [
|
$data = [
|
||||||
$code_name => $throwable->getCode() ?? $throwable->getStatusCode(),
|
$code_name => $throwable->getCode() ?: $throwable->getStatusCode(),
|
||||||
$message_name => $throwable->getMessage(),
|
$message_name => $throwable->getMessage(),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,5 +6,4 @@ use Hyperf\Server\Exception\ServerException;
|
|||||||
|
|
||||||
class ThirdPartyException extends ServerException
|
class ThirdPartyException extends ServerException
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,6 +12,14 @@ use Throwable;
|
|||||||
*/
|
*/
|
||||||
class ValidateException extends HttpException
|
class ValidateException extends HttpException
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* @param int $code
|
||||||
|
* @param string|null $message
|
||||||
|
* @param string $field
|
||||||
|
* @param mixed|null $currentValue
|
||||||
|
* @param array<int, mixed> $availableValue
|
||||||
|
* @param Throwable|null $previous
|
||||||
|
*/
|
||||||
public function __construct(
|
public function __construct(
|
||||||
int $code = CommonErrorCode::FORMATTER_ERROR,
|
int $code = CommonErrorCode::FORMATTER_ERROR,
|
||||||
?string $message = null,
|
?string $message = null,
|
||||||
@@ -52,7 +60,7 @@ class ValidateException extends HttpException
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return array
|
* @return array<int, mixed>
|
||||||
*/
|
*/
|
||||||
public function getAvailableValue(): array
|
public function getAvailableValue(): array
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ class ClassicCoreMiddleware extends CoreMiddleware
|
|||||||
/**
|
/**
|
||||||
* Transfer the non-standard response content to a standard response object.
|
* Transfer the non-standard response content to a standard response object.
|
||||||
*
|
*
|
||||||
* @param null|array|Arrayable|Jsonable|string $response
|
* @param null|array<string, mixed>|Arrayable<string, mixed>|Jsonable|string $response
|
||||||
*/
|
*/
|
||||||
protected function transferToResponse($response, ServerRequestInterface $request): ResponseInterface
|
protected function transferToResponse($response, ServerRequestInterface $request): ResponseInterface
|
||||||
{
|
{
|
||||||
@@ -50,10 +50,10 @@ class ClassicCoreMiddleware extends CoreMiddleware
|
|||||||
// 分页数据
|
// 分页数据
|
||||||
if ($response instanceof LengthAwarePaginatorInterface) {
|
if ($response instanceof LengthAwarePaginatorInterface) {
|
||||||
$paginator = $response;
|
$paginator = $response;
|
||||||
$fact_response = $this->response()->withHeader('Per-Page', $paginator->perPage())
|
$fact_response = $this->response()->withHeader('Per-Page', (string)$paginator->perPage())
|
||||||
->withHeader('Total', $paginator->total())
|
->withHeader('Total', (string)$paginator->total())
|
||||||
->withHeader('Current-Page', $paginator->currentPage())
|
->withHeader('Current-Page', (string)$paginator->currentPage())
|
||||||
->withHeader('Total-Pages', $paginator->hasPages());
|
->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 = $this->utilsService->extendLinkToHeader(
|
||||||
@@ -86,7 +86,7 @@ class ClassicCoreMiddleware extends CoreMiddleware
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
// 可 Json 化的数据结构
|
// 可 Json 化的数据结构
|
||||||
if ($response instanceof Jsonable) {
|
if ($response instanceof Jsonable && $response instanceof Arrayable) {
|
||||||
$response = [
|
$response = [
|
||||||
$code_name => 200,
|
$code_name => 200,
|
||||||
$message_name => 'ok',
|
$message_name => 'ok',
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ class CorsMiddleware implements MiddlewareInterface, Method
|
|||||||
$response = $response
|
$response = $response
|
||||||
->withHeader(
|
->withHeader(
|
||||||
Header::ACCESS_CONTROL_ALLOW_ORIGIN,
|
Header::ACCESS_CONTROL_ALLOW_ORIGIN,
|
||||||
$request->getHeaderLine('Origin') ?? '',
|
$request->getHeaderLine('Origin'),
|
||||||
)
|
)
|
||||||
->withHeader(Header::ACCESS_CONTROL_ALLOW_CREDENTIALS, 'true')
|
->withHeader(Header::ACCESS_CONTROL_ALLOW_CREDENTIALS, 'true')
|
||||||
->withHeader(
|
->withHeader(
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ declare(strict_types=1);
|
|||||||
namespace Singularity\HDK\Core\Service;
|
namespace Singularity\HDK\Core\Service;
|
||||||
|
|
||||||
use JetBrains\PhpStorm\Deprecated;
|
use JetBrains\PhpStorm\Deprecated;
|
||||||
|
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
|
||||||
use Symfony\Component\Mailer\Mailer;
|
use Symfony\Component\Mailer\Mailer;
|
||||||
use Symfony\Component\Mailer\Transport;
|
use Symfony\Component\Mailer\Transport;
|
||||||
use Symfony\Component\Mime\Address;
|
use Symfony\Component\Mime\Address;
|
||||||
@@ -26,8 +27,11 @@ class EmailService
|
|||||||
|
|
||||||
private string $from;
|
private string $from;
|
||||||
|
|
||||||
public function __construct($dsn = null, $mailSenderName = null, $mailSender = null)
|
public function __construct(
|
||||||
{
|
?string $dsn = null,
|
||||||
|
?string $mailSenderName = null,
|
||||||
|
?string $mailSender = null
|
||||||
|
) {
|
||||||
$dsn ??= config('common.third_party.email.dsn');
|
$dsn ??= config('common.third_party.email.dsn');
|
||||||
$transport = Transport::fromDsn($dsn);
|
$transport = Transport::fromDsn($dsn);
|
||||||
$this->mailer = new Mailer($transport);
|
$this->mailer = new Mailer($transport);
|
||||||
@@ -47,9 +51,9 @@ class EmailService
|
|||||||
* @param string $text
|
* @param string $text
|
||||||
*
|
*
|
||||||
* @return bool
|
* @return bool
|
||||||
* @throws \Symfony\Component\Mailer\Exception\TransportExceptionInterface
|
* @throws TransportExceptionInterface
|
||||||
* @see \Singularity\HDK\Core\Service\EmailService::sendText()
|
* @see EmailService::sendText
|
||||||
* @see \Singularity\HDK\Core\Service\EmailService::sendHtml()
|
* @see EmailService::sendHtml
|
||||||
*/
|
*/
|
||||||
#[Deprecated]
|
#[Deprecated]
|
||||||
public function sendCode(
|
public function sendCode(
|
||||||
@@ -61,13 +65,13 @@ class EmailService
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string|array $target
|
* @param string|array<string> $target
|
||||||
* @param string $subject
|
* @param string $subject
|
||||||
* @param string $text
|
* @param string $text
|
||||||
* @param array $cc
|
* @param array<string> $cc
|
||||||
*
|
*
|
||||||
* @return bool
|
* @return bool
|
||||||
* @throws \Symfony\Component\Mailer\Exception\TransportExceptionInterface
|
* @throws TransportExceptionInterface
|
||||||
*/
|
*/
|
||||||
public function sendText(
|
public function sendText(
|
||||||
string|array $target,
|
string|array $target,
|
||||||
@@ -90,13 +94,13 @@ class EmailService
|
|||||||
/**
|
/**
|
||||||
* 以 HTML 格式发送邮件
|
* 以 HTML 格式发送邮件
|
||||||
*
|
*
|
||||||
* @param string|array $target
|
* @param string|array<string> $target
|
||||||
* @param string $subject
|
* @param string $subject
|
||||||
* @param string $html
|
* @param string $html
|
||||||
* @param array $cc
|
* @param array<string> $cc
|
||||||
*
|
*
|
||||||
* @return bool
|
* @return bool
|
||||||
* @throws \Symfony\Component\Mailer\Exception\TransportExceptionInterface
|
* @throws TransportExceptionInterface
|
||||||
*/
|
*/
|
||||||
public function sendHtml(
|
public function sendHtml(
|
||||||
string|array $target,
|
string|array $target,
|
||||||
|
|||||||
@@ -11,8 +11,8 @@ use Psr\Http\Message\ServerRequestInterface;
|
|||||||
class ExtendService
|
class ExtendService
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @param \Psr\Http\Message\ServerRequestInterface $request
|
* @param ServerRequestInterface|null $request
|
||||||
*
|
* @param array<string, string>|null $params
|
||||||
* @return string[]
|
* @return string[]
|
||||||
*/
|
*/
|
||||||
public function parse(
|
public function parse(
|
||||||
@@ -30,7 +30,7 @@ class ExtendService
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return array
|
* @return array<string, string>
|
||||||
*/
|
*/
|
||||||
public function getExtends(): array
|
public function getExtends(): array
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,86 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Singularity\HDK\Core\Service;
|
|
||||||
|
|
||||||
|
|
||||||
use GuzzleHttp\Client;
|
|
||||||
use GuzzleHttp\Cookie\CookieJar;
|
|
||||||
use GuzzleHttp\Exception\ClientException;
|
|
||||||
use GuzzleHttp\Exception\ServerException;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 发起 Http 请求的类
|
|
||||||
*/
|
|
||||||
class HttpRequestService
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* @var \GuzzleHttp\Client
|
|
||||||
*/
|
|
||||||
protected Client $client;
|
|
||||||
|
|
||||||
protected function __construct(array $headers = [], string $base_uri = '', array $cookie = [])
|
|
||||||
{
|
|
||||||
$options = [
|
|
||||||
'timeout' => 20,
|
|
||||||
'headers' => $headers
|
|
||||||
];
|
|
||||||
if (!empty($base_uri)) {
|
|
||||||
$options['base_uri'] = $base_uri;
|
|
||||||
}
|
|
||||||
if (!empty($cookie)) {
|
|
||||||
$options['cookies'] = CookieJar::fromArray(
|
|
||||||
[
|
|
||||||
$this->cookie['key'] => $this->cookie['value']
|
|
||||||
],
|
|
||||||
parse_url($base_uri)['host']
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->client = new Client($options);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function get($uri, $params = [])
|
|
||||||
{
|
|
||||||
return $this->bar($uri, $params);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function post($uri, $params = [])
|
|
||||||
{
|
|
||||||
return $this->bar($uri, $params, 'post');
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function put($uri, $params = [])
|
|
||||||
{
|
|
||||||
return $this->bar($uri, $params, 'put');
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function delete($uri, $params = [])
|
|
||||||
{
|
|
||||||
return $this->bar($uri, $params, 'delete');
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function patch($uri, $params = [])
|
|
||||||
{
|
|
||||||
return $this->bar($uri, $params, 'patch');
|
|
||||||
}
|
|
||||||
|
|
||||||
private function bar($uri, $params = [], $method = 'get')
|
|
||||||
{
|
|
||||||
|
|
||||||
try {
|
|
||||||
$http = $this->client->{$method}($uri, ['json' => $params]);
|
|
||||||
$result = $this->jsonToArray($http);
|
|
||||||
} catch (ClientException|ServerException $e) {
|
|
||||||
$response = $e->getResponse();
|
|
||||||
$result = $this->jsonToArray($response);
|
|
||||||
$result['httpCode'] = $response->getStatusCode();
|
|
||||||
}
|
|
||||||
|
|
||||||
return $result;
|
|
||||||
}
|
|
||||||
|
|
||||||
private function jsonToArray($http)
|
|
||||||
{
|
|
||||||
return json_decode($http->getBody()->getContents(), true);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -14,36 +14,47 @@ use Hyperf\Utils\Codec\Json;
|
|||||||
class OssService
|
class OssService
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @const 30s 过期
|
* @param string|null $accessKeyId 不传则取配置文件
|
||||||
|
* @param string|null $accessKeySecret 不传则取配置文件
|
||||||
|
* @param string|null $host 不传则取配置文件
|
||||||
|
* @param int|null $expiration 过期时间,默认30s
|
||||||
*/
|
*/
|
||||||
public const EXPIRE = 30;
|
public function __construct(
|
||||||
|
public ?string $accessKeyId = null,
|
||||||
protected string $accessKeyId;
|
public ?string $accessKeySecret = null,
|
||||||
protected string $accessKeySecret;
|
public ?string $host = null,
|
||||||
protected string $host;
|
public ?string $callbackUrl = null,
|
||||||
|
public ?int $expiration = 30
|
||||||
public function __construct()
|
) {
|
||||||
{
|
$this->accessKeyId ??= config('common.third_party.storage.oss.access_key_id');
|
||||||
$this->accessKeyId = config('common.third_party.storage.oss.access_key_id');
|
$this->accessKeySecret ??= config('common.third_party.storage.oss.access_key_secret');
|
||||||
$this->accessKeySecret = config('common.third_party.storage.oss.access_key_secret');
|
$this->host ??= config('common.third_party.storage.oss.oss_host');
|
||||||
$this->host = config('common.third_party.storage.oss.oss_host');
|
$this->callbackUrl ??= config('oss_callback');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 生成签证
|
* 生成签证
|
||||||
*
|
*
|
||||||
* @param string|null $dir
|
* @param string|null $dir
|
||||||
* @param bool $isImage
|
* @param bool $isImage
|
||||||
* @param int $maxFileSize 最大文件大小(单位:b)
|
* @param int $maxFileSize 最大文件大小(单位:b)
|
||||||
*
|
*
|
||||||
* @return array
|
* @return array{
|
||||||
|
* 'accessid' : string,
|
||||||
|
* 'host' : string,
|
||||||
|
* 'policy': string,
|
||||||
|
* 'signature': string,
|
||||||
|
* 'expire' : int,
|
||||||
|
* 'dir' : string,
|
||||||
|
* 'callback':string
|
||||||
|
* }
|
||||||
*/
|
*/
|
||||||
public function generatePolicy(
|
public function generatePolicy(
|
||||||
string $dir = null,
|
string $dir = null,
|
||||||
bool $isImage = false,
|
bool $isImage = false,
|
||||||
int $maxFileSize = 1048576000
|
int $maxFileSize = 1048576000
|
||||||
): array {
|
): array {
|
||||||
$expire_time = time() + self::EXPIRE;
|
$expire_time = time() + $this->expiration;
|
||||||
$expiration = $this->gmtIso8601($expire_time);
|
$expiration = $this->gmtIso8601($expire_time);
|
||||||
|
|
||||||
//最大文件大小.用户可以自己设置
|
//最大文件大小.用户可以自己设置
|
||||||
@@ -74,7 +85,7 @@ class OssService
|
|||||||
$signature = base64_encode(hash_hmac('sha1', $string_to_sign, $this->accessKeySecret, true));
|
$signature = base64_encode(hash_hmac('sha1', $string_to_sign, $this->accessKeySecret, true));
|
||||||
|
|
||||||
$callback = Json::encode([
|
$callback = Json::encode([
|
||||||
'callbackUrl' => config('oss_callback'),
|
'callbackUrl' => $this->callbackUrl,
|
||||||
'callbackBody' => $isImage ? <<<'callbackBody'
|
'callbackBody' => $isImage ? <<<'callbackBody'
|
||||||
{
|
{
|
||||||
"bucket": ${bucket},
|
"bucket": ${bucket},
|
||||||
@@ -91,7 +102,7 @@ class OssService
|
|||||||
"name": ${x:name},
|
"name": ${x:name},
|
||||||
"hash": ${x:hash}
|
"hash": ${x:hash}
|
||||||
}
|
}
|
||||||
callbackBody: <<<'callbackBody'
|
callbackBody : <<<'callbackBody'
|
||||||
{
|
{
|
||||||
"bucket": ${bucket},
|
"bucket": ${bucket},
|
||||||
"object": ${object},
|
"object": ${object},
|
||||||
|
|||||||
@@ -46,7 +46,7 @@ class SmsService
|
|||||||
*
|
*
|
||||||
* @param string $phone 接收短信的手机号码
|
* @param string $phone 接收短信的手机号码
|
||||||
* @param string|null $templateCode 短信模板CODE
|
* @param string|null $templateCode 短信模板CODE
|
||||||
* @param array|null $templateParam 短信模板变量对应的实际值(不支持空数组)
|
* @param array<string, string>|null $templateParam 短信模板变量对应的实际值(不支持空数组)
|
||||||
* @param string|null $signName 短信签名名称
|
* @param string|null $signName 短信签名名称
|
||||||
*
|
*
|
||||||
* @return bool
|
* @return bool
|
||||||
|
|||||||
@@ -10,7 +10,6 @@ use Generator;
|
|||||||
use Hyperf\Contract\StdoutLoggerInterface;
|
use Hyperf\Contract\StdoutLoggerInterface;
|
||||||
use Hyperf\Di\Annotation\Inject;
|
use Hyperf\Di\Annotation\Inject;
|
||||||
use Hyperf\HttpServer\Contract\RequestInterface;
|
use Hyperf\HttpServer\Contract\RequestInterface;
|
||||||
use JetBrains\PhpStorm\ArrayShape;
|
|
||||||
use JetBrains\PhpStorm\Pure;
|
use JetBrains\PhpStorm\Pure;
|
||||||
use Psr\Http\Message\ResponseInterface;
|
use Psr\Http\Message\ResponseInterface;
|
||||||
|
|
||||||
@@ -21,13 +20,13 @@ class UtilsService
|
|||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @Inject
|
* @Inject
|
||||||
* @var \Hyperf\HttpServer\Contract\RequestInterface
|
* @var RequestInterface
|
||||||
*/
|
*/
|
||||||
private RequestInterface $request;
|
private RequestInterface $request;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @Inject()
|
* @Inject()
|
||||||
* @var \Hyperf\Contract\StdoutLoggerInterface
|
* @var StdoutLoggerInterface
|
||||||
*/
|
*/
|
||||||
private StdoutLoggerInterface $logger;
|
private StdoutLoggerInterface $logger;
|
||||||
|
|
||||||
@@ -51,12 +50,12 @@ class UtilsService
|
|||||||
{
|
{
|
||||||
$ip = $this->request->server('remote_addr');
|
$ip = $this->request->server('remote_addr');
|
||||||
if (empty($ip) or in_array(
|
if (empty($ip) or in_array(
|
||||||
$ip,
|
$ip,
|
||||||
[
|
[
|
||||||
'localhost',
|
'localhost',
|
||||||
'127.0.0.1',
|
'127.0.0.1',
|
||||||
]
|
]
|
||||||
)) {
|
)) {
|
||||||
$ip = $this->request->header('x-real-ip');
|
$ip = $this->request->header('x-real-ip');
|
||||||
}
|
}
|
||||||
return $ip ?? 'unknown';
|
return $ip ?? 'unknown';
|
||||||
@@ -65,13 +64,13 @@ class UtilsService
|
|||||||
/**
|
/**
|
||||||
* 在响应头中添加 Link 字段
|
* 在响应头中添加 Link 字段
|
||||||
*
|
*
|
||||||
* @param \Psr\Http\Message\ResponseInterface $response
|
* @param ResponseInterface $response
|
||||||
* @param string|null $urlReference
|
* @param string|null $urlReference
|
||||||
* @param string $rel
|
* @param string $rel
|
||||||
* @param array $params
|
* @param array<string, string> $params
|
||||||
* @param bool $trimQuote
|
* @param bool $trimQuote
|
||||||
*
|
*
|
||||||
* @return \Psr\Http\Message\ResponseInterface
|
* @return ResponseInterface
|
||||||
*/
|
*/
|
||||||
public function extendLinkToHeader(
|
public function extendLinkToHeader(
|
||||||
ResponseInterface $response,
|
ResponseInterface $response,
|
||||||
@@ -87,7 +86,7 @@ class UtilsService
|
|||||||
$params['rel'] = $rel;
|
$params['rel'] = $rel;
|
||||||
if (!$trimQuote) {
|
if (!$trimQuote) {
|
||||||
foreach ($params as &$value) {
|
foreach ($params as &$value) {
|
||||||
$value = '"' . trim($value, '"') . '"';
|
$value = sprintf("\"%s\"", trim($value, '"'));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -109,14 +108,14 @@ class UtilsService
|
|||||||
* 构建 URL
|
* 构建 URL
|
||||||
*
|
*
|
||||||
* @param string $url
|
* @param string $url
|
||||||
* @param array $params
|
* @param array<string, string> $params
|
||||||
*
|
*
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function buildUrl(string $url, array $params = []): string
|
public function buildUrl(string $url, array $params = []): string
|
||||||
{
|
{
|
||||||
$url_info = parse_url($url);
|
$url_info = parse_url($url);
|
||||||
$base_url = str_replace(('?' . $url_info['query'] ?? ''), '', $url);
|
$base_url = str_replace('?' . $url_info['query'], '', $url);
|
||||||
parse_str($url_info['query'], $origin_params);
|
parse_str($url_info['query'], $origin_params);
|
||||||
$origin_params += $params;
|
$origin_params += $params;
|
||||||
|
|
||||||
@@ -128,8 +127,8 @@ class UtilsService
|
|||||||
*
|
*
|
||||||
* @param string $fullName
|
* @param string $fullName
|
||||||
*
|
*
|
||||||
* @return \Generator
|
* @return Generator
|
||||||
* @throws \Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function csvReaderGenerator(string $fullName): Generator
|
public function csvReaderGenerator(string $fullName): Generator
|
||||||
{
|
{
|
||||||
@@ -148,7 +147,11 @@ class UtilsService
|
|||||||
/**
|
/**
|
||||||
* 以数组形式响应
|
* 以数组形式响应
|
||||||
*
|
*
|
||||||
* @throws \Exception
|
* @param string $fullName
|
||||||
|
* @param array<string> $headers
|
||||||
|
* @param int|null $batchNumber
|
||||||
|
* @return array<int, mixed>
|
||||||
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function csvReader(string $fullName, array $headers = [], ?int $batchNumber = null): array
|
public function csvReader(string $fullName, array $headers = [], ?int $batchNumber = null): array
|
||||||
{
|
{
|
||||||
@@ -180,11 +183,11 @@ class UtilsService
|
|||||||
* (level倒序)快速无极分类(时间复杂度 O(n),空间复杂度 O(1))
|
* (level倒序)快速无极分类(时间复杂度 O(n),空间复杂度 O(1))
|
||||||
* 条件:数组索引是数据parent_id对应的id,只支持level倒序
|
* 条件:数组索引是数据parent_id对应的id,只支持level倒序
|
||||||
*
|
*
|
||||||
* @param array $list
|
* @param array<int, mixed> $list
|
||||||
* @param int $level
|
* @param int $level
|
||||||
* @param string $parentIdName
|
* @param string $parentIdName
|
||||||
* @param string $childrenName
|
* @param string $childrenName
|
||||||
* @param \Closure|null $filterCallback
|
* @param Closure|null $filterCallback
|
||||||
*
|
*
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
@@ -229,16 +232,13 @@ class UtilsService
|
|||||||
/**
|
/**
|
||||||
* 将内存/硬盘大小转化为带单位大小
|
* 将内存/硬盘大小转化为带单位大小
|
||||||
*
|
*
|
||||||
* @param float|int|string $size 空间大小(单位:Byte)
|
* @param float|int|string $size 空间大小(单位:Byte)
|
||||||
* @param bool $unitToUpper 单位转换为大写(默认 false)
|
* @param bool $unitToUpper 单位转换为大写(默认 false)
|
||||||
* @param int $precision 小数点后保留的位数(默认 2)
|
* @param int $precision 小数点后保留的位数(默认 2)
|
||||||
*
|
*
|
||||||
* @return array
|
* @return array{'size': float, 'unit': string}
|
||||||
*/
|
*/
|
||||||
#[
|
#[Pure]
|
||||||
ArrayShape(['size' => 'float', 'unit' => 'string']),
|
|
||||||
Pure
|
|
||||||
]
|
|
||||||
public function convertStorageSize(
|
public function convertStorageSize(
|
||||||
float|int|string $size,
|
float|int|string $size,
|
||||||
bool $unitToUpper = false,
|
bool $unitToUpper = false,
|
||||||
@@ -262,18 +262,13 @@ class UtilsService
|
|||||||
* 将时长转换为带单位时间
|
* 将时长转换为带单位时间
|
||||||
*
|
*
|
||||||
* @param int|float $duration 时长(单位:ms)
|
* @param int|float $duration 时长(单位:ms)
|
||||||
* @param bool $unitToUpper
|
* @param bool $unitToUpper
|
||||||
* @param bool $format
|
* @param bool $format
|
||||||
* @param integer $precision
|
* @param integer $precision
|
||||||
*
|
*
|
||||||
* @return array
|
* @return array{'duration': float, 'unit': string}
|
||||||
*/
|
*/
|
||||||
#[
|
#[Pure]
|
||||||
ArrayShape([
|
|
||||||
'duration' => 'float',
|
|
||||||
'unit' => 'string',
|
|
||||||
])
|
|
||||||
]
|
|
||||||
public function convertDuration(
|
public function convertDuration(
|
||||||
int|float $duration,
|
int|float $duration,
|
||||||
bool $unitToUpper = false,
|
bool $unitToUpper = false,
|
||||||
|
|||||||
@@ -24,7 +24,7 @@
|
|||||||
|
|
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
function something()
|
/*function something()
|
||||||
{
|
{
|
||||||
// ..
|
// ..
|
||||||
}
|
}*/
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ namespace Singularity\HDK\Test\Core\Unit;
|
|||||||
use Singularity\HDK\Core\Service\Base64Wrapper;
|
use Singularity\HDK\Core\Service\Base64Wrapper;
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
it('assertions base64 wrapper ENCODE', function(string $source, string $expected) {
|
it('assertions base64 wrapper ENCODE', function (string $source, string $expected) {
|
||||||
$encoded = (new Base64Wrapper())->encode($source);
|
$encoded = (new Base64Wrapper())->encode($source);
|
||||||
|
|
||||||
expect($expected)->toBe($encoded);
|
expect($expected)->toBe($encoded);
|
||||||
@@ -22,7 +22,7 @@ it('assertions base64 wrapper ENCODE', function(string $source, string $expected
|
|||||||
['YWJ/+j', 'YWJ_-j']
|
['YWJ/+j', 'YWJ_-j']
|
||||||
]);
|
]);
|
||||||
|
|
||||||
it('assertions base64 wrapper DECODE', function(string $source, string $expected) {
|
it('assertions base64 wrapper DECODE', function (string $source, string $expected) {
|
||||||
$decoded = (new Base64Wrapper())->decode($source);
|
$decoded = (new Base64Wrapper())->decode($source);
|
||||||
|
|
||||||
expect($expected)->toBe($decoded);
|
expect($expected)->toBe($decoded);
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ it('assertions that send HTML is available', function () use ($email) {
|
|||||||
HTML
|
HTML
|
||||||
);
|
);
|
||||||
expect($result)->toBeTrue();
|
expect($result)->toBeTrue();
|
||||||
})->skip();
|
});
|
||||||
|
|
||||||
it('assertions that send Text is available', function () use ($email) {
|
it('assertions that send Text is available', function () use ($email) {
|
||||||
$result = $email->sendText(
|
$result = $email->sendText(
|
||||||
@@ -37,7 +37,7 @@ it('assertions that send Text is available', function () use ($email) {
|
|||||||
Text
|
Text
|
||||||
);
|
);
|
||||||
expect($result)->toBeTrue();
|
expect($result)->toBeTrue();
|
||||||
})->skip();
|
});
|
||||||
|
|
||||||
it('assertions Error Receiver can be catch', function () use ($email) {
|
it('assertions Error Receiver can be catch', function () use ($email) {
|
||||||
try {
|
try {
|
||||||
|
|||||||
Reference in New Issue
Block a user