mirror of
http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore.git
synced 2026-01-15 05:35:09 +08:00
Compare commits
8 Commits
v1.0.0-alp
...
v1.0.0-alp
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fcb0cd8852 | ||
|
|
7a209e876f | ||
|
|
4032d67f1e | ||
|
|
dbbe278aa6 | ||
|
|
d84855f038 | ||
|
|
2d95fbae9e | ||
|
|
3042ce0054 | ||
|
|
b30c84488b |
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1,3 +1,4 @@
|
||||
vendor/
|
||||
.phpunit.result.cache
|
||||
.php-cs-fixer.cache
|
||||
.php-cs-fixer.cache
|
||||
runtime/
|
||||
6
.idea/HDK-Core.iml
generated
6
.idea/HDK-Core.iml
generated
@@ -2,7 +2,6 @@
|
||||
<module type="WEB_MODULE" version="4">
|
||||
<component name="NewModuleRootManager">
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/spec" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" packagePrefix="Singularity\HDK\Core\" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/tests" isTestSource="true" packagePrefix="Singularity\HDK\Test\Core\" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/sebastian/recursion-context" />
|
||||
@@ -31,7 +30,6 @@
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/composer" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/fig/http-message-util" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/lmc/http-constants" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/psr/cache" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/psr/http-server-handler" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/psr/event-dispatcher" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/psr/http-server-middleware" />
|
||||
@@ -111,7 +109,6 @@
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/theseer/tokenizer" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/doctrine/lexer" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/doctrine/inflector" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/doctrine/annotations" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/doctrine/instantiator" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/ergebnis/http-method" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/firebase/php-jwt" />
|
||||
@@ -158,6 +155,9 @@
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/hyperf/serializer" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/hyperf/stringable" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/hyperf/support" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/cooper/hyperf-pest" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/psr/clock" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/symfony/polyfill-php83" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
|
||||
12
.idea/codeception.xml
generated
Normal file
12
.idea/codeception.xml
generated
Normal file
@@ -0,0 +1,12 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="Codeception">
|
||||
<option name="configurations">
|
||||
<list>
|
||||
<Configuration>
|
||||
<option name="path" value="$PROJECT_DIR$/tests" />
|
||||
</Configuration>
|
||||
</list>
|
||||
</option>
|
||||
</component>
|
||||
</project>
|
||||
6
.idea/pest.xml
generated
Normal file
6
.idea/pest.xml
generated
Normal file
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="PestSettings">
|
||||
<option name="preferredTestFlavor" value="TEST" />
|
||||
</component>
|
||||
</project>
|
||||
7
.idea/php.xml
generated
7
.idea/php.xml
generated
@@ -46,7 +46,6 @@
|
||||
<path value="$PROJECT_DIR$/vendor/composer" />
|
||||
<path value="$PROJECT_DIR$/vendor/fig/http-message-util" />
|
||||
<path value="$PROJECT_DIR$/vendor/lmc/http-constants" />
|
||||
<path value="$PROJECT_DIR$/vendor/psr/cache" />
|
||||
<path value="$PROJECT_DIR$/vendor/psr/http-server-handler" />
|
||||
<path value="$PROJECT_DIR$/vendor/psr/event-dispatcher" />
|
||||
<path value="$PROJECT_DIR$/vendor/psr/http-server-middleware" />
|
||||
@@ -130,7 +129,6 @@
|
||||
<path value="$PROJECT_DIR$/vendor/theseer/tokenizer" />
|
||||
<path value="$PROJECT_DIR$/vendor/doctrine/lexer" />
|
||||
<path value="$PROJECT_DIR$/vendor/doctrine/inflector" />
|
||||
<path value="$PROJECT_DIR$/vendor/doctrine/annotations" />
|
||||
<path value="$PROJECT_DIR$/vendor/doctrine/instantiator" />
|
||||
<path value="$PROJECT_DIR$/vendor/ergebnis/http-method" />
|
||||
<path value="$PROJECT_DIR$/vendor/firebase/php-jwt" />
|
||||
@@ -175,9 +173,12 @@
|
||||
<path value="$PROJECT_DIR$/vendor/hyperf/stringable" />
|
||||
<path value="$PROJECT_DIR$/vendor/hyperf/support" />
|
||||
<path value="$PROJECT_DIR$/vendor/ralouphie/getallheaders" />
|
||||
<path value="$PROJECT_DIR$/vendor/cooper/hyperf-pest" />
|
||||
<path value="$PROJECT_DIR$/vendor/psr/clock" />
|
||||
<path value="$PROJECT_DIR$/vendor/symfony/polyfill-php83" />
|
||||
</include_path>
|
||||
</component>
|
||||
<component name="PhpProjectSharedConfiguration" php_language_level="8.0" />
|
||||
<component name="PhpProjectSharedConfiguration" php_language_level="8.1" />
|
||||
<component name="PhpStan">
|
||||
<PhpStan_settings>
|
||||
<PhpStanConfiguration tool_path="$PROJECT_DIR$/vendor/bin/phpstan" />
|
||||
|
||||
31
.idea/phpspec.xml
generated
Normal file
31
.idea/phpspec.xml
generated
Normal file
@@ -0,0 +1,31 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="PHPSpec">
|
||||
<suites>
|
||||
<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>
|
||||
<PhpSpecSuiteConfiguration>
|
||||
<option name="myPath" value="$PROJECT_DIR$" />
|
||||
</PhpSpecSuiteConfiguration>
|
||||
<PhpSpecSuiteConfiguration>
|
||||
<option name="myPath" value="$PROJECT_DIR$" />
|
||||
</PhpSpecSuiteConfiguration>
|
||||
</suites>
|
||||
</component>
|
||||
</project>
|
||||
31
CHANGELOG.md
31
CHANGELOG.md
@@ -1,4 +1,35 @@
|
||||
# 版本更新日志
|
||||
## [1.0.0-alpha.4](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/compare/v1.0.0-alpha.3...v1.0.0-alpha.4) (2023-09-26)
|
||||
|
||||
|
||||
### 📦 Build System | 打包构建
|
||||
|
||||
* **composer:** 引入 pest 并升级到 php8.1 ([dbbe278](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/commit/dbbe278aa6c96634ed5792b5782825770c5cabe8))
|
||||
|
||||
|
||||
### ✨ Features | 新功能
|
||||
|
||||
* **i18n:** 增加枚举值,更新中间件调用方式 ([4032d67](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/commit/4032d67f1e56b8ce29fbfa5bd77ead2636a6ffd3))
|
||||
|
||||
|
||||
### ✅ Tests | 测试
|
||||
|
||||
* **http:** 完成并通过了 http 请求的测试 ([7a209e8](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/commit/7a209e876f8b4301b7bc78f05b68798eb65e77f7))
|
||||
|
||||
## [1.0.0-alpha.3](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/compare/v1.0.0-alpha.2...v1.0.0-alpha.3) (2023-08-30)
|
||||
|
||||
|
||||
### ♻️ Code Refactoring | 代码重构
|
||||
|
||||
* **http:** 整理了 Http 请求服务 ([2d95fba](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/commit/2d95fbae9e926788402c24266ab0d232732a7754))
|
||||
|
||||
## [1.0.0-alpha.2](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/compare/v1.0.0-alpha.1...v1.0.0-alpha.2) (2023-07-21)
|
||||
|
||||
|
||||
### 🐛 Bug Fixes | Bug 修复
|
||||
|
||||
* **controller:** 修复方法访问级别不足以继承的问题 ([b30c844](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/commit/b30c84488b7f85447bd802df77c9decbf315ed85))
|
||||
|
||||
## [1.0.0-alpha.1](http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore/compare/v1.0.0-alpha.0...v1.0.0-alpha.1) (2023-07-21)
|
||||
|
||||
|
||||
|
||||
@@ -1 +1 @@
|
||||
1.0.0-alpha.1
|
||||
1.0.0-alpha.4
|
||||
@@ -15,7 +15,7 @@
|
||||
}
|
||||
],
|
||||
"require": {
|
||||
"php": "^8.0",
|
||||
"php": "^8.1",
|
||||
"ext-redis": "^5.3",
|
||||
"ext-swoole": "*",
|
||||
"composer/composer": ">=2.0",
|
||||
@@ -41,6 +41,7 @@
|
||||
},
|
||||
"require-dev": {
|
||||
"alibabacloud/dysmsapi-20170525": "^2.0",
|
||||
"cooper/hyperf-pest": "^1.1",
|
||||
"firebase/php-jwt": "^6.1",
|
||||
"friendsofphp/php-cs-fixer": "^3.13",
|
||||
"guzzlehttp/guzzle": "^7.5",
|
||||
@@ -91,7 +92,11 @@
|
||||
"prefer-stable": true,
|
||||
"scripts": {
|
||||
"post-root-package-install": [],
|
||||
"test": "vendor/bin/pest $1",
|
||||
"test": [
|
||||
"rm -rf runtime",
|
||||
"Composer\\Config::disableProcessTimeout",
|
||||
"co-pest --prepend tests/bootstrap.php -c phpunit.xml --colors=always $1"
|
||||
],
|
||||
"cs-fix": "vendor/bin/php-cs-fixer fix $1 --rules=@PSR12 --allow-risky=yes",
|
||||
"analyse": "vendor/bin/phpstan analyse $1",
|
||||
"ci": [
|
||||
@@ -111,5 +116,5 @@
|
||||
"url": "https://mirrors.aliyun.com/composer/"
|
||||
}
|
||||
},
|
||||
"version": "1.0.0-alpha.1"
|
||||
"version": "1.0.0-alpha.4"
|
||||
}
|
||||
|
||||
23168
composer.lock
generated
23168
composer.lock
generated
File diff suppressed because it is too large
Load Diff
26
docs/Http 请求.md
Normal file
26
docs/Http 请求.md
Normal file
@@ -0,0 +1,26 @@
|
||||
# Http 请求
|
||||
|
||||
## 1 组成
|
||||
|
||||
* 单例类
|
||||
* \Singularity\HDK\Core\Http\RequestService
|
||||
* 工厂类
|
||||
* \Singularity\HDK\Core\Http\RequestServiceFactory
|
||||
|
||||
## 2 初始化服务
|
||||
|
||||
创建一个请求服务的方法有两种,效果是一致的:
|
||||
|
||||
* 工厂模式
|
||||
* `\Singularity\HDK\Core\Http\RequestServiceFactory::make($options)`
|
||||
* `\Singularity\HDK\Core\Http\RequestServiceFactory::make()->setOptions($options)`
|
||||
* 单例模式
|
||||
* `new \Singularity\HDK\Core\Http\RequestService($options)`
|
||||
* `(new \Singularity\HDK\Core\Http\RequestService())->setOptions($options)`
|
||||
|
||||
这两个方法都回返回一个 `\Singularity\HDK\Core\Http\RequestService` 实例
|
||||
|
||||
## 3 规范
|
||||
|
||||
每个通过本包提供的 Http 请求服务创建的服务,都应该在构建方法中实例化一个 RequestService 服务,
|
||||
之后该服务中使用的所有请求都基于该服务,这样可以避免共用单例模式导致的 options 污染
|
||||
@@ -7,4 +7,4 @@ docker run \
|
||||
-v "$(pwd)":/srv/www \
|
||||
-v ~/.ssh:/root/.ssh \
|
||||
-v ~/.gitconfig:/root/.gitconfig \
|
||||
harbor.luxcreo.cn/library/hyperf:8.0-swoole /bin/ash
|
||||
harbor.luxcreo.cn/library/hyperf:8.1-swoole /bin/ash
|
||||
|
||||
191
src/Http/RequestService.php
Normal file
191
src/Http/RequestService.php
Normal file
@@ -0,0 +1,191 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Singularity\HDK\Core\Http;
|
||||
|
||||
use Ergebnis\Http\Method;
|
||||
use GuzzleHttp\Client;
|
||||
use GuzzleHttp\Exception\GuzzleException;
|
||||
use GuzzleHttp\Psr7\Request;
|
||||
use GuzzleHttp\Psr7\Utils;
|
||||
use Hyperf\Codec\Json;
|
||||
use Hyperf\Guzzle\ClientFactory;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Singularity\HDK\Core\Service\AbstractSingleton;
|
||||
|
||||
/**
|
||||
* Singularity\HDK\Core\Service\Http\RequestService@Core
|
||||
*
|
||||
* @author 李东云 <Dongyun.Li@LuxCreo.Ai>
|
||||
* Powered by PhpStorm
|
||||
* Created on 2023/8/30
|
||||
*
|
||||
* @template Options of array<literal-string, mixed>
|
||||
*/
|
||||
final class RequestService extends AbstractSingleton
|
||||
{
|
||||
// public const TIMEOUT = 20;
|
||||
|
||||
private ClientFactory $client;
|
||||
|
||||
/**
|
||||
* @var Options
|
||||
*/
|
||||
private array $options = [];
|
||||
|
||||
/**
|
||||
* @param Options $options
|
||||
*/
|
||||
public function __construct(array $options = [])
|
||||
{
|
||||
$this->setOptions($options);
|
||||
/** @var ClientFactory $client */
|
||||
$client = \Hyperf\Support\make(ClientFactory::class);
|
||||
$this->client = $client;
|
||||
}
|
||||
|
||||
/**
|
||||
* 定制 options
|
||||
* @param Options $options
|
||||
* @return $this
|
||||
*/
|
||||
public function setOptions(array $options): self
|
||||
{
|
||||
$this->options = array_replace_recursive($this->options, $options);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $url
|
||||
* @param array<string, string|int> $params
|
||||
* @param array $data
|
||||
* @param Options $options
|
||||
* @return ResponseInterface
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public function requestGet(
|
||||
string $url,
|
||||
array $params = [],
|
||||
array $data = [],
|
||||
array $options = []
|
||||
): ResponseInterface {
|
||||
$request = new Request(
|
||||
method: Method\Rfc\Rfc7231::GET, uri: $url
|
||||
);
|
||||
return $this->getClient($options)->send($request, ['query' => $params, 'json' => $data,]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 工厂模式初始化请求
|
||||
* @param Options $options
|
||||
* @return Client
|
||||
*/
|
||||
private function getClient(array $options): Client
|
||||
{
|
||||
$options = array_replace_recursive($this->options, $options);
|
||||
return $this->client->create($options);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $url
|
||||
* @param array<string, string|int> $params
|
||||
* @param array<string, mixed> $data
|
||||
* @param Options $options
|
||||
* @return ResponseInterface
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public function requestPost(
|
||||
string $url,
|
||||
array $params = [],
|
||||
array $data = [],
|
||||
array $options = []
|
||||
): ResponseInterface {
|
||||
$data = Json::encode($data);
|
||||
$request = new Request(
|
||||
method: Method\Rfc\Rfc7231::POST,
|
||||
uri: $url,
|
||||
headers: ['Content-Type' => 'application/json'],
|
||||
body: Utils::streamFor($data),
|
||||
);
|
||||
return $this->getClient($options)->send($request, ['query' => $params,]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $url
|
||||
* @param array<string, mixed> $data
|
||||
* @param array<string, string|int> $params
|
||||
* @param Options $options
|
||||
* @return ResponseInterface
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public function requestPut(
|
||||
string $url,
|
||||
array $params = [],
|
||||
array $data = [],
|
||||
array $options = []
|
||||
): ResponseInterface {
|
||||
$data = Json::encode($data);
|
||||
$request = new Request(
|
||||
method: Method\Rfc\Rfc7231::PUT,
|
||||
uri: $url,
|
||||
headers: ['Content-Type' => 'application/json'],
|
||||
body: Utils::streamFor($data)
|
||||
);
|
||||
return $this->getClient($options)->send(
|
||||
$request, ['query' => $params,]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $url
|
||||
* @param array<string, mixed> $data
|
||||
* @param array<string, string|int> $params
|
||||
* @param Options $options
|
||||
* @return ResponseInterface
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public function requestPatch(
|
||||
string $url,
|
||||
array $params = [],
|
||||
array $data = [],
|
||||
array $options = []
|
||||
): ResponseInterface {
|
||||
$data = Json::encode($data);
|
||||
$request = new Request(
|
||||
method: Method\Rfc\Rfc5789::PATCH,
|
||||
uri: $url,
|
||||
headers: ['Content-Type' => 'application/json'],
|
||||
body: Utils::streamFor($data)
|
||||
);
|
||||
return $this->getClient($options)->send(
|
||||
$request, ['query' => $params,]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $url
|
||||
* @param array<string, mixed> $data
|
||||
* @param array<string, string|int> $params
|
||||
* @param Options $options
|
||||
* @return ResponseInterface
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public function requestDelete(
|
||||
string $url,
|
||||
array $params = [],
|
||||
array $data = [],
|
||||
array $options = []
|
||||
): ResponseInterface {
|
||||
$data = Json::encode($data);
|
||||
$request = new Request(
|
||||
method: Method\Rfc\Rfc7231::DELETE,
|
||||
uri: $url,
|
||||
headers: ['Content-Type' => 'application/json'],
|
||||
body: Utils::streamFor($data)
|
||||
);
|
||||
return $this->getClient($options)->send(
|
||||
$request, ['query' => $params,]
|
||||
);
|
||||
}
|
||||
}
|
||||
22
src/Http/RequestServiceFactory.php
Normal file
22
src/Http/RequestServiceFactory.php
Normal file
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* RequestFactory.php@Core
|
||||
*
|
||||
* @author 李东云 <Dongyun.Li@LuxCreo.Ai>
|
||||
* Powered by PhpStorm
|
||||
* Created on 2023/8/30
|
||||
*/
|
||||
|
||||
namespace Singularity\HDK\Core\Http;
|
||||
|
||||
use Singularity\HDK\Core\Service\AbstractFactory;
|
||||
|
||||
final class RequestServiceFactory extends AbstractFactory
|
||||
{
|
||||
public static function make(array $options = []): RequestService
|
||||
{
|
||||
return new RequestService($options);
|
||||
}
|
||||
}
|
||||
28
src/I18n/Enum/Languages.php
Normal file
28
src/I18n/Enum/Languages.php
Normal file
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
/**
|
||||
* Languages.php@Core
|
||||
*
|
||||
* @author 李东云 <Dongyun.Li@LuxCreo.Ai>
|
||||
* Powered by PhpStorm
|
||||
* Created on 2023/9/26
|
||||
*/
|
||||
|
||||
namespace Singularity\HDK\Core\I18n\Enum;
|
||||
|
||||
use function Hyperf\Config\config;
|
||||
|
||||
enum Languages: string
|
||||
{
|
||||
case ZH_CN = 'zh_CN';
|
||||
|
||||
case EN = 'en';
|
||||
|
||||
public static function getLanguage(string $language): Languages
|
||||
{
|
||||
return match ($language) {
|
||||
'en', 'en_us', 'en-us', 'en-uk', 'en_uk' => self::EN,
|
||||
'zh', 'zh_cn', 'zh-cn' => self::ZH_CN,
|
||||
default => self::from(config('translation.locale')),
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -12,10 +12,9 @@ use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Psr\Http\Server\MiddlewareInterface;
|
||||
use Psr\Http\Server\RequestHandlerInterface;
|
||||
use Singularity\HDK\Core\I18n\Enum\Languages;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
|
||||
use function Hyperf\Config\config;
|
||||
|
||||
/**
|
||||
* Singularity\HDK\Utils\Middleware\InternationalizationMiddleware@HDK
|
||||
*
|
||||
@@ -41,12 +40,8 @@ class InternationalizationMiddleware implements MiddlewareInterface
|
||||
|
||||
$language = $req->getPreferredLanguage();
|
||||
if (!empty($language)) {
|
||||
$language = match (strtolower($language)) {
|
||||
'en', 'en_us', 'en-us', 'en-uk', 'en_uk' => 'en',
|
||||
'zh', 'zh_cn', 'zh-cn' => 'zh_CN',
|
||||
default => config('translation.locale'),
|
||||
};
|
||||
$this->translator->setLocale($language);
|
||||
$language = Languages::getLanguage($language);
|
||||
$this->translator->setLocale($language->value);
|
||||
}
|
||||
$response = Context::get(ResponseInterface::class);
|
||||
$response = $response->withAddedHeader(
|
||||
|
||||
30
src/Service/AbstractFactory.php
Normal file
30
src/Service/AbstractFactory.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* AbstractFactory.php@Core
|
||||
*
|
||||
* @author 李东云 <Dongyun.Li@LuxCreo.Ai>
|
||||
* Powered by PhpStorm
|
||||
* Created on 2023/8/30
|
||||
*/
|
||||
|
||||
namespace Singularity\HDK\Core\Service;
|
||||
|
||||
use Dont\DontCall;
|
||||
use Dont\DontInstantiate;
|
||||
|
||||
/**
|
||||
* Singularity\HDK\Core\Service\AbstractFactory@Core
|
||||
*
|
||||
* @author 李东云 <Dongyun.Li@LuxCreo.Ai>
|
||||
* Powered by PhpStorm
|
||||
* Created on 2023/8/30
|
||||
*/
|
||||
abstract class AbstractFactory
|
||||
{
|
||||
use DontInstantiate;
|
||||
use DontCall;
|
||||
|
||||
abstract static function make();
|
||||
}
|
||||
26
src/Service/AbstractSingleton.php
Normal file
26
src/Service/AbstractSingleton.php
Normal file
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
/**
|
||||
* SingletonInterface.php@Core
|
||||
*
|
||||
* @author 李东云 <Dongyun.Li@LuxCreo.Ai>
|
||||
* Powered by PhpStorm
|
||||
* Created on 2023/8/30
|
||||
*/
|
||||
|
||||
namespace Singularity\HDK\Core\Service;
|
||||
|
||||
use Dont\DontCallStatic;
|
||||
use Dont\DontClone;
|
||||
|
||||
/**
|
||||
* Singularity\HDK\Core\Service\SingletonInterface@Core
|
||||
*
|
||||
* @author 李东云 <Dongyun.Li@LuxCreo.Ai>
|
||||
* Powered by PhpStorm
|
||||
* Created on 2023/8/30
|
||||
*/
|
||||
abstract class AbstractSingleton
|
||||
{
|
||||
use DontCallStatic;
|
||||
use DontClone;
|
||||
}
|
||||
@@ -16,6 +16,7 @@ use Psr\Http\Message\ResponseInterface;
|
||||
|
||||
/**
|
||||
* 发起 Http 请求的类
|
||||
* @deprecated since version 1.0-alpha.3, to be removed in 1.0
|
||||
*/
|
||||
class HttpRequestService
|
||||
{
|
||||
|
||||
@@ -20,11 +20,11 @@ use Singularity\HDK\Core\Service\ExtendService;
|
||||
trait RestfulList
|
||||
{
|
||||
#[Inject]
|
||||
private ExtendService $extend;
|
||||
protected ExtendService $extend;
|
||||
#[Inject]
|
||||
private RequestInterface $request;
|
||||
protected RequestInterface $request;
|
||||
|
||||
public function restSelect(Closure $callback, ?Closure $filter = null)
|
||||
protected function restSelect(Closure $callback, ?Closure $filter = null)
|
||||
{
|
||||
$options = [];
|
||||
$result = $this->parseParameters(function ($query, $parsedOptions) use ($callback, &$options) {
|
||||
@@ -34,7 +34,7 @@ trait RestfulList
|
||||
return $this->responseFormatter($options, $result, $filter);
|
||||
}
|
||||
|
||||
public function parseParameters(Closure $callback): mixed
|
||||
protected function parseParameters(Closure $callback): mixed
|
||||
{
|
||||
$query = $this->request->getQueryParams();
|
||||
$options = [
|
||||
@@ -49,7 +49,7 @@ trait RestfulList
|
||||
return $callback(query: $query, options: $options);
|
||||
}
|
||||
|
||||
public function responseFormatter(
|
||||
protected function responseFormatter(
|
||||
array $options,
|
||||
Builder $builder,
|
||||
?Closure $filter = null
|
||||
|
||||
41
tests/Unit/RequestServiceTest.php
Normal file
41
tests/Unit/RequestServiceTest.php
Normal file
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* RequestServiceTest.php@Core
|
||||
*
|
||||
* @author 李东云 <Dongyun.Li@LuxCreo.Ai>
|
||||
* Powered by PhpStorm
|
||||
* Created on 2023/9/20
|
||||
*/
|
||||
|
||||
namespace Singularity\HDK\Test\Core\Unit;
|
||||
use Singularity\HDK\Core\Http\RequestService;
|
||||
use Singularity\HDK\Core\Http\RequestServiceFactory;
|
||||
|
||||
class RequestServiceTest
|
||||
{
|
||||
private RequestService $service;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->service = RequestServiceFactory::make();
|
||||
}
|
||||
|
||||
public function getInstance(): RequestService
|
||||
{
|
||||
return $this->service;
|
||||
}
|
||||
}
|
||||
|
||||
test('Http 请求服务可以正常使用', function () {
|
||||
/** @var RequestServiceTest $class */
|
||||
$class = \Hyperf\Support\make(RequestServiceTest::class);
|
||||
$response = $class->getInstance()->requestGet('http://baidu.com/');
|
||||
$content = $response->getBody()->getContents();
|
||||
expect($content)->toBe(<<<HTML
|
||||
<html>
|
||||
<meta http-equiv="refresh" content="0;url=http://www.baidu.com/">
|
||||
</html>
|
||||
|
||||
HTML);
|
||||
});
|
||||
@@ -9,8 +9,29 @@ declare(strict_types=1);
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
|
||||
use Hyperf\Context\ApplicationContext;
|
||||
use Hyperf\Di\Container;
|
||||
use Hyperf\Di\Definition\DefinitionSource;
|
||||
use Psr\Container\ContainerInterface;
|
||||
|
||||
ini_set('display_errors', 'on');
|
||||
ini_set('display_startup_errors', 'on');
|
||||
|
||||
error_reporting(E_ALL);
|
||||
date_default_timezone_set('Asia/Shanghai');
|
||||
|
||||
|
||||
!defined('BASE_PATH') && define('BASE_PATH', dirname(__DIR__, 1));
|
||||
!defined('SWOOLE_HOOK_FLAGS') && define('SWOOLE_HOOK_FLAGS', SWOOLE_HOOK_ALL);
|
||||
|
||||
Swoole\Runtime::enableCoroutine(true);
|
||||
|
||||
require BASE_PATH . '/vendor/autoload.php';
|
||||
Hyperf\Di\ClassLoader::init();
|
||||
|
||||
$container = new Container(new DefinitionSource([]));
|
||||
if (!$container instanceof ContainerInterface) {
|
||||
throw new RuntimeException('The dependency injection container is invalid.');
|
||||
}
|
||||
$container = ApplicationContext::setContainer($container);
|
||||
Reference in New Issue
Block a user