Skip to content

Commit 541983c

Browse files
committed
Update clue/redis-protocol to v0.2.0 and explicitly pass its arguments
Explicitly pass the protocol factory and serializer to the Client
1 parent 32c11d7 commit 541983c

File tree

5 files changed

+48
-35
lines changed

5 files changed

+48
-35
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"react/promise": "~1",
1515
"react/socket-client": "0.3.*",
1616
"react/event-loop": "0.3.*",
17-
"clue/redis-protocol": "0.1.*"
17+
"clue/redis-protocol": "0.2.*"
1818
},
1919
"autoload": {
2020
"psr-0": { "Clue\\Redis\\React": "src" }

composer.lock

Lines changed: 10 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Clue/Redis/React/Client.php

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44

55
use Evenement\EventEmitter;
66
use React\Stream\Stream;
7-
use Clue\Redis\Protocol\ProtocolInterface;
8-
use Clue\Redis\Protocol\ParserException;
9-
use Clue\Redis\Protocol\ErrorReplyException;
7+
use Clue\Redis\Protocol\Parser\ParserInterface;
8+
use Clue\Redis\Protocol\Parser\ParserException;
9+
use Clue\Redis\Protocol\Model\ErrorReplyException;
10+
use Clue\Redis\Protocol\Serializer\SerializerInterface;
1011
use Clue\Redis\Protocol\Factory as ProtocolFactory;
1112
use Clue\Redis\React\Request;
1213
use React\Promise\When;
@@ -16,29 +17,36 @@
1617
class Client extends EventEmitter
1718
{
1819
private $stream;
19-
private $protocol;
20+
private $parser;
21+
private $serializer;
2022
private $requests = array();
2123
private $ending = false;
2224

23-
public function __construct(Stream $stream, ProtocolInterface $protocol = null)
25+
public function __construct(Stream $stream, ParserInterface $parser = null, SerializerInterface $serializer = null)
2426
{
25-
if ($protocol === null) {
26-
$protocol = ProtocolFactory::create();
27+
if ($parser === null || $serializer === null) {
28+
$factory = new ProtocolFactory();
29+
if ($parser === null) {
30+
$parser = $factory->createParser();
31+
}
32+
if ($serializer === null) {
33+
$serializer = $factory->createSerializer();
34+
}
2735
}
2836

2937
$that = $this;
30-
$stream->on('data', function($chunk) use ($protocol, $that) {
38+
$stream->on('data', function($chunk) use ($parser, $that) {
3139
try {
32-
$protocol->pushIncoming($chunk);
40+
$parser->pushIncoming($chunk);
3341
}
3442
catch (ParserException $error) {
3543
$that->emit('error', array($error));
3644
$that->close();
3745
return;
3846
}
3947

40-
while ($protocol->hasIncoming()) {
41-
$data = $protocol->popIncoming();
48+
while ($parser->hasIncomingModel()) {
49+
$data = $parser->popIncomingModel();
4250

4351
try {
4452
$that->handleReply($data);
@@ -56,7 +64,8 @@ public function __construct(Stream $stream, ProtocolInterface $protocol = null)
5664
});
5765
$stream->resume();
5866
$this->stream = $stream;
59-
$this->protocol = $protocol;
67+
$this->parser = $parser;
68+
$this->serializer = $serializer;
6069
}
6170

6271
public function __call($name, $args)
@@ -70,7 +79,7 @@ public function __call($name, $args)
7079
/* Build the Redis unified protocol command */
7180
array_unshift($args, $name);
7281

73-
$this->stream->write($this->protocol->createMessage($args));
82+
$this->stream->write($this->serializer->createRequestMessage($args));
7483

7584
$request = new Request($name);
7685
$this->requests []= $request;

src/Clue/Redis/React/Factory.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,17 @@ class Factory
1818
{
1919
private $loop;
2020
private $connector;
21+
private $protocol;
2122

22-
public function __construct(LoopInterface $loop, ConnectorInterface $connector = null)
23+
public function __construct(LoopInterface $loop, ConnectorInterface $connector = null, ProtocolFactory $protocol = null)
2324
{
2425
$this->loop = $loop;
2526
$this->connector = $connector;
27+
28+
if ($protocol === null) {
29+
$protocol = new ProtocolFactory();
30+
}
31+
$this->protocol = $protocol;
2632
}
2733

2834
/**
@@ -33,12 +39,12 @@ public function __construct(LoopInterface $loop, ConnectorInterface $connector =
3339
*/
3440
public function createClient($target = null)
3541
{
36-
$that = $this;
3742
$auth = $this->getAuthFromTarget($target);
3843
$db = $this->getDatabaseFromTarget($target);
44+
$protocol = $this->protocol;
3945

40-
return $this->connect($target)->then(function (Stream $stream) use ($that, $auth, $db) {
41-
$client = new Client($stream, $that->createProtocol());
46+
return $this->connect($target)->then(function (Stream $stream) use ($auth, $db, $protocol) {
47+
$client = new Client($stream, $protocol->createParser(), $protocol->createSerializer());
4248

4349
return When::all(
4450
array(
@@ -56,11 +62,6 @@ function($error) use ($client) {
5662
});
5763
}
5864

59-
public function createProtocol()
60-
{
61-
return ProtocolFactory::create();
62-
}
63-
6465
private function parseUrl($target)
6566
{
6667
if ($target === null) {

src/Clue/Redis/React/Request.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,18 @@
22

33
namespace Clue\Redis\React;
44

5-
use Clue\Redis\Protocol\ErrorReplyException;
5+
use Clue\Redis\Protocol\Model\ModelInterface;
6+
use Clue\Redis\Protocol\Model\ErrorReply;
67
use React\Promise\Deferred;
78

89
class Request extends Deferred
910
{
10-
public function handleReply($data)
11+
public function handleReply(ModelInterface $data)
1112
{
12-
if ($data instanceof ErrorReplyException) {
13+
if ($data instanceof ErrorReply) {
1314
$this->reject($data);
1415
} else {
15-
$this->resolve($data);
16+
$this->resolve($data->getValueNative());
1617
}
1718
}
1819
}

0 commit comments

Comments
 (0)