Skip to content

Commit ac52e08

Browse files
author
Aleksey Kulagin
committed
fix ssl connection bug
1 parent e17696a commit ac52e08

File tree

3 files changed

+47
-12
lines changed

3 files changed

+47
-12
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'],

controllers/RabbitMQController.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ public function actionDeleteAll(string $connection = Configuration::DEFAULT_CONN
250250
if ($this->interactive)
251251
{
252252
$input = Console::prompt('Are you sure you want to delete all queues and exchanges?', ['default' => 'yes']);
253-
if ($input !== 'yes')
253+
if ($input !== 'yes' && $input !== 'y')
254254
{
255255
$this->stderr(Console::ansiFormat("Aborted.\n", [Console::FG_RED]));
256256

0 commit comments

Comments
 (0)