Skip to content

Commit

Permalink
Merge pull request #33 from Evaneos/add_lazy_ssl_connection
Browse files Browse the repository at this point in the history
Add a way to create lazy SSL connections
  • Loading branch information
Rénald Casagraude authored Apr 18, 2020
2 parents cb743b3 + eba7751 commit 749d895
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 11 deletions.
52 changes: 52 additions & 0 deletions src/Connection/PhpAmqpLib/AMQPLazySSLConnection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Burrow\Connection\PhpAmqpLib;

use PhpAmqpLib\Connection\AMQPSSLConnection;

class AMQPLazySSLConnection extends AMQPSSLConnection
{
/**
* Gets socket from current connection
*
* @deprecated
*/
public function getSocket()
{
$this->connect();

return parent::getSocket();
}

/**
* {@inheritdoc}
*/
public function channel($channel_id = null)
{
$this->connect();

return parent::channel($channel_id);
}

/**
* @return null|\PhpAmqpLib\Wire\IO\AbstractIO
*/
public function getIO()
{
if (empty($this->io)) {
$this->connect();
}

return $this->io;
}

/**
* Should the connection be attempted during construction?
*
* @return bool
*/
public function connectOnConstruct()
{
return false;
}
}
41 changes: 30 additions & 11 deletions src/Driver/DriverFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

namespace Burrow\Driver;

use Burrow\Connection\PhpAmqpLib\AMQPLazySSLConnection;
use Burrow\Driver;
use PhpAmqpLib\Connection\AbstractConnection;
use PhpAmqpLib\Connection\AMQPLazyConnection;
use PhpAmqpLib\Connection\AMQPStreamConnection;

class DriverFactory
{
Expand All @@ -14,12 +16,18 @@ class DriverFactory
* It accepts a PECL connection, a PhpAmqpLib connection or an array following
* the following schema :
* [
* 'host' => '<hostValue>',
* 'port' => '<portValue>',
* 'user' => '<userValue>',
* 'pwd' => '<pwdValue>'
* 'host' => '<hostValue>',
* 'port' => '<portValue>',
* 'user' => '<userValue>',
* 'pwd' => '<pwdValue>',
* 'ssl' => true, // Optional: default to false
* 'ssl_options' => [ // Optional: default to [], see https://www.php.net/manual/context.ssl.php
* 'capath' => '/etc/ssl/certs',
* 'verify_peer' => true,
* ],
* ]
*
* SSL options is limited to PhpAmqpLib.
* If you provide an array, the PECL extension has precedence over the PhpAmqpLib.
*
* @param $connection
Expand Down Expand Up @@ -87,18 +95,29 @@ protected static function getPeclConnection(array $connection)
/**
* @param string[] $connection
*
* @return AMQPLazyConnection
* @return AMQPStreamConnection
*
* @codeCoverageIgnore
*/
protected static function getPhpAmqpLibConnection(array $connection)
{
return new AMQPLazyConnection(
$connection['host'],
$connection['port'],
$connection['user'],
$connection['pwd']
);
if (isset($connection['ssl']) && $connection['ssl']) {
return new AMQPLazySSLConnection(
$connection['host'],
$connection['port'],
$connection['user'],
$connection['pwd'],
'/',
isset($connection['ssl_options']) ? $connection['ssl_options'] : []
);
} else {
return new AMQPLazyConnection(
$connection['host'],
$connection['port'],
$connection['user'],
$connection['pwd']
);
}
}

/**
Expand Down

0 comments on commit 749d895

Please sign in to comment.