forked from clue/reactphp-redis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFactory.php
133 lines (113 loc) · 3.9 KB
/
Factory.php
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
namespace Clue\React\Redis;
use React\SocketClient\ConnectorInterface;
use React\Stream\Stream;
use Clue\React\Redis\StreamingClient;
use Clue\Redis\Protocol\Factory as ProtocolFactory;
use React\SocketClient\Connector;
use React\Dns\Resolver\Factory as ResolverFactory;
use InvalidArgumentException;
use React\EventLoop\LoopInterface;
use React\Promise;
class Factory
{
private $connector;
private $protocol;
public function __construct(LoopInterface $loop, ConnectorInterface $connector = null, ProtocolFactory $protocol = null)
{
if ($connector === null) {
$resolverFactory = new ResolverFactory();
$connector = new Connector($loop, $resolverFactory->create('8.8.8.8', $loop));
}
if ($protocol === null) {
$protocol = new ProtocolFactory();
}
$this->connector = $connector;
$this->protocol = $protocol;
}
/**
* create redis client connected to address of given redis instance
*
* @param string|null $target
* @return \React\Promise\PromiseInterface resolves with Client or rejects with \Exception
*/
public function createClient($target = null)
{
try {
$parts = $this->parseUrl($target);
} catch (InvalidArgumentException $e) {
return Promise\reject($e);
}
$protocol = $this->protocol;
$promise = $this->connector->create($parts['host'], $parts['port'])->then(function (Stream $stream) use ($protocol) {
return new StreamingClient($stream, $protocol->createResponseParser(), $protocol->createSerializer());
});
if (isset($parts['auth'])) {
$promise = $promise->then(function (StreamingClient $client) use ($parts) {
return $client->auth($parts['auth'])->then(
function () use ($client) {
return $client;
},
function ($error) use ($client) {
$client->close();
throw $error;
}
);
});
}
if (isset($parts['db'])) {
$promise = $promise->then(function (StreamingClient $client) use ($parts) {
return $client->select($parts['db'])->then(
function () use ($client) {
return $client;
},
function ($error) use ($client) {
$client->close();
throw $error;
}
);
});
}
return $promise;
}
/**
* @param string|null $target
* @return array with keys host, port, auth and db
* @throws InvalidArgumentException
*/
private function parseUrl($target)
{
if ($target === null) {
$target = 'tcp://127.0.0.1';
}
if (strpos($target, '://') === false) {
$target = 'tcp://' . $target;
}
$parts = parse_url($target);
if ($parts === false || !isset($parts['host']) || $parts['scheme'] !== 'tcp') {
throw new InvalidArgumentException('Given URL can not be parsed');
}
if (!isset($parts['port'])) {
$parts['port'] = 6379;
}
if ($parts['host'] === 'localhost') {
$parts['host'] = '127.0.0.1';
}
$auth = null;
if (isset($parts['user'])) {
$auth = $parts['user'];
}
if (isset($parts['pass'])) {
$auth .= ':' . $parts['pass'];
}
if ($auth !== null) {
$parts['auth'] = $auth;
}
if (isset($parts['path']) && $parts['path'] !== '') {
// skip first slash
$parts['db'] = substr($parts['path'], 1);
}
unset($parts['scheme'], $parts['user'], $parts['pass'], $parts['path']);
return $parts;
}
}