|
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
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
|
10
|
|
|
|
|
11
|
|
|
class PhpAmqpDriver extends AbstractDriver |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var AMQPStreamConnection |
|
15
|
|
|
*/ |
|
16
|
|
|
private $connection; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var AMQPChannel |
|
20
|
|
|
*/ |
|
21
|
|
|
private $channel; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
private $exchange; |
|
27
|
|
|
/** |
|
28
|
|
|
* @var array|null |
|
29
|
|
|
*/ |
|
30
|
|
|
private $defaultMessageParams; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param AMQPStreamConnection $connection |
|
34
|
|
|
* @param string $exchange |
|
35
|
|
|
* @param array $defaultMessageParams |
|
36
|
|
|
*/ |
|
37
|
|
|
public function __construct(AMQPStreamConnection $connection, $exchange, array $defaultMessageParams = null) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->connection = $connection; |
|
40
|
|
|
$this->exchange = $exchange; |
|
41
|
|
|
$this->defaultMessageParams = $defaultMessageParams; |
|
42
|
|
|
|
|
43
|
|
|
$this->channel = $this->connection->channel(); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Returns a list of all queue names. |
|
48
|
|
|
* |
|
49
|
|
|
* @return array |
|
50
|
|
|
*/ |
|
51
|
|
|
public function listQueues() |
|
52
|
|
|
{ |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* {@inheritdoc} |
|
57
|
|
|
*/ |
|
58
|
|
|
public function createQueue($queueName, array $options = []) |
|
59
|
|
|
{ |
|
60
|
|
|
$options = $this->validateQueueOptions($options); |
|
61
|
|
|
|
|
62
|
|
|
$this->channel->exchange_declare($this->exchange, 'direct', false, true, false); |
|
63
|
|
|
$this->channel->queue_declare($queueName, false, true, false, false); |
|
64
|
|
|
$this->channel->queue_bind($queueName, $this->exchange, $options['routingkey']); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Count the number of messages in queue. This can be a approximately number. |
|
69
|
|
|
* |
|
70
|
|
|
* @return int |
|
71
|
|
|
*/ |
|
72
|
|
|
public function countMessages($queueName) |
|
73
|
|
|
{ |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* {@inheritdoc} |
|
78
|
|
|
*/ |
|
79
|
|
|
public function pushMessage($queueName, $message, array $options = []) |
|
80
|
|
|
{ |
|
81
|
|
|
$amqpMessage = new AMQPMessage($message, $this->defaultMessageParams); |
|
|
|
|
|
|
82
|
|
|
$options = $this->validatePushOptions($options); |
|
83
|
|
|
|
|
84
|
|
|
$this->channel->basic_publish( |
|
85
|
|
|
$amqpMessage, |
|
86
|
|
|
$this->exchange, |
|
87
|
|
|
$options['routingkey'], |
|
88
|
|
|
$options['mandatory'], |
|
89
|
|
|
$options['immediate'], |
|
90
|
|
|
$options['ticket'] |
|
91
|
|
|
); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Remove the next message in line. And if no message is available |
|
96
|
|
|
* wait $interval seconds. |
|
97
|
|
|
* |
|
98
|
|
|
* @param string $queueName |
|
99
|
|
|
* @param int $interval |
|
100
|
|
|
* |
|
101
|
|
|
* @return array An array like array($message, $receipt); |
|
102
|
|
|
*/ |
|
103
|
|
|
public function popMessage($queueName, $interval = 5) |
|
104
|
|
|
{ |
|
105
|
|
|
$message = $this->channel->basic_get($queueName); |
|
106
|
|
|
if (!$message) { |
|
107
|
|
|
// sleep for 10 ms to prevent hammering CPU |
|
108
|
|
|
usleep(10000); |
|
109
|
|
|
|
|
110
|
|
|
return [null, null]; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
return [$message->body, $message->get('delivery_tag')]; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* If the driver supports it, this will be called when a message |
|
118
|
|
|
* have been consumed. |
|
119
|
|
|
* |
|
120
|
|
|
* @param string $queueName |
|
121
|
|
|
* @param mixed $receipt |
|
122
|
|
|
*/ |
|
123
|
|
|
public function acknowledgeMessage($queueName, $receipt) |
|
124
|
|
|
{ |
|
125
|
|
|
$this->channel->basic_ack($receipt); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Returns a $limit numbers of messages without removing them |
|
130
|
|
|
* from the queue. |
|
131
|
|
|
* |
|
132
|
|
|
* @param string $queueName |
|
133
|
|
|
* @param int $index |
|
134
|
|
|
* @param int $limit |
|
135
|
|
|
*/ |
|
136
|
|
|
public function peekQueue($queueName, $index = 0, $limit = 20) |
|
137
|
|
|
{ |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Removes the queue. |
|
142
|
|
|
* |
|
143
|
|
|
* @param string $queueName |
|
144
|
|
|
*/ |
|
145
|
|
|
public function removeQueue($queueName) |
|
146
|
|
|
{ |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* @return array |
|
151
|
|
|
*/ |
|
152
|
|
|
public function info() |
|
153
|
|
|
{ |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
public function __destruct() |
|
157
|
|
|
{ |
|
158
|
|
|
$this->channel->close(); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
public function configureQueueOptions(OptionsResolver $resolver) |
|
162
|
|
|
{ |
|
163
|
|
|
//BC layer to support 2.3+ and 2.7+/3.0+ versions |
|
164
|
|
|
// if (class_exists('Symfony\Component\OptionsResolver\OptionsResolverInterface')) { |
|
165
|
|
|
// //2.3+ |
|
166
|
|
|
// $resolver->setDefaults(array( |
|
167
|
|
|
// 'routingkey' => '', |
|
168
|
|
|
// )); |
|
169
|
|
|
// } else { |
|
170
|
|
|
// //2.7+ |
|
171
|
|
|
// } |
|
172
|
|
|
|
|
173
|
|
|
$resolver->setDefaults(array( |
|
174
|
|
|
'routingkey' => '', |
|
175
|
|
|
)); |
|
176
|
|
|
|
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
public function configurePushOptions(OptionsResolver $resolver) |
|
180
|
|
|
{ |
|
181
|
|
|
$resolver->setDefaults(array( |
|
182
|
|
|
'routingkey' => '', |
|
183
|
|
|
'mandatory' => false, |
|
184
|
|
|
'immediate' => false, |
|
185
|
|
|
'ticket' => null, |
|
186
|
|
|
)); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
|
|
190
|
|
|
} |
|
191
|
|
|
|
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.