From fbf3dcd12f111593c12903c03eb667bc5952ad8c Mon Sep 17 00:00:00 2001 From: Deeka Wong Date: Wed, 20 Jul 2022 17:14:43 +0800 Subject: [PATCH 01/43] AddsThrow NoNodesAvailableException when cannot select any node (#6) --- .gitignore | 1 + src/Exception/NoNodesAvailableException.php | 18 ++++++++++++++++++ src/LoadBalancer/Random.php | 4 +++- src/LoadBalancer/RoundRobin.php | 4 ++-- src/Transporter/StreamSocketTransporter.php | 2 +- 5 files changed, 25 insertions(+), 4 deletions(-) create mode 100644 src/Exception/NoNodesAvailableException.php diff --git a/.gitignore b/.gitignore index 5aaa618..b85e3f7 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ composer.lock *.bak /phpunit.xml /.phpunit.result.cache +.idea \ No newline at end of file diff --git a/src/Exception/NoNodesAvailableException.php b/src/Exception/NoNodesAvailableException.php new file mode 100644 index 0000000..b3febb8 --- /dev/null +++ b/src/Exception/NoNodesAvailableException.php @@ -0,0 +1,18 @@ +nodes)) { - throw new \RuntimeException('Cannot select any node from load balancer.'); + throw new NoNodesAvailableException('Cannot select any node from load balancer.'); } $key = array_rand($this->nodes); diff --git a/src/LoadBalancer/RoundRobin.php b/src/LoadBalancer/RoundRobin.php index beb1d35..df6c6b9 100644 --- a/src/LoadBalancer/RoundRobin.php +++ b/src/LoadBalancer/RoundRobin.php @@ -11,7 +11,7 @@ */ namespace FriendsOfHyperf\Jet\LoadBalancer; -use RuntimeException; +use FriendsOfHyperf\Jet\Exception\NoNodesAvailableException; class RoundRobin extends AbstractLoadBalancer { @@ -28,7 +28,7 @@ public function select(): Node $count = count($this->nodes); if ($count <= 0) { - throw new RuntimeException('Nodes missing.'); + throw new NoNodesAvailableException('Nodes missing.'); } $item = $this->nodes[self::$current % $count]; diff --git a/src/Transporter/StreamSocketTransporter.php b/src/Transporter/StreamSocketTransporter.php index bd3c68e..b519f4d 100644 --- a/src/Transporter/StreamSocketTransporter.php +++ b/src/Transporter/StreamSocketTransporter.php @@ -27,7 +27,7 @@ class StreamSocketTransporter extends AbstractTransporter protected $client; /** - * @var float + * @var int */ protected $timeout; From 0aed51e19a28a529081263b312b8dd66356cb909 Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Thu, 8 Aug 2024 18:11:01 +0800 Subject: [PATCH 02/43] Create release.yaml --- .github/workflows/release.yaml | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 .github/workflows/release.yaml diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml new file mode 100644 index 0000000..84136de --- /dev/null +++ b/.github/workflows/release.yaml @@ -0,0 +1,28 @@ +name: Release + +on: + push: + tags: + - "v*.*.*" + +jobs: + build: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Get the version + id: get_version + if: startsWith(github.ref, 'refs/tags/') + run: echo ::set-output name=VERSION::${GITHUB_REF#refs/tags/} + + - name: Release + uses: softprops/action-gh-release@v2 + with: + token: ${{ secrets.GITHUB_TOKEN }} + tag_name: ${{ github.ref }} + name: Release ${{ steps.get_version.outputs.VERSION }} + draft: false + prerelease: false + generate_release_notes: true \ No newline at end of file From f48dbda2d568a5b38fe9d63ca1820057c0b66c2d Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Thu, 8 Aug 2024 18:15:49 +0800 Subject: [PATCH 03/43] Cs Fix --- classmap/GuzzleHttp/Client.php | 3 ++- src/Client.php | 4 ++-- src/ClientFactory.php | 7 +++--- src/Consul/Agent.php | 1 + src/Consul/Catalog.php | 3 ++- src/Consul/Client.php | 1 + src/Consul/Health.php | 1 + src/Consul/Response.php | 5 +++-- src/Contract/DataFormatterInterface.php | 1 + src/Contract/LoadBalancerInterface.php | 1 + src/Contract/PackerInterface.php | 1 + src/Contract/PathGeneratorInterface.php | 1 + src/Contract/RegistryInterface.php | 1 + src/Contract/TransporterInterface.php | 1 + src/DataFormatter/DataFormatter.php | 1 + src/Exception/ClientException.php | 5 ++--- src/Exception/ConnectionException.php | 5 ++--- src/Exception/ExceptionThrower.php | 9 ++++---- src/Exception/JetException.php | 7 ++---- src/Exception/NoNodesAvailableException.php | 7 ++---- src/Exception/RecvFailedException.php | 5 ++--- src/Exception/ServerException.php | 5 ++--- src/Facade.php | 15 +++++-------- src/LoadBalancer/AbstractLoadBalancer.php | 1 + src/LoadBalancer/Node.php | 1 + src/LoadBalancer/Random.php | 1 + src/LoadBalancer/RoundRobin.php | 1 + src/Metadata.php | 4 ++-- src/Packer/JsonEofPacker.php | 1 + src/Packer/JsonLengthPacker.php | 1 + src/PathGenerator/DotPathGenerator.php | 1 + src/PathGenerator/FullPathGenerator.php | 1 + src/PathGenerator/PathGenerator.php | 1 + src/Registry/ConsulRegistry.php | 4 ++-- src/RegistryManager.php | 8 +++---- src/ServiceManager.php | 5 ++--- src/Transporter/AbstractTransporter.php | 1 + src/Transporter/GuzzleHttpTransporter.php | 1 + src/Transporter/StreamSocketTransporter.php | 25 +++++++++------------ src/helpers.php | 16 ++++++------- tests/ClientTest.php | 1 + tests/RegistryTest.php | 1 + tests/TestCase.php | 1 + 43 files changed, 87 insertions(+), 79 deletions(-) diff --git a/classmap/GuzzleHttp/Client.php b/classmap/GuzzleHttp/Client.php index 79be25f..094008e 100644 --- a/classmap/GuzzleHttp/Client.php +++ b/classmap/GuzzleHttp/Client.php @@ -9,6 +9,7 @@ * @contact huangdijia@gmail.com * @license https://github.com/friendsofhyperf/jet/blob/main/LICENSE */ + namespace GuzzleHttp; use GuzzleHttp\Cookie\CookieJar; @@ -63,7 +64,7 @@ class Client implements ClientInterface, \Psr\Http\Client\ClientInterface * * @param array $config client configuration settings * - * @see \GuzzleHttp\RequestOptions for a list of available request options. + * @see RequestOptions for a list of available request options. */ public function __construct(array $config = []) { diff --git a/src/Client.php b/src/Client.php index 832c244..b43f8c2 100644 --- a/src/Client.php +++ b/src/Client.php @@ -9,11 +9,11 @@ * @contact huangdijia@gmail.com * @license https://github.com/friendsofhyperf/jet/blob/main/LICENSE */ + namespace FriendsOfHyperf\Jet; use FriendsOfHyperf\Jet\Exception\RecvFailedException; use FriendsOfHyperf\Jet\Exception\ServerException; -use Throwable; class Client { @@ -35,8 +35,8 @@ public function __construct(Metadata $metadata) /** * @param string $name * @param array $arguments - * @throws Throwable * @return mixed + * @throws \Throwable */ public function __call($name, $arguments) { diff --git a/src/ClientFactory.php b/src/ClientFactory.php index fb43fca..8792bef 100644 --- a/src/ClientFactory.php +++ b/src/ClientFactory.php @@ -9,15 +9,14 @@ * @contact huangdijia@gmail.com * @license https://github.com/friendsofhyperf/jet/blob/main/LICENSE */ + namespace FriendsOfHyperf\Jet; -use Exception; use FriendsOfHyperf\Jet\Contract\DataFormatterInterface; use FriendsOfHyperf\Jet\Contract\PackerInterface; use FriendsOfHyperf\Jet\Contract\PathGeneratorInterface; use FriendsOfHyperf\Jet\Contract\TransporterInterface; use GuzzleHttp\ClientInterface; -use InvalidArgumentException; class ClientFactory { @@ -52,8 +51,8 @@ public static function getUserAgent(): string /** * Create a client. * @param null|int|string|TransporterInterface $transporter transporter, protocol, timeout or null - * @throws InvalidArgumentException - * @throws Exception + * @throws \InvalidArgumentException + * @throws \Exception */ public static function create(string $service, $transporter = null, ?PackerInterface $packer = null, ?DataFormatterInterface $dataFormatter = null, ?PathGeneratorInterface $pathGenerator = null, ?int $tries = null): Client { diff --git a/src/Consul/Agent.php b/src/Consul/Agent.php index 88d4196..cff0325 100644 --- a/src/Consul/Agent.php +++ b/src/Consul/Agent.php @@ -9,6 +9,7 @@ * @contact huangdijia@gmail.com * @license https://github.com/friendsofhyperf/jet/blob/main/LICENSE */ + namespace FriendsOfHyperf\Jet\Consul; class Agent extends Client diff --git a/src/Consul/Catalog.php b/src/Consul/Catalog.php index 4f73393..27a08d8 100644 --- a/src/Consul/Catalog.php +++ b/src/Consul/Catalog.php @@ -9,6 +9,7 @@ * @contact huangdijia@gmail.com * @license https://github.com/friendsofhyperf/jet/blob/main/LICENSE */ + namespace FriendsOfHyperf\Jet\Consul; use FriendsOfHyperf\Jet\Exception\ClientException; @@ -18,10 +19,10 @@ class Catalog extends Client { /** + * @return Response * @throws ServerException * @throws ClientException * @throws GuzzleException - * @return Response */ public function services(array $options = []) { diff --git a/src/Consul/Client.php b/src/Consul/Client.php index 07cfc40..e051247 100644 --- a/src/Consul/Client.php +++ b/src/Consul/Client.php @@ -9,6 +9,7 @@ * @contact huangdijia@gmail.com * @license https://github.com/friendsofhyperf/jet/blob/main/LICENSE */ + namespace FriendsOfHyperf\Jet\Consul; use FriendsOfHyperf\Jet\Exception\ClientException; diff --git a/src/Consul/Health.php b/src/Consul/Health.php index 44c649b..2950f72 100644 --- a/src/Consul/Health.php +++ b/src/Consul/Health.php @@ -9,6 +9,7 @@ * @contact huangdijia@gmail.com * @license https://github.com/friendsofhyperf/jet/blob/main/LICENSE */ + namespace FriendsOfHyperf\Jet\Consul; class Health extends Client diff --git a/src/Consul/Response.php b/src/Consul/Response.php index 89b145a..38999e5 100644 --- a/src/Consul/Response.php +++ b/src/Consul/Response.php @@ -9,6 +9,7 @@ * @contact huangdijia@gmail.com * @license https://github.com/friendsofhyperf/jet/blob/main/LICENSE */ + namespace FriendsOfHyperf\Jet\Consul; use FriendsOfHyperf\Jet\Exception\ServerException; @@ -38,10 +39,10 @@ public function __call($name, $arguments) /** * @param null|mixed $default - * @throws ServerException * @return mixed + * @throws ServerException */ - public function json(string $key = null, $default = null) + public function json(?string $key = null, $default = null) { if (is_null($this->decoded)) { if ($this->response->getHeaderLine('Content-Type') !== 'application/json') { diff --git a/src/Contract/DataFormatterInterface.php b/src/Contract/DataFormatterInterface.php index 61c1df7..d765626 100644 --- a/src/Contract/DataFormatterInterface.php +++ b/src/Contract/DataFormatterInterface.php @@ -9,6 +9,7 @@ * @contact huangdijia@gmail.com * @license https://github.com/friendsofhyperf/jet/blob/main/LICENSE */ + namespace FriendsOfHyperf\Jet\Contract; interface DataFormatterInterface diff --git a/src/Contract/LoadBalancerInterface.php b/src/Contract/LoadBalancerInterface.php index 6137920..11cca3b 100644 --- a/src/Contract/LoadBalancerInterface.php +++ b/src/Contract/LoadBalancerInterface.php @@ -9,6 +9,7 @@ * @contact huangdijia@gmail.com * @license https://github.com/friendsofhyperf/jet/blob/main/LICENSE */ + namespace FriendsOfHyperf\Jet\Contract; use FriendsOfHyperf\Jet\LoadBalancer\Node; diff --git a/src/Contract/PackerInterface.php b/src/Contract/PackerInterface.php index 7fe52a3..6ea8829 100644 --- a/src/Contract/PackerInterface.php +++ b/src/Contract/PackerInterface.php @@ -9,6 +9,7 @@ * @contact huangdijia@gmail.com * @license https://github.com/friendsofhyperf/jet/blob/main/LICENSE */ + namespace FriendsOfHyperf\Jet\Contract; interface PackerInterface diff --git a/src/Contract/PathGeneratorInterface.php b/src/Contract/PathGeneratorInterface.php index 96cefb1..89bb105 100644 --- a/src/Contract/PathGeneratorInterface.php +++ b/src/Contract/PathGeneratorInterface.php @@ -9,6 +9,7 @@ * @contact huangdijia@gmail.com * @license https://github.com/friendsofhyperf/jet/blob/main/LICENSE */ + namespace FriendsOfHyperf\Jet\Contract; interface PathGeneratorInterface diff --git a/src/Contract/RegistryInterface.php b/src/Contract/RegistryInterface.php index 65a2ca4..a7ffdf9 100644 --- a/src/Contract/RegistryInterface.php +++ b/src/Contract/RegistryInterface.php @@ -9,6 +9,7 @@ * @contact huangdijia@gmail.com * @license https://github.com/friendsofhyperf/jet/blob/main/LICENSE */ + namespace FriendsOfHyperf\Jet\Contract; use FriendsOfHyperf\Jet\LoadBalancer\Node; diff --git a/src/Contract/TransporterInterface.php b/src/Contract/TransporterInterface.php index 728c3e7..a2410ca 100644 --- a/src/Contract/TransporterInterface.php +++ b/src/Contract/TransporterInterface.php @@ -9,6 +9,7 @@ * @contact huangdijia@gmail.com * @license https://github.com/friendsofhyperf/jet/blob/main/LICENSE */ + namespace FriendsOfHyperf\Jet\Contract; interface TransporterInterface diff --git a/src/DataFormatter/DataFormatter.php b/src/DataFormatter/DataFormatter.php index 402bccd..e2e9fe9 100644 --- a/src/DataFormatter/DataFormatter.php +++ b/src/DataFormatter/DataFormatter.php @@ -9,6 +9,7 @@ * @contact huangdijia@gmail.com * @license https://github.com/friendsofhyperf/jet/blob/main/LICENSE */ + namespace FriendsOfHyperf\Jet\DataFormatter; use FriendsOfHyperf\Jet\Contract\DataFormatterInterface; diff --git a/src/Exception/ClientException.php b/src/Exception/ClientException.php index 9c5ef4d..eac679a 100644 --- a/src/Exception/ClientException.php +++ b/src/Exception/ClientException.php @@ -9,8 +9,7 @@ * @contact huangdijia@gmail.com * @license https://github.com/friendsofhyperf/jet/blob/main/LICENSE */ + namespace FriendsOfHyperf\Jet\Exception; -class ClientException extends JetException -{ -} +class ClientException extends JetException {} diff --git a/src/Exception/ConnectionException.php b/src/Exception/ConnectionException.php index 79eb5ae..f6f170c 100644 --- a/src/Exception/ConnectionException.php +++ b/src/Exception/ConnectionException.php @@ -9,8 +9,7 @@ * @contact huangdijia@gmail.com * @license https://github.com/friendsofhyperf/jet/blob/main/LICENSE */ + namespace FriendsOfHyperf\Jet\Exception; -class ConnectionException extends JetException -{ -} +class ConnectionException extends JetException {} diff --git a/src/Exception/ExceptionThrower.php b/src/Exception/ExceptionThrower.php index 1e81b9a..b4af9c3 100644 --- a/src/Exception/ExceptionThrower.php +++ b/src/Exception/ExceptionThrower.php @@ -9,23 +9,22 @@ * @contact huangdijia@gmail.com * @license https://github.com/friendsofhyperf/jet/blob/main/LICENSE */ -namespace FriendsOfHyperf\Jet\Exception; -use Throwable; +namespace FriendsOfHyperf\Jet\Exception; final class ExceptionThrower { /** - * @var Throwable + * @var \Throwable */ private $throwable; - public function __construct(Throwable $throwable) + public function __construct(\Throwable $throwable) { $this->throwable = $throwable; } - public function getThrowable(): Throwable + public function getThrowable(): \Throwable { return $this->throwable; } diff --git a/src/Exception/JetException.php b/src/Exception/JetException.php index 94f6b8f..20bb1c9 100644 --- a/src/Exception/JetException.php +++ b/src/Exception/JetException.php @@ -9,10 +9,7 @@ * @contact huangdijia@gmail.com * @license https://github.com/friendsofhyperf/jet/blob/main/LICENSE */ -namespace FriendsOfHyperf\Jet\Exception; -use RuntimeException; +namespace FriendsOfHyperf\Jet\Exception; -class JetException extends RuntimeException -{ -} +class JetException extends \RuntimeException {} diff --git a/src/Exception/NoNodesAvailableException.php b/src/Exception/NoNodesAvailableException.php index b3febb8..7129447 100644 --- a/src/Exception/NoNodesAvailableException.php +++ b/src/Exception/NoNodesAvailableException.php @@ -9,10 +9,7 @@ * @contact huangdijia@gmail.com * @license https://github.com/friendsofhyperf/jet/blob/main/LICENSE */ -namespace FriendsOfHyperf\Jet\Exception; -use RuntimeException; +namespace FriendsOfHyperf\Jet\Exception; -class NoNodesAvailableException extends RuntimeException -{ -} +class NoNodesAvailableException extends \RuntimeException {} diff --git a/src/Exception/RecvFailedException.php b/src/Exception/RecvFailedException.php index d9387f4..17283b2 100644 --- a/src/Exception/RecvFailedException.php +++ b/src/Exception/RecvFailedException.php @@ -9,8 +9,7 @@ * @contact huangdijia@gmail.com * @license https://github.com/friendsofhyperf/jet/blob/main/LICENSE */ + namespace FriendsOfHyperf\Jet\Exception; -class RecvFailedException extends JetException -{ -} +class RecvFailedException extends JetException {} diff --git a/src/Exception/ServerException.php b/src/Exception/ServerException.php index 31cf794..fdecd7e 100644 --- a/src/Exception/ServerException.php +++ b/src/Exception/ServerException.php @@ -9,9 +9,8 @@ * @contact huangdijia@gmail.com * @license https://github.com/friendsofhyperf/jet/blob/main/LICENSE */ -namespace FriendsOfHyperf\Jet\Exception; -use Throwable; +namespace FriendsOfHyperf\Jet\Exception; class ServerException extends JetException { @@ -20,7 +19,7 @@ class ServerException extends JetException */ protected $error; - public function __construct(array $error = [], Throwable $previous = null) + public function __construct(array $error = [], ?\Throwable $previous = null) { $code = $error['code'] ?? 0; $message = $error['message'] ?? 'Server Error'; diff --git a/src/Facade.php b/src/Facade.php index 52a8558..3eeb6b9 100644 --- a/src/Facade.php +++ b/src/Facade.php @@ -9,11 +9,8 @@ * @contact huangdijia@gmail.com * @license https://github.com/friendsofhyperf/jet/blob/main/LICENSE */ -namespace FriendsOfHyperf\Jet; -use Exception; -use InvalidArgumentException; -use RuntimeException; +namespace FriendsOfHyperf\Jet; abstract class Facade { @@ -34,8 +31,8 @@ public static function __callStatic($name, $arguments) } /** - * @throws RuntimeException * @return Client + * @throws \RuntimeException */ protected static function getFacadeRoot() { @@ -44,9 +41,9 @@ protected static function getFacadeRoot() /** * @param mixed $name - * @throws InvalidArgumentException - * @throws Exception * @return mixed + * @throws \InvalidArgumentException + * @throws \Exception */ protected static function resolveFacadeInstance($name) { @@ -62,11 +59,11 @@ protected static function resolveFacadeInstance($name) } /** - * @throws RuntimeException * @return Client|string + * @throws \RuntimeException */ protected static function getFacadeAccessor() { - throw new RuntimeException('Facade does not implement getFacadeAccessor method.'); + throw new \RuntimeException('Facade does not implement getFacadeAccessor method.'); } } diff --git a/src/LoadBalancer/AbstractLoadBalancer.php b/src/LoadBalancer/AbstractLoadBalancer.php index 9d1a752..2809db4 100644 --- a/src/LoadBalancer/AbstractLoadBalancer.php +++ b/src/LoadBalancer/AbstractLoadBalancer.php @@ -9,6 +9,7 @@ * @contact huangdijia@gmail.com * @license https://github.com/friendsofhyperf/jet/blob/main/LICENSE */ + namespace FriendsOfHyperf\Jet\LoadBalancer; use FriendsOfHyperf\Jet\Contract\LoadBalancerInterface; diff --git a/src/LoadBalancer/Node.php b/src/LoadBalancer/Node.php index 06119fe..b6a400a 100644 --- a/src/LoadBalancer/Node.php +++ b/src/LoadBalancer/Node.php @@ -9,6 +9,7 @@ * @contact huangdijia@gmail.com * @license https://github.com/friendsofhyperf/jet/blob/main/LICENSE */ + namespace FriendsOfHyperf\Jet\LoadBalancer; class Node diff --git a/src/LoadBalancer/Random.php b/src/LoadBalancer/Random.php index bd12515..c9e19de 100644 --- a/src/LoadBalancer/Random.php +++ b/src/LoadBalancer/Random.php @@ -9,6 +9,7 @@ * @contact huangdijia@gmail.com * @license https://github.com/friendsofhyperf/jet/blob/main/LICENSE */ + namespace FriendsOfHyperf\Jet\LoadBalancer; use FriendsOfHyperf\Jet\Exception\NoNodesAvailableException; diff --git a/src/LoadBalancer/RoundRobin.php b/src/LoadBalancer/RoundRobin.php index df6c6b9..ce0cc96 100644 --- a/src/LoadBalancer/RoundRobin.php +++ b/src/LoadBalancer/RoundRobin.php @@ -9,6 +9,7 @@ * @contact huangdijia@gmail.com * @license https://github.com/friendsofhyperf/jet/blob/main/LICENSE */ + namespace FriendsOfHyperf\Jet\LoadBalancer; use FriendsOfHyperf\Jet\Exception\NoNodesAvailableException; diff --git a/src/Metadata.php b/src/Metadata.php index 62baba6..4932e9a 100644 --- a/src/Metadata.php +++ b/src/Metadata.php @@ -9,6 +9,7 @@ * @contact huangdijia@gmail.com * @license https://github.com/friendsofhyperf/jet/blob/main/LICENSE */ + namespace FriendsOfHyperf\Jet; use FriendsOfHyperf\Jet\Contract\DataFormatterInterface; @@ -19,7 +20,6 @@ use FriendsOfHyperf\Jet\DataFormatter\DataFormatter; use FriendsOfHyperf\Jet\Packer\JsonEofPacker; use FriendsOfHyperf\Jet\PathGenerator\PathGenerator; -use RuntimeException; class Metadata { @@ -121,7 +121,7 @@ public function getTransporter() return $this->registry->getTransporter($this->name, $this->protocol, $this->timeout); } - throw new RuntimeException('Transporter not registered yet.'); + throw new \RuntimeException('Transporter not registered yet.'); } /** diff --git a/src/Packer/JsonEofPacker.php b/src/Packer/JsonEofPacker.php index 9d7d05c..ca92c5a 100644 --- a/src/Packer/JsonEofPacker.php +++ b/src/Packer/JsonEofPacker.php @@ -9,6 +9,7 @@ * @contact huangdijia@gmail.com * @license https://github.com/friendsofhyperf/jet/blob/main/LICENSE */ + namespace FriendsOfHyperf\Jet\Packer; use FriendsOfHyperf\Jet\Contract\PackerInterface; diff --git a/src/Packer/JsonLengthPacker.php b/src/Packer/JsonLengthPacker.php index 5ef9af0..d6703e4 100644 --- a/src/Packer/JsonLengthPacker.php +++ b/src/Packer/JsonLengthPacker.php @@ -9,6 +9,7 @@ * @contact huangdijia@gmail.com * @license https://github.com/friendsofhyperf/jet/blob/main/LICENSE */ + namespace FriendsOfHyperf\Jet\Packer; use FriendsOfHyperf\Jet\Contract\PackerInterface; diff --git a/src/PathGenerator/DotPathGenerator.php b/src/PathGenerator/DotPathGenerator.php index ad7111c..4a47a97 100644 --- a/src/PathGenerator/DotPathGenerator.php +++ b/src/PathGenerator/DotPathGenerator.php @@ -9,6 +9,7 @@ * @contact huangdijia@gmail.com * @license https://github.com/friendsofhyperf/jet/blob/main/LICENSE */ + namespace FriendsOfHyperf\Jet\PathGenerator; use FriendsOfHyperf\Jet\Contract\PathGeneratorInterface; diff --git a/src/PathGenerator/FullPathGenerator.php b/src/PathGenerator/FullPathGenerator.php index 0bc29a2..f3eb28a 100644 --- a/src/PathGenerator/FullPathGenerator.php +++ b/src/PathGenerator/FullPathGenerator.php @@ -9,6 +9,7 @@ * @contact huangdijia@gmail.com * @license https://github.com/friendsofhyperf/jet/blob/main/LICENSE */ + namespace FriendsOfHyperf\Jet\PathGenerator; use FriendsOfHyperf\Jet\Contract\PathGeneratorInterface; diff --git a/src/PathGenerator/PathGenerator.php b/src/PathGenerator/PathGenerator.php index 8ed5621..4674071 100644 --- a/src/PathGenerator/PathGenerator.php +++ b/src/PathGenerator/PathGenerator.php @@ -9,6 +9,7 @@ * @contact huangdijia@gmail.com * @license https://github.com/friendsofhyperf/jet/blob/main/LICENSE */ + namespace FriendsOfHyperf\Jet\PathGenerator; use FriendsOfHyperf\Jet\Contract\PathGeneratorInterface; diff --git a/src/Registry/ConsulRegistry.php b/src/Registry/ConsulRegistry.php index 666a40d..1a4c5f1 100644 --- a/src/Registry/ConsulRegistry.php +++ b/src/Registry/ConsulRegistry.php @@ -9,6 +9,7 @@ * @contact huangdijia@gmail.com * @license https://github.com/friendsofhyperf/jet/blob/main/LICENSE */ + namespace FriendsOfHyperf\Jet\Registry; use FriendsOfHyperf\Jet\Consul\Catalog; @@ -20,7 +21,6 @@ use FriendsOfHyperf\Jet\Transporter\GuzzleHttpTransporter; use FriendsOfHyperf\Jet\Transporter\StreamSocketTransporter; use GuzzleHttp\Client; -use RuntimeException; class ConsulRegistry implements RegistryInterface { @@ -146,7 +146,7 @@ public function getTransporter(string $service, ?string $protocol = null, int $t $nodes = $this->getServiceNodes($service, $protocol); if (count($nodes) <= 0) { - throw new RuntimeException('Service nodes not found!'); + throw new \RuntimeException('Service nodes not found!'); } $serviceBalancer = new RoundRobin($nodes); diff --git a/src/RegistryManager.php b/src/RegistryManager.php index d22e9ad..6ad101c 100644 --- a/src/RegistryManager.php +++ b/src/RegistryManager.php @@ -9,11 +9,11 @@ * @contact huangdijia@gmail.com * @license https://github.com/friendsofhyperf/jet/blob/main/LICENSE */ + namespace FriendsOfHyperf\Jet; use FriendsOfHyperf\Jet\Contract\RegistryInterface; use FriendsOfHyperf\Jet\Exception\JetException; -use InvalidArgumentException; class RegistryManager { @@ -36,13 +36,13 @@ public static function get($name = self::DEFAULT) /** * @param string $name * @param RegistryInterface $registry - * @throws InvalidArgumentException + * @throws \InvalidArgumentException * @throws JetException */ public static function register($name, $registry, bool $force = false) { - if (! ($registry instanceof RegistryInterface)) { - throw new InvalidArgumentException('$registry must be instanceof RegistryInterface'); + if (! $registry instanceof RegistryInterface) { + throw new \InvalidArgumentException('$registry must be instanceof RegistryInterface'); } if (! $force && self::isRegistered($name)) { diff --git a/src/ServiceManager.php b/src/ServiceManager.php index 9bc4057..cba5c79 100644 --- a/src/ServiceManager.php +++ b/src/ServiceManager.php @@ -9,9 +9,8 @@ * @contact huangdijia@gmail.com * @license https://github.com/friendsofhyperf/jet/blob/main/LICENSE */ -namespace FriendsOfHyperf\Jet; -use InvalidArgumentException; +namespace FriendsOfHyperf\Jet; class ServiceManager { @@ -37,7 +36,7 @@ public static function isRegistered(string $service) } /** - * @throws InvalidArgumentException + * @throws \InvalidArgumentException */ public static function register(string $service, Metadata $metadata) { diff --git a/src/Transporter/AbstractTransporter.php b/src/Transporter/AbstractTransporter.php index b339e8b..36f466a 100644 --- a/src/Transporter/AbstractTransporter.php +++ b/src/Transporter/AbstractTransporter.php @@ -9,6 +9,7 @@ * @contact huangdijia@gmail.com * @license https://github.com/friendsofhyperf/jet/blob/main/LICENSE */ + namespace FriendsOfHyperf\Jet\Transporter; use FriendsOfHyperf\Jet\Contract\LoadBalancerInterface; diff --git a/src/Transporter/GuzzleHttpTransporter.php b/src/Transporter/GuzzleHttpTransporter.php index 268650d..14bf63e 100644 --- a/src/Transporter/GuzzleHttpTransporter.php +++ b/src/Transporter/GuzzleHttpTransporter.php @@ -9,6 +9,7 @@ * @contact huangdijia@gmail.com * @license https://github.com/friendsofhyperf/jet/blob/main/LICENSE */ + namespace FriendsOfHyperf\Jet\Transporter; use FriendsOfHyperf\Jet\ClientFactory; diff --git a/src/Transporter/StreamSocketTransporter.php b/src/Transporter/StreamSocketTransporter.php index b519f4d..04d0a83 100644 --- a/src/Transporter/StreamSocketTransporter.php +++ b/src/Transporter/StreamSocketTransporter.php @@ -9,15 +9,12 @@ * @contact huangdijia@gmail.com * @license https://github.com/friendsofhyperf/jet/blob/main/LICENSE */ + namespace FriendsOfHyperf\Jet\Transporter; -use Exception; use FriendsOfHyperf\Jet\Exception\ConnectionException; use FriendsOfHyperf\Jet\Exception\ExceptionThrower; use FriendsOfHyperf\Jet\Exception\RecvFailedException; -use InvalidArgumentException; -use RuntimeException; -use Throwable; class StreamSocketTransporter extends AbstractTransporter { @@ -42,8 +39,8 @@ public function __destruct() } /** - * @throws InvalidArgumentException - * @throws RuntimeException + * @throws \InvalidArgumentException + * @throws \RuntimeException */ public function send(string $data) { @@ -52,14 +49,14 @@ public function send(string $data) } /** - * @throws Throwable * @return string + * @throws \Throwable */ public function recv() { try { return $this->receive(); - } catch (Throwable $e) { + } catch (\Throwable $e) { $this->close(); throw $e; } @@ -110,9 +107,9 @@ public function receive() } /** - * @throws InvalidArgumentException - * @throws Exception - * @return (string|int)[] + * @return (int|string)[] + * @throws \InvalidArgumentException + * @throws \Exception */ protected function getTarget() { @@ -123,15 +120,15 @@ protected function getTarget() } if (! $node->host || ! $node->port) { - throw new InvalidArgumentException(sprintf('Invalid host %s or port %s.', $node->host, $node->port)); + throw new \InvalidArgumentException(sprintf('Invalid host %s or port %s.', $node->host, $node->port)); } return [$node->host, $node->port]; } /** - * @throws InvalidArgumentException - * @throws Exception + * @throws \InvalidArgumentException + * @throws \Exception */ protected function connect() { diff --git a/src/helpers.php b/src/helpers.php index 367fb96..24d1c33 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -12,10 +12,10 @@ if (! function_exists('retry')) { /** * Retry. - * @throws Throwable * @return mixed + * @throws Throwable */ - function retry(int $times, callable $callback, int $sleep = 0, callable $when = null) + function retry(int $times, callable $callback, int $sleep = 0, ?callable $when = null) { $attempts = 0; @@ -42,15 +42,15 @@ function retry(int $times, callable $callback, int $sleep = 0, callable $when = if (! function_exists('throw_if')) { /** * @param mixed $condition - * @param string|\Throwable $exception + * @param string|Throwable $exception + * @return mixed * @throws InvalidArgumentException * @throws Exception - * @return mixed */ function throw_if($condition, $exception, ...$parameters) { if ($condition) { - throw (is_string($exception) ? new $exception(...$parameters) : $exception); + throw is_string($exception) ? new $exception(...$parameters) : $exception; } return $condition; @@ -93,7 +93,7 @@ public function __call($method, $parameters) * @param mixed $value * @return mixed */ - function with($value, callable $callback = null) + function with($value, ?callable $callback = null) { return is_null($callback) ? $value : $callback($value); } @@ -186,7 +186,7 @@ function value($value) /** * Get an item from an array using "dot" notation. * - * @param array|\ArrayAccess $array + * @param array|ArrayAccess $array * @param null|int|string $key * @param mixed $default */ @@ -220,7 +220,7 @@ function array_get($array, $key = null, $default = null) /** * Check if an item or items exist in an array using "dot" notation. * - * @param array|\ArrayAccess $array + * @param array|ArrayAccess $array * @param null|array|string $keys */ function array_has($array, $keys) diff --git a/tests/ClientTest.php b/tests/ClientTest.php index 25c3251..d41d378 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -9,6 +9,7 @@ * @contact huangdijia@gmail.com * @license https://github.com/friendsofhyperf/jet/blob/main/LICENSE */ + namespace FriendsOfHyperf\Jet\Tests; use FriendsOfHyperf\Jet\ClientFactory; diff --git a/tests/RegistryTest.php b/tests/RegistryTest.php index 9862c5e..c7a490b 100644 --- a/tests/RegistryTest.php +++ b/tests/RegistryTest.php @@ -9,6 +9,7 @@ * @contact huangdijia@gmail.com * @license https://github.com/friendsofhyperf/jet/blob/main/LICENSE */ + namespace FriendsOfHyperf\Jet\Tests; use FriendsOfHyperf\Jet\Contract\RegistryInterface; diff --git a/tests/TestCase.php b/tests/TestCase.php index a7aaf55..4c15b38 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -9,6 +9,7 @@ * @contact huangdijia@gmail.com * @license https://github.com/friendsofhyperf/jet/blob/main/LICENSE */ + namespace FriendsOfHyperf\Jet\Tests; use FriendsOfHyperf\Jet\Registry\ConsulRegistry; From ee8a400b0e9637ba53a5aeebb394c2b741203ee4 Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Thu, 8 Aug 2024 18:17:41 +0800 Subject: [PATCH 04/43] chore: Comment out unnecessary setup services and test cases --- .github/workflows/tests.yaml | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index 2a984dc..5692bf5 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/tests.yaml @@ -25,13 +25,13 @@ jobs: - name: Run Analyse run: | composer analyse src - - name: Setup Services - run: | - docker run -d --name jsonrpc -p 9501:9501 -p 9502:9502 -p 9503:9503 -p 9504:9504 limingxinleo/hyperf-jsonrpc-demo:latest - docker run -d --name dev-consul -e CONSUL_BIND_INTERFACE=eth0 --network host consul - sleep 10 - php ./tests/register.php - - name: Run Test Cases - run: | - cp phpunit.xml.dist phpunit.xml - composer test \ No newline at end of file + # - name: Setup Services + # run: | + # docker run -d --name jsonrpc -p 9501:9501 -p 9502:9502 -p 9503:9503 -p 9504:9504 limingxinleo/hyperf-jsonrpc-demo:latest + # docker run -d --name dev-consul -e CONSUL_BIND_INTERFACE=eth0 --network host consul + # sleep 10 + # php ./tests/register.php + # - name: Run Test Cases + # run: | + # cp phpunit.xml.dist phpunit.xml + # composer test \ No newline at end of file From 3e4ca1340ef70a2c4009b01402dde6cbc4eb8e9c Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Thu, 8 Aug 2024 18:33:14 +0800 Subject: [PATCH 05/43] chore: Update badges in README.md to use shields.io for consistency --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 72ee5a4..fd95fbd 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,8 @@ # Jet [![Latest Test](https://github.com/friendsofhyperf/jet/workflows/tests/badge.svg)](https://github.com/friendsofhyperf/jet/actions) -[![Latest Stable Version](https://poser.pugx.org/friendsofhyperf/jet/version.png)](https://packagist.org/packages/friendsofhyperf/jet) -[![Total Downloads](https://poser.pugx.org/friendsofhyperf/jet/d/total.png)](https://packagist.org/packages/friendsofhyperf/jet) +[![Latest Stable Version](https://img.shields.io/packagist/v/friendsofhyperf/jet)](https://packagist.org/packages/friendsofhyperf/jet) +[![Total Downloads](https://img.shields.io/packagist/dt/friendsofhyperf/jet)](https://packagist.org/packages/friendsofhyperf/jet) [![GitHub license](https://img.shields.io/github/license/friendsofhyperf/jet)](https://github.com/friendsofhyperf/jet) Another jet client for Hyperf From a01b6df6d861974599c7aaf84bcb988dd0f2dc3e Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Fri, 9 Aug 2024 10:20:11 +0800 Subject: [PATCH 06/43] chore: Update tests.yaml to setup Consul and services for testing --- .github/workflows/tests.yaml | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index 5692bf5..f1f0841 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/tests.yaml @@ -25,13 +25,14 @@ jobs: - name: Run Analyse run: | composer analyse src - # - name: Setup Services - # run: | - # docker run -d --name jsonrpc -p 9501:9501 -p 9502:9502 -p 9503:9503 -p 9504:9504 limingxinleo/hyperf-jsonrpc-demo:latest - # docker run -d --name dev-consul -e CONSUL_BIND_INTERFACE=eth0 --network host consul - # sleep 10 - # php ./tests/register.php - # - name: Run Test Cases - # run: | - # cp phpunit.xml.dist phpunit.xml - # composer test \ No newline at end of file + - name: Setup Consul + run: docker run -d --name=dev-consul -e CONSUL_BIND_INTERFACE=eth0 --net=host consul:1.15.4 + - name: Setup Services + run: | + docker run -d --name jsonrpc -p 9501:9501 -p 9502:9502 -p 9503:9503 -p 9504:9504 limingxinleo/hyperf-jsonrpc-demo:latest + sleep 10 + php ./tests/register.php + - name: Run Test Cases + run: | + cp phpunit.xml.dist phpunit.xml + composer test \ No newline at end of file From 4185b78ee129c2e9c9b8094efd53332b415f78fe Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Fri, 9 Aug 2024 10:21:02 +0800 Subject: [PATCH 07/43] chore: Increase max-parallel value in tests.yaml to 20 --- .github/workflows/tests.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index f1f0841..f6ff717 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/tests.yaml @@ -10,7 +10,7 @@ jobs: matrix: os: [ubuntu-latest] php-version: ['7.1', '7.2', '7.3', '7.4', '8.0'] - max-parallel: 3 + max-parallel: 20 steps: - name: Checkout uses: actions/checkout@v2 From f5785e6700e723eb376935302bc6bd64197ffdb0 Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Sun, 11 Aug 2024 10:13:21 +0800 Subject: [PATCH 08/43] chore: Update README.md and tests.yaml for consistency and setup improvements --- .vscode/cspell.json | 5 + README.md | 74 +++-- classmap/GuzzleHttp/Client.php | 490 --------------------------------- 3 files changed, 57 insertions(+), 512 deletions(-) create mode 100644 .vscode/cspell.json delete mode 100644 classmap/GuzzleHttp/Client.php diff --git a/.vscode/cspell.json b/.vscode/cspell.json new file mode 100644 index 0000000..e668a63 --- /dev/null +++ b/.vscode/cspell.json @@ -0,0 +1,5 @@ +{ + "words": [ + "Laravel" + ] +} diff --git a/README.md b/README.md index fd95fbd..cb1792e 100644 --- a/README.md +++ b/README.md @@ -11,15 +11,15 @@ Another jet client for Hyperf ### Composer -~~~php +```php composer require "friendsofhyperf/jet:^2.0" -~~~ +``` -## Quickstart +## QuickStart ### Register with metadata -~~~php +```php use FriendsOfHyperf\Jet\Metadata; use FriendsOfHyperf\Jet\ServiceManager; use FriendsOfHyperf\Jet\Registry\ConsulRegistry; @@ -30,16 +30,16 @@ $metadata->setTransporter(new GuzzleHttpTransporter('127.0.0.1', 9502)); $metadata->setRegistry(new ConsulRegistry(['uri' => 'http://127.0.0.1:8500'])); ServiceManager::register('CalculatorService', $metadata); -~~~ +``` ### Register default registry -~~~php +```php use FriendsOfHyperf\Jet\RegistryManager; use FriendsOfHyperf\Jet\Registry\ConsulRegistry; RegistryManager::register(RegistryManager::DEFAULT, new ConsulRegistry(['uri' => $uri, 'timeout' => 1])); -~~~ +``` > In Laravel project, Add to `boot()` in `App/Providers/AppServiceProvider.php` @@ -47,16 +47,16 @@ RegistryManager::register(RegistryManager::DEFAULT, new ConsulRegistry(['uri' => ### Call by ClientFactory -~~~php +```php use FriendsOfHyperf\Jet\ClientFactory; $client = ClientFactory::create('CalculatorService'); var_dump($client->add(1, 20)); -~~~ +``` ### Call by custom client -~~~php +```php use FriendsOfHyperf\Jet\Client; use FriendsOfHyperf\Jet\Transporter\GuzzleHttpTransporter; use FriendsOfHyperf\Jet\Registry\ConsulRegistry; @@ -82,11 +82,11 @@ class CalculatorService extends Client $service = new CalculatorService; var_dump($service->add(3, 10)); -~~~ +``` ### Call by custom facade -~~~php +```php use FriendsOfHyperf\Jet\Facade; use FriendsOfHyperf\Jet\ClientFactory; @@ -103,22 +103,52 @@ class Calculator extends Facade } var_dump(Calculator::add(rand(0, 100), rand(0, 100))); -~~~ +``` ## Coroutine support in Hyperf -~~~php -// config/autoload/annotations.php +- Aspect + +```php +clientFactory = $clientFactory; + } + + public function process(ProceedingJoinPoint $proceedingJoinPoint) + { + $instance = $proceedingJoinPoint->getInstance(); + $config = (function () { return $this->config; })->call($instance); + + return $this->clientFactory->create($config); + } +} +``` + +- Config `config/autoload/aspects.php` + +```php [ - // ... - 'class_map' => [ - GuzzleHttp\Client::class => BASE_PATH . '/vendor/friendsofhyperf/jet/classmap/GuzzleHttp/Client.php', - ], - ], + 'App\Aspect\GuzzleHttpTransporterAspect', ]; -~~~ +``` diff --git a/classmap/GuzzleHttp/Client.php b/classmap/GuzzleHttp/Client.php deleted file mode 100644 index 094008e..0000000 --- a/classmap/GuzzleHttp/Client.php +++ /dev/null @@ -1,490 +0,0 @@ - 'http://www.foo.com/1.0/', - * 'timeout' => 0, - * 'allow_redirects' => false, - * 'proxy' => '192.168.16.1:10' - * ]); - * - * Client configuration settings include the following options: - * - * - handler: (callable) Function that transfers HTTP requests over the - * wire. The function is called with a Psr7\Http\Message\RequestInterface - * and array of transfer options, and must return a - * GuzzleHttp\Promise\PromiseInterface that is fulfilled with a - * Psr7\Http\Message\ResponseInterface on success. - * If no handler is provided, a default handler will be created - * that enables all of the request options below by attaching all of the - * default middleware to the handler. - * - base_uri: (string|UriInterface) Base URI of the client that is merged - * into relative URIs. Can be a string or instance of UriInterface. - * - **: any request option - * - * @param array $config client configuration settings - * - * @see RequestOptions for a list of available request options. - */ - public function __construct(array $config = []) - { - $inCoroutine = Coroutine::inCoroutine(); - if (! isset($config['handler'])) { - // 对应的 Handler 可以按需选择 CoroutineHandler 或 PoolHandler - $config['handler'] = HandlerStack::create($inCoroutine ? new CoroutineHandler() : null); - } elseif ($inCoroutine && $config['handler'] instanceof HandlerStack) { - $config['handler']->setHandler(new CoroutineHandler()); - } elseif (! is_callable($config['handler'])) { - throw new \InvalidArgumentException('handler must be a callable'); - } - - // Convert the base_uri to a UriInterface - if (isset($config['base_uri'])) { - $config['base_uri'] = Psr7\uri_for($config['base_uri']); - } - - $this->configureDefaults($config); - } - - /** - * @param string $method - * @param array $args - * - * @return PromiseInterface|ResponseInterface - * - * @deprecated Client::__call will be removed in guzzlehttp/guzzle:8.0. - */ - public function __call($method, $args) - { - if (\count($args) < 1) { - throw new InvalidArgumentException('Magic request methods require a URI and optional options array'); - } - - $uri = $args[0]; - $opts = $args[1] ?? []; - - return \substr($method, -5) === 'Async' - ? $this->requestAsync(\substr($method, 0, -5), $uri, $opts) - : $this->request($method, $uri, $opts); - } - - /** - * Asynchronously send an HTTP request. - * - * @param array $options Request options to apply to the given - * request and to the transfer. See \GuzzleHttp\RequestOptions. - */ - public function sendAsync(RequestInterface $request, array $options = []): PromiseInterface - { - // Merge the base URI into the request URI if needed. - $options = $this->prepareDefaults($options); - - return $this->transfer( - $request->withUri($this->buildUri($request->getUri(), $options), $request->hasHeader('Host')), - $options - ); - } - - /** - * Send an HTTP request. - * - * @param array $options Request options to apply to the given - * request and to the transfer. See \GuzzleHttp\RequestOptions. - * - * @throws GuzzleException - */ - public function send(RequestInterface $request, array $options = []): ResponseInterface - { - $options[RequestOptions::SYNCHRONOUS] = true; - return $this->sendAsync($request, $options)->wait(); - } - - /** - * The HttpClient PSR (PSR-18) specify this method. - * - * {@inheritDoc} - */ - public function sendRequest(RequestInterface $request): ResponseInterface - { - $options[RequestOptions::SYNCHRONOUS] = true; - $options[RequestOptions::ALLOW_REDIRECTS] = false; - $options[RequestOptions::HTTP_ERRORS] = false; - - return $this->sendAsync($request, $options)->wait(); - } - - /** - * Create and send an asynchronous HTTP request. - * - * Use an absolute path to override the base path of the client, or a - * relative path to append to the base path of the client. The URL can - * contain the query string as well. Use an array to provide a URL - * template and additional variables to use in the URL template expansion. - * - * @param string $method HTTP method - * @param string|UriInterface $uri URI object or string - * @param array $options Request options to apply. See \GuzzleHttp\RequestOptions. - */ - public function requestAsync(string $method, $uri = '', array $options = []): PromiseInterface - { - $options = $this->prepareDefaults($options); - // Remove request modifying parameter because it can be done up-front. - $headers = $options['headers'] ?? []; - $body = $options['body'] ?? null; - $version = $options['version'] ?? '1.1'; - // Merge the URI into the base URI. - $uri = $this->buildUri(Psr7\Utils::uriFor($uri), $options); - if (\is_array($body)) { - throw $this->invalidBody(); - } - $request = new Psr7\Request($method, $uri, $headers, $body, $version); - // Remove the option so that they are not doubly-applied. - unset($options['headers'], $options['body'], $options['version']); - - return $this->transfer($request, $options); - } - - /** - * Create and send an HTTP request. - * - * Use an absolute path to override the base path of the client, or a - * relative path to append to the base path of the client. The URL can - * contain the query string as well. - * - * @param string $method HTTP method - * @param string|UriInterface $uri URI object or string - * @param array $options Request options to apply. See \GuzzleHttp\RequestOptions. - * - * @throws GuzzleException - */ - public function request(string $method, $uri = '', array $options = []): ResponseInterface - { - $options[RequestOptions::SYNCHRONOUS] = true; - return $this->requestAsync($method, $uri, $options)->wait(); - } - - /** - * Get a client configuration option. - * - * These options include default request options of the client, a "handler" - * (if utilized by the concrete client), and a "base_uri" if utilized by - * the concrete client. - * - * @param null|string $option the config option to retrieve - * - * @return mixed - * - * @deprecated Client::getConfig will be removed in guzzlehttp/guzzle:8.0. - */ - public function getConfig(?string $option = null) - { - return $option === null - ? $this->config - : (isset($this->config[$option]) ? $this->config[$option] : null); - } - - private function buildUri(UriInterface $uri, array $config): UriInterface - { - if (isset($config['base_uri'])) { - $uri = Psr7\UriResolver::resolve(Psr7\Utils::uriFor($config['base_uri']), $uri); - } - - if (isset($config['idn_conversion']) && ($config['idn_conversion'] !== false)) { - $idnOptions = ($config['idn_conversion'] === true) ? \IDNA_DEFAULT : $config['idn_conversion']; - $uri = Utils::idnUriConvert($uri, $idnOptions); - } - - return $uri->getScheme() === '' && $uri->getHost() !== '' ? $uri->withScheme('http') : $uri; - } - - /** - * Configures the default options for a client. - */ - private function configureDefaults(array $config): void - { - $defaults = [ - 'allow_redirects' => RedirectMiddleware::$defaultSettings, - 'http_errors' => true, - 'decode_content' => true, - 'verify' => true, - 'cookies' => false, - 'idn_conversion' => false, - ]; - - // Use the standard Linux HTTP_PROXY and HTTPS_PROXY if set. - - // We can only trust the HTTP_PROXY environment variable in a CLI - // process due to the fact that PHP has no reliable mechanism to - // get environment variables that start with "HTTP_". - if (\PHP_SAPI === 'cli' && ($proxy = Utils::getenv('HTTP_PROXY'))) { - $defaults['proxy']['http'] = $proxy; - } - - if ($proxy = Utils::getenv('HTTPS_PROXY')) { - $defaults['proxy']['https'] = $proxy; - } - - if ($noProxy = Utils::getenv('NO_PROXY')) { - $cleanedNoProxy = \str_replace(' ', '', $noProxy); - $defaults['proxy']['no'] = \explode(',', $cleanedNoProxy); - } - - $this->config = $config + $defaults; - - if (! empty($config['cookies']) && $config['cookies'] === true) { - $this->config['cookies'] = new CookieJar(); - } - - // Add the default user-agent header. - if (! isset($this->config['headers'])) { - $this->config['headers'] = ['User-Agent' => Utils::defaultUserAgent()]; - } else { - // Add the User-Agent header if one was not already set. - foreach (\array_keys($this->config['headers']) as $name) { - if (\strtolower($name) === 'user-agent') { - return; - } - } - $this->config['headers']['User-Agent'] = Utils::defaultUserAgent(); - } - } - - /** - * Merges default options into the array. - * - * @param array $options Options to modify by reference - */ - private function prepareDefaults(array $options): array - { - $defaults = $this->config; - - if (! empty($defaults['headers'])) { - // Default headers are only added if they are not present. - $defaults['_conditional'] = $defaults['headers']; - unset($defaults['headers']); - } - - // Special handling for headers is required as they are added as - // conditional headers and as headers passed to a request ctor. - if (\array_key_exists('headers', $options)) { - // Allows default headers to be unset. - if ($options['headers'] === null) { - $defaults['_conditional'] = []; - unset($options['headers']); - } elseif (! \is_array($options['headers'])) { - throw new InvalidArgumentException('headers must be an array'); - } - } - - // Shallow merge defaults underneath options. - $result = $options + $defaults; - - // Remove null values. - foreach ($result as $k => $v) { - if ($v === null) { - unset($result[$k]); - } - } - - return $result; - } - - /** - * Transfers the given request and applies request options. - * - * The URI of the request is not modified and the request options are used - * as-is without merging in default options. - * - * @param array $options see \GuzzleHttp\RequestOptions - */ - private function transfer(RequestInterface $request, array $options): PromiseInterface - { - $request = $this->applyOptions($request, $options); - /** @var HandlerStack $handler */ - $handler = $options['handler']; - - try { - return P\Create::promiseFor($handler($request, $options)); - } catch (\Exception $e) { - return P\Create::rejectionFor($e); - } - } - - /** - * Applies the array of request options to a request. - */ - private function applyOptions(RequestInterface $request, array &$options): RequestInterface - { - $modify = [ - 'set_headers' => [], - ]; - - if (isset($options['headers'])) { - $modify['set_headers'] = $options['headers']; - unset($options['headers']); - } - - if (isset($options['form_params'])) { - if (isset($options['multipart'])) { - throw new InvalidArgumentException('You cannot use ' - . 'form_params and multipart at the same time. Use the ' - . 'form_params option if you want to send application/' - . 'x-www-form-urlencoded requests, and the multipart ' - . 'option to send multipart/form-data requests.'); - } - $options['body'] = \http_build_query($options['form_params'], '', '&'); - unset($options['form_params']); - // Ensure that we don't have the header in different case and set the new value. - $options['_conditional'] = Psr7\Utils::caselessRemove(['Content-Type'], $options['_conditional']); - $options['_conditional']['Content-Type'] = 'application/x-www-form-urlencoded'; - } - - if (isset($options['multipart'])) { - $options['body'] = new Psr7\MultipartStream($options['multipart']); - unset($options['multipart']); - } - - if (isset($options['json'])) { - $options['body'] = Utils::jsonEncode($options['json']); - unset($options['json']); - // Ensure that we don't have the header in different case and set the new value. - $options['_conditional'] = Psr7\Utils::caselessRemove(['Content-Type'], $options['_conditional']); - $options['_conditional']['Content-Type'] = 'application/json'; - } - - if (! empty($options['decode_content']) - && $options['decode_content'] !== true - ) { - // Ensure that we don't have the header in different case and set the new value. - $options['_conditional'] = Psr7\Utils::caselessRemove(['Accept-Encoding'], $options['_conditional']); - $modify['set_headers']['Accept-Encoding'] = $options['decode_content']; - } - - if (isset($options['body'])) { - if (\is_array($options['body'])) { - throw $this->invalidBody(); - } - $modify['body'] = Psr7\Utils::streamFor($options['body']); - unset($options['body']); - } - - if (! empty($options['auth']) && \is_array($options['auth'])) { - $value = $options['auth']; - $type = isset($value[2]) ? \strtolower($value[2]) : 'basic'; - switch ($type) { - case 'basic': - // Ensure that we don't have the header in different case and set the new value. - $modify['set_headers'] = Psr7\Utils::caselessRemove(['Authorization'], $modify['set_headers']); - $modify['set_headers']['Authorization'] = 'Basic ' - . \base64_encode("{$value[0]}:{$value[1]}"); - break; - case 'digest': - // @todo: Do not rely on curl - $options['curl'][\CURLOPT_HTTPAUTH] = \CURLAUTH_DIGEST; - $options['curl'][\CURLOPT_USERPWD] = "{$value[0]}:{$value[1]}"; - break; - case 'ntlm': - $options['curl'][\CURLOPT_HTTPAUTH] = \CURLAUTH_NTLM; - $options['curl'][\CURLOPT_USERPWD] = "{$value[0]}:{$value[1]}"; - break; - } - } - - if (isset($options['query'])) { - $value = $options['query']; - if (\is_array($value)) { - $value = \http_build_query($value, '', '&', \PHP_QUERY_RFC3986); - } - if (! \is_string($value)) { - throw new InvalidArgumentException('query must be a string or array'); - } - $modify['query'] = $value; - unset($options['query']); - } - - // Ensure that sink is not an invalid value. - if (isset($options['sink'])) { - // TODO: Add more sink validation? - if (\is_bool($options['sink'])) { - throw new InvalidArgumentException('sink must not be a boolean'); - } - } - - $request = Psr7\Utils::modifyRequest($request, $modify); - if ($request->getBody() instanceof Psr7\MultipartStream) { - // Use a multipart/form-data POST if a Content-Type is not set. - // Ensure that we don't have the header in different case and set the new value. - $options['_conditional'] = Psr7\Utils::caselessRemove(['Content-Type'], $options['_conditional']); - $options['_conditional']['Content-Type'] = 'multipart/form-data; boundary=' - . $request->getBody()->getBoundary(); - } - - // Merge in conditional headers if they are not present. - if (isset($options['_conditional'])) { - // Build up the changes so it's in a single clone of the message. - $modify = []; - foreach ($options['_conditional'] as $k => $v) { - if (! $request->hasHeader($k)) { - $modify['set_headers'][$k] = $v; - } - } - $request = Psr7\Utils::modifyRequest($request, $modify); - // Don't pass this internal value along to middleware/handlers. - unset($options['_conditional']); - } - - return $request; - } - - /** - * Return an InvalidArgumentException with pre-set message. - */ - private function invalidBody(): InvalidArgumentException - { - return new InvalidArgumentException('Passing in the "body" request ' - . 'option as an array to send a request is not supported. ' - . 'Please use the "form_params" request option to send a ' - . 'application/x-www-form-urlencoded request, or the "multipart" ' - . 'request option to send a multipart/form-data request.'); - } -} From 7cefb4a627143c2ad2d7660cb9d14e5ba18b3de1 Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Sun, 11 Aug 2024 10:32:56 +0800 Subject: [PATCH 09/43] chore: Add .bashrc to .gitignore --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index b85e3f7..d52b9bd 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,5 @@ composer.lock *.bak /phpunit.xml /.phpunit.result.cache -.idea \ No newline at end of file +.idea +.bashrc \ No newline at end of file From 8d78a676f6d81ed9555679f30076771ff110f26d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=A3=E8=A8=80=E5=B0=B1=E6=98=AFSiam?= <59419979@qq.com> Date: Fri, 9 Aug 2024 22:26:08 +0800 Subject: [PATCH 10/43] Siam/multiplex rpc (#12) Co-Authored-By: hzh Co-Authored-By: Deeka Wong <8337659+huangdijia@users.noreply.github.com> --- src/DataFormatter/MultiplexDataFormatter.php | 74 ++++++++++++++++++++ src/Packer/JsonMultiplexPacker.php | 43 ++++++++++++ 2 files changed, 117 insertions(+) create mode 100644 src/DataFormatter/MultiplexDataFormatter.php create mode 100644 src/Packer/JsonMultiplexPacker.php diff --git a/src/DataFormatter/MultiplexDataFormatter.php b/src/DataFormatter/MultiplexDataFormatter.php new file mode 100644 index 0000000..d8df897 --- /dev/null +++ b/src/DataFormatter/MultiplexDataFormatter.php @@ -0,0 +1,74 @@ + $id, + 'path' => $path, + 'data' => $params, + 'extra' => [], + 'context' => [], + ]; + } + + /** + * @param array $data + */ + public function formatResponse($data): array + { + [$id, $result] = $data; + + return [ + 'id' => $id, + 'result' => $result, + 'context' => [], + ]; + } + + /** + * @param array $data + */ + public function formatErrorResponse($data): array + { + [$id, $code, $message, $data] = $data; + + if (isset($data) && $data instanceof \Throwable) { + $data = [ + 'class' => get_class($data), + 'code' => $data->getCode(), + 'message' => $data->getMessage(), + ]; + } + + return [ + 'id' => $id, + 'error' => [ + 'code' => $code, + 'message' => $message, + 'data' => $data, + ], + 'context' => [], + ]; + } +} diff --git a/src/Packer/JsonMultiplexPacker.php b/src/Packer/JsonMultiplexPacker.php new file mode 100644 index 0000000..5953b99 --- /dev/null +++ b/src/Packer/JsonMultiplexPacker.php @@ -0,0 +1,43 @@ + Date: Sun, 11 Aug 2024 15:00:44 +0800 Subject: [PATCH 11/43] Adds namespace for helper functions --- composer.json | 2 +- src/Consul/Response.php | 2 + src/Functions.php | 258 ++++++++++++++++++ src/PathGenerator/DotPathGenerator.php | 3 + src/PathGenerator/PathGenerator.php | 2 + src/Registry/ConsulRegistry.php | 4 + src/Transporter/StreamSocketTransporter.php | 2 + src/helpers.php | 283 -------------------- 8 files changed, 272 insertions(+), 284 deletions(-) create mode 100644 src/Functions.php delete mode 100644 src/helpers.php diff --git a/composer.json b/composer.json index 9cb412b..8c4a4a6 100644 --- a/composer.json +++ b/composer.json @@ -22,7 +22,7 @@ "FriendsOfHyperf\\Jet\\": "src/" }, "files": [ - "src/helpers.php" + "src/Functions.php" ] }, "autoload-dev": { diff --git a/src/Consul/Response.php b/src/Consul/Response.php index 38999e5..e251e07 100644 --- a/src/Consul/Response.php +++ b/src/Consul/Response.php @@ -15,6 +15,8 @@ use FriendsOfHyperf\Jet\Exception\ServerException; use Psr\Http\Message\ResponseInterface; +use function FriendsOfHyperf\Jet\array_get; + class Response { /** diff --git a/src/Functions.php b/src/Functions.php new file mode 100644 index 0000000..31ebe49 --- /dev/null +++ b/src/Functions.php @@ -0,0 +1,258 @@ +target = $target; + } + + public function __call($method, $parameters) + { + $this->target->{$method}(...$parameters); + + return $this->target; + } + }; + } + + $callback($value); + + return $value; +} + +/** + * @param mixed $value + * @return mixed + */ +function with($value, ?callable $callback = null) +{ + return is_null($callback) ? $value : $callback($value); +} + +/** + * @param string $delimiter + * @return string + */ +function str_snake(string $value, $delimiter = '_') +{ + if (! ctype_lower($value)) { + $value = preg_replace('/\s+/u', '', ucwords($value)); + $value = str_lower(preg_replace('/(.)(?=[A-Z])/u', '$1' . $delimiter, $value)); + } + + return $value; +} + +/** + * @return string + */ +function str_lower(string $value) +{ + return mb_strtolower($value, 'UTF-8'); +} + +/** + * @return string + */ +function str_studly(string $value, string $gap = '') +{ + $value = ucwords(str_replace(['-', '_'], ' ', $value)); + + return str_replace(' ', $gap, $value); +} + +/** + * @return string + */ +function str_replace_first(string $search, string $replace, string $subject) +{ + if ($search == '') { + return $subject; + } + + $position = strpos($subject, $search); + + if ($position !== false) { + return substr_replace($subject, $replace, $position, strlen($search)); + } + + return $subject; +} + +/** + * @return string + */ +function str_replace_array(string $search, array $replace, string $subject) +{ + foreach ($replace as $value) { + $subject = str_replace_first($search, (string) $value, $subject); + } + + return $subject; +} + +/** + * @param mixed $value + * @return mixed + */ +function value($value) +{ + return $value instanceof \Closure ? $value() : $value; +} + +/** + * Get an item from an array using "dot" notation. + * + * @param array|\ArrayAccess $array + * @param null|int|string $key + * @param mixed $default + */ +function array_get($array, $key = null, $default = null) +{ + if (is_null($key)) { + return $array; + } + + if (isset($array[$key])) { + return $array[$key]; + } + + if (! is_string($key) || strpos($key, '.') === false) { + return $array[$key] ?? value($default); + } + + foreach (explode('.', $key) as $segment) { + if (array_accessible($array) && array_exists($array, $segment)) { + $array = $array[$segment]; + } else { + return value($default); + } + } + + return $array; +} + +/** + * Check if an item or items exist in an array using "dot" notation. + * + * @param array|\ArrayAccess $array + * @param null|array|string $keys + */ +function array_has($array, $keys) +{ + if (is_null($keys)) { + return false; + } + + $keys = (array) $keys; + + if (! $array || $keys === []) { + return false; + } + + foreach ($keys as $key) { + $subKeyArray = $array; + + if (array_exists($array, $key)) { + continue; + } + + foreach (explode('.', $key) as $segment) { + if (array_accessible($subKeyArray) && array_exists($subKeyArray, $segment)) { + $subKeyArray = $subKeyArray[$segment]; + } else { + return false; + } + } + } + + return true; +} + +/** + * @param mixed $array + * @param int|string $key + * @return bool + */ +function array_exists($array, $key) +{ + if ($array instanceof \ArrayAccess) { + return $array->offsetExists($key); + } + + return array_key_exists($key, $array); +} + +/** + * @param mixed $value + * @return bool + */ +function array_accessible($value) +{ + return is_array($value) || $value instanceof \ArrayAccess; +} diff --git a/src/PathGenerator/DotPathGenerator.php b/src/PathGenerator/DotPathGenerator.php index 4a47a97..ce9e367 100644 --- a/src/PathGenerator/DotPathGenerator.php +++ b/src/PathGenerator/DotPathGenerator.php @@ -14,6 +14,9 @@ use FriendsOfHyperf\Jet\Contract\PathGeneratorInterface; +use function FriendsOfHyperf\Jet\str_replace_array; +use function FriendsOfHyperf\Jet\str_studly; + class DotPathGenerator implements PathGeneratorInterface { public function generate(string $service, string $method): string diff --git a/src/PathGenerator/PathGenerator.php b/src/PathGenerator/PathGenerator.php index 4674071..97c06b7 100644 --- a/src/PathGenerator/PathGenerator.php +++ b/src/PathGenerator/PathGenerator.php @@ -14,6 +14,8 @@ use FriendsOfHyperf\Jet\Contract\PathGeneratorInterface; +use function FriendsOfHyperf\Jet\str_snake; + class PathGenerator implements PathGeneratorInterface { public function generate(string $service, string $method): string diff --git a/src/Registry/ConsulRegistry.php b/src/Registry/ConsulRegistry.php index 1a4c5f1..abeae67 100644 --- a/src/Registry/ConsulRegistry.php +++ b/src/Registry/ConsulRegistry.php @@ -22,6 +22,10 @@ use FriendsOfHyperf\Jet\Transporter\StreamSocketTransporter; use GuzzleHttp\Client; +use function FriendsOfHyperf\Jet\array_get; +use function FriendsOfHyperf\Jet\retry; +use function FriendsOfHyperf\Jet\with; + class ConsulRegistry implements RegistryInterface { /** diff --git a/src/Transporter/StreamSocketTransporter.php b/src/Transporter/StreamSocketTransporter.php index 04d0a83..668f2d2 100644 --- a/src/Transporter/StreamSocketTransporter.php +++ b/src/Transporter/StreamSocketTransporter.php @@ -16,6 +16,8 @@ use FriendsOfHyperf\Jet\Exception\ExceptionThrower; use FriendsOfHyperf\Jet\Exception\RecvFailedException; +use function FriendsOfHyperf\Jet\retry; + class StreamSocketTransporter extends AbstractTransporter { /** diff --git a/src/helpers.php b/src/helpers.php deleted file mode 100644 index 24d1c33..0000000 --- a/src/helpers.php +++ /dev/null @@ -1,283 +0,0 @@ -target = $target; - } - - public function __call($method, $parameters) - { - $this->target->{$method}(...$parameters); - - return $this->target; - } - }; - } - - $callback($value); - - return $value; - } -} - -if (! function_exists('with')) { - /** - * @param mixed $value - * @return mixed - */ - function with($value, ?callable $callback = null) - { - return is_null($callback) ? $value : $callback($value); - } -} - -if (! function_exists('str_snake')) { - /** - * @param string $delimiter - * @return string - */ - function str_snake(string $value, $delimiter = '_') - { - if (! ctype_lower($value)) { - $value = preg_replace('/\s+/u', '', ucwords($value)); - $value = str_lower(preg_replace('/(.)(?=[A-Z])/u', '$1' . $delimiter, $value)); - } - - return $value; - } -} - -if (! function_exists('str_lower')) { - /** - * @return string - */ - function str_lower(string $value) - { - return mb_strtolower($value, 'UTF-8'); - } -} - -if (! function_exists('str_studly')) { - /** - * @return string - */ - function str_studly(string $value, string $gap = '') - { - $value = ucwords(str_replace(['-', '_'], ' ', $value)); - - return str_replace(' ', $gap, $value); - } -} - -if (! function_exists('str_replace_first')) { - /** - * @return string - */ - function str_replace_first(string $search, string $replace, string $subject) - { - if ($search == '') { - return $subject; - } - - $position = strpos($subject, $search); - - if ($position !== false) { - return substr_replace($subject, $replace, $position, strlen($search)); - } - - return $subject; - } -} - -if (! function_exists('str_replace_array')) { - /** - * @return string - */ - function str_replace_array(string $search, array $replace, string $subject) - { - foreach ($replace as $value) { - $subject = str_replace_first($search, (string) $value, $subject); - } - - return $subject; - } -} - -if (! function_exists('value')) { - /** - * @param mixed $value - * @return mixed - */ - function value($value) - { - return $value instanceof Closure ? $value() : $value; - } -} - -if (! function_exists('array_get')) { - /** - * Get an item from an array using "dot" notation. - * - * @param array|ArrayAccess $array - * @param null|int|string $key - * @param mixed $default - */ - function array_get($array, $key = null, $default = null) - { - if (is_null($key)) { - return $array; - } - - if (isset($array[$key])) { - return $array[$key]; - } - - if (! is_string($key) || strpos($key, '.') === false) { - return $array[$key] ?? value($default); - } - - foreach (explode('.', $key) as $segment) { - if (array_accessible($array) && array_exists($array, $segment)) { - $array = $array[$segment]; - } else { - return value($default); - } - } - - return $array; - } -} - -if (! function_exists('array_has')) { - /** - * Check if an item or items exist in an array using "dot" notation. - * - * @param array|ArrayAccess $array - * @param null|array|string $keys - */ - function array_has($array, $keys) - { - if (is_null($keys)) { - return false; - } - - $keys = (array) $keys; - - if (! $array || $keys === []) { - return false; - } - - foreach ($keys as $key) { - $subKeyArray = $array; - - if (array_exists($array, $key)) { - continue; - } - - foreach (explode('.', $key) as $segment) { - if (array_accessible($subKeyArray) && array_exists($subKeyArray, $segment)) { - $subKeyArray = $subKeyArray[$segment]; - } else { - return false; - } - } - } - - return true; - } -} - -if (! function_exists('array_exists')) { - /** - * @param mixed $array - * @param int|string $key - * @return bool - */ - function array_exists($array, $key) - { - if ($array instanceof ArrayAccess) { - return $array->offsetExists($key); - } - - return array_key_exists($key, $array); - } -} - -if (! function_exists('array_accessible')) { - /** - * @param mixed $value - * @return bool - */ - function array_accessible($value) - { - return is_array($value) || $value instanceof ArrayAccess; - } -} From 08faed42e1d219c0624ef44ea9f08ff03080a950 Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Sun, 11 Aug 2024 15:02:07 +0800 Subject: [PATCH 12/43] chore: Update phpstan/phpstan dependency to version 1.0 --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 8c4a4a6..7086892 100644 --- a/composer.json +++ b/composer.json @@ -14,7 +14,7 @@ "require-dev": { "friendsofphp/php-cs-fixer": "^3.0", "mockery/mockery": "^1.0", - "phpstan/phpstan": "^0.12", + "phpstan/phpstan": "^1.0", "phpunit/phpunit": "^7.0|^8.0|^9.0|^10.0" }, "autoload": { From d0c36ca9695f839818ddde73d7d3b09d2616858d Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Sun, 11 Aug 2024 15:04:26 +0800 Subject: [PATCH 13/43] chore: Update tests.yaml to include fail-fast option --- .github/workflows/tests.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index f6ff717..a362e62 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/tests.yaml @@ -11,6 +11,7 @@ jobs: os: [ubuntu-latest] php-version: ['7.1', '7.2', '7.3', '7.4', '8.0'] max-parallel: 20 + fail-fast: false steps: - name: Checkout uses: actions/checkout@v2 From 91a3051b5ee3f2f825b949ea968d29ceb5286abe Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Sun, 11 Aug 2024 15:06:10 +0800 Subject: [PATCH 14/43] Update JsonMultiplexPacker to accept string type for unpack method --- src/Packer/JsonMultiplexPacker.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Packer/JsonMultiplexPacker.php b/src/Packer/JsonMultiplexPacker.php index 5953b99..df72576 100644 --- a/src/Packer/JsonMultiplexPacker.php +++ b/src/Packer/JsonMultiplexPacker.php @@ -34,7 +34,7 @@ public function pack($data): string * @param string $data * @return array */ - public function unpack($data) + public function unpack(string $data) { // $unpacked = unpack('Nid', substr($data, 4, 4)); $body = substr($data, 8); From ad1bd96c604d3b5ff42591f37771fc0251152ad8 Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Sun, 11 Aug 2024 15:13:18 +0800 Subject: [PATCH 15/43] feat: Add return type annotations to helper functions --- src/Functions.php | 31 ++++++++++++++++++++---------- src/Packer/JsonMultiplexPacker.php | 1 - 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/src/Functions.php b/src/Functions.php index 31ebe49..ace058b 100644 --- a/src/Functions.php +++ b/src/Functions.php @@ -13,8 +13,10 @@ namespace FriendsOfHyperf\Jet; /** - * Retry. - * @return mixed + * @template TReturn + * + * @param callable(int):TReturn $callback + * @return TReturn * @throws \Throwable */ function retry(int $times, callable $callback, int $sleep = 0, ?callable $when = null) @@ -38,13 +40,16 @@ function retry(int $times, callable $callback, int $sleep = 0, ?callable $when = goto beginning; } + + return null; } /** - * @param mixed $condition - * @param string|\Throwable $exception - * @return mixed - * @throws \InvalidArgumentException + * @template TValue + * + * @param TValue $condition + * @param class-string<\Throwable>|\Throwable $exception + * @return TValue * @throws \Throwable */ function throw_if($condition, $exception, ...$parameters) @@ -57,8 +62,10 @@ function throw_if($condition, $exception, ...$parameters) } /** - * @param mixed $value - * @return mixed + * @template TValue + * + * @param TValue $value + * @return TValue */ function tap($value, ?callable $callback = null) { @@ -86,8 +93,12 @@ public function __call($method, $parameters) } /** - * @param mixed $value - * @return mixed + * @template TValue + * @template TReturn + * + * @param TValue $value + * @param null|(callable(TValue):TReturn) $callback + * @return ($callback is null ? TValue : TReturn) */ function with($value, ?callable $callback = null) { diff --git a/src/Packer/JsonMultiplexPacker.php b/src/Packer/JsonMultiplexPacker.php index df72576..77d413c 100644 --- a/src/Packer/JsonMultiplexPacker.php +++ b/src/Packer/JsonMultiplexPacker.php @@ -31,7 +31,6 @@ public function pack($data): string } /** - * @param string $data * @return array */ public function unpack(string $data) From 7bddf891760893c6202b35a59b12d591a67897b6 Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Sun, 11 Aug 2024 15:17:33 +0800 Subject: [PATCH 16/43] chore: Update PHP requirement to version 7.1 --- composer.json | 2 +- src/Functions.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 7086892..c25ce0a 100644 --- a/composer.json +++ b/composer.json @@ -8,7 +8,7 @@ "email": "huangdijia@gmail.com" }], "require": { - "php": ">=7.0", + "php": ">=7.1", "guzzlehttp/guzzle": "^6.0|^7.0" }, "require-dev": { diff --git a/src/Functions.php b/src/Functions.php index ace058b..9d4f514 100644 --- a/src/Functions.php +++ b/src/Functions.php @@ -100,7 +100,7 @@ public function __call($method, $parameters) * @param null|(callable(TValue):TReturn) $callback * @return ($callback is null ? TValue : TReturn) */ -function with($value, ?callable $callback = null) +function with($value, ?callable $callback = null) // @phpstan-ignore-line { return is_null($callback) ? $value : $callback($value); } From 1efa5315255f74a89f883ebda8a7c42a3a6cbc53 Mon Sep 17 00:00:00 2001 From: Deeka Wong Date: Sun, 11 Aug 2024 16:08:28 +0800 Subject: [PATCH 17/43] Split UserAgent (#18) Co-authored-by: Deeka Wong <8337659+huangdijia@users.noreply.github.com> --- src/ClientFactory.php | 29 ---------------- src/Support/UserAgent.php | 40 +++++++++++++++++++++++ src/Transporter/GuzzleHttpTransporter.php | 4 +-- 3 files changed, 42 insertions(+), 31 deletions(-) create mode 100644 src/Support/UserAgent.php diff --git a/src/ClientFactory.php b/src/ClientFactory.php index 8792bef..efb28ac 100644 --- a/src/ClientFactory.php +++ b/src/ClientFactory.php @@ -16,38 +16,9 @@ use FriendsOfHyperf\Jet\Contract\PackerInterface; use FriendsOfHyperf\Jet\Contract\PathGeneratorInterface; use FriendsOfHyperf\Jet\Contract\TransporterInterface; -use GuzzleHttp\ClientInterface; class ClientFactory { - /** - * User agent. - * @var string - */ - protected static $userAgent; - - /** - * Set user agent. - */ - public static function setUserAgent(string $userAgent): void - { - self::$userAgent = $userAgent; - } - - /** - * Get user agent. - */ - public static function getUserAgent(): string - { - return self::$userAgent ?: sprintf( - 'jet/%s php/%s guzzle/%s curl/%s', - Client::MAJOR_VERSION, - PHP_VERSION, - defined(ClientInterface::class . '::VERSION') ? constant(ClientInterface::class . '::VERSION') : constant(ClientInterface::class . '::MAJOR_VERSION'), - curl_version()['version'] - ); - } - /** * Create a client. * @param null|int|string|TransporterInterface $transporter transporter, protocol, timeout or null diff --git a/src/Support/UserAgent.php b/src/Support/UserAgent.php new file mode 100644 index 0000000..8313c2d --- /dev/null +++ b/src/Support/UserAgent.php @@ -0,0 +1,40 @@ + 'application/json', 'X-Real-Ip' => $_SERVER['SERVER_ADDR'] ?? '', 'X-Forwarded-For' => $_SERVER['REMOTE_ADDR'] ?? '', - 'User-Agent' => ClientFactory::getUserAgent(), + 'User-Agent' => UserAgent::get(), ], 'http_errors' => false, ]); From ac30e2f15ed8df6e7af86253a9f999b4b2cb93c4 Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Sun, 11 Aug 2024 16:16:03 +0800 Subject: [PATCH 18/43] Cs Fix Co-Authored-By: Deeka Wong <8337659+huangdijia@users.noreply.github.com> --- .php-cs-fixer.php | 3 +-- bootstrap.php | 3 +-- src/ClientFactory.php | 3 +-- src/Consul/Agent.php | 3 +-- src/Consul/Catalog.php | 3 +-- src/Consul/Client.php | 3 +-- src/Consul/Health.php | 3 +-- src/Consul/Response.php | 3 +-- src/Contract/DataFormatterInterface.php | 3 +-- src/Contract/LoadBalancerInterface.php | 3 +-- src/Contract/PackerInterface.php | 3 +-- src/Contract/PathGeneratorInterface.php | 3 +-- src/Contract/RegistryInterface.php | 3 +-- src/Contract/TransporterInterface.php | 3 +-- src/Functions.php | 3 +-- src/LoadBalancer/AbstractLoadBalancer.php | 3 +-- src/LoadBalancer/Node.php | 3 +-- src/LoadBalancer/Random.php | 3 +-- src/LoadBalancer/RoundRobin.php | 3 +-- src/Metadata.php | 3 +-- src/PathGenerator/DotPathGenerator.php | 3 +-- src/PathGenerator/FullPathGenerator.php | 3 +-- src/PathGenerator/PathGenerator.php | 3 +-- src/RegistryManager.php | 3 +-- src/ServiceManager.php | 3 +-- src/Support/UserAgent.php | 3 +-- src/Transporter/AbstractTransporter.php | 3 +-- src/Transporter/GuzzleHttpTransporter.php | 3 +-- src/Transporter/StreamSocketTransporter.php | 3 +-- tests/ClientTest.php | 3 +-- tests/RegistryTest.php | 3 +-- tests/TestCase.php | 3 +-- tests/register.php | 3 +-- 33 files changed, 33 insertions(+), 66 deletions(-) diff --git a/.php-cs-fixer.php b/.php-cs-fixer.php index c087f33..fb874a4 100644 --- a/.php-cs-fixer.php +++ b/.php-cs-fixer.php @@ -1,12 +1,11 @@ Date: Sun, 11 Aug 2024 16:16:31 +0800 Subject: [PATCH 19/43] Cs fix Co-Authored-By: Deeka Wong <8337659+huangdijia@users.noreply.github.com> --- src/Client.php | 3 +-- src/DataFormatter/DataFormatter.php | 3 +-- src/DataFormatter/MultiplexDataFormatter.php | 3 +-- src/Exception/ClientException.php | 3 +-- src/Exception/ConnectionException.php | 3 +-- src/Exception/ExceptionThrower.php | 3 +-- src/Exception/JetException.php | 3 +-- src/Exception/NoNodesAvailableException.php | 3 +-- src/Exception/RecvFailedException.php | 3 +-- src/Exception/ServerException.php | 3 +-- src/Facade.php | 3 +-- src/Packer/JsonEofPacker.php | 3 +-- src/Packer/JsonLengthPacker.php | 3 +-- src/Packer/JsonMultiplexPacker.php | 3 +-- src/Registry/ConsulRegistry.php | 3 +-- 15 files changed, 15 insertions(+), 30 deletions(-) diff --git a/src/Client.php b/src/Client.php index b43f8c2..6396414 100644 --- a/src/Client.php +++ b/src/Client.php @@ -2,12 +2,11 @@ declare(strict_types=1); /** - * This file is part of jet. + * This file is part of friendsofhyperf/jet. * * @link https://github.com/friendsofhyperf/jet * @document https://github.com/friendsofhyperf/jet/blob/main/README.md * @contact huangdijia@gmail.com - * @license https://github.com/friendsofhyperf/jet/blob/main/LICENSE */ namespace FriendsOfHyperf\Jet; diff --git a/src/DataFormatter/DataFormatter.php b/src/DataFormatter/DataFormatter.php index e2e9fe9..343e74e 100644 --- a/src/DataFormatter/DataFormatter.php +++ b/src/DataFormatter/DataFormatter.php @@ -2,12 +2,11 @@ declare(strict_types=1); /** - * This file is part of jet. + * This file is part of friendsofhyperf/jet. * * @link https://github.com/friendsofhyperf/jet * @document https://github.com/friendsofhyperf/jet/blob/main/README.md * @contact huangdijia@gmail.com - * @license https://github.com/friendsofhyperf/jet/blob/main/LICENSE */ namespace FriendsOfHyperf\Jet\DataFormatter; diff --git a/src/DataFormatter/MultiplexDataFormatter.php b/src/DataFormatter/MultiplexDataFormatter.php index d8df897..fe470ed 100644 --- a/src/DataFormatter/MultiplexDataFormatter.php +++ b/src/DataFormatter/MultiplexDataFormatter.php @@ -2,12 +2,11 @@ declare(strict_types=1); /** - * This file is part of jet. + * This file is part of friendsofhyperf/jet. * * @link https://github.com/friendsofhyperf/jet * @document https://github.com/friendsofhyperf/jet/blob/main/README.md * @contact huangdijia@gmail.com - * @license https://github.com/friendsofhyperf/jet/blob/main/LICENSE */ namespace FriendsOfHyperf\Jet\DataFormatter; diff --git a/src/Exception/ClientException.php b/src/Exception/ClientException.php index eac679a..002868e 100644 --- a/src/Exception/ClientException.php +++ b/src/Exception/ClientException.php @@ -2,12 +2,11 @@ declare(strict_types=1); /** - * This file is part of jet. + * This file is part of friendsofhyperf/jet. * * @link https://github.com/friendsofhyperf/jet * @document https://github.com/friendsofhyperf/jet/blob/main/README.md * @contact huangdijia@gmail.com - * @license https://github.com/friendsofhyperf/jet/blob/main/LICENSE */ namespace FriendsOfHyperf\Jet\Exception; diff --git a/src/Exception/ConnectionException.php b/src/Exception/ConnectionException.php index f6f170c..9a70138 100644 --- a/src/Exception/ConnectionException.php +++ b/src/Exception/ConnectionException.php @@ -2,12 +2,11 @@ declare(strict_types=1); /** - * This file is part of jet. + * This file is part of friendsofhyperf/jet. * * @link https://github.com/friendsofhyperf/jet * @document https://github.com/friendsofhyperf/jet/blob/main/README.md * @contact huangdijia@gmail.com - * @license https://github.com/friendsofhyperf/jet/blob/main/LICENSE */ namespace FriendsOfHyperf\Jet\Exception; diff --git a/src/Exception/ExceptionThrower.php b/src/Exception/ExceptionThrower.php index b4af9c3..56e13b0 100644 --- a/src/Exception/ExceptionThrower.php +++ b/src/Exception/ExceptionThrower.php @@ -2,12 +2,11 @@ declare(strict_types=1); /** - * This file is part of jet. + * This file is part of friendsofhyperf/jet. * * @link https://github.com/friendsofhyperf/jet * @document https://github.com/friendsofhyperf/jet/blob/main/README.md * @contact huangdijia@gmail.com - * @license https://github.com/friendsofhyperf/jet/blob/main/LICENSE */ namespace FriendsOfHyperf\Jet\Exception; diff --git a/src/Exception/JetException.php b/src/Exception/JetException.php index 20bb1c9..d58779e 100644 --- a/src/Exception/JetException.php +++ b/src/Exception/JetException.php @@ -2,12 +2,11 @@ declare(strict_types=1); /** - * This file is part of jet. + * This file is part of friendsofhyperf/jet. * * @link https://github.com/friendsofhyperf/jet * @document https://github.com/friendsofhyperf/jet/blob/main/README.md * @contact huangdijia@gmail.com - * @license https://github.com/friendsofhyperf/jet/blob/main/LICENSE */ namespace FriendsOfHyperf\Jet\Exception; diff --git a/src/Exception/NoNodesAvailableException.php b/src/Exception/NoNodesAvailableException.php index 7129447..6e71a78 100644 --- a/src/Exception/NoNodesAvailableException.php +++ b/src/Exception/NoNodesAvailableException.php @@ -2,12 +2,11 @@ declare(strict_types=1); /** - * This file is part of jet. + * This file is part of friendsofhyperf/jet. * * @link https://github.com/friendsofhyperf/jet * @document https://github.com/friendsofhyperf/jet/blob/main/README.md * @contact huangdijia@gmail.com - * @license https://github.com/friendsofhyperf/jet/blob/main/LICENSE */ namespace FriendsOfHyperf\Jet\Exception; diff --git a/src/Exception/RecvFailedException.php b/src/Exception/RecvFailedException.php index 17283b2..55eaf91 100644 --- a/src/Exception/RecvFailedException.php +++ b/src/Exception/RecvFailedException.php @@ -2,12 +2,11 @@ declare(strict_types=1); /** - * This file is part of jet. + * This file is part of friendsofhyperf/jet. * * @link https://github.com/friendsofhyperf/jet * @document https://github.com/friendsofhyperf/jet/blob/main/README.md * @contact huangdijia@gmail.com - * @license https://github.com/friendsofhyperf/jet/blob/main/LICENSE */ namespace FriendsOfHyperf\Jet\Exception; diff --git a/src/Exception/ServerException.php b/src/Exception/ServerException.php index fdecd7e..555596a 100644 --- a/src/Exception/ServerException.php +++ b/src/Exception/ServerException.php @@ -2,12 +2,11 @@ declare(strict_types=1); /** - * This file is part of jet. + * This file is part of friendsofhyperf/jet. * * @link https://github.com/friendsofhyperf/jet * @document https://github.com/friendsofhyperf/jet/blob/main/README.md * @contact huangdijia@gmail.com - * @license https://github.com/friendsofhyperf/jet/blob/main/LICENSE */ namespace FriendsOfHyperf\Jet\Exception; diff --git a/src/Facade.php b/src/Facade.php index 3eeb6b9..f7270e8 100644 --- a/src/Facade.php +++ b/src/Facade.php @@ -2,12 +2,11 @@ declare(strict_types=1); /** - * This file is part of jet. + * This file is part of friendsofhyperf/jet. * * @link https://github.com/friendsofhyperf/jet * @document https://github.com/friendsofhyperf/jet/blob/main/README.md * @contact huangdijia@gmail.com - * @license https://github.com/friendsofhyperf/jet/blob/main/LICENSE */ namespace FriendsOfHyperf\Jet; diff --git a/src/Packer/JsonEofPacker.php b/src/Packer/JsonEofPacker.php index ca92c5a..2c144d0 100644 --- a/src/Packer/JsonEofPacker.php +++ b/src/Packer/JsonEofPacker.php @@ -2,12 +2,11 @@ declare(strict_types=1); /** - * This file is part of jet. + * This file is part of friendsofhyperf/jet. * * @link https://github.com/friendsofhyperf/jet * @document https://github.com/friendsofhyperf/jet/blob/main/README.md * @contact huangdijia@gmail.com - * @license https://github.com/friendsofhyperf/jet/blob/main/LICENSE */ namespace FriendsOfHyperf\Jet\Packer; diff --git a/src/Packer/JsonLengthPacker.php b/src/Packer/JsonLengthPacker.php index d6703e4..7062241 100644 --- a/src/Packer/JsonLengthPacker.php +++ b/src/Packer/JsonLengthPacker.php @@ -2,12 +2,11 @@ declare(strict_types=1); /** - * This file is part of jet. + * This file is part of friendsofhyperf/jet. * * @link https://github.com/friendsofhyperf/jet * @document https://github.com/friendsofhyperf/jet/blob/main/README.md * @contact huangdijia@gmail.com - * @license https://github.com/friendsofhyperf/jet/blob/main/LICENSE */ namespace FriendsOfHyperf\Jet\Packer; diff --git a/src/Packer/JsonMultiplexPacker.php b/src/Packer/JsonMultiplexPacker.php index 77d413c..328b54d 100644 --- a/src/Packer/JsonMultiplexPacker.php +++ b/src/Packer/JsonMultiplexPacker.php @@ -2,12 +2,11 @@ declare(strict_types=1); /** - * This file is part of jet. + * This file is part of friendsofhyperf/jet. * * @link https://github.com/friendsofhyperf/jet * @document https://github.com/friendsofhyperf/jet/blob/main/README.md * @contact huangdijia@gmail.com - * @license https://github.com/friendsofhyperf/jet/blob/main/LICENSE */ namespace FriendsOfHyperf\Jet\Packer; diff --git a/src/Registry/ConsulRegistry.php b/src/Registry/ConsulRegistry.php index abeae67..67765a5 100644 --- a/src/Registry/ConsulRegistry.php +++ b/src/Registry/ConsulRegistry.php @@ -2,12 +2,11 @@ declare(strict_types=1); /** - * This file is part of jet. + * This file is part of friendsofhyperf/jet. * * @link https://github.com/friendsofhyperf/jet * @document https://github.com/friendsofhyperf/jet/blob/main/README.md * @contact huangdijia@gmail.com - * @license https://github.com/friendsofhyperf/jet/blob/main/LICENSE */ namespace FriendsOfHyperf\Jet\Registry; From d34146f8f0e025daf2f291bffd2cc5ecb8e586cc Mon Sep 17 00:00:00 2001 From: Deeka Wong Date: Sun, 11 Aug 2024 16:44:22 +0800 Subject: [PATCH 20/43] Split Arr and Str (#19) * Split Arr and Str Co-Authored-By: Deeka Wong <8337659+huangdijia@users.noreply.github.com> * feat: Update path generation to handle namespaces with backslashes * feat: Fix backslash handling in path generation --------- Co-authored-by: Deeka Wong <8337659+huangdijia@users.noreply.github.com> --- src/Consul/Response.php | 5 +- src/Functions.php | 154 ------------------------- src/PathGenerator/DotPathGenerator.php | 10 +- src/PathGenerator/PathGenerator.php | 5 +- src/Registry/ConsulRegistry.php | 14 +-- src/Support/Arr.php | 111 ++++++++++++++++++ src/Support/Str.php | 87 ++++++++++++++ 7 files changed, 213 insertions(+), 173 deletions(-) create mode 100644 src/Support/Arr.php create mode 100644 src/Support/Str.php diff --git a/src/Consul/Response.php b/src/Consul/Response.php index 62ccbdb..6b05760 100644 --- a/src/Consul/Response.php +++ b/src/Consul/Response.php @@ -12,10 +12,9 @@ namespace FriendsOfHyperf\Jet\Consul; use FriendsOfHyperf\Jet\Exception\ServerException; +use FriendsOfHyperf\Jet\Support\Arr; use Psr\Http\Message\ResponseInterface; -use function FriendsOfHyperf\Jet\array_get; - class Response { /** @@ -57,7 +56,7 @@ public function json(?string $key = null, $default = null) return $this->decoded; } - return array_get($this->decoded, $key, $default); + return Arr::get($this->decoded, $key, $default); } /** diff --git a/src/Functions.php b/src/Functions.php index b21025c..370c9eb 100644 --- a/src/Functions.php +++ b/src/Functions.php @@ -104,68 +104,6 @@ function with($value, ?callable $callback = null) // @phpstan-ignore-line return is_null($callback) ? $value : $callback($value); } -/** - * @param string $delimiter - * @return string - */ -function str_snake(string $value, $delimiter = '_') -{ - if (! ctype_lower($value)) { - $value = preg_replace('/\s+/u', '', ucwords($value)); - $value = str_lower(preg_replace('/(.)(?=[A-Z])/u', '$1' . $delimiter, $value)); - } - - return $value; -} - -/** - * @return string - */ -function str_lower(string $value) -{ - return mb_strtolower($value, 'UTF-8'); -} - -/** - * @return string - */ -function str_studly(string $value, string $gap = '') -{ - $value = ucwords(str_replace(['-', '_'], ' ', $value)); - - return str_replace(' ', $gap, $value); -} - -/** - * @return string - */ -function str_replace_first(string $search, string $replace, string $subject) -{ - if ($search == '') { - return $subject; - } - - $position = strpos($subject, $search); - - if ($position !== false) { - return substr_replace($subject, $replace, $position, strlen($search)); - } - - return $subject; -} - -/** - * @return string - */ -function str_replace_array(string $search, array $replace, string $subject) -{ - foreach ($replace as $value) { - $subject = str_replace_first($search, (string) $value, $subject); - } - - return $subject; -} - /** * @param mixed $value * @return mixed @@ -174,95 +112,3 @@ function value($value) { return $value instanceof \Closure ? $value() : $value; } - -/** - * Get an item from an array using "dot" notation. - * - * @param array|\ArrayAccess $array - * @param null|int|string $key - * @param mixed $default - */ -function array_get($array, $key = null, $default = null) -{ - if (is_null($key)) { - return $array; - } - - if (isset($array[$key])) { - return $array[$key]; - } - - if (! is_string($key) || strpos($key, '.') === false) { - return $array[$key] ?? value($default); - } - - foreach (explode('.', $key) as $segment) { - if (array_accessible($array) && array_exists($array, $segment)) { - $array = $array[$segment]; - } else { - return value($default); - } - } - - return $array; -} - -/** - * Check if an item or items exist in an array using "dot" notation. - * - * @param array|\ArrayAccess $array - * @param null|array|string $keys - */ -function array_has($array, $keys) -{ - if (is_null($keys)) { - return false; - } - - $keys = (array) $keys; - - if (! $array || $keys === []) { - return false; - } - - foreach ($keys as $key) { - $subKeyArray = $array; - - if (array_exists($array, $key)) { - continue; - } - - foreach (explode('.', $key) as $segment) { - if (array_accessible($subKeyArray) && array_exists($subKeyArray, $segment)) { - $subKeyArray = $subKeyArray[$segment]; - } else { - return false; - } - } - } - - return true; -} - -/** - * @param mixed $array - * @param int|string $key - * @return bool - */ -function array_exists($array, $key) -{ - if ($array instanceof \ArrayAccess) { - return $array->offsetExists($key); - } - - return array_key_exists($key, $array); -} - -/** - * @param mixed $value - * @return bool - */ -function array_accessible($value) -{ - return is_array($value) || $value instanceof \ArrayAccess; -} diff --git a/src/PathGenerator/DotPathGenerator.php b/src/PathGenerator/DotPathGenerator.php index 9655799..78b6003 100644 --- a/src/PathGenerator/DotPathGenerator.php +++ b/src/PathGenerator/DotPathGenerator.php @@ -12,18 +12,16 @@ namespace FriendsOfHyperf\Jet\PathGenerator; use FriendsOfHyperf\Jet\Contract\PathGeneratorInterface; - -use function FriendsOfHyperf\Jet\str_replace_array; -use function FriendsOfHyperf\Jet\str_studly; +use FriendsOfHyperf\Jet\Support\Str; class DotPathGenerator implements PathGeneratorInterface { public function generate(string $service, string $method): string { $handledNamespace = explode('\\', $service); - $handledNamespace = str_replace_array('\\', ['/'], end($handledNamespace)); - $path = str_studly($handledNamespace); + $handledNamespace = Str::replaceArray('\\', ['/'], end($handledNamespace)); + $path = Str::studly($handledNamespace); - return $path . '.' . str_studly($method); + return $path . '.' . Str::studly($method); } } diff --git a/src/PathGenerator/PathGenerator.php b/src/PathGenerator/PathGenerator.php index 080a9d3..4d3a8f1 100644 --- a/src/PathGenerator/PathGenerator.php +++ b/src/PathGenerator/PathGenerator.php @@ -12,8 +12,7 @@ namespace FriendsOfHyperf\Jet\PathGenerator; use FriendsOfHyperf\Jet\Contract\PathGeneratorInterface; - -use function FriendsOfHyperf\Jet\str_snake; +use FriendsOfHyperf\Jet\Support\Str; class PathGenerator implements PathGeneratorInterface { @@ -22,7 +21,7 @@ public function generate(string $service, string $method): string $handledNamespace = explode('\\', $service); $handledNamespace = str_replace('\\', '/', end($handledNamespace)); $handledNamespace = str_replace('Service', '', $handledNamespace); - $path = str_snake($handledNamespace); + $path = Str::snake($handledNamespace); if ($path[0] !== '/') { $path = '/' . $path; diff --git a/src/Registry/ConsulRegistry.php b/src/Registry/ConsulRegistry.php index 67765a5..b4534b4 100644 --- a/src/Registry/ConsulRegistry.php +++ b/src/Registry/ConsulRegistry.php @@ -17,11 +17,11 @@ use FriendsOfHyperf\Jet\Contract\RegistryInterface; use FriendsOfHyperf\Jet\LoadBalancer\Node; use FriendsOfHyperf\Jet\LoadBalancer\RoundRobin; +use FriendsOfHyperf\Jet\Support\Arr; use FriendsOfHyperf\Jet\Transporter\GuzzleHttpTransporter; use FriendsOfHyperf\Jet\Transporter\StreamSocketTransporter; use GuzzleHttp\Client; -use function FriendsOfHyperf\Jet\array_get; use function FriendsOfHyperf\Jet\retry; use function FriendsOfHyperf\Jet\with; @@ -120,21 +120,21 @@ public function getServiceNodes(string $service, ?string $protocol = null) $nodes = []; foreach ($serviceNodes as $node) { - if (array_get($node, 'Checks.1.Status') != 'passing') { + if (Arr::get($node, 'Checks.1.Status') != 'passing') { continue; } - if (! is_null($protocol) && $protocol != array_get($node, 'Service.Meta.Protocol')) { + if (! is_null($protocol) && $protocol != Arr::get($node, 'Service.Meta.Protocol')) { continue; } $nodes[] = new Node( - array_get($node, 'Service.Address'), - (int) array_get($node, 'Service.Port'), + Arr::get($node, 'Service.Address'), + (int) Arr::get($node, 'Service.Port'), 1, [ - 'type' => array_get($node, 'Checks.1.Type'), - 'protocol' => array_get($node, 'Service.Meta.Protocol'), + 'type' => Arr::get($node, 'Checks.1.Type'), + 'protocol' => Arr::get($node, 'Service.Meta.Protocol'), ] ); } diff --git a/src/Support/Arr.php b/src/Support/Arr.php new file mode 100644 index 0000000..afc1d8e --- /dev/null +++ b/src/Support/Arr.php @@ -0,0 +1,111 @@ +offsetExists($key); + } + + return array_key_exists($key, $array); + } + + /** + * @param mixed $value + * @return bool + */ + public static function accessible($value) + { + return is_array($value) || $value instanceof \ArrayAccess; + } +} diff --git a/src/Support/Str.php b/src/Support/Str.php new file mode 100644 index 0000000..e17bb4b --- /dev/null +++ b/src/Support/Str.php @@ -0,0 +1,87 @@ + $replace + * @param string $subject + * @return string + */ + public static function replaceArray($search, $replace, $subject) + { + foreach ($replace as $value) { + $subject = self::replaceFirst($search, $value, $subject); + } + + return $subject; + } +} From be7a17200acf8971f11e089185f22eb4ce77ff68 Mon Sep 17 00:00:00 2001 From: Deeka Wong Date: Sun, 11 Aug 2024 21:19:55 +0800 Subject: [PATCH 21/43] Optimized timeout of transporter (#22) * Optimized timeout of transporter Co-Authored-By: Deeka Wong <8337659+huangdijia@users.noreply.github.com> * chore: Remove unused timeout property in StreamSocketTransporter * feat: Update GuzzleHttpTransporter timeout handling The code changes in `GuzzleHttpTransporter.php` update the timeout handling in the constructor. The `timeout` value is now retrieved from the `$config` array and assigned to the transporter's `timeout` property. This change ensures that the timeout value is correctly set when creating a new instance of `GuzzleHttpTransporter`. Based on recent user commits and repository commits, it seems that there have been optimizations and fixes related to transporters and helper functions. However, these changes are not directly related to the current code changes in `GuzzleHttpTransporter.php`. Please note that the suggested commit message follows the format "feat: ". Feel free to modify it according to your repository's commit message conventions. * feat: Update GuzzleHttpTransporter timeout handling * feat: Update GuzzleHttpTransporter timeout handling * feat: Update GuzzleHttpTransporter timeout handling --------- Co-authored-by: Deeka Wong <8337659+huangdijia@users.noreply.github.com> --- src/Transporter/GuzzleHttpTransporter.php | 4 ++-- src/Transporter/StreamSocketTransporter.php | 13 ++++--------- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/src/Transporter/GuzzleHttpTransporter.php b/src/Transporter/GuzzleHttpTransporter.php index 509f541..57af769 100644 --- a/src/Transporter/GuzzleHttpTransporter.php +++ b/src/Transporter/GuzzleHttpTransporter.php @@ -35,8 +35,8 @@ class GuzzleHttpTransporter extends AbstractTransporter public function __construct(string $host = '', int $port = 9501, array $config = []) { - $this->host = $host; - $this->port = $port; + parent::__construct($host, $port); + $this->config = array_merge_recursive($config, [ 'headers' => [ 'Content-Type' => 'application/json', diff --git a/src/Transporter/StreamSocketTransporter.php b/src/Transporter/StreamSocketTransporter.php index 396962f..b9a0f51 100644 --- a/src/Transporter/StreamSocketTransporter.php +++ b/src/Transporter/StreamSocketTransporter.php @@ -24,11 +24,6 @@ class StreamSocketTransporter extends AbstractTransporter */ protected $client; - /** - * @var int - */ - protected $timeout; - /** * @var bool */ @@ -69,19 +64,19 @@ public function recv() public function receive() { $buf = ''; - $timeout = 1000; + $timeoutMs = $this->timeout > 0 ? $this->timeout * 1000 : 1000; stream_set_blocking($this->client, false); // The maximum number of retries is 12, and 1000 microseconds is the minimum waiting time. // The waiting time is doubled each time until the server writes data to the buffer. // Usually, the data can be obtained within 1 microsecond. - $result = retry(12, function () use (&$buf, &$timeout) { + $result = retry(12, function () use (&$buf, &$timeoutMs) { $read = [$this->client]; $write = null; $except = null; - while (stream_select($read, $write, $except, 0, $timeout)) { + while (stream_select($read, $write, $except, 0, $timeoutMs)) { foreach ($read as $r) { $res = fread($r, 8192); if (feof($r)) { @@ -92,7 +87,7 @@ public function receive() } if (! $buf) { - $timeout *= 2; + $timeoutMs *= 2; throw new RecvFailedException('No data was received'); } From 9b842992668548dec41a28f0b699be1ef19bbbd8 Mon Sep 17 00:00:00 2001 From: Deeka Wong Date: Sun, 11 Aug 2024 21:39:10 +0800 Subject: [PATCH 22/43] Optimize config of GuzzleHttpClient (#24) * Optimize config of GuzzleHttpClient * feat: Update GuzzleHttpTransporter configuration handling The code changes in `GuzzleHttpTransporter.php` update the configuration handling in the constructor. The `$config` array is now merged with a default configuration array using `array_replace()`. This change ensures that the transporter has the correct configuration values when creating a new instance of `GuzzleHttpTransporter`. Co-authored-by: Deeka Wong <8337659+huangdijia@users.noreply.github.com> --------- Co-authored-by: Deeka Wong <8337659+huangdijia@users.noreply.github.com> --- src/Transporter/GuzzleHttpTransporter.php | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/Transporter/GuzzleHttpTransporter.php b/src/Transporter/GuzzleHttpTransporter.php index 57af769..192eec6 100644 --- a/src/Transporter/GuzzleHttpTransporter.php +++ b/src/Transporter/GuzzleHttpTransporter.php @@ -37,15 +37,16 @@ public function __construct(string $host = '', int $port = 9501, array $config = { parent::__construct($host, $port); - $this->config = array_merge_recursive($config, [ - 'headers' => [ - 'Content-Type' => 'application/json', - 'X-Real-Ip' => $_SERVER['SERVER_ADDR'] ?? '', - 'X-Forwarded-For' => $_SERVER['REMOTE_ADDR'] ?? '', - 'User-Agent' => UserAgent::get(), - ], + $this->config['headers'] = array_replace([ + 'Content-Type' => 'application/json', + 'X-Real-Ip' => $_SERVER['SERVER_ADDR'] ?? '', + 'X-Forwarded-For' => $_SERVER['REMOTE_ADDR'] ?? '', + 'User-Agent' => UserAgent::get(), + ], $config['headers'] ?? []); + $this->config = array_replace([ 'http_errors' => false, - ]); + 'timeout' => $this->timeout, + ], $config); } public function send(string $data) From 779f0fd601bdfdc8310b23969dc6ffc27a6b1879 Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Sun, 11 Aug 2024 21:43:45 +0800 Subject: [PATCH 23/43] feat: Update GuzzleHttpTransporter configuration handling The code changes in `GuzzleHttpTransporter.php` update the configuration handling in the constructor. The `$config` array is now merged with a default configuration array using `array_replace()`. This change ensures that the transporter has the correct configuration values when creating a new instance of `GuzzleHttpTransporter`. --- src/Transporter/GuzzleHttpTransporter.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Transporter/GuzzleHttpTransporter.php b/src/Transporter/GuzzleHttpTransporter.php index 192eec6..d561645 100644 --- a/src/Transporter/GuzzleHttpTransporter.php +++ b/src/Transporter/GuzzleHttpTransporter.php @@ -37,16 +37,16 @@ public function __construct(string $host = '', int $port = 9501, array $config = { parent::__construct($host, $port); + $this->config = array_replace([ + 'http_errors' => false, + 'timeout' => $this->timeout, + ], $this->config); $this->config['headers'] = array_replace([ 'Content-Type' => 'application/json', 'X-Real-Ip' => $_SERVER['SERVER_ADDR'] ?? '', 'X-Forwarded-For' => $_SERVER['REMOTE_ADDR'] ?? '', 'User-Agent' => UserAgent::get(), - ], $config['headers'] ?? []); - $this->config = array_replace([ - 'http_errors' => false, - 'timeout' => $this->timeout, - ], $config); + ], $this->config['headers'] ?? []); } public function send(string $data) From 987f436ffb2b8790487a068647655fbbe76c4476 Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Sun, 11 Aug 2024 21:46:39 +0800 Subject: [PATCH 24/43] feat: Update GuzzleHttpTransporter configuration handling --- src/Transporter/GuzzleHttpTransporter.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Transporter/GuzzleHttpTransporter.php b/src/Transporter/GuzzleHttpTransporter.php index d561645..845e979 100644 --- a/src/Transporter/GuzzleHttpTransporter.php +++ b/src/Transporter/GuzzleHttpTransporter.php @@ -40,13 +40,13 @@ public function __construct(string $host = '', int $port = 9501, array $config = $this->config = array_replace([ 'http_errors' => false, 'timeout' => $this->timeout, - ], $this->config); + ], $config); $this->config['headers'] = array_replace([ 'Content-Type' => 'application/json', 'X-Real-Ip' => $_SERVER['SERVER_ADDR'] ?? '', 'X-Forwarded-For' => $_SERVER['REMOTE_ADDR'] ?? '', 'User-Agent' => UserAgent::get(), - ], $this->config['headers'] ?? []); + ], $config['headers'] ?? []); } public function send(string $data) From dd72661dc7f14ba3f83d5b5693780d451def35fe Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Tue, 13 Aug 2024 15:56:26 +0800 Subject: [PATCH 25/43] feat: Update setProtocol, setTransporter, setPacker, setDataFormatter, setPathGenerator, setRegistry, setTries, and setTimeout methods in Metadata class Refactor the setProtocol, setTransporter, setPacker, setDataFormatter, setPathGenerator, setRegistry, setTries, and setTimeout methods in the Metadata class to include a return type annotation of $this. This change improves the code readability and maintainability by explicitly indicating that these methods return an instance of the Metadata class. Co-authored-by: Deeka Wong <8337659+huangdijia@users.noreply.github.com> --- src/Metadata.php | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/Metadata.php b/src/Metadata.php index b8064b9..b0462bc 100644 --- a/src/Metadata.php +++ b/src/Metadata.php @@ -83,10 +83,13 @@ public function getName() /** * Set protocol. + * @return $this */ public function setProtocol(string $protocol) { $this->protocol = $protocol; + + return $this; } /** @@ -100,10 +103,13 @@ public function getProtocol() /** * Set transporter. + * @return $this */ public function setTransporter(TransporterInterface $transporter) { $this->transporter = $transporter; + + return $this; } /** @@ -125,10 +131,13 @@ public function getTransporter() /** * Set packer. + * @return $this */ public function setPacker(PackerInterface $packer) { $this->packer = $packer; + + return $this; } /** @@ -146,10 +155,13 @@ public function getPacker() /** * Set data formatter. + * @return $this */ public function setDataFormatter(DataFormatterInterface $dataFormatter) { $this->dataFormatter = $dataFormatter; + + return $this; } /** @@ -167,10 +179,13 @@ public function getDataFormatter() /** * Set path generator. + * @return $this */ public function setPathGenerator(PathGeneratorInterface $pathGenerator) { $this->pathGenerator = $pathGenerator; + + return $this; } /** @@ -188,10 +203,13 @@ public function getPathGenerator() /** * Set registry. + * @return $this */ public function setRegistry(RegistryInterface $registry) { $this->registry = $registry; + + return $this; } /** @@ -205,10 +223,13 @@ public function getRegistry() /** * Set tries. + * @return $this */ public function setTries(int $tries) { $this->tries = $tries; + + return $this; } /** @@ -222,10 +243,13 @@ public function getTries() /** * Set timeout. + * @return $this */ public function setTimeout(int $timeout) { $this->timeout = $timeout; + + return $this; } /** From 2547b6558eaf644dad6eed47fbc790823abf16cf Mon Sep 17 00:00:00 2001 From: Deeka Wong Date: Wed, 14 Aug 2024 11:40:19 +0800 Subject: [PATCH 26/43] Adds MetadataManager (#27) * Adds MetadataManager * feat: Refactor MetadataManager to use static methods for registration and retrieval The code changes in `MetadataManager.php` remove the unnecessary comments and update the `register` and `get` methods to be static. This refactor improves the code readability and maintainability by clearly indicating that these methods can be accessed directly on the class without needing an instance. Co-authored-by: Deeka Wong <8337659+huangdijia@users.noreply.github.com> * feat: Update MetadataManager to use static methods for registration and retrieval * feat: Update ClientTest to use service parameter in ClientFactory The code changes in `ClientTest.php` update the `ClientFactory::create()` method to pass the `this->service` parameter as the first argument instead of `'test'`. This change ensures that the correct service is used when creating a new instance of the client. --------- Co-authored-by: Deeka Wong <8337659+huangdijia@users.noreply.github.com> --- src/ClientFactory.php | 80 ++++++++++++++++++++++++----------------- src/Metadata.php | 13 ++++++- src/MetadataManager.php | 33 +++++++++++++++++ tests/ClientTest.php | 17 +++++++++ 4 files changed, 110 insertions(+), 33 deletions(-) create mode 100644 src/MetadataManager.php diff --git a/src/ClientFactory.php b/src/ClientFactory.php index 035a376..0583410 100644 --- a/src/ClientFactory.php +++ b/src/ClientFactory.php @@ -24,38 +24,54 @@ class ClientFactory * @throws \InvalidArgumentException * @throws \Exception */ - public static function create(string $service, $transporter = null, ?PackerInterface $packer = null, ?DataFormatterInterface $dataFormatter = null, ?PathGeneratorInterface $pathGenerator = null, ?int $tries = null): Client - { - if (! $metadata = ServiceManager::get($service)) { - $metadata = new Metadata($service); - - if (RegistryManager::isRegistered(RegistryManager::DEFAULT)) { - $metadata->setRegistry(RegistryManager::get(RegistryManager::DEFAULT)); - } - - if ($transporter instanceof TransporterInterface) { - $metadata->setTransporter($transporter); - } elseif (is_numeric($transporter)) { - $metadata->setTimeout($transporter); - } elseif (is_string($transporter)) { - $metadata->setProtocol($transporter); - } - - if ($packer) { - $metadata->setPacker($packer); - } - - if ($dataFormatter) { - $metadata->setDataFormatter($dataFormatter); - } - - if ($pathGenerator) { - $metadata->setPathGenerator($pathGenerator); - } - - if ($tries) { - $metadata->setTries($tries); - } + public static function create( + string $service, + $transporter = null, + ?PackerInterface $packer = null, + ?DataFormatterInterface $dataFormatter = null, + ?PathGeneratorInterface $pathGenerator = null, + ?int $tries = null + ): Client { + if ($metadata = ServiceManager::get($service)) { + return new Client($metadata); + } + + if ( + func_num_args() == 2 + && is_string($transporter) + && $metadata = MetadataManager::get($transporter) + ) { + return new Client($metadata->withName($service)); + } + + $metadata = new Metadata($service); + + if (RegistryManager::isRegistered(RegistryManager::DEFAULT)) { + $metadata->setRegistry(RegistryManager::get(RegistryManager::DEFAULT)); + } + + if ($transporter instanceof TransporterInterface) { + $metadata->setTransporter($transporter); + } elseif (is_numeric($transporter)) { + $metadata->setTimeout($transporter); + } elseif (is_string($transporter)) { + $metadata->setProtocol($transporter); + } + + if ($packer) { + $metadata->setPacker($packer); + } + + if ($dataFormatter) { + $metadata->setDataFormatter($dataFormatter); + } + + if ($pathGenerator) { + $metadata->setPathGenerator($pathGenerator); + } + + if ($tries) { + $metadata->setTries($tries); } return new Client($metadata); diff --git a/src/Metadata.php b/src/Metadata.php index b0462bc..a4f162b 100644 --- a/src/Metadata.php +++ b/src/Metadata.php @@ -67,11 +67,22 @@ class Metadata */ protected $timeout = 3; - public function __construct(string $name) + public function __construct(string $name = '') { $this->name = $name; } + /** + * @return static + */ + public function withName(string $name) + { + $clone = clone $this; + $clone->name = $name; + + return $clone; + } + /** * Get name. * @return string diff --git a/src/MetadataManager.php b/src/MetadataManager.php new file mode 100644 index 0000000..d86ae69 --- /dev/null +++ b/src/MetadataManager.php @@ -0,0 +1,33 @@ + + */ + protected static $metadata = []; + + public static function register(string $name, Metadata $metadata) + { + static::$metadata[$name] = $metadata; + } + + /** + * @return null|Metadata + */ + public static function get(string $name) + { + return isset(static::$metadata[$name]) ? clone static::$metadata[$name] : null; + } +} diff --git a/tests/ClientTest.php b/tests/ClientTest.php index 0210142..da5403b 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -12,6 +12,8 @@ namespace FriendsOfHyperf\Jet\Tests; use FriendsOfHyperf\Jet\ClientFactory; +use FriendsOfHyperf\Jet\Metadata; +use FriendsOfHyperf\Jet\MetadataManager; use FriendsOfHyperf\Jet\RegistryManager; /** @@ -62,4 +64,19 @@ public function testCalculatorServiceByStreamSocketTransporter() $this->assertSame($a + $b, $client->add($a, $b)); } + + public function testMetadataManager() + { + MetadataManager::register( + $name = 'test', + (new Metadata())->setTransporter($this->createGuzzleHttpTransporter()) + ); + + $client = ClientFactory::create($this->service, $name); + + $a = rand(1, 99); + $b = rand(1, 99); + + $this->assertSame($a + $b, $client->add($a, $b)); + } } From 14e18c8e1af98c3d5d419d253320714b36d930f3 Mon Sep 17 00:00:00 2001 From: Deeka Wong Date: Wed, 14 Aug 2024 12:38:47 +0800 Subject: [PATCH 27/43] Optimized Metadata (#30) Co-authored-by: Deeka Wong <8337659+huangdijia@users.noreply.github.com> --- .gitattributes | 5 +++ README.md | 16 +++----- src/Metadata.php | 93 ++++++++++++++++++++++++++++++++++++++++++++ tests/ClientTest.php | 2 +- 4 files changed, 105 insertions(+), 11 deletions(-) create mode 100644 .gitattributes diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..d5071af --- /dev/null +++ b/.gitattributes @@ -0,0 +1,5 @@ +/.github export-ignore +/.vscode export-ignore +/tests export-ignore +.gitattributes export-ignore +/bin export-ignore \ No newline at end of file diff --git a/README.md b/README.md index cb1792e..83187a8 100644 --- a/README.md +++ b/README.md @@ -25,9 +25,9 @@ use FriendsOfHyperf\Jet\ServiceManager; use FriendsOfHyperf\Jet\Registry\ConsulRegistry; use FriendsOfHyperf\Jet\Transporter\GuzzleHttpTransporter; -$metadata = new Metadata('CalculatorService'); -$metadata->setTransporter(new GuzzleHttpTransporter('127.0.0.1', 9502)); -$metadata->setRegistry(new ConsulRegistry(['uri' => 'http://127.0.0.1:8500'])); +$metadata = (new Metadata('CalculatorService')) + ->withTransporter(new GuzzleHttpTransporter('127.0.0.1', 9502)) + ->withRegistry(new ConsulRegistry(['uri' => 'http://127.0.0.1:8500'])); ServiceManager::register('CalculatorService', $metadata); ``` @@ -68,13 +68,9 @@ class CalculatorService extends Client { public function __construct($service = 'CalculatorService') { - $metadata = new Metadata($service); - - // Custom transporter - $metadata->setTransporter(new GuzzleHttpTransporter('127.0.0.1', 9502)); - - // Custom registry - $metadata->setRegistry(new ConsulRegistry(['uri' => 'http://127.0.0.1:8500'])); + $metadata = (new Metadata($service)) + ->withTransporter(new GuzzleHttpTransporter('127.0.0.1', 9502)) + ->withRegistry(new ConsulRegistry(['uri' => 'http://127.0.0.1:8500'])); parent::__construct($metadata); } diff --git a/src/Metadata.php b/src/Metadata.php index a4f162b..44ab3f1 100644 --- a/src/Metadata.php +++ b/src/Metadata.php @@ -92,8 +92,20 @@ public function getName() return $this->name; } + /** + * @return static + */ + public function withProtocol(string $protocol) + { + $clone = clone $this; + $clone->protocol = $protocol; + + return $clone; + } + /** * Set protocol. + * @deprecated use withProtocol instead * @return $this */ public function setProtocol(string $protocol) @@ -112,8 +124,20 @@ public function getProtocol() return $this->protocol; } + /** + * @return static + */ + public function withTransporter(TransporterInterface $transporter) + { + $clone = clone $this; + $clone->transporter = $transporter; + + return $clone; + } + /** * Set transporter. + * @deprecated use withTransporter instead * @return $this */ public function setTransporter(TransporterInterface $transporter) @@ -140,8 +164,20 @@ public function getTransporter() throw new \RuntimeException('Transporter not registered yet.'); } + /** + * @return static + */ + public function withPacker(PackerInterface $packer) + { + $clone = clone $this; + $clone->packer = $packer; + + return $clone; + } + /** * Set packer. + * @deprecated use withPacker instead * @return $this */ public function setPacker(PackerInterface $packer) @@ -164,8 +200,20 @@ public function getPacker() return $this->packer; } + /** + * @return static + */ + public function withDataFormatter(DataFormatterInterface $dataFormatter) + { + $clone = clone $this; + $clone->dataFormatter = $dataFormatter; + + return $clone; + } + /** * Set data formatter. + * @deprecated use withDataFormatter instead * @return $this */ public function setDataFormatter(DataFormatterInterface $dataFormatter) @@ -188,6 +236,17 @@ public function getDataFormatter() return $this->dataFormatter; } + /** + * @return static + */ + public function withPathGenerator(PathGeneratorInterface $pathGenerator) + { + $clone = clone $this; + $clone->pathGenerator = $pathGenerator; + + return $clone; + } + /** * Set path generator. * @return $this @@ -212,8 +271,20 @@ public function getPathGenerator() return $this->pathGenerator; } + /** + * @return static + */ + public function withRegistry(RegistryInterface $registry) + { + $clone = clone $this; + $clone->registry = $registry; + + return $clone; + } + /** * Set registry. + * @deprecated use withRegistry instead * @return $this */ public function setRegistry(RegistryInterface $registry) @@ -232,6 +303,17 @@ public function getRegistry() return $this->registry; } + /** + * @return static + */ + public function withTries(int $tries) + { + $clone = clone $this; + $clone->tries = $tries; + + return $clone; + } + /** * Set tries. * @return $this @@ -252,6 +334,17 @@ public function getTries() return (int) $this->tries; } + /** + * @return static + */ + public function withTimeout(int $timeout) + { + $clone = clone $this; + $clone->timeout = $timeout; + + return $clone; + } + /** * Set timeout. * @return $this diff --git a/tests/ClientTest.php b/tests/ClientTest.php index da5403b..9b5505b 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -69,7 +69,7 @@ public function testMetadataManager() { MetadataManager::register( $name = 'test', - (new Metadata())->setTransporter($this->createGuzzleHttpTransporter()) + (new Metadata())->withTransporter($this->createGuzzleHttpTransporter()) ); $client = ClientFactory::create($this->service, $name); From a37189f4ac857904ce29446fb5af2d6aadb52ae0 Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Wed, 14 Aug 2024 12:56:57 +0800 Subject: [PATCH 28/43] Updated phpdoc --- src/Metadata.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Metadata.php b/src/Metadata.php index 44ab3f1..9113e49 100644 --- a/src/Metadata.php +++ b/src/Metadata.php @@ -105,7 +105,7 @@ public function withProtocol(string $protocol) /** * Set protocol. - * @deprecated use withProtocol instead + * @deprecated use withProtocol instead, will be removed in v4.0 * @return $this */ public function setProtocol(string $protocol) @@ -137,7 +137,7 @@ public function withTransporter(TransporterInterface $transporter) /** * Set transporter. - * @deprecated use withTransporter instead + * @deprecated use withTransporter instead, will be removed in v4.0 * @return $this */ public function setTransporter(TransporterInterface $transporter) @@ -177,7 +177,7 @@ public function withPacker(PackerInterface $packer) /** * Set packer. - * @deprecated use withPacker instead + * @deprecated use withPacker instead, will be removed in v4.0 * @return $this */ public function setPacker(PackerInterface $packer) @@ -213,7 +213,7 @@ public function withDataFormatter(DataFormatterInterface $dataFormatter) /** * Set data formatter. - * @deprecated use withDataFormatter instead + * @deprecated use withDataFormatter instead, will be removed in v4.0 * @return $this */ public function setDataFormatter(DataFormatterInterface $dataFormatter) @@ -284,7 +284,7 @@ public function withRegistry(RegistryInterface $registry) /** * Set registry. - * @deprecated use withRegistry instead + * @deprecated use withRegistry instead, will be removed in v4.0 * @return $this */ public function setRegistry(RegistryInterface $registry) From e1593c19de23f568b398bc1321e56b01fe350dae Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Fri, 16 Aug 2024 13:11:00 +0800 Subject: [PATCH 29/43] Optimized ClientFactory --- src/ClientFactory.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/ClientFactory.php b/src/ClientFactory.php index 0583410..8691c6c 100644 --- a/src/ClientFactory.php +++ b/src/ClientFactory.php @@ -47,31 +47,31 @@ public static function create( $metadata = new Metadata($service); if (RegistryManager::isRegistered(RegistryManager::DEFAULT)) { - $metadata->setRegistry(RegistryManager::get(RegistryManager::DEFAULT)); + $metadata = $metadata->withRegistry(RegistryManager::get(RegistryManager::DEFAULT)); } if ($transporter instanceof TransporterInterface) { - $metadata->setTransporter($transporter); + $metadata = $metadata->withTransporter($transporter); } elseif (is_numeric($transporter)) { - $metadata->setTimeout($transporter); + $metadata = $metadata->withTimeout($transporter); } elseif (is_string($transporter)) { - $metadata->setProtocol($transporter); + $metadata = $metadata->withProtocol($transporter); } if ($packer) { - $metadata->setPacker($packer); + $metadata = $metadata->withPacker($packer); } if ($dataFormatter) { - $metadata->setDataFormatter($dataFormatter); + $metadata = $metadata->withDataFormatter($dataFormatter); } if ($pathGenerator) { - $metadata->setPathGenerator($pathGenerator); + $metadata = $metadata->withPathGenerator($pathGenerator); } if ($tries) { - $metadata->setTries($tries); + $metadata = $metadata->withTries($tries); } return new Client($metadata); From b7b43f332220a8c184995f8b5dcc91efb723f5f4 Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Wed, 28 Aug 2024 08:20:58 +0800 Subject: [PATCH 30/43] feat: Update return type annotation in StreamSocketTransporter.php Refactor the return type annotation in the `StreamSocketTransporter.php` file to use the `array{string, int}` type instead of `(int|string)[]`. This change improves the code readability and maintainability by explicitly indicating the expected return type. Co-authored-by: Deeka Wong <8337659+huangdijia@users.noreply.github.com> --- src/Transporter/StreamSocketTransporter.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Transporter/StreamSocketTransporter.php b/src/Transporter/StreamSocketTransporter.php index b9a0f51..0e32651 100644 --- a/src/Transporter/StreamSocketTransporter.php +++ b/src/Transporter/StreamSocketTransporter.php @@ -103,7 +103,7 @@ public function receive() } /** - * @return (int|string)[] + * @return array{string, int} * @throws \InvalidArgumentException * @throws \Exception */ From 275708b5dd4d0dbe836f3d50f5a7ac1b408fae2c Mon Sep 17 00:00:00 2001 From: Deeka Wong Date: Wed, 28 Aug 2024 09:54:43 +0800 Subject: [PATCH 31/43] feat: Retry connection in StreamSocketTransporter.php (#35) Retry the connection in the `StreamSocketTransporter.php` file when establishing a stream socket client. This change improves the reliability of the connection by attempting to connect multiple times before throwing a connection exception. Co-authored-by: Deeka Wong <8337659+huangdijia@users.noreply.github.com> --- src/Transporter/StreamSocketTransporter.php | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/Transporter/StreamSocketTransporter.php b/src/Transporter/StreamSocketTransporter.php index 0e32651..76e98af 100644 --- a/src/Transporter/StreamSocketTransporter.php +++ b/src/Transporter/StreamSocketTransporter.php @@ -131,21 +131,24 @@ protected function connect() if ($this->isConnected) { return; } + if ($this->client) { fclose($this->client); unset($this->client); } - [$host, $port] = $this->getTarget(); + retry(5, function() { + [$host, $port] = $this->getTarget(); - $client = stream_socket_client("tcp://{$host}:{$port}", $errno, $errstr, $this->timeout); + $client = stream_socket_client("tcp://{$host}:{$port}", $errno, $errstr, $this->timeout); - if ($client === false) { - throw new ConnectionException(sprintf('[%d] %s', $errno, $errstr)); - } + if ($client === false) { + throw new ConnectionException(sprintf('[%d] %s', $errno, $errstr)); + } - $this->client = $client; - $this->isConnected = true; + $this->client = $client; + $this->isConnected = true; + }); } protected function close() From 401bcf59b9d586be7ea652067979584a5a839dc9 Mon Sep 17 00:00:00 2001 From: guandeng Date: Thu, 9 Oct 2025 10:32:20 +0800 Subject: [PATCH 32/43] Fix: Ensure unpacked data is an array before using array_key_exists() (#39) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Fix: Ensure unpacked data is an array before using array_key_exists() * Add validation for unpacked data in Client Throws a RecvFailedException if the unpacked data is not an array, improving error handling for invalid responses. * Fix: Ensure unpacked data is treated as an array in Client * chore: Add allow-plugins configuration for composer-normalize * Fix: Ensure unpacked data is treated as an array and improve error handling in Client * Fix: Improve error message for invalid data in ServerException --------- Co-authored-by: 范冠登 Co-authored-by: Deeka Wong <8337659+huangdijia@users.noreply.github.com> --- composer.json | 5 ++++- src/Client.php | 4 ++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index c25ce0a..7673da2 100644 --- a/composer.json +++ b/composer.json @@ -31,7 +31,10 @@ } }, "config": { - "sort-packages": true + "sort-packages": true, + "allow-plugins": { + "ergebnis/composer-normalize": true + } }, "suggest": { "swoole": ">=4.6.0" diff --git a/src/Client.php b/src/Client.php index 6396414..bedb535 100644 --- a/src/Client.php +++ b/src/Client.php @@ -63,12 +63,12 @@ public function __call($name, $arguments) throw new RecvFailedException('Recv failed'); } - return with($packer->unpack($ret), function ($data) { + return with((array) $packer->unpack($ret), function ($data) use ($ret) { if (array_key_exists('result', $data)) { return $data['result']; } - throw new ServerException($data['error'] ?? []); + throw new ServerException($data['error'] ?? ['code' => 0, 'message' => 'Invalid data: ' . $ret]); }); }; From 91fed247cd1f8425b3856050f3647edad77b8ff8 Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Mon, 22 Jun 2026 16:05:35 +0800 Subject: [PATCH 33/43] =?UTF-8?q?=E2=9C=A8=20feat(transporter):=20add=20Mu?= =?UTF-8?q?ltiplexRpcTransporter?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Transporter/MultiplexRpcTransporter.php | 86 +++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 src/Transporter/MultiplexRpcTransporter.php diff --git a/src/Transporter/MultiplexRpcTransporter.php b/src/Transporter/MultiplexRpcTransporter.php new file mode 100644 index 0000000..cfd220e --- /dev/null +++ b/src/Transporter/MultiplexRpcTransporter.php @@ -0,0 +1,86 @@ +client, false); + + while (true) { + $header = $this->readBytes(4); + + $unpacked = unpack('Nlength', $header); + $length = $unpacked['length']; + + if ($length < 4) { + throw new RecvFailedException(sprintf('Invalid package length: %d', $length)); + } + $body = $this->readBytes($length); + if (in_array($body, [self::PING, self::PONG], true)) { + continue; + } + + return $header . $body; + } + } + + /** + * @throws Exception + */ + private function readBytes(int $length): string + { + $buffer = ''; + + while (strlen($buffer) < $length) { + $read = [$this->client]; + $write = null; + $except = null; + + $selected = stream_select($read, $write, $except, $this->timeout); + if ($selected === false) { + throw new RuntimeException('Failed to select stream.'); + } + + if ($selected === 0) { + throw new RecvFailedException('Receive timeout.'); + } + + foreach ($read as $stream) { + /** @var false|string $chunk */ + $chunk = fread($stream, $length - strlen($buffer)); + + if ($chunk === false) { + throw new RecvFailedException('Receive failed.'); + } + + if ($chunk === '' && feof($stream)) { + throw new ConnectionException('Connection was closed.'); + } + + $buffer .= $chunk; + } + } + + return $buffer; + } +} From d72db5c2f53158aeae750ef44f4e872521b31629 Mon Sep 17 00:00:00 2001 From: siam Date: Mon, 22 Jun 2026 16:48:34 +0800 Subject: [PATCH 34/43] add tests --- tests/ClientTest.php | 10 ++++++++++ tests/TestCase.php | 6 ++++++ 2 files changed, 16 insertions(+) diff --git a/tests/ClientTest.php b/tests/ClientTest.php index 9b5505b..1fb2ea4 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -65,6 +65,16 @@ public function testCalculatorServiceByStreamSocketTransporter() $this->assertSame($a + $b, $client->add($a, $b)); } + public function testCalculatorServiceByMultiplexRpcTransporter() + { + $client = ClientFactory::create($this->service, $this->createMultiplexRpcTransporter()); + + $a = rand(1, 99); + $b = rand(1, 99); + + $this->assertSame($a + $b, $client->add($a, $b)); + } + public function testMetadataManager() { MetadataManager::register( diff --git a/tests/TestCase.php b/tests/TestCase.php index 95e1f3a..a8b360e 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -13,6 +13,7 @@ use FriendsOfHyperf\Jet\Registry\ConsulRegistry; use FriendsOfHyperf\Jet\Transporter\GuzzleHttpTransporter; +use FriendsOfHyperf\Jet\Transporter\MultiplexRpcTransporter; use FriendsOfHyperf\Jet\Transporter\StreamSocketTransporter; /** @@ -63,6 +64,11 @@ public function createStreamSocketTransporter() return new StreamSocketTransporter($this->jsonrpcHost, $this->jsonrpcPort, $this->jsonrpcTimeout); } + public function createMultiplexRpcTransporter() + { + return new MultiplexRpcTransporter($this->jsonrpcHost, $this->jsonrpcPort, $this->jsonrpcTimeout); + } + protected function createRegistry() { return new ConsulRegistry(['uri' => $this->consulUri, 'timeout' => $this->consulTimeout]); From 39c844f8ad078253b8670617a3c655f5fcd86690 Mon Sep 17 00:00:00 2001 From: siam Date: Mon, 22 Jun 2026 16:53:12 +0800 Subject: [PATCH 35/43] Update TestCase.php --- tests/TestCase.php | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/tests/TestCase.php b/tests/TestCase.php index a8b360e..2c625e0 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -38,6 +38,12 @@ class TestCase extends \PHPUnit\Framework\TestCase private $jsonrpcHttpTimeout; + private $jsonrpcLengthCheckHost; + + private $jsonrpcLengthCheckPort; + + private $jsonrpcLengthCheckTimeout; + public function __construct($name = null, array $data = [], $dataName = '') { parent::__construct($name, $data, $dataName); @@ -52,6 +58,10 @@ public function __construct($name = null, array $data = [], $dataName = '') $this->jsonrpcHttpHost = $_ENV['JSONRPC_HTTP_HOST'] ?? '127.0.0.1'; $this->jsonrpcHttpPort = (int) ($_ENV['JSONRPC_HTTP_PORT'] ?? 9502); $this->jsonrpcHttpTimeout = (int) ($_ENV['JSONRPC_HTTP_TIMEOUT'] ?? 2); + + $this->jsonrpcLengthCheckHost = $_ENV['JSONRPC_LENGTH_CHECK_HOST'] ?? '127.0.0.1'; + $this->jsonrpcLengthCheckPort = (int) ($_ENV['JSONRPC_LENGTH_CHECK_PORT'] ?? 9504); + $this->jsonrpcLengthCheckTimeout = (int) ($_ENV['JSONRPC_LENGTH_CHECK_TIMEOUT'] ?? 2); } public function createGuzzleHttpTransporter() @@ -66,7 +76,7 @@ public function createStreamSocketTransporter() public function createMultiplexRpcTransporter() { - return new MultiplexRpcTransporter($this->jsonrpcHost, $this->jsonrpcPort, $this->jsonrpcTimeout); + return new MultiplexRpcTransporter($this->jsonrpcLengthCheckHost, $this->jsonrpcLengthCheckPort, $this->jsonrpcLengthCheckTimeout); } protected function createRegistry() From 414abf8e9c129c55cedae1e8e86074116fc49a84 Mon Sep 17 00:00:00 2001 From: siam Date: Mon, 22 Jun 2026 16:55:27 +0800 Subject: [PATCH 36/43] Update phpunit.xml.dist --- phpunit.xml.dist | 3 +++ 1 file changed, 3 insertions(+) diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 3b21407..a7028ab 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -27,5 +27,8 @@ + + + From 1447d0cb86c3008d34c794c0484481da01d1c0eb Mon Sep 17 00:00:00 2001 From: siam Date: Mon, 22 Jun 2026 17:24:39 +0800 Subject: [PATCH 37/43] Update MultiplexRpcTransporter.php --- src/Transporter/MultiplexRpcTransporter.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/Transporter/MultiplexRpcTransporter.php b/src/Transporter/MultiplexRpcTransporter.php index cfd220e..bfb6d17 100644 --- a/src/Transporter/MultiplexRpcTransporter.php +++ b/src/Transporter/MultiplexRpcTransporter.php @@ -11,10 +11,8 @@ namespace FriendsOfHyperf\Jet\Transporter; -use Exception; use FriendsOfHyperf\Jet\Exception\ConnectionException; use FriendsOfHyperf\Jet\Exception\RecvFailedException; -use RuntimeException; class MultiplexRpcTransporter extends StreamSocketTransporter { @@ -45,7 +43,7 @@ public function receive() } /** - * @throws Exception + * @throws \Exception */ private function readBytes(int $length): string { @@ -58,7 +56,7 @@ private function readBytes(int $length): string $selected = stream_select($read, $write, $except, $this->timeout); if ($selected === false) { - throw new RuntimeException('Failed to select stream.'); + throw new \RuntimeException('Failed to select stream.'); } if ($selected === 0) { @@ -79,6 +77,7 @@ private function readBytes(int $length): string $buffer .= $chunk; } + var_dump($buffer); } return $buffer; From e946ffbcdbdca83a1df2d6acb4a1a5fb98c941b4 Mon Sep 17 00:00:00 2001 From: siam Date: Mon, 22 Jun 2026 17:37:21 +0800 Subject: [PATCH 38/43] Update ClientTest.php --- tests/ClientTest.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/tests/ClientTest.php b/tests/ClientTest.php index 1fb2ea4..423a2e4 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -12,8 +12,10 @@ namespace FriendsOfHyperf\Jet\Tests; use FriendsOfHyperf\Jet\ClientFactory; +use FriendsOfHyperf\Jet\DataFormatter\MultiplexDataFormatter; use FriendsOfHyperf\Jet\Metadata; use FriendsOfHyperf\Jet\MetadataManager; +use FriendsOfHyperf\Jet\Packer\JsonMultiplexPacker; use FriendsOfHyperf\Jet\RegistryManager; /** @@ -67,7 +69,12 @@ public function testCalculatorServiceByStreamSocketTransporter() public function testCalculatorServiceByMultiplexRpcTransporter() { - $client = ClientFactory::create($this->service, $this->createMultiplexRpcTransporter()); + $client = ClientFactory::create( + $this->service, + $this->createMultiplexRpcTransporter(), + new JsonMultiplexPacker(), + new MultiplexDataFormatter(), + ); $a = rand(1, 99); $b = rand(1, 99); From e42874980bf44a251c594fc92e9032685b19352d Mon Sep 17 00:00:00 2001 From: siam Date: Mon, 22 Jun 2026 17:42:31 +0800 Subject: [PATCH 39/43] Update MultiplexRpcTransporter.php --- src/Transporter/MultiplexRpcTransporter.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Transporter/MultiplexRpcTransporter.php b/src/Transporter/MultiplexRpcTransporter.php index bfb6d17..5b89b04 100644 --- a/src/Transporter/MultiplexRpcTransporter.php +++ b/src/Transporter/MultiplexRpcTransporter.php @@ -77,7 +77,6 @@ private function readBytes(int $length): string $buffer .= $chunk; } - var_dump($buffer); } return $buffer; From c80c9668385322bc12ed8c50417bff18227f286e Mon Sep 17 00:00:00 2001 From: siam Date: Mon, 22 Jun 2026 18:09:19 +0800 Subject: [PATCH 40/43] Update phpunit.xml.dist --- phpunit.xml.dist | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpunit.xml.dist b/phpunit.xml.dist index a7028ab..5b4e2dd 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -28,7 +28,7 @@ - + From abaa1b0274e7b2642a166f7da03f9d118ceb2af2 Mon Sep 17 00:00:00 2001 From: siam Date: Mon, 22 Jun 2026 18:10:11 +0800 Subject: [PATCH 41/43] Update tests.yaml --- .github/workflows/tests.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index a362e62..32e85c1 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/tests.yaml @@ -30,7 +30,7 @@ jobs: run: docker run -d --name=dev-consul -e CONSUL_BIND_INTERFACE=eth0 --net=host consul:1.15.4 - name: Setup Services run: | - docker run -d --name jsonrpc -p 9501:9501 -p 9502:9502 -p 9503:9503 -p 9504:9504 limingxinleo/hyperf-jsonrpc-demo:latest + docker run -d --name jsonrpc -p 9501:9501 -p 9502:9502 -p 9503:9503 -p 9504:9504 -p 9505:9505 friendsofhyperf/hyperf-rpc-demo:latest sleep 10 php ./tests/register.php - name: Run Test Cases From 795a74a4a7779cafa2af7e9fe521032228e02186 Mon Sep 17 00:00:00 2001 From: siam Date: Mon, 22 Jun 2026 18:35:01 +0800 Subject: [PATCH 42/43] Update tests.yaml --- .github/workflows/tests.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index 32e85c1..05aa6e2 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/tests.yaml @@ -30,7 +30,7 @@ jobs: run: docker run -d --name=dev-consul -e CONSUL_BIND_INTERFACE=eth0 --net=host consul:1.15.4 - name: Setup Services run: | - docker run -d --name jsonrpc -p 9501:9501 -p 9502:9502 -p 9503:9503 -p 9504:9504 -p 9505:9505 friendsofhyperf/hyperf-rpc-demo:latest + docker run -d --name jsonrpc -p 9501:9501 -p 9502:9502 -p 9503:9503 -p 9504:9504 -p 9505:9505 iisiam/hyperf-rpc-demo:latest sleep 10 php ./tests/register.php - name: Run Test Cases From 074c186dda7a67775ae7f1d4f7fc5458d13b205b Mon Sep 17 00:00:00 2001 From: siam Date: Mon, 22 Jun 2026 18:38:17 +0800 Subject: [PATCH 43/43] Update ClientTest.php --- tests/ClientTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/ClientTest.php b/tests/ClientTest.php index 423a2e4..2c1ada5 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -73,7 +73,7 @@ public function testCalculatorServiceByMultiplexRpcTransporter() $this->service, $this->createMultiplexRpcTransporter(), new JsonMultiplexPacker(), - new MultiplexDataFormatter(), + new MultiplexDataFormatter() ); $a = rand(1, 99);