1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Dead simple, high performance, drop-in bridge to Golang RPC with zero dependencies |
5
|
|
|
* |
6
|
|
|
* @author Valentin V |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
declare(strict_types=1); |
10
|
|
|
|
11
|
|
|
namespace Spiral\Goridge; |
12
|
|
|
|
13
|
|
|
use Throwable; |
14
|
|
|
|
15
|
|
|
abstract class Relay |
16
|
|
|
{ |
17
|
|
|
public const TCP_SOCKET = 'tcp'; |
18
|
|
|
public const UNIX_SOCKET = 'unix'; |
19
|
|
|
public const STREAM = 'pipes'; |
20
|
|
|
|
21
|
|
|
private const CONNECTION = '/(?P<protocol>[^:\/]+):\/\/(?P<arg1>[^:]+)(:(?P<arg2>[^:]+))?/'; |
22
|
|
|
|
23
|
|
|
public static function create(string $connection): RelayInterface |
24
|
|
|
{ |
25
|
|
|
if (!preg_match(self::CONNECTION, strtolower($connection), $match)) { |
26
|
|
|
throw new Exceptions\RelayFactoryException('unsupported connection format'); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
switch ($match['protocol']) { |
30
|
|
|
case self::TCP_SOCKET: |
31
|
|
|
//fall through |
32
|
|
|
case self::UNIX_SOCKET: |
33
|
|
|
return new SocketRelay( |
34
|
|
|
$match['arg1'], |
35
|
|
|
isset($match['arg2']) ? (int)$match['arg2'] : null, |
36
|
|
|
$match['protocol'] === self::TCP_SOCKET ? SocketRelay::SOCK_TCP : SocketRelay::SOCK_UNIX |
37
|
|
|
); |
38
|
|
|
|
39
|
|
|
case self::STREAM: |
40
|
|
|
if (!isset($match['arg2'])) { |
41
|
|
|
throw new Exceptions\RelayFactoryException('unsupported stream connection format'); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return new StreamRelay(self::openIn($match['arg1']), self::openOut($match['arg2'])); |
45
|
|
|
default: |
46
|
|
|
throw new Exceptions\RelayFactoryException('unknown connection protocol'); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param string $input |
52
|
|
|
* @return resource |
53
|
|
|
*/ |
54
|
|
|
private static function openIn(string $input) |
55
|
|
|
{ |
56
|
|
|
try { |
57
|
|
|
$resource = fopen("php://$input", 'rb'); |
58
|
|
|
if ($resource === false) { |
59
|
|
|
throw new Exceptions\RelayFactoryException('could not initiate `in` stream resource'); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return $resource; |
63
|
|
|
} catch (Throwable $e) { |
64
|
|
|
throw new Exceptions\RelayFactoryException( |
65
|
|
|
'could not initiate `in` stream resource', |
66
|
|
|
$e->getCode(), |
67
|
|
|
$e |
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param string $output |
74
|
|
|
* @return resource |
75
|
|
|
*/ |
76
|
|
|
private static function openOut(string $output) |
77
|
|
|
{ |
78
|
|
|
try { |
79
|
|
|
$resource = fopen("php://$output", 'wb'); |
80
|
|
|
if ($resource === false) { |
81
|
|
|
throw new Exceptions\RelayFactoryException('could not initiate `out` stream resource'); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $resource; |
85
|
|
|
} catch (Throwable $e) { |
86
|
|
|
throw new Exceptions\RelayFactoryException( |
87
|
|
|
'could not initiate `out` stream resource', |
88
|
|
|
$e->getCode(), |
89
|
|
|
$e |
90
|
|
|
); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|