1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bernard\Driver\Amqp; |
4
|
|
|
|
5
|
|
|
use PhpAmqpLib\Channel\AMQPChannel; |
6
|
|
|
use PhpAmqpLib\Connection\AbstractConnection; |
7
|
|
|
use PhpAmqpLib\Message\AMQPMessage; |
8
|
|
|
|
9
|
|
|
final class Driver implements \Bernard\Driver |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var AbstractConnection |
13
|
|
|
*/ |
14
|
|
|
private $connection; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var AMQPChannel |
18
|
|
|
*/ |
19
|
|
|
private $channel; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
private $exchange; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var array|null |
28
|
|
|
*/ |
29
|
|
|
private $defaultMessageParams; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param AbstractConnection $connection |
33
|
|
|
* @param string $exchange |
34
|
|
|
* @param array $defaultMessageParams |
35
|
|
|
*/ |
36
|
|
|
public function __construct(AbstractConnection $connection, $exchange, array $defaultMessageParams = null) |
37
|
|
|
{ |
38
|
|
|
$this->connection = $connection; |
39
|
|
|
$this->exchange = $exchange; |
40
|
|
|
$this->defaultMessageParams = $defaultMessageParams; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Returns a list of all queue names. |
45
|
|
|
* |
46
|
|
|
* @return array |
47
|
|
|
*/ |
48
|
|
|
public function listQueues() |
49
|
|
|
{ |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Create a queue. |
54
|
|
|
* |
55
|
|
|
* @param string $queueName |
56
|
|
|
*/ |
57
|
|
|
public function createQueue($queueName) |
58
|
|
|
{ |
59
|
|
|
$channel = $this->getChannel(); |
60
|
|
|
$channel->exchange_declare($this->exchange, 'direct', false, true, false); |
61
|
|
|
$channel->queue_declare($queueName, false, true, false, false); |
62
|
|
|
$channel->queue_bind($queueName, $this->exchange, $queueName); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Count the number of messages in queue. This can be a approximately number. |
67
|
|
|
* |
68
|
|
|
* @param string $queueName |
69
|
|
|
* |
70
|
|
|
* @return int |
71
|
|
|
*/ |
72
|
|
|
public function countMessages($queueName) |
73
|
|
|
{ |
74
|
|
|
list(,$messageCount) = $this->getChannel()->queue_declare($queueName, true); |
75
|
|
|
|
76
|
|
|
return $messageCount; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Insert a message at the top of the queue. |
81
|
|
|
* |
82
|
|
|
* @param string $queueName |
83
|
|
|
* @param string $message |
84
|
|
|
*/ |
85
|
|
|
public function pushMessage($queueName, $message) |
86
|
|
|
{ |
87
|
|
|
$amqpMessage = new AMQPMessage($message, $this->defaultMessageParams); |
|
|
|
|
88
|
|
|
$this->getChannel()->basic_publish($amqpMessage, $this->exchange, $queueName); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Remove the next message in line. And if no message is available |
93
|
|
|
* wait $duration seconds. |
94
|
|
|
* |
95
|
|
|
* @param string $queueName |
96
|
|
|
* @param int $duration |
97
|
|
|
* |
98
|
|
|
* @return array An array like array($message, $receipt); |
99
|
|
|
*/ |
100
|
|
|
public function popMessage($queueName, $duration = 5) |
101
|
|
|
{ |
102
|
|
|
$runtime = microtime(true) + $duration; |
103
|
|
|
|
104
|
|
|
while (microtime(true) < $runtime) { |
105
|
|
|
$message = $this->getChannel()->basic_get($queueName); |
106
|
|
|
|
107
|
|
|
if ($message) { |
108
|
|
|
return [$message->body, $message->get('delivery_tag')]; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
// sleep for 10 ms to prevent hammering CPU |
112
|
|
|
usleep(10000); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return [null, null]; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* If the driver supports it, this will be called when a message |
120
|
|
|
* have been consumed. |
121
|
|
|
* |
122
|
|
|
* @param string $queueName |
123
|
|
|
* @param mixed $receipt |
124
|
|
|
*/ |
125
|
|
|
public function acknowledgeMessage($queueName, $receipt) |
126
|
|
|
{ |
127
|
|
|
$this->getChannel()->basic_ack($receipt); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Returns a $limit numbers of messages without removing them |
132
|
|
|
* from the queue. |
133
|
|
|
* |
134
|
|
|
* @param string $queueName |
135
|
|
|
* @param int $index |
136
|
|
|
* @param int $limit |
137
|
|
|
*/ |
138
|
|
|
public function peekQueue($queueName, $index = 0, $limit = 20) |
139
|
|
|
{ |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Removes the queue. |
144
|
|
|
* |
145
|
|
|
* @param string $queueName |
146
|
|
|
*/ |
147
|
|
|
public function removeQueue($queueName) |
148
|
|
|
{ |
149
|
|
|
$this->getChannel()->queue_delete($queueName); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @return array |
154
|
|
|
*/ |
155
|
|
|
public function info() |
156
|
|
|
{ |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
public function __destruct() |
160
|
|
|
{ |
161
|
|
|
if (null !== $this->channel) { |
162
|
|
|
$this->channel->close(); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
private function getChannel() |
167
|
|
|
{ |
168
|
|
|
if (null === $this->channel) { |
169
|
|
|
$this->channel = $this->connection->channel(); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
return $this->channel; |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.