Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 48 additions & 32 deletions src/ClientFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
13 changes: 12 additions & 1 deletion src/Metadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 33 additions & 0 deletions src/MetadataManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);
/**
* 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
*/

namespace FriendsOfHyperf\Jet;

class MetadataManager
{
/**
* @var array<string, Metadata>
*/
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;
}
}
17 changes: 17 additions & 0 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
namespace FriendsOfHyperf\Jet\Tests;

use FriendsOfHyperf\Jet\ClientFactory;
use FriendsOfHyperf\Jet\Metadata;
use FriendsOfHyperf\Jet\MetadataManager;
use FriendsOfHyperf\Jet\RegistryManager;

/**
Expand Down Expand Up @@ -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));
}
}