1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Phloppy; |
4
|
|
|
|
5
|
|
|
use Phloppy\Exception\CommandException; |
6
|
|
|
use Phloppy\Stream\StreamInterface; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Redis protocol implementation. |
10
|
|
|
* |
11
|
|
|
* This class implements serialization and deserialization of |
12
|
|
|
* the redis binary protocol. A lot of the code was inspired by |
13
|
|
|
* ptrofimov/tinyredisclient. |
14
|
|
|
*/ |
15
|
|
|
class RespUtils |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @param array $args |
20
|
|
|
* |
21
|
|
|
* @return mixed |
22
|
|
|
*/ |
23
|
30 |
|
public static function serialize(array $args) |
24
|
|
|
{ |
25
|
30 |
|
return array_reduce( |
26
|
30 |
|
$args, |
27
|
30 |
|
function ($msg, $arg) { |
28
|
30 |
|
return $msg.'$'.strlen($arg)."\r\n".$arg."\r\n"; |
29
|
30 |
|
}, |
30
|
30 |
|
'*'.count($args)."\r\n" |
31
|
30 |
|
); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param StreamInterface $stream |
37
|
|
|
* |
38
|
|
|
* @return array|int|null|string |
39
|
|
|
* |
40
|
|
|
* @throws CommandException |
41
|
|
|
* @throws \RuntimeException |
42
|
|
|
*/ |
43
|
35 |
|
public static function deserialize(StreamInterface $stream) |
44
|
|
|
{ |
45
|
35 |
|
$rsp = $stream->readLine(); |
46
|
|
|
|
47
|
35 |
|
list($type, $result) = [$rsp[0], trim(substr($rsp, 1, strlen($rsp)))]; |
48
|
|
|
|
49
|
|
|
switch ($type) { |
50
|
35 |
|
case '-': // ERRORS |
51
|
8 |
|
throw new CommandException($result); |
52
|
|
|
|
53
|
29 |
|
case '+': // SIMPLE STRINGS |
54
|
22 |
|
return $result; |
55
|
|
|
|
56
|
20 |
|
case ':': // INTEGERS |
57
|
13 |
|
return (int) $result; |
58
|
|
|
|
59
|
16 |
|
case '$': // BULK STRINGS |
60
|
14 |
|
$result = (int) $result; |
61
|
|
|
|
62
|
14 |
|
if ($result == -1) { |
63
|
2 |
|
return null; |
64
|
|
|
} |
65
|
|
|
|
66
|
13 |
|
return trim($stream->readBytes($result + 2)); |
67
|
|
|
|
68
|
14 |
|
case '*': // ARRAYS |
69
|
13 |
|
$cnt = (int)$result; |
70
|
13 |
|
$out = []; |
71
|
|
|
|
72
|
13 |
|
for ($i = 0; $i < $cnt; $i++) { |
73
|
12 |
|
$out[] = static::deserialize($stream); |
74
|
12 |
|
} |
75
|
|
|
|
76
|
13 |
|
return $out; |
77
|
|
|
|
78
|
1 |
|
default: |
79
|
1 |
|
throw new \RuntimeException('unhandled protocol response: '.$rsp); |
80
|
1 |
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Map RESP responses to an associative array. |
86
|
|
|
* |
87
|
|
|
* @param array $response |
88
|
|
|
* |
89
|
|
|
* @return array |
90
|
|
|
*/ |
91
|
1 |
|
public static function toAssoc(array $response) |
92
|
|
|
{ |
93
|
1 |
|
$out = []; |
|
|
|
|
94
|
1 |
|
$count = count($response); |
95
|
|
|
|
96
|
1 |
|
for ($i = 1; $i < $count; $i += 2) { |
97
|
1 |
|
$out[$response[$i - 1]] = $response[$i]; |
98
|
1 |
|
} |
99
|
|
|
|
100
|
1 |
|
return $out; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.