1
1
<?php
2
2
3
3
namespace OldSound \RabbitMqBundle \RabbitMq ;
4
+ <<<<<<< 5818690e0890 fb32c1d873645f59b33c73e3598c
4
5
use OldSound \RabbitMqBundle \Event \AMQPEvent ;
6
+ =======
7
+
8
+ >>>>>>> add shutdown method. When called kernel shutdown, we must close opened connection
5
9
use PhpAmqpLib \Channel \AMQPChannel;
6
10
use PhpAmqpLib \Connection \AbstractConnection ;
7
11
use PhpAmqpLib \Connection \AMQPLazyConnection ;
@@ -19,35 +23,35 @@ abstract class BaseAmqp
19
23
protected $ queueDeclared = false ;
20
24
protected $ routingKey = '' ;
21
25
protected $ autoSetupFabric = true ;
22
- protected $ basicProperties = array ( 'content_type ' => 'text/plain ' , 'delivery_mode ' => 2 ) ;
26
+ protected $ basicProperties = [ 'content_type ' => 'text/plain ' , 'delivery_mode ' => 2 ] ;
23
27
24
28
/**
25
29
* @var LoggerInterface
26
30
*/
27
31
protected $ logger ;
28
32
29
- protected $ exchangeOptions = array (
30
- 'passive ' => false ,
31
- 'durable ' => true ,
33
+ protected $ exchangeOptions = [
34
+ 'passive ' => false ,
35
+ 'durable ' => true ,
32
36
'auto_delete ' => false ,
33
- 'internal ' => false ,
34
- 'nowait ' => false ,
35
- 'arguments ' => null ,
36
- 'ticket ' => null ,
37
- 'declare ' => true ,
38
- ) ;
39
-
40
- protected $ queueOptions = array (
41
- 'name ' => '' ,
42
- 'passive ' => false ,
43
- 'durable ' => true ,
44
- 'exclusive ' => false ,
37
+ 'internal ' => false ,
38
+ 'nowait ' => false ,
39
+ 'arguments ' => null ,
40
+ 'ticket ' => null ,
41
+ 'declare ' => true ,
42
+ ] ;
43
+
44
+ protected $ queueOptions = [
45
+ 'name ' => '' ,
46
+ 'passive ' => false ,
47
+ 'durable ' => true ,
48
+ 'exclusive ' => false ,
45
49
'auto_delete ' => false ,
46
- 'nowait ' => false ,
47
- 'arguments ' => null ,
48
- 'ticket ' => null ,
49
- 'declare ' => true ,
50
- ) ;
50
+ 'nowait ' => false ,
51
+ 'arguments ' => null ,
52
+ 'ticket ' => null ,
53
+ 'declare ' => true ,
54
+ ] ;
51
55
52
56
/**
53
57
* @var EventDispatcherInterface
@@ -68,12 +72,21 @@ public function __construct(AbstractConnection $conn, AMQPChannel $ch = null, $c
68
72
$ this ->getChannel ();
69
73
}
70
74
71
- $ this ->consumerTag = empty ($ consumerTag ) ? sprintf ("PHPPROCESS_%s_%s " , gethostname (), getmypid ()) : $ consumerTag ;
75
+ $ this ->consumerTag = empty ($ consumerTag ) ? sprintf (
76
+ "PHPPROCESS_%s_%s " ,
77
+ gethostname (),
78
+ getmypid ()
79
+ ) : $ consumerTag ;
72
80
73
81
$ this ->logger = new NullLogger ();
74
82
}
75
83
76
84
public function __destruct ()
85
+ {
86
+ $ this ->close ();
87
+ }
88
+
89
+ public function close ()
77
90
{
78
91
if ($ this ->ch ) {
79
92
try {
@@ -115,6 +128,7 @@ public function getChannel()
115
128
116
129
/**
117
130
* @param AMQPChannel $ch
131
+ *
118
132
* @return void
119
133
*/
120
134
public function setChannel (AMQPChannel $ ch )
@@ -124,10 +138,12 @@ public function setChannel(AMQPChannel $ch)
124
138
125
139
/**
126
140
* @throws \InvalidArgumentException
127
- * @param array $options
141
+ *
142
+ * @param array $options
143
+ *
128
144
* @return void
129
145
*/
130
- public function setExchangeOptions (array $ options = array () )
146
+ public function setExchangeOptions (array $ options = [] )
131
147
{
132
148
if (!isset ($ options ['name ' ])) {
133
149
throw new \InvalidArgumentException ('You must provide an exchange name ' );
@@ -142,22 +158,51 @@ public function setExchangeOptions(array $options = array())
142
158
143
159
/**
144
160
* @param array $options
161
+ *
145
162
* @return void
146
163
*/
147
- public function setQueueOptions (array $ options = array () )
164
+ public function setQueueOptions (array $ options = [] )
148
165
{
149
166
$ this ->queueOptions = array_merge ($ this ->queueOptions , $ options );
150
167
}
151
168
152
169
/**
153
170
* @param string $routingKey
171
+ *
154
172
* @return void
155
173
*/
156
174
public function setRoutingKey ($ routingKey )
157
175
{
158
176
$ this ->routingKey = $ routingKey ;
159
177
}
160
178
179
+ public function setupFabric ()
180
+ {
181
+ if (!$ this ->exchangeDeclared ) {
182
+ $ this ->exchangeDeclare ();
183
+ }
184
+
185
+ if (!$ this ->queueDeclared ) {
186
+ $ this ->queueDeclare ();
187
+ }
188
+ }
189
+
190
+ /**
191
+ * disables the automatic SetupFabric when using a consumer or producer
192
+ */
193
+ public function disableAutoSetupFabric ()
194
+ {
195
+ $ this ->autoSetupFabric = false ;
196
+ }
197
+
198
+ /**
199
+ * @param LoggerInterface $logger
200
+ */
201
+ public function setLogger ($ logger )
202
+ {
203
+ $ this ->logger = $ logger ;
204
+ }
205
+
161
206
/**
162
207
* Declares exchange
163
208
*/
@@ -173,7 +218,8 @@ protected function exchangeDeclare()
173
218
$ this ->exchangeOptions ['internal ' ],
174
219
$ this ->exchangeOptions ['nowait ' ],
175
220
$ this ->exchangeOptions ['arguments ' ],
176
- $ this ->exchangeOptions ['ticket ' ]);
221
+ $ this ->exchangeOptions ['ticket ' ]
222
+ );
177
223
178
224
$ this ->exchangeDeclared = true ;
179
225
}
@@ -185,10 +231,16 @@ protected function exchangeDeclare()
185
231
protected function queueDeclare ()
186
232
{
187
233
if ($ this ->queueOptions ['declare ' ]) {
188
- list ($ queueName , ,) = $ this ->getChannel ()->queue_declare ($ this ->queueOptions ['name ' ], $ this ->queueOptions ['passive ' ],
189
- $ this ->queueOptions ['durable ' ], $ this ->queueOptions ['exclusive ' ],
190
- $ this ->queueOptions ['auto_delete ' ], $ this ->queueOptions ['nowait ' ],
191
- $ this ->queueOptions ['arguments ' ], $ this ->queueOptions ['ticket ' ]);
234
+ list ($ queueName , ,) = $ this ->getChannel ()->queue_declare (
235
+ $ this ->queueOptions ['name ' ],
236
+ $ this ->queueOptions ['passive ' ],
237
+ $ this ->queueOptions ['durable ' ],
238
+ $ this ->queueOptions ['exclusive ' ],
239
+ $ this ->queueOptions ['auto_delete ' ],
240
+ $ this ->queueOptions ['nowait ' ],
241
+ $ this ->queueOptions ['arguments ' ],
242
+ $ this ->queueOptions ['ticket ' ]
243
+ );
192
244
193
245
if (isset ($ this ->queueOptions ['routing_keys ' ]) && count ($ this ->queueOptions ['routing_keys ' ]) > 0 ) {
194
246
foreach ($ this ->queueOptions ['routing_keys ' ] as $ routingKey ) {
0 commit comments