From 0e968eafe5e44a18e328bd91c687ba79c93d6dd3 Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Wed, 14 Aug 2024 12:37:28 +0800 Subject: [PATCH] Optimized Metadata --- .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);