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)); + } }