1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Stream class |
7
|
|
|
* |
8
|
|
|
* @author Sergei Karii <[email protected]> |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Asiries335\redisSteamPhp; |
12
|
|
|
|
13
|
|
|
use Asiries335\redisSteamPhp\Data\Collection; |
14
|
|
|
use Asiries335\redisSteamPhp\Data\Message; |
15
|
|
|
use Asiries335\redisSteamPhp\Hydrator\CollectionHydrator; |
16
|
|
|
use Asiries335\redisSteamPhp\Hydrator\MessageHydrator; |
17
|
|
|
use Redis; |
18
|
|
|
|
19
|
|
|
final class Stream |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Client |
23
|
|
|
* |
24
|
|
|
* @var Redis |
25
|
|
|
*/ |
26
|
|
|
private $_client; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Name stream |
30
|
|
|
* |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
private $_streamName; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Stream constructor. |
37
|
|
|
* |
38
|
|
|
* @param Redis $client Redis Client |
39
|
|
|
* @param string $nameStream Name stream |
40
|
|
|
*/ |
41
|
3 |
|
public function __construct(\Redis $client, string $nameStream) |
42
|
|
|
{ |
43
|
3 |
|
$this->_client = $client; |
44
|
3 |
|
$this->_streamName = $nameStream; |
45
|
3 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Appends the specified stream entry to the stream at the specified key |
49
|
|
|
* |
50
|
|
|
* @param string $key Key Message |
51
|
|
|
* @param array $values Value Message |
52
|
|
|
* |
53
|
|
|
* @return string |
54
|
|
|
* |
55
|
|
|
* @throws \Exception |
56
|
|
|
* |
57
|
|
|
* @see https://redis.io/commands/xadd |
58
|
|
|
*/ |
59
|
1 |
|
public function add(string $key, array $values) : string |
60
|
|
|
{ |
61
|
|
|
try { |
62
|
1 |
|
return (string) $this->_client->rawCommand( |
63
|
1 |
|
'xadd', |
64
|
1 |
|
$this->_streamName, |
65
|
1 |
|
'*', |
|
|
|
|
66
|
|
|
$key, |
67
|
1 |
|
json_encode($values) |
68
|
|
|
); |
69
|
|
|
} catch (\Exception $exception) { |
70
|
|
|
throw $exception; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Get data from stream |
76
|
|
|
* |
77
|
|
|
* @return Collection |
78
|
|
|
* |
79
|
|
|
* @throws \Exception |
80
|
|
|
* |
81
|
|
|
* @see https://redis.io/commands/xread |
82
|
|
|
*/ |
83
|
2 |
|
public function get() : Collection |
84
|
|
|
{ |
85
|
|
|
try { |
86
|
2 |
|
$items = $this->_client->rawCommand( |
87
|
2 |
|
'xread', |
88
|
2 |
|
'STREAMS', |
89
|
2 |
|
$this->_streamName, |
|
|
|
|
90
|
2 |
|
'0' |
91
|
|
|
); |
92
|
|
|
|
93
|
2 |
|
$collection = new CollectionHydrator(); |
94
|
|
|
|
95
|
2 |
|
if (empty($items) === true) { |
96
|
1 |
|
return new Collection; |
97
|
|
|
} |
98
|
|
|
|
99
|
1 |
|
return $collection->hydrate($items, Collection::class); |
100
|
|
|
|
101
|
|
|
} catch (\Exception $exception) { |
102
|
|
|
throw $exception; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Listen stream |
108
|
|
|
* |
109
|
|
|
* @param \Closure $closure User callback |
110
|
|
|
* |
111
|
|
|
* @return void |
112
|
|
|
* |
113
|
|
|
* @throws \ErrorException |
114
|
|
|
*/ |
115
|
|
|
public function listen(\Closure $closure) : void |
116
|
|
|
{ |
117
|
|
|
$messageHydrate = new MessageHydrator(); |
118
|
|
|
|
119
|
|
|
$lastMessageId = null; |
120
|
|
|
|
121
|
|
|
while (true) { |
122
|
|
|
$data = $this->_client->rawCommand( |
123
|
|
|
'xrevrange', |
124
|
|
|
$this->_streamName, |
125
|
|
|
'+', |
|
|
|
|
126
|
|
|
'-', |
127
|
|
|
'COUNT', |
128
|
|
|
1 |
129
|
|
|
); |
130
|
|
|
|
131
|
|
|
if (empty($data) === true) { |
132
|
|
|
usleep(1); |
133
|
|
|
continue; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
$message = $messageHydrate->hydrate($data[0], Message::class); |
137
|
|
|
|
138
|
|
|
if ($message->getId() !== $lastMessageId) { |
139
|
|
|
$lastMessageId = $message->getId(); |
140
|
|
|
$closure->call($this, $message); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
usleep(1); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
} |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.