From 73a2aae1d45d817c3c588d8066ea8206d4c54c4a Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Wed, 14 Aug 2024 11:33:20 +0800 Subject: [PATCH 1/4] Adds MetadataManager --- src/ClientFactory.php | 80 ++++++++++++++++++++++++----------------- src/Metadata.php | 13 ++++++- src/MetadataManager.php | 38 ++++++++++++++++++++ tests/ClientTest.php | 17 +++++++++ 4 files changed, 115 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..02e543d --- /dev/null +++ b/src/MetadataManager.php @@ -0,0 +1,38 @@ + + */ + protected static array $metadata = []; + + /** + * @param string $name + * @param Metadata $metadata + */ + public static function register(string $name, Metadata $metadata) + { + static::$metadata[$name] = $metadata; + } + + /** + * @param string $name + * @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..8404602 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($name, 'test'); + + $a = rand(1, 99); + $b = rand(1, 99); + + $this->assertSame($a + $b, $client->add($a, $b)); + } } From fd5714567b318c273a5dd8909fa3c70b5e1b37ab Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Wed, 14 Aug 2024 11:33:50 +0800 Subject: [PATCH 2/4] 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> --- src/MetadataManager.php | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/MetadataManager.php b/src/MetadataManager.php index 02e543d..a7864b8 100644 --- a/src/MetadataManager.php +++ b/src/MetadataManager.php @@ -18,17 +18,12 @@ class MetadataManager */ protected static array $metadata = []; - /** - * @param string $name - * @param Metadata $metadata - */ public static function register(string $name, Metadata $metadata) { static::$metadata[$name] = $metadata; } /** - * @param string $name * @return null|Metadata */ public static function get(string $name) From 3e55f294d2af83fb054e73a9c4a509dce6bd0eb2 Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Wed, 14 Aug 2024 11:36:13 +0800 Subject: [PATCH 3/4] feat: Update MetadataManager to use static methods for registration and retrieval --- src/MetadataManager.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/MetadataManager.php b/src/MetadataManager.php index a7864b8..d86ae69 100644 --- a/src/MetadataManager.php +++ b/src/MetadataManager.php @@ -16,7 +16,7 @@ class MetadataManager /** * @var array */ - protected static array $metadata = []; + protected static $metadata = []; public static function register(string $name, Metadata $metadata) { From 6b0df7d0f0393e5ec3614343f2d0e6560c7ef419 Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Wed, 14 Aug 2024 11:38:27 +0800 Subject: [PATCH 4/4] 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. --- tests/ClientTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/ClientTest.php b/tests/ClientTest.php index 8404602..da5403b 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -72,7 +72,7 @@ public function testMetadataManager() (new Metadata())->setTransporter($this->createGuzzleHttpTransporter()) ); - $client = ClientFactory::create($name, 'test'); + $client = ClientFactory::create($this->service, $name); $a = rand(1, 99); $b = rand(1, 99);