1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace devtransition\rabbitmq\base; |
4
|
|
|
|
5
|
|
|
use PhpAmqpLib\Channel\AMQPChannel; |
6
|
|
|
use PhpAmqpLib\Message\AMQPMessage; |
7
|
|
|
use yii\base\Component; |
8
|
|
|
use yii\helpers\ArrayHelper; |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
class Channel extends Component |
12
|
|
|
{ |
13
|
|
|
const EXCHANGE_TYPE_TOPIC = 'topic'; |
14
|
|
|
const EXCHANGE_TYPE_DIRECT = 'direct'; |
15
|
|
|
const EXCHANGE_TYPE_HEADERS = 'headers'; |
16
|
|
|
const EXCHANGE_TYPE_FANOUT = 'fanout'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var Connection |
20
|
|
|
*/ |
21
|
|
|
protected $connection; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var AMQPChannel |
25
|
|
|
*/ |
26
|
|
|
protected $amqpChannel; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @inheritdoc |
30
|
|
|
*/ |
31
|
|
|
public function __construct(ConnectionInterface $connection, $config = []) |
32
|
|
|
{ |
33
|
|
|
$this->connection = $connection; |
|
|
|
|
34
|
|
|
$this->amqpChannel = $this->connection->getAmqpInstance()->channel(); |
35
|
|
|
parent::__construct($config); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function __destruct() |
39
|
|
|
{ |
40
|
|
|
$this->amqpChannel->close(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Server should continue sending data to us. Default on new channels. |
45
|
|
|
*/ |
46
|
|
|
public function enableFlow() |
47
|
|
|
{ |
48
|
|
|
$this->amqpChannel->flow(true); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Server should stop sending data to us. |
53
|
|
|
*/ |
54
|
|
|
public function disableFlow() |
55
|
|
|
{ |
56
|
|
|
$this->amqpChannel->flow(true); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function send() |
60
|
|
|
{ |
61
|
|
|
$msg = new AMQPMessage('test'); |
62
|
|
|
$this->amqpChannel->basic_publish($msg, '', 'test'); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function getMessageOne($queue, $no_ack = false) |
66
|
|
|
{ |
67
|
|
|
$msg = $this->amqpChannel->basic_get($queue, $no_ack = false); |
68
|
|
|
if ($msg instanceof AMQPMessage) { |
69
|
|
|
$res = new AMQPMessage(); |
70
|
|
|
return $res; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
throw new InvalidValueException("response was not a valid amqp message object"); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function getMessageConsume($queue, Callable $callback, Array $options = []) |
77
|
|
|
{ |
78
|
|
|
return $this->amqpChannel->basic_consume( |
79
|
|
|
$queue = '', |
80
|
|
|
$consumer_tag = ArrayHelper::getValue($options, 'consumer_tag', ''), |
81
|
|
|
$no_local = ArrayHelper::getValue($options, 'no_local', false), |
82
|
|
|
$no_ack = ArrayHelper::getValue($options, 'no_ack', false), |
83
|
|
|
$exclusive = ArrayHelper::getValue($options, 'exclusive', false), |
84
|
|
|
$nowait = ArrayHelper::getValue($options, 'nowait', false), |
85
|
|
|
$callback, |
86
|
|
|
$ticket = null, |
87
|
|
|
$arguments = array() |
88
|
|
|
); |
89
|
|
|
|
90
|
|
|
/* |
|
|
|
|
91
|
|
|
while (count($this->channel->callbacks)) { |
92
|
|
|
$this->channel->wait(); |
93
|
|
|
} |
94
|
|
|
*/ |
95
|
|
|
|
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function declareExchange($name, $type, $passive = false, $durable = false, $auto_delete = true, $internal = false) |
99
|
|
|
{ |
100
|
|
|
return $this->amqpChannel->exchange_declare($name, $type, $passive, $durable, $auto_delete, $internal); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function deleteExchange($name, $if_unused = false, $if_empty = false, $nowait = false) |
104
|
|
|
{ |
105
|
|
|
return $this->amqpChannel->exchange_delete($name, $if_unused, $if_empty, $nowait); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function declareQueue($name, $passive = false, $durable = false, $auto_delete = true, $internal = false) |
109
|
|
|
{ |
110
|
|
|
return $this->amqpChannel->queue_declare($name, $passive, $durable, $auto_delete, $internal); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function deleteQueue($name, $if_unused = false, $if_empty = false, $nowait = false) |
114
|
|
|
{ |
115
|
|
|
return $this->amqpChannel->queue_delete($name, $if_unused, $if_empty, $nowait); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function purgeQueue($name, $nowait = false) |
119
|
|
|
{ |
120
|
|
|
return $this->amqpChannel->queue_purge($name, $nowait); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function bindQueueExchange($queue, $exchange, $routing_key) |
124
|
|
|
{ |
125
|
|
|
return $this->amqpChannel->queue_bind($queue, $exchange, $routing_key); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.