|
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\Actions\ListenAction; |
|
|
|
|
|
|
14
|
|
|
use Asiries335\redisSteamPhp\Data\Collection; |
|
15
|
|
|
use Asiries335\redisSteamPhp\Data\Constants; |
|
16
|
|
|
use Asiries335\redisSteamPhp\Data\Message; |
|
17
|
|
|
use Asiries335\redisSteamPhp\Hydrator\CollectionHydrator; |
|
18
|
|
|
use Asiries335\redisSteamPhp\Hydrator\MessageHydrator; |
|
19
|
|
|
use Redis; |
|
20
|
|
|
|
|
21
|
|
|
final class Stream |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* Client |
|
25
|
|
|
* |
|
26
|
|
|
* @var Redis |
|
27
|
|
|
*/ |
|
28
|
|
|
private $_client; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Name stream |
|
32
|
|
|
* |
|
33
|
|
|
* @var string |
|
34
|
|
|
*/ |
|
35
|
|
|
private $_streamName; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Stream constructor. |
|
39
|
|
|
* |
|
40
|
|
|
* @param Redis $client Redis Client |
|
41
|
|
|
* @param string $nameStream Name stream |
|
42
|
|
|
*/ |
|
43
|
3 |
|
public function __construct(\Redis $client, string $nameStream) |
|
44
|
|
|
{ |
|
45
|
3 |
|
$this->_client = $client; |
|
46
|
3 |
|
$this->_streamName = $nameStream; |
|
47
|
3 |
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Appends the specified stream entry to the stream at the specified key |
|
51
|
|
|
* |
|
52
|
|
|
* @param string $key Key Message |
|
53
|
|
|
* @param array $values Value Message |
|
54
|
|
|
* |
|
55
|
|
|
* @return string |
|
56
|
|
|
* |
|
57
|
|
|
* @throws \Exception |
|
58
|
|
|
* |
|
59
|
|
|
* @see https://redis.io/commands/xadd |
|
60
|
|
|
*/ |
|
61
|
1 |
|
public function add(string $key, array $values) : string |
|
62
|
|
|
{ |
|
63
|
|
|
try { |
|
64
|
1 |
|
return (string) $this->_client->rawCommand( |
|
65
|
1 |
|
Constants::COMMAND_XADD, |
|
66
|
1 |
|
$this->_streamName, |
|
67
|
1 |
|
'*', |
|
|
|
|
|
|
68
|
|
|
$key, |
|
69
|
1 |
|
json_encode($values) |
|
70
|
|
|
); |
|
71
|
|
|
} catch (\Exception $exception) { |
|
72
|
|
|
throw $exception; |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Get data from stream |
|
78
|
|
|
* |
|
79
|
|
|
* @return Collection |
|
80
|
|
|
* |
|
81
|
|
|
* @throws \Exception |
|
82
|
|
|
* |
|
83
|
|
|
* @see https://redis.io/commands/xread |
|
84
|
|
|
*/ |
|
85
|
2 |
|
public function get() : Collection |
|
86
|
|
|
{ |
|
87
|
|
|
try { |
|
88
|
2 |
|
$items = $this->_client->rawCommand( |
|
89
|
2 |
|
Constants::COMMAND_XREAD, |
|
90
|
2 |
|
'STREAMS', |
|
91
|
2 |
|
$this->_streamName, |
|
|
|
|
|
|
92
|
2 |
|
'0' |
|
93
|
|
|
); |
|
94
|
|
|
|
|
95
|
2 |
|
$collection = new CollectionHydrator(); |
|
96
|
|
|
|
|
97
|
2 |
|
if (empty($items) === true) { |
|
98
|
1 |
|
return new Collection; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
1 |
|
return $collection->hydrate($items, Collection::class); |
|
102
|
|
|
|
|
103
|
|
|
} catch (\Exception $exception) { |
|
104
|
|
|
throw $exception; |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Listen stream |
|
110
|
|
|
* |
|
111
|
|
|
* @param \Closure $closure User callback |
|
112
|
|
|
* |
|
113
|
|
|
* @return void |
|
114
|
|
|
* |
|
115
|
|
|
* @throws \ErrorException |
|
116
|
|
|
*/ |
|
117
|
|
|
public function listen(\Closure $closure) : void |
|
118
|
|
|
{ |
|
119
|
|
|
$messageHydrate = new MessageHydrator(); |
|
120
|
|
|
|
|
121
|
|
|
$loop = \React\EventLoop\Factory::create(); |
|
122
|
|
|
|
|
123
|
|
|
$loop->addPeriodicTimer( |
|
124
|
|
|
Constants::TIME_TICK_INTERVAL, |
|
125
|
|
|
function () use ($closure, $messageHydrate, $loop) { |
|
|
|
|
|
|
126
|
|
|
$rows = $this->_client->rawCommand( |
|
127
|
|
|
Constants::COMMAND_XRANGE, |
|
128
|
|
|
$this->_streamName, |
|
129
|
|
|
'-', |
|
|
|
|
|
|
130
|
|
|
'+' |
|
131
|
|
|
); |
|
132
|
|
|
|
|
133
|
|
|
if (empty($rows) === true) { |
|
134
|
|
|
return; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
foreach ($rows as $row) { |
|
138
|
|
|
$message = $messageHydrate->hydrate($row, Message::class); |
|
139
|
|
|
$closure->call($this, $message); |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
); |
|
143
|
|
|
|
|
144
|
|
|
$loop->run(); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
} |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths