From 97131d224cfb601ba075ff1af1c4ca333680b459 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E4=B8=9C=E4=BA=91?= Date: Thu, 14 Dec 2023 16:03:23 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E8=BF=81=E7=A7=BB=E5=88=B0=20hyper?= =?UTF-8?q?f3.1=20=E7=9A=84=E5=86=99=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 李东云 --- composer.json | 1 + composer.lock | 2 +- hyperf31_rector.php | 197 +++++++++++++++++++++++++++++ src/Http/RequestService.php | 52 +++++--- src/Service/HttpRequestService.php | 22 ++-- tests/Unit/RequestServiceTest.php | 4 +- tests/bootstrap.php | 6 +- 7 files changed, 251 insertions(+), 33 deletions(-) create mode 100644 hyperf31_rector.php diff --git a/composer.json b/composer.json index dd8d928..92da1a6 100755 --- a/composer.json +++ b/composer.json @@ -57,6 +57,7 @@ "hyperf/validation": "3.1.*", "pestphp/pest": "^2.23", "phpstan/phpstan": "^1.10.49", + "rector/rector": "^0.15.25", "roave/security-advisories": "dev-latest", "spatie/pest-plugin-test-time": "^2.0", "swoole/ide-helper": "*", diff --git a/composer.lock b/composer.lock index a1e5aac..e5124a3 100755 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "56d5112e454a3ffc90fcdde8306fdb9a", + "content-hash": "917798670c1e06a83de5fc813e78b389", "packages": [ { "name": "composer/ca-bundle", diff --git a/hyperf31_rector.php b/hyperf31_rector.php new file mode 100644 index 0000000..ae7f833 --- /dev/null +++ b/hyperf31_rector.php @@ -0,0 +1,197 @@ + + * Powered by PhpStorm + * Created on 2023/12/14 + */ +declare(strict_types=1); + +use Rector\Config\RectorConfig; +use Rector\Renaming\Rector\FuncCall\RenameFunctionRector; +use Rector\Renaming\Rector\MethodCall\RenameMethodRector; +use Rector\Renaming\Rector\Name\RenameClassRector; +use Rector\Renaming\ValueObject\MethodCallRename; + +return static function (RectorConfig $rectorConfig): void { + $rectorConfig->importNames(true, false); + + $rectorConfig->paths([ + __DIR__ . '/src', + __DIR__ . '/publish', + __DIR__ . '/tests', + ]); + + $rectorConfig + ->ruleWithConfiguration(RenameClassRector::class, [ + // AsyncQueue + 'Hyperf\AsyncQueue\Message' => 'Hyperf\AsyncQueue\JobMessage', + + // DbConnection + 'Hyperf\DbConnection\Listener\CreatingListener' => 'Hyperf\DbConnection\Listener\InitUidOnCreatingListener', + + // HttpMessage + 'Hyperf\HttpMessage\Server\ConnectionInterface' => 'Hyperf\Engine\Contract\Http\Writable', + 'Hyperf\HttpMessage\Server\Connection\SwooleConnection' => 'Hyperf\Engine\Http\WritableConnection', + + // Redis + 'Hyperf\Redis\ScanCaller' => 'Hyperf\Redis\Traits\ScanCaller', + + // Channel + 'Hyperf\Utils\Channel\Caller' => 'Hyperf\Coroutine\Channel\Caller', + 'Hyperf\Utils\Channel\ChannelManager' => 'Hyperf\Coroutine\Channel\Manager', + + // Codec + 'Hyperf\Utils\Codec\Base62' => 'Hyperf\Codec\Base62', + 'Hyperf\Utils\Codec\Json' => 'Hyperf\Codec\Json', + 'Hyperf\Utils\Codec\Xml' => 'Hyperf\Codec\Xml', + + // CodeGen + 'Hyperf\Utils\CodeGen\Package' => 'Hyperf\CodeParser\Package', + 'Hyperf\Utils\CodeGen\PhpDocReader' => 'Hyperf\CodeParser\PhpDocReader', + 'Hyperf\Utils\CodeGen\PhpDocReaderManager' => 'Hyperf\CodeParser\PhpDocReaderManager', + 'Hyperf\Utils\CodeGen\PhpParser' => 'Hyperf\CodeParser\PhpParser', + 'Hyperf\Utils\CodeGen\Project' => 'Hyperf\CodeParser\Project', + + // Command + 'Hyperf\Command\ConfirmableTrait' => 'Hyperf\Command\Concerns\Confirmable', + 'Hyperf\Command\DisableEventDispatcher' => 'Hyperf\Command\Concerns\DisableEventDispatcher', + 'Hyperf\Command\NullDisableEventDispatcher' => 'Hyperf\Command\Concerns\NullDisableEventDispatcher', + + // Contracts + 'Hyperf\Utils\Contracts\Arrayable' => 'Hyperf\Contract\Arrayable', + 'Hyperf\Utils\Contracts\Jsonable' => 'Hyperf\Contract\Jsonable', + 'Hyperf\Utils\Contracts\MessageBag' => 'Hyperf\Contract\MessageBag', + 'Hyperf\Utils\Contracts\MessageProvider' => 'Hyperf\Contract\MessageProvider', + 'Hyperf\Utils\Contracts\Xmlable' => 'Hyperf\Contract\Xmlable', + + // Coordinator + 'Hyperf\Utils\Coordinator\Constants' => 'Hyperf\Coordinator\Constants', + 'Hyperf\Utils\Coordinator\Coordinator' => 'Hyperf\Coordinator\Coordinator', + 'Hyperf\Utils\Coordinator\CoordinatorManager' => 'Hyperf\Coordinator\CoordinatorManager', + + // Coroutine + 'Hyperf\Utils\Coroutine\Concurrent' => 'Hyperf\Coroutine\Concurrent', + 'Hyperf\Utils\Coroutine\Locker' => 'Hyperf\Coroutine\Locker', + + // Exception + 'Hyperf\Utils\Exception\ChannelClosedException' => 'Hyperf\Coroutine\Exception\ChannelClosedException', + 'Hyperf\Utils\Exception\ExceptionThrower' => 'Hyperf\Support\Exception\ExceptionThrower', + 'Hyperf\Utils\Exception\InvalidArgumentException' => 'Hyperf\Support\Exception\InvalidArgumentException', + 'Hyperf\Utils\Exception\IPReadFailedException' => 'Hyperf\Support\Exception\IPReadFailedException', + 'Hyperf\Utils\Exception\ParallelExecutionException' => 'Hyperf\Coroutine\Exception\ParallelExecutionException', + 'Hyperf\Utils\Exception\TimeoutException' => 'Hyperf\Coroutine\Exception\TimeoutException', + 'Hyperf\Utils\Exception\WaitTimeoutException' => 'Hyperf\Coroutine\Exception\WaitTimeoutException', + + // Filesystem + 'Hyperf\Utils\Filesystem\FileNotFoundException' => 'Hyperf\Support\Filesystem\FileNotFoundException', + 'Hyperf\Utils\Filesystem\Filesystem' => 'Hyperf\Support\Filesystem\Filesystem', + + // Packer + 'Hyperf\Utils\Packer\JsonPacker' => 'Hyperf\Codec\Packer\JsonPacker', + 'Hyperf\Utils\Packer\PhpSerializerPacker' => 'Hyperf\Codec\Packer\PhpSerializerPacker', + + // Reflection + 'Hyperf\Utils\Reflection\ClassInvoker' => 'Hyperf\Support\Reflection\ClassInvoker', + + // Serializer + 'Hyperf\Utils\Serializer\ExceptionNormalizer' => 'Hyperf\Serializer\ExceptionNormalizer', + 'Hyperf\Utils\Serializer\JsonDeNormalizer' => 'Hyperf\Serializer\JsonDeNormalizer', + 'Hyperf\Utils\Serializer\ScalarNormalizer' => 'Hyperf\Serializer\ScalarNormalizer', + 'Hyperf\Utils\Serializer\Serializer' => 'Hyperf\Serializer\Serializer', + 'Hyperf\Utils\Serializer\SerializerFactory' => 'Hyperf\Serializer\SerializerFactory', + 'Hyperf\Utils\Serializer\SimpleNormalizer' => 'Hyperf\Serializer\SimpleNormalizer', + 'Hyperf\Utils\Serializer\SymfonyNormalizer' => 'Hyperf\Serializer\SymfonyNormalizer', + + // Traits + 'Hyperf\Utils\Traits\Conditionable' => 'Hyperf\Conditionable\Conditionable', + 'Hyperf\Utils\Traits\Container' => 'Hyperf\Support\Traits\Container', + 'Hyperf\Utils\Traits\CoroutineProxy' => 'Hyperf\Context\Traits\CoroutineProxy', + 'Hyperf\Utils\Traits\ForwardsCalls' => 'Hyperf\Support\Traits\ForwardsCalls', + 'Hyperf\Utils\Traits\StaticInstance' => 'Hyperf\Support\Traits\StaticInstance', + 'Hyperf\Utils\Traits\Tappable' => 'Hyperf\Tappable\Tappable', + + // Utils + 'Hyperf\Utils\ApplicationContext' => 'Hyperf\Context\ApplicationContext', + 'Hyperf\Utils\Arr' => 'Hyperf\Collection\Arr', + 'Hyperf\Utils\Backoff' => 'Hyperf\Support\Backoff', + 'Hyperf\Utils\ChannelPool' => 'Hyperf\Coroutine\Channel\Pool', + 'Hyperf\Utils\ClearStatCache' => 'Hyperf\Support\ClearStatCache', + 'Hyperf\Utils\Collection' => 'Hyperf\Collection\Collection', + 'Hyperf\Utils\Composer' => 'Hyperf\Support\Composer', + 'Hyperf\Utils\Coroutine' => 'Hyperf\Coroutine\Coroutine', + 'Hyperf\Utils\Fluent' => 'Hyperf\Support\Fluent', + 'Hyperf\Utils\HigherOrderCollectionProxy' => 'Hyperf\Collection\HigherOrderCollectionProxy', + 'Hyperf\Utils\HigherOrderTapProxy' => 'Hyperf\Tappable\HigherOrderTapProxy', + 'Hyperf\Utils\InteractsWithTime' => 'Hyperf\Support\Traits\InteractsWithTime', + 'Hyperf\Utils\IPReader' => 'Hyperf\Support\IPReader', + 'Hyperf\Utils\MessageBag' => 'Hyperf\Support\MessageBag', + 'Hyperf\Utils\MimeTypeExtensionGuesser' => 'Hyperf\Support\MimeTypeExtensionGuesser', + 'Hyperf\Utils\Network' => 'Hyperf\Support\Network', + 'Hyperf\Utils\Optional' => 'Hyperf\Support\Optional', + 'Hyperf\Utils\Parallel' => 'Hyperf\Coroutine\Parallel', + 'Hyperf\Utils\Pipeline' => 'Hyperf\Pipeline\Pipeline', + 'Hyperf\Utils\Pluralizer' => 'Hyperf\Support\Pluralizer', + 'Hyperf\Utils\ResourceGenerator' => 'Hyperf\Support\ResourceGenerator', + 'Hyperf\Utils\SafeCaller' => 'Hyperf\Support\SafeCaller', + 'Hyperf\Utils\Str' => 'Hyperf\Stringable\Str', + 'Hyperf\Utils\Stringable' => 'Hyperf\Stringable\Stringable', + 'Hyperf\Utils\System' => 'Hyperf\Support\System', + 'Hyperf\Utils\Waiter' => 'Hyperf\Coroutine\Waiter', + 'Hyperf\Utils\WaitGroup' => 'Hyperf\Coroutine\WaitGroup', + ]); + + $rectorConfig + ->ruleWithConfiguration(RenameFunctionRector::class, [ + // hyperf/config + 'config' => 'Hyperf\Config\config', + + // hyperf/collection + 'collect' => 'Hyperf\Collection\collect', + 'data_fill' => 'Hyperf\Collection\data_fill', + 'data_get' => 'Hyperf\Collection\data_get', + 'data_set' => 'Hyperf\Collection\data_set', + 'head' => 'Hyperf\Collection\head', + 'last' => 'Hyperf\Collection\last', + + // hyperf/continue + 'co' => 'Hyperf\Coroutine\co', + 'defer' => 'Hyperf\Coroutine\defer', + 'go' => 'Hyperf\Coroutine\go', + 'parallel' => 'Hyperf\Coroutine\parallel', + 'run' => 'Hyperf\Coroutine\run', + 'wait' => 'Hyperf\Coroutine\wait', + + // hyperf/tappable + 'tap' => 'Hyperf\Tappable\tap', + + // hyperf/translation + '__' => 'Hyperf\Translation\__', + 'trans' => 'Hyperf\Translation\trans', + 'trans_choice' => 'Hyperf\Translation\trans_choice', + + // hyperf/utils + 'value' => 'Hyperf\Support\value', + 'env' => 'Hyperf\Support\env', + 'retry' => 'Hyperf\Support\retry', + 'with' => 'Hyperf\Support\with', + 'call' => 'Hyperf\Support\call', + 'class_basename' => 'Hyperf\Support\class_basename', + 'trait_uses_recursive' => 'Hyperf\Support\trait_uses_recursive', + 'class_uses_recursive' => 'Hyperf\Support\class_uses_recursive', + 'setter' => 'Hyperf\Support\setter', + 'getter' => 'Hyperf\Support\getter', + 'make' => 'Hyperf\Support\make', + 'swoole_hook_flags' => 'Hyperf\Support\swoole_hook_flags', + 'optional' => 'Hyperf\Support\optional', + ]); + + $rectorConfig->ruleWithConfiguration(RenameMethodRector::class, [ + new MethodCallRename('Hyperf\Dag\Dag', 'checkCircularDependences', 'checkCircularDependencies'), + // new MethodCallRename('Hyperf\DbConnection\Connection', 'isTransaction', 'transactionLevel'), + new MethodCallRename('Hyperf\Testing\Client', 'init', 'initRequest'), + new MethodCallRename('Hyperf\Cache\CacheListenerCollector', 'getListner', 'getListener'), + ]); +}; \ No newline at end of file diff --git a/src/Http/RequestService.php b/src/Http/RequestService.php index c42386d..5c30293 100644 --- a/src/Http/RequestService.php +++ b/src/Http/RequestService.php @@ -4,7 +4,8 @@ declare(strict_types=1); namespace Singularity\HDK\Core\Http; -use Ergebnis\Http\Method; +use Ergebnis\Http\Method\Rfc\Rfc5789; +use Ergebnis\Http\Method\Rfc\Rfc7231; use GuzzleHttp\Client; use GuzzleHttp\Exception\GuzzleException; use GuzzleHttp\Psr7\Request; @@ -49,7 +50,9 @@ final class RequestService extends AbstractSingleton /** * 定制 options + * * @param Options $options + * * @return $this */ public function setOptions(array $options): self @@ -59,10 +62,11 @@ final class RequestService extends AbstractSingleton } /** - * @param string $url + * @param string $url * @param array $params - * @param array $data - * @param Options $options + * @param array $data + * @param Options $options + * * @return ResponseInterface * @throws GuzzleException */ @@ -73,7 +77,7 @@ final class RequestService extends AbstractSingleton array $options = [] ): ResponseInterface { $request = new Request( - method: Method\Rfc\Rfc7231::GET, + method: Rfc7231::GET, uri: $url ); return $this->getClient($options)->send($request, ['query' => $params, 'json' => $data,]); @@ -81,7 +85,9 @@ final class RequestService extends AbstractSingleton /** * 工厂模式初始化请求 + * * @param Options $options + * * @return Client */ private function getClient(array $options): Client @@ -91,10 +97,11 @@ final class RequestService extends AbstractSingleton } /** - * @param string $url + * @param string $url * @param array $params - * @param array $data - * @param Options $options + * @param array $data + * @param Options $options + * * @return ResponseInterface * @throws GuzzleException */ @@ -106,7 +113,7 @@ final class RequestService extends AbstractSingleton ): ResponseInterface { $data = Json::encode($data); $request = new Request( - method: Method\Rfc\Rfc7231::POST, + method: Rfc7231::POST, uri: $url, headers: ['Content-Type' => 'application/json'], body: Utils::streamFor($data), @@ -115,10 +122,11 @@ final class RequestService extends AbstractSingleton } /** - * @param string $url - * @param array $data + * @param string $url + * @param array $data * @param array $params - * @param Options $options + * @param Options $options + * * @return ResponseInterface * @throws GuzzleException */ @@ -130,7 +138,7 @@ final class RequestService extends AbstractSingleton ): ResponseInterface { $data = Json::encode($data); $request = new Request( - method: Method\Rfc\Rfc7231::PUT, + method: Rfc7231::PUT, uri: $url, headers: ['Content-Type' => 'application/json'], body: Utils::streamFor($data) @@ -142,10 +150,11 @@ final class RequestService extends AbstractSingleton } /** - * @param string $url - * @param array $data + * @param string $url + * @param array $data * @param array $params - * @param Options $options + * @param Options $options + * * @return ResponseInterface * @throws GuzzleException */ @@ -157,7 +166,7 @@ final class RequestService extends AbstractSingleton ): ResponseInterface { $data = Json::encode($data); $request = new Request( - method: Method\Rfc\Rfc5789::PATCH, + method: Rfc5789::PATCH, uri: $url, headers: ['Content-Type' => 'application/json'], body: Utils::streamFor($data) @@ -169,10 +178,11 @@ final class RequestService extends AbstractSingleton } /** - * @param string $url - * @param array $data + * @param string $url + * @param array $data * @param array $params - * @param Options $options + * @param Options $options + * * @return ResponseInterface * @throws GuzzleException */ @@ -184,7 +194,7 @@ final class RequestService extends AbstractSingleton ): ResponseInterface { $data = Json::encode($data); $request = new Request( - method: Method\Rfc\Rfc7231::DELETE, + method: Rfc7231::DELETE, uri: $url, headers: ['Content-Type' => 'application/json'], body: Utils::streamFor($data) diff --git a/src/Service/HttpRequestService.php b/src/Service/HttpRequestService.php index c5748d9..53d97f7 100644 --- a/src/Service/HttpRequestService.php +++ b/src/Service/HttpRequestService.php @@ -4,7 +4,7 @@ declare(strict_types=1); namespace Singularity\HDK\Core\Service; -use Ergebnis\Http\Method; +use Ergebnis\Http\Method\Rfc\Rfc7231; use GuzzleHttp\Client; use GuzzleHttp\Exception\GuzzleException; use GuzzleHttp\Psr7\Request; @@ -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 @@ -31,15 +32,16 @@ class HttpRequestService private array $options = []; /** - * @param string $url + * @param string $url * @param array $params - * @param array $data + * @param array $data + * * @return ResponseInterface * @throws GuzzleException */ public function requestGet(string $url, array $params = [], array $data = []): ResponseInterface { - $request = new Request(Method\Rfc\Rfc7231::GET, $url); + $request = new Request(Rfc7231::GET, $url); return $this->getClient()->send($request, [ 'query' => $params, 'json' => $data, @@ -52,9 +54,10 @@ class HttpRequestService } /** - * @param string $url - * @param array $data + * @param string $url + * @param array $data * @param array $params + * * @return ResponseInterface * @throws GuzzleException */ @@ -74,9 +77,10 @@ class HttpRequestService /** - * @param string $url - * @param array $data + * @param string $url + * @param array $data * @param array $params + * * @return ResponseInterface * @throws GuzzleException */ @@ -91,7 +95,9 @@ class HttpRequestService /** * 定制 options + * * @param array $options + * * @return $this */ public function setOptions(array $options): HttpRequestService diff --git a/tests/Unit/RequestServiceTest.php b/tests/Unit/RequestServiceTest.php index dae4834..eb585ee 100644 --- a/tests/Unit/RequestServiceTest.php +++ b/tests/Unit/RequestServiceTest.php @@ -14,6 +14,8 @@ use Singularity\HDK\Core\Http\RequestService; use Singularity\HDK\Core\Http\RequestServiceFactory; use Teapot\StatusCode\RFC\RFC7231; +use function Hyperf\Support\make; + class RequestServiceTest { private RequestService $service; @@ -31,7 +33,7 @@ class RequestServiceTest test('Http 请求服务可以正常使用', function () { /** @var RequestServiceTest $class */ - $class = \Hyperf\Support\make(RequestServiceTest::class); + $class = make(RequestServiceTest::class); $response = $class->getInstance()->setOptions([ 'allow_redirects' => false, ])->requestGet('https://www.baidu.com'); diff --git a/tests/bootstrap.php b/tests/bootstrap.php index c3a61ad..5cbe900 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -3,9 +3,11 @@ declare(strict_types=1); use Hyperf\Context\ApplicationContext; +use Hyperf\Di\ClassLoader; use Hyperf\Di\Container; use Hyperf\Di\Definition\DefinitionSource; use Psr\Container\ContainerInterface; +use Swoole\Runtime; ini_set('display_errors', 'on'); ini_set('display_startup_errors', 'on'); @@ -17,10 +19,10 @@ 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); +Runtime::enableCoroutine(true); require BASE_PATH . '/vendor/autoload.php'; -Hyperf\Di\ClassLoader::init(); +ClassLoader::init(); $container = new Container(new DefinitionSource([])); if (!$container instanceof ContainerInterface) {