1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bernard\Driver; |
4
|
|
|
|
5
|
|
|
use Bernard\Driver; |
6
|
|
|
use PhpAmqpLib\Channel\AMQPChannel; |
7
|
|
|
use PhpAmqpLib\Connection\AMQPStreamConnection; |
8
|
|
|
use PhpAmqpLib\Message\AMQPMessage; |
9
|
|
|
|
10
|
|
|
class PhpAmqpDriver implements Driver |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var AMQPStreamConnection |
14
|
|
|
*/ |
15
|
|
|
private $connection; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var AMQPChannel |
19
|
|
|
*/ |
20
|
|
|
private $channel; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
private $exchange; |
26
|
|
|
/** |
27
|
|
|
* @var array|null |
28
|
|
|
*/ |
29
|
|
|
private $defaultMessageParams; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param AMQPStreamConnection $connection |
33
|
|
|
* @param string $exchange |
34
|
|
|
* @param array $defaultMessageParams |
35
|
|
|
*/ |
36
|
|
|
public function __construct(AMQPStreamConnection $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); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Count the number of messages in queue. This can be a approximately number. |
67
|
|
|
* |
68
|
|
|
* @return int |
69
|
|
|
*/ |
70
|
|
|
public function countMessages($queueName) |
71
|
|
|
{ |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Insert a message at the top of the queue. |
76
|
|
|
* |
77
|
|
|
* @param string $queueName |
78
|
|
|
* @param string $message |
79
|
|
|
*/ |
80
|
|
|
public function pushMessage($queueName, $message) |
81
|
|
|
{ |
82
|
|
|
$amqpMessage = new AMQPMessage($message, $this->defaultMessageParams); |
|
|
|
|
83
|
|
|
$this->getChannel()->basic_publish($amqpMessage, $this->exchange); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Remove the next message in line. And if no message is available |
88
|
|
|
* wait $interval seconds. |
89
|
|
|
* |
90
|
|
|
* @param string $queueName |
91
|
|
|
* @param int $interval |
92
|
|
|
* |
93
|
|
|
* @return array An array like array($message, $receipt); |
94
|
|
|
*/ |
95
|
|
|
public function popMessage($queueName, $interval = 5) |
96
|
|
|
{ |
97
|
|
|
$message = $this->getChannel()->basic_get($queueName); |
98
|
|
|
if (!$message) { |
99
|
|
|
// sleep for 10 ms to prevent hammering CPU |
100
|
|
|
usleep(10000); |
101
|
|
|
|
102
|
|
|
return [null, null]; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return [$message->body, $message->get('delivery_tag')]; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* If the driver supports it, this will be called when a message |
110
|
|
|
* have been consumed. |
111
|
|
|
* |
112
|
|
|
* @param string $queueName |
113
|
|
|
* @param mixed $receipt |
114
|
|
|
*/ |
115
|
|
|
public function acknowledgeMessage($queueName, $receipt) |
116
|
|
|
{ |
117
|
|
|
$this->getChannel()->basic_ack($receipt); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Returns a $limit numbers of messages without removing them |
122
|
|
|
* from the queue. |
123
|
|
|
* |
124
|
|
|
* @param string $queueName |
125
|
|
|
* @param int $index |
126
|
|
|
* @param int $limit |
127
|
|
|
*/ |
128
|
|
|
public function peekQueue($queueName, $index = 0, $limit = 20) |
129
|
|
|
{ |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Removes the queue. |
134
|
|
|
* |
135
|
|
|
* @param string $queueName |
136
|
|
|
*/ |
137
|
|
|
public function removeQueue($queueName) |
138
|
|
|
{ |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @return array |
143
|
|
|
*/ |
144
|
|
|
public function info() |
145
|
|
|
{ |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function __destruct() |
149
|
|
|
{ |
150
|
|
|
if (null !== $this->channel) { |
151
|
|
|
$this->channel->close(); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
private function getChannel() |
156
|
|
|
{ |
157
|
|
|
if (null === $this->channel) { |
158
|
|
|
$this->channel = $this->connection->channel(); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
return $this->channel; |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
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.