Skip to content

Commit 2c5552a

Browse files
author
Mikhail Bakulin
authored
Merge pull request #53 from ale10257/master
Fix ssl connection bug
2 parents 457e697 + ac52e08 commit 2c5552a

6 files changed

+72
-32
lines changed

Configuration.php

+29-11
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88
use mikemadisonweb\rabbitmq\exceptions\InvalidConfigException;
99
use PhpAmqpLib\Connection\AbstractConnection;
1010
use PhpAmqpLib\Connection\AMQPLazyConnection;
11+
use PhpAmqpLib\Connection\AMQPSSLConnection;
12+
use Yii;
1113
use yii\base\Component;
14+
use yii\di\NotInstantiableException;
1215
use yii\helpers\ArrayHelper;
1316

1417
class Configuration extends Component
@@ -145,44 +148,53 @@ public function getConfig() : Configuration
145148
/**
146149
* Get connection service
147150
* @param string $connectionName
148-
* @return AbstractConnection
151+
* @return object|AbstractConnection
152+
* @throws NotInstantiableException
153+
* @throws \yii\base\InvalidConfigException
149154
*/
150155
public function getConnection(string $connectionName = '') : AbstractConnection
151156
{
152157
if ('' === $connectionName) {
153158
$connectionName = self::DEFAULT_CONNECTION_NAME;
154159
}
155160

156-
return \Yii::$container->get(sprintf(self::CONNECTION_SERVICE_NAME, $connectionName));
161+
return Yii::$container->get(sprintf(self::CONNECTION_SERVICE_NAME, $connectionName));
157162
}
158163

159164
/**
160165
* Get producer service
161166
* @param string $producerName
162-
* @return Producer
167+
* @return Producer|object
168+
* @throws \yii\base\InvalidConfigException
169+
* @throws NotInstantiableException
163170
*/
164-
public function getProducer(string $producerName) : Producer
171+
public function getProducer(string $producerName)
165172
{
166-
return \Yii::$container->get(sprintf(self::PRODUCER_SERVICE_NAME, $producerName));
173+
return Yii::$container->get(sprintf(self::PRODUCER_SERVICE_NAME, $producerName));
167174
}
168175

169176
/**
170177
* Get consumer service
171178
* @param string $consumerName
172-
* @return Consumer
179+
* @return Consumer|object
180+
* @throws NotInstantiableException
181+
* @throws \yii\base\InvalidConfigException
173182
*/
174-
public function getConsumer(string $consumerName) : Consumer
183+
public function getConsumer(string $consumerName)
175184
{
176-
return \Yii::$container->get(sprintf(self::CONSUMER_SERVICE_NAME, $consumerName));
185+
return Yii::$container->get(sprintf(self::CONSUMER_SERVICE_NAME, $consumerName));
177186
}
178187

179188
/**
180189
* Get routing service
181-
* @return Routing
190+
* @param AbstractConnection $connection
191+
* @return Routing|object|string
192+
* @throws NotInstantiableException
193+
* @throws \yii\base\InvalidConfigException
182194
*/
183-
public function getRouting(AbstractConnection $connection) : Routing
195+
public function getRouting(AbstractConnection $connection)
184196
{
185-
return \Yii::$container->get(Configuration::ROUTING_SERVICE_NAME, ['conn' => $connection]);
197+
return Yii::$container->get(Configuration::ROUTING_SERVICE_NAME, ['conn' => $connection]);
186198
}
187199

188200
/**
@@ -261,6 +273,12 @@ protected function validateRequired()
261273
if (isset($connection['type']) && !is_subclass_of($connection['type'], AbstractConnection::class)) {
262274
throw new InvalidConfigException('Connection type should be a subclass of PhpAmqpLib\Connection\AbstractConnection.');
263275
}
276+
if (!empty($connection['ssl_context']) && empty($connection['type'])) {
277+
throw new InvalidConfigException('If you are using a ssl connection, the connection type must be AMQPSSLConnection::class');
278+
}
279+
if (!empty($connection['ssl_context']) && $connection['type'] !== AMQPSSLConnection::class) {
280+
throw new InvalidConfigException('If you are using a ssl connection, the connection type must be AMQPSSLConnection::class');
281+
}
264282
}
265283

266284
foreach ($this->exchanges as $exchange) {

components/AbstractConnectionFactory.php

+17
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,23 @@ public function __construct($class, array $parameters)
2929
*/
3030
public function createConnection() : AbstractConnection
3131
{
32+
if ($this->_parameters['ssl_context'] !== null) {
33+
return new $this->_class(
34+
$this->_parameters['host'],
35+
$this->_parameters['port'],
36+
$this->_parameters['user'],
37+
$this->_parameters['password'],
38+
$this->_parameters['vhost'],
39+
$this->_parameters['ssl_context'],
40+
[
41+
'connection_timeout' => $this->_parameters['connection_timeout'],
42+
'read_write_timeout' => $this->_parameters['read_write_timeout'],
43+
'keepalive' => $this->_parameters['keepalive'],
44+
'heartbeat' => $this->_parameters['heartbeat'],
45+
'channel_rpc_timeout' => $this->_parameters['channel_rpc_timeout'],
46+
]
47+
);
48+
}
3249
return new $this->_class(
3350
$this->_parameters['host'],
3451
$this->_parameters['port'],

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
],
1313
"require": {
1414
"php": "^7.0",
15-
"yiisoft/yii2": "^2.0",
15+
"yiisoft/yii2": "^2.0.13",
1616
"php-amqplib/php-amqplib": "^2.9"
1717
},
1818
"require-dev": {

controllers/RabbitMQController.php

+21-20
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use ReflectionException;
1111
use yii\base\Action;
1212
use yii\console\Controller;
13+
use yii\console\ExitCode;
1314
use yii\helpers\Console;
1415

1516
/**
@@ -101,7 +102,7 @@ public function actionConsume(string $name): int
101102
{
102103
$this->stderr(Console::ansiFormat("Consumer `{$name}` doesn't exist: {$e->getMessage()}\n", [Console::FG_RED]));
103104

104-
return self::EXIT_CODE_ERROR;
105+
return ExitCode::UNSPECIFIED_ERROR;
105106
}
106107

107108
$this->validateConsumerOptions($consumer);
@@ -111,7 +112,7 @@ public function actionConsume(string $name): int
111112
}
112113
$consumer->consume($this->messagesLimit);
113114

114-
return self::EXIT_CODE_NORMAL;
115+
return ExitCode::OK;
115116
}
116117

117118
/**
@@ -136,15 +137,15 @@ public function actionPublish(string $producerName, string $exchangeName, string
136137
{
137138
$this->stderr(Console::ansiFormat("Producer `{$producerName}` doesn't exist: {$e->getMessage()}\n", [Console::FG_RED]));
138139

139-
return self::EXIT_CODE_ERROR;
140+
return ExitCode::UNSPECIFIED_ERROR;
140141
}
141142

142143
$data = '';
143144
if (posix_isatty(STDIN))
144145
{
145146
$this->stderr(Console::ansiFormat("Please pipe in some data in order to send it.\n", [Console::FG_RED]));
146147

147-
return self::EXIT_CODE_ERROR;
148+
return ExitCode::UNSPECIFIED_ERROR;
148149
}
149150
while (!feof(STDIN))
150151
{
@@ -153,7 +154,7 @@ public function actionPublish(string $producerName, string $exchangeName, string
153154
$producer->publish($data, $exchangeName, $routingKey);
154155
$this->stdout("Message was successfully published.\n", Console::FG_GREEN);
155156

156-
return self::EXIT_CODE_NORMAL;
157+
return ExitCode::OK;
157158
}
158159

159160
/**
@@ -175,11 +176,11 @@ public function actionDeclareAll(string $connectionName = Configuration::DEFAULT
175176
Console::ansiFormat("All configured entries was successfully declared.\n", [Console::FG_GREEN])
176177
);
177178

178-
return self::EXIT_CODE_NORMAL;
179+
return ExitCode::OK;
179180
}
180181
$this->stderr(Console::ansiFormat("No queues, exchanges or bindings configured.\n", [Console::FG_RED]));
181182

182-
return self::EXIT_CODE_ERROR;
183+
return ExitCode::UNSPECIFIED_ERROR;
183184
}
184185

185186
/**
@@ -201,12 +202,12 @@ public function actionDeclareExchange(
201202
{
202203
$this->stderr(Console::ansiFormat("Exchange `{$exchangeName}` is already exists.\n", [Console::FG_RED]));
203204

204-
return self::EXIT_CODE_ERROR;
205+
return ExitCode::UNSPECIFIED_ERROR;
205206
}
206207
$routing->declareExchange($exchangeName);
207208
$this->stdout(Console::ansiFormat("Exchange `{$exchangeName}` was declared.\n", [Console::FG_GREEN]));
208209

209-
return self::EXIT_CODE_NORMAL;
210+
return ExitCode::OK;
210211
}
211212

212213
/**
@@ -228,12 +229,12 @@ public function actionDeclareQueue(
228229
{
229230
$this->stderr(Console::ansiFormat("Queue `{$queueName}` is already exists.\n", [Console::FG_RED]));
230231

231-
return self::EXIT_CODE_ERROR;
232+
return ExitCode::UNSPECIFIED_ERROR;
232233
}
233234
$routing->declareQueue($queueName);
234235
$this->stdout(Console::ansiFormat("Queue `{$queueName}` was declared.\n", [Console::FG_GREEN]));
235236

236-
return self::EXIT_CODE_NORMAL;
237+
return ExitCode::OK;
237238
}
238239

239240
/**
@@ -249,19 +250,19 @@ public function actionDeleteAll(string $connection = Configuration::DEFAULT_CONN
249250
if ($this->interactive)
250251
{
251252
$input = Console::prompt('Are you sure you want to delete all queues and exchanges?', ['default' => 'yes']);
252-
if ($input !== 'yes')
253+
if ($input !== 'yes' && $input !== 'y')
253254
{
254255
$this->stderr(Console::ansiFormat("Aborted.\n", [Console::FG_RED]));
255256

256-
return self::EXIT_CODE_ERROR;
257+
return ExitCode::UNSPECIFIED_ERROR;
257258
}
258259
}
259260
$conn = \Yii::$app->rabbitmq->getConnection($connection);
260261
$routing = \Yii::$app->rabbitmq->getRouting($conn);
261262
$routing->deleteAll();
262263
$this->stdout(Console::ansiFormat("All configured entries was deleted.\n", [Console::FG_GREEN]));
263264

264-
return self::EXIT_CODE_NORMAL;
265+
return ExitCode::OK;
265266
}
266267

267268
/**
@@ -284,15 +285,15 @@ public function actionDeleteExchange(
284285
{
285286
$this->stderr(Console::ansiFormat("Aborted.\n", [Console::FG_RED]));
286287

287-
return self::EXIT_CODE_ERROR;
288+
return ExitCode::UNSPECIFIED_ERROR;
288289
}
289290
}
290291
$conn = \Yii::$app->rabbitmq->getConnection($connectionName);
291292
$routing = \Yii::$app->rabbitmq->getRouting($conn);
292293
$routing->deleteExchange($exchangeName);
293294
$this->stdout(Console::ansiFormat("Exchange `{$exchangeName}` was deleted.\n", [Console::FG_GREEN]));
294295

295-
return self::EXIT_CODE_NORMAL;
296+
return ExitCode::OK;
296297
}
297298

298299
/**
@@ -315,7 +316,7 @@ public function actionDeleteQueue(
315316
{
316317
$this->stderr(Console::ansiFormat("Aborted.\n", [Console::FG_RED]));
317318

318-
return self::EXIT_CODE_ERROR;
319+
return ExitCode::UNSPECIFIED_ERROR;
319320
}
320321
}
321322

@@ -324,7 +325,7 @@ public function actionDeleteQueue(
324325
$routing->deleteQueue($queueName);
325326
$this->stdout(Console::ansiFormat("Queue `{$queueName}` was deleted.\n", [Console::FG_GREEN]));
326327

327-
return self::EXIT_CODE_NORMAL;
328+
return ExitCode::OK;
328329
}
329330

330331
/**
@@ -350,7 +351,7 @@ public function actionPurgeQueue(
350351
{
351352
$this->stderr(Console::ansiFormat("Aborted.\n", [Console::FG_RED]));
352353

353-
return self::EXIT_CODE_ERROR;
354+
return ExitCode::UNSPECIFIED_ERROR;
354355
}
355356
}
356357

@@ -359,7 +360,7 @@ public function actionPurgeQueue(
359360
$routing->purgeQueue($queueName);
360361
$this->stdout(Console::ansiFormat("Queue `{$queueName}` was purged.\n", [Console::FG_GREEN]));
361362

362-
return self::EXIT_CODE_NORMAL;
363+
return ExitCode::OK;
363364
}
364365

365366
/**

events/RabbitMQConsumerEvent.php

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace mikemadisonweb\rabbitmq\events;
44

5+
use mikemadisonweb\rabbitmq\components\Consumer;
6+
use PhpAmqpLib\Message\AMQPMessage;
57
use yii\base\Event;
68

79
class RabbitMQConsumerEvent extends Event

events/RabbitMQPublisherEvent.php

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace mikemadisonweb\rabbitmq\events;
44

5+
use mikemadisonweb\rabbitmq\components\Consumer;
6+
use PhpAmqpLib\Message\AMQPMessage;
57
use yii\base\Event;
68

79
class RabbitMQPublisherEvent extends Event

0 commit comments

Comments
 (0)