1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ETNA\Silex\Provider\RabbitMQ; |
4
|
|
|
|
5
|
|
|
use PhpAmqpLib\Message\AMQPMessage; |
6
|
|
|
use PhpAmqpLib\Channel\AMQPChannel; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* |
10
|
|
|
*/ |
11
|
|
|
class Queue |
12
|
|
|
{ |
13
|
|
|
public function __construct($name, Exchange $exchange, AMQPChannel $channel, $options) |
14
|
|
|
{ |
15
|
|
|
$this->name = $name; |
|
|
|
|
16
|
|
|
$this->exchange = $exchange; |
|
|
|
|
17
|
|
|
$this->channel = $channel; |
|
|
|
|
18
|
|
|
$this->passive = $options["passive"]; |
|
|
|
|
19
|
|
|
$this->durable = $options["durable"]; |
|
|
|
|
20
|
|
|
$this->exclusive = $options["exclusive"]; |
|
|
|
|
21
|
|
|
$this->auto_delete = $options["auto_delete"]; |
|
|
|
|
22
|
|
|
|
23
|
|
|
$channel->queue_declare( |
24
|
|
|
$name, |
25
|
|
|
$this->passive, |
26
|
|
|
$this->durable, |
27
|
|
|
$this->exclusive, |
28
|
|
|
$this->auto_delete |
29
|
|
|
); |
30
|
|
|
|
31
|
|
|
if ($exchange->getName()) { |
32
|
|
|
$routing_key = $exchange->getType() == "fanout" ? null : $name; |
33
|
|
|
$channel->queue_bind($name, $exchange->getName(), $routing_key); |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function getName() |
38
|
|
|
{ |
39
|
|
|
return $this->name; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function getChannel() |
43
|
|
|
{ |
44
|
|
|
return $this->channel; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function isPassive() |
48
|
|
|
{ |
49
|
|
|
return $this->passive; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function isDurable() |
53
|
|
|
{ |
54
|
|
|
return $this->durable; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function isExclusive() |
58
|
|
|
{ |
59
|
|
|
return $this->exclusive; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function isAutoDelete() |
63
|
|
|
{ |
64
|
|
|
return $this->auto_delete; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function send($message, $routing_key = null, $mandatory = false, $immediate = false, $ticket = null) |
68
|
|
|
{ |
69
|
|
|
$routing_key = $routing_key !== null ? $routing_key : $this->name; |
70
|
|
|
$message = new AMQPMessage(json_encode($message), ["Content-Type" => "application/json"]); |
71
|
|
|
$this->channel->basic_publish($message, $this->exchange->getName(), $routing_key, $mandatory, $immediate, $ticket); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function listen(\Closure $callback, $no_local = false, $no_ack = false, $exclusive = false, $nowait = false) |
75
|
|
|
{ |
76
|
|
|
$chars = preg_split("//", "QWERTYUIOPLKJHGFDSAZXCVBNMqwertyuioplkjhgfdsazxcvbnm1234567890"); |
77
|
|
|
shuffle($chars); |
78
|
|
|
$chars = array_slice($chars, 12); |
79
|
|
|
$consumer_tag = implode("", $chars); |
80
|
|
|
$consumer_tag = md5("{$this->name}/{$consumer_tag}"); |
81
|
|
|
$this->channel->basic_consume($this->name, $consumer_tag, $no_local, $no_ack, $exclusive, $nowait, $callback); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: