From b2e2e1bf04c976855d142805721d757cb173ab39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Wed, 24 Mar 2021 18:32:14 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Rpc/Producer.php | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/Rpc/Producer.php b/Rpc/Producer.php index 496cecc7..ef885809 100644 --- a/Rpc/Producer.php +++ b/Rpc/Producer.php @@ -5,7 +5,11 @@ namespace Rpc; use Exception; +use JetBrains\PhpStorm\ArrayShape; +use ReflectionException; use Snowflake\Abstracts\Component; +use Snowflake\Exception\ComponentException; +use Snowflake\Exception\NotFindClassException; use Snowflake\Snowflake; @@ -112,29 +116,41 @@ class Producer extends Component /** * @param string $name + * @param array|null $config * @return mixed + * @throws ReflectionException + * @throws ComponentException + * @throws NotFindClassException * @throws Exception */ - public function getClient(string $name): Client + public function getClient(string $name, array $config = null): Client { - if (!is_array($producer = $this->producers[$name] ?? null)) { + $producer = $config ?? $this->producers[$name] ?? null; + if ($producer === null) { throw new Exception('Unknown rpc client config.'); } $producerName = $this->getName($name, $producer); $snowflake = Snowflake::app(); if (!$snowflake->has($producerName)) { - return $snowflake->set($producerName, [ - 'class' => Client::class, - 'service' => $name, - 'config' => $producer - ]); + return $snowflake->set($producerName, $this->definer($name, $producer)); } else { return $snowflake->get($producerName); } } + /** + * @param $name + * @param $producer + * @return array + */ + private function definer($name, $producer): array + { + return ['class' => Client::class, 'service' => $name, 'config' => $producer]; + } + + /** * @param $name * @return Client|bool