|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace AMQPAL\Adapter\PhpAmqpLib; |
|
4
|
|
|
|
|
5
|
|
|
use PhpAmqpLib\Message\AMQPMessage; |
|
6
|
|
|
use AMQPAL\Adapter\ExchangeInterface; |
|
7
|
|
|
use AMQPAL\Options; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class Exchange |
|
11
|
|
|
* |
|
12
|
|
|
* @package AMQPAL\Adapter\PhpAmqpLib |
|
13
|
|
|
*/ |
|
14
|
|
|
class Exchange implements ExchangeInterface |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var Channel |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $channel; |
|
20
|
|
|
/** |
|
21
|
|
|
* @var Options\ExchangeOptions |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $options; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @return Options\ExchangeOptions |
|
27
|
|
|
*/ |
|
28
|
|
|
public function getOptions() |
|
29
|
|
|
{ |
|
30
|
|
|
return $this->options; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param Options\ExchangeOptions $exchangeOptions |
|
35
|
|
|
* @return $this |
|
36
|
|
|
*/ |
|
37
|
14 |
|
public function setOptions(Options\ExchangeOptions $exchangeOptions) |
|
38
|
|
|
{ |
|
39
|
14 |
|
$this->options = $exchangeOptions; |
|
40
|
14 |
|
return $this; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Declare a new exchange on the broker. |
|
45
|
|
|
* |
|
46
|
|
|
* @return $this |
|
47
|
|
|
*/ |
|
48
|
2 |
|
public function declareExchange() |
|
49
|
|
|
{ |
|
50
|
2 |
|
$this->channel->getResource()->exchange_declare( |
|
51
|
2 |
|
$this->options->getName(), |
|
52
|
2 |
|
$this->options->getType(), |
|
53
|
2 |
|
$this->options->isPassive(), |
|
54
|
2 |
|
$this->options->isDurable(), |
|
55
|
2 |
|
$this->options->isAutoDelete(), |
|
56
|
2 |
|
$this->options->isInternal(), |
|
57
|
2 |
|
$this->options->isNoWait(), |
|
58
|
2 |
|
$this->options->getArguments() |
|
59
|
|
|
); |
|
60
|
|
|
|
|
61
|
2 |
|
return $this; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Delete the exchange from the broker. |
|
66
|
|
|
* |
|
67
|
|
|
* @param bool $ifUnused Optional if the exchange should not be |
|
68
|
|
|
* deleted until no clients are connected to |
|
69
|
|
|
* it. |
|
70
|
|
|
* @param bool $noWait No wait for a reply |
|
71
|
|
|
* |
|
72
|
|
|
* @return $this |
|
73
|
|
|
*/ |
|
74
|
4 |
|
public function delete($ifUnused = false, $noWait = false) |
|
75
|
|
|
{ |
|
76
|
4 |
|
$this->channel->getResource()->exchange_delete($this->options->getName(), $ifUnused, $noWait); |
|
77
|
|
|
|
|
78
|
4 |
|
return $this; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Bind to another exchange. |
|
83
|
|
|
* |
|
84
|
|
|
* Bind an exchange to another exchange using the specified routing key. |
|
85
|
|
|
* |
|
86
|
|
|
* @param string $exchangeName Name of the exchange to bind. |
|
87
|
|
|
* @param string $routingKey The routing key to use for binding. |
|
88
|
|
|
* @param bool $noWait No wait for a reply |
|
89
|
|
|
* @param array $arguments Additional binding arguments. |
|
90
|
|
|
* |
|
91
|
|
|
* @return $this |
|
92
|
|
|
*/ |
|
93
|
2 |
|
public function bind($exchangeName, $routingKey = null, $noWait = false, array $arguments = []) |
|
94
|
|
|
{ |
|
95
|
2 |
|
if (null === $routingKey) { |
|
96
|
1 |
|
$routingKey = ''; |
|
97
|
|
|
} |
|
98
|
2 |
|
$name = $this->options->getName(); |
|
99
|
2 |
|
$this->channel->getResource()->exchange_bind($name, $exchangeName, $routingKey, $noWait, $arguments); |
|
100
|
|
|
|
|
101
|
2 |
|
return $this; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Remove binding to another exchange. |
|
106
|
|
|
* |
|
107
|
|
|
* Remove a routing key binding on an another exchange from the given exchange. |
|
108
|
|
|
* |
|
109
|
|
|
* @param string $exchangeName Name of the exchange to bind. |
|
110
|
|
|
* @param string $routingKey The routing key to use for binding. |
|
111
|
|
|
* @param array $arguments Additional binding arguments. |
|
112
|
|
|
* |
|
113
|
|
|
* @return $this |
|
114
|
|
|
*/ |
|
115
|
2 |
|
public function unbind($exchangeName, $routingKey = null, array $arguments = []) |
|
116
|
|
|
{ |
|
117
|
2 |
|
if (null === $routingKey) { |
|
118
|
1 |
|
$routingKey = ''; |
|
119
|
|
|
} |
|
120
|
2 |
|
$name = $this->options->getName(); |
|
121
|
2 |
|
$this->channel->getResource()->exchange_unbind($name, $exchangeName, $routingKey, $arguments); |
|
122
|
|
|
|
|
123
|
2 |
|
return $this; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Publish a message to an exchange. |
|
128
|
|
|
* |
|
129
|
|
|
* Publish a message to the exchange represented by the Exchange object. |
|
130
|
|
|
* |
|
131
|
|
|
* @param string $message The message to publish. |
|
132
|
|
|
* @param string $routingKey The optional routing key to which to |
|
133
|
|
|
* publish to. |
|
134
|
|
|
* @param bool $mandatory Mandatory |
|
135
|
|
|
* @param bool $immediate Immediate |
|
136
|
|
|
* @param array $attributes One of content_type, content_encoding, |
|
137
|
|
|
* message_id, user_id, app_id, delivery_mode, |
|
138
|
|
|
* priority, timestamp, expiration, type |
|
139
|
|
|
* or reply_to, headers. |
|
140
|
|
|
* |
|
141
|
|
|
* @return $this |
|
142
|
|
|
*/ |
|
143
|
4 |
|
public function publish( |
|
144
|
|
|
$message, |
|
145
|
|
|
$routingKey = null, |
|
146
|
|
|
$mandatory = false, |
|
147
|
|
|
$immediate = false, |
|
148
|
|
|
array $attributes = [] |
|
149
|
|
|
) { |
|
150
|
4 |
|
if (null === $routingKey) { |
|
151
|
1 |
|
$routingKey = ''; |
|
152
|
|
|
} |
|
153
|
4 |
|
$options = $this->options; |
|
154
|
|
|
|
|
155
|
4 |
|
$AMQPMessage = new AMQPMessage($message, $attributes); |
|
156
|
|
|
|
|
157
|
4 |
|
$this->channel->getResource()->basic_publish( |
|
158
|
|
|
$AMQPMessage, |
|
159
|
4 |
|
$options->getName(), |
|
160
|
|
|
$routingKey, |
|
161
|
|
|
$mandatory, |
|
162
|
|
|
$immediate |
|
163
|
|
|
); |
|
164
|
|
|
|
|
165
|
|
|
return $this; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* @param Channel $channel |
|
170
|
|
|
* @return $this |
|
171
|
|
|
*/ |
|
172
|
14 |
|
public function setChannel(Channel $channel) |
|
173
|
|
|
{ |
|
174
|
14 |
|
$this->channel = $channel; |
|
175
|
14 |
|
return $this; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* Get the Channel object in use |
|
180
|
|
|
* |
|
181
|
|
|
* @return Channel |
|
182
|
|
|
*/ |
|
183
|
|
|
public function getChannel() |
|
184
|
|
|
{ |
|
185
|
|
|
return $this->channel; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* Get the Connection object in use |
|
190
|
|
|
* |
|
191
|
|
|
* @return Connection |
|
192
|
|
|
*/ |
|
193
|
|
|
public function getConnection() |
|
194
|
|
|
{ |
|
195
|
|
|
return $this->channel->getConnection(); |
|
196
|
|
|
} |
|
197
|
|
|
} |
|
198
|
|
|
|