1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of JSON RPC Client. |
4
|
|
|
* |
5
|
|
|
* (c) Igor Lazarev <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Strider2038\JsonRpcClient\Transport; |
12
|
|
|
|
13
|
|
|
use Psr\Log\LoggerInterface; |
14
|
|
|
use Strider2038\JsonRpcClient\Configuration\GeneralOptions; |
15
|
|
|
use Strider2038\JsonRpcClient\Exception\InvalidConfigException; |
16
|
|
|
use Strider2038\JsonRpcClient\Transport\Http\HttpTransportFactory; |
17
|
|
|
use Strider2038\JsonRpcClient\Transport\Socket\SocketClient; |
18
|
|
|
use Strider2038\JsonRpcClient\Transport\Socket\SocketConnector; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @internal |
22
|
|
|
* |
23
|
|
|
* @author Igor Lazarev <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
class MultiTransportFactory implements TransportFactoryInterface |
26
|
|
|
{ |
27
|
|
|
/** @var HttpTransportFactory */ |
28
|
|
|
private $httpTransportFactory; |
29
|
|
|
|
30
|
|
|
/** @var LoggerInterface|null */ |
31
|
|
|
private $logger; |
32
|
|
|
|
33
|
28 |
|
public function __construct(LoggerInterface $logger = null) |
34
|
|
|
{ |
35
|
28 |
|
$this->httpTransportFactory = new HttpTransportFactory(); |
36
|
28 |
|
$this->logger = $logger; |
37
|
28 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @throws InvalidConfigException |
41
|
|
|
*/ |
42
|
28 |
|
public function createTransport(string $connection, GeneralOptions $options): TransportInterface |
43
|
|
|
{ |
44
|
28 |
|
$protocol = $this->parseProtocol($connection); |
45
|
|
|
|
46
|
28 |
|
if ('tcp' === $protocol || 'unix' === $protocol) { |
47
|
12 |
|
$transport = $this->createSocketTransport($connection, $options); |
48
|
16 |
|
} elseif ('http' === $protocol || 'https' === $protocol) { |
49
|
14 |
|
$transport = $this->httpTransportFactory->createTransport($connection, $options); |
50
|
|
|
} else { |
51
|
2 |
|
throw new InvalidConfigException( |
52
|
2 |
|
sprintf( |
53
|
2 |
|
'Unsupported protocol: "%s". Supported protocols: "unix", "tcp", "http", "https".', |
54
|
2 |
|
$protocol |
55
|
|
|
) |
56
|
|
|
); |
57
|
|
|
} |
58
|
|
|
|
59
|
26 |
|
if ($this->logger) { |
60
|
2 |
|
$transport = new TransportLoggingDecorator($transport, $this->logger); |
61
|
|
|
} |
62
|
|
|
|
63
|
26 |
|
return $transport; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @throws InvalidConfigException |
68
|
|
|
*/ |
69
|
12 |
|
private function createSocketTransport(string $connection, GeneralOptions $options): SocketTransport |
70
|
|
|
{ |
71
|
12 |
|
$connector = new SocketConnector(); |
72
|
12 |
|
$client = new SocketClient($connector, $connection, $options->getConnectionOptions(), $options->getRequestTimeoutUs()); |
73
|
|
|
|
74
|
12 |
|
return new SocketTransport($client); |
75
|
|
|
} |
76
|
|
|
|
77
|
28 |
|
private function parseProtocol(string $connection): string |
78
|
|
|
{ |
79
|
28 |
|
$protocol = ''; |
80
|
|
|
|
81
|
28 |
|
if (false !== preg_match('/^(.*):/U', $connection, $matches)) { |
82
|
28 |
|
$protocol = strtolower($matches[1] ?? ''); |
83
|
|
|
} |
84
|
|
|
|
85
|
28 |
|
return $protocol; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|