Repository navigation
Expand file tree
/
Copy pathClient.php
More file actions
91 lines (75 loc) · 2.89 KB
/
Copy pathClient.php
File metadata and controls
91 lines (75 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact [email protected]
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace Hyperf\RpcMultiplex;
use Hyperf\Contract\IdGeneratorInterface;
use Hyperf\Contract\NormalizerInterface;
use Hyperf\Di\MethodDefinitionCollectorInterface;
use Hyperf\Rpc\Contract\DataFormatterInterface;
use Hyperf\RpcClient\AbstractServiceClient;
use Hyperf\RpcClient\Exception\RequestException;
use Hyperf\RpcMultiplex\Contract\DataFetcherInterface;
use Hyperf\RpcMultiplex\Exception\InvalidArgumentException;
use JetBrains\PhpStorm\ArrayShape;
use Psr\Container\ContainerInterface;
class Client extends AbstractServiceClient
{
protected MethodDefinitionCollectorInterface $methodDefinitionCollector;
protected string $serviceInterface;
/**
* @var DataFetcherInterface&DataFormatterInterface
*/
protected DataFormatterInterface $dataFormatter;
private NormalizerInterface $normalizer;
public function __construct(
ContainerInterface $container,
string $serviceName,
string $protocol = 'jsonrpc-http',
#[ArrayShape([
'service_interface' => 'string',
'load_balancer' => 'string',
])]
array $options = []
) {
$this->serviceName = $serviceName;
$this->protocol = $protocol;
$this->setOptions($options);
parent::__construct($container);
$this->normalizer = $container->get(NormalizerInterface::class);
$this->methodDefinitionCollector = $container->get(MethodDefinitionCollectorInterface::class);
if (! $this->dataFormatter instanceof DataFetcherInterface) {
throw new InvalidArgumentException('The data formatter must instanceof DataFetcherInterface.');
}
}
protected function __request(string $method, array $params, ?string $id = null)
{
if ($this->idGenerator instanceof IdGeneratorInterface && ! $id) {
$id = $this->idGenerator->generate();
}
$response = $this->client->send($this->__generateData($method, $params, $id));
if (! is_array($response)) {
throw new RequestException('Invalid response.');
}
$fetched = $this->dataFormatter->fetch($response);
$type = $this->methodDefinitionCollector->getReturnType($this->serviceInterface, $method);
return $this->normalizer->denormalize($fetched, $type->getName());
}
public function __call(string $method, array $params)
{
return $this->__request($method, $params);
}
protected function setOptions(array $options): void
{
$this->serviceInterface = $options['service_interface'] ?? $this->serviceName;
if (isset($options['load_balancer'])) {
$this->loadBalancer = $options['load_balancer'];
}
}
}