Skip to content

Commit

Permalink
Merge pull request #271 from KingCrunch/connect-via-url
Browse files Browse the repository at this point in the history
Configuration of connection settings as URL
  • Loading branch information
skafandri authored Oct 17, 2016
2 parents a7f2745 + 914fe28 commit 82ee45b
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 7 deletions.
1 change: 1 addition & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ protected function addConnections(ArrayNodeDefinition $node)
->canBeUnset()
->prototype('array')
->children()
->scalarNode('url')->defaultValue('')->end()
->scalarNode('host')->defaultValue('localhost')->end()
->scalarNode('port')->defaultValue(5672)->end()
->scalarNode('user')->defaultValue('guest')->end()
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@ old_sound_rabbit_mq:
#requires php_sockets.dll
use_socket: true # default false
another:
# A different (unused) connection defined by an URL. One can omit all parts,
# except the scheme (amqp:). If both segment in the URL and a key value (see above)
# are given the value from the URL takes precedence.
url: 'amqp://guest:password@localhost:5672/vhost?lazy=1&connection_timeout=6'
producers:
upload_picture:
connection: default
Expand Down
43 changes: 43 additions & 0 deletions RabbitMq/AMQPConnectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

namespace OldSound\RabbitMqBundle\RabbitMq;

use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;

class AMQPConnectionFactory
{
/** @var \ReflectionClass */
private $class;

/** @var array */
private $parameters = array(
'url' => '',
'host' => 'localhost',
'port' => 5672,
'user' => 'guest',
Expand All @@ -31,6 +34,7 @@ public function __construct($class, array $parameters)
{
$this->class = $class;
$this->parameters = array_merge($this->parameters, $parameters);
$this->parameters = $this->parseUrl($this->parameters);
if (is_array($this->parameters['ssl_context'])) {
$this->parameters['ssl_context'] = ! empty($this->parameters['ssl_context'])
? stream_context_create(array('ssl' => $this->parameters['ssl_context']))
Expand All @@ -57,4 +61,43 @@ public function createConnection()
$this->parameters['heartbeat']
);
}

private function parseUrl($parameters)
{
if (!$parameters['url']) {
return $parameters;
}

$url = parse_url($parameters['url']);

if ($url === false || !isset($url['scheme']) || $url['scheme'] !== 'amqp') {
throw new InvalidConfigurationException('Malformed parameter "url".');
}

if (isset($url['host'])) {
$parameters['host'] = $url['host'];
}
if (isset($url['port'])) {
$parameters['port'] = $url['port'];
}
if (isset($url['user'])) {
$parameters['user'] = $url['user'];
}
if (isset($url['pass'])) {
$parameters['password'] = $url['pass'];
}
if (isset($url['path'])) {
$parameters['vhost'] = $url['path'];
}

if (isset($url['query'])) {
$query = array();
parse_str($url['query'], $query);
$parameters = array_merge($parameters, $query);
}

unset($parameters['url']);

return $parameters;
}
}
18 changes: 11 additions & 7 deletions Tests/DependencyInjection/OldSoundRabbitMqExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ public function testFooConnectionDefinition()
'ssl_context' => array(),
'keepalive' => false,
'heartbeat' => 0,
'use_socket' => false
'use_socket' => false,
'url' => '',
), $factory->getArgument(1));
$this->assertEquals('%old_sound_rabbit_mq.connection.class%', $definition->getClass());
}
Expand Down Expand Up @@ -61,7 +62,8 @@ public function testSslConnectionDefinition()
),
'keepalive' => false,
'heartbeat' => 0,
'use_socket' => false
'use_socket' => false,
'url' => '',
), $factory->getArgument(1));
$this->assertEquals('%old_sound_rabbit_mq.connection.class%', $definition->getClass());
}
Expand All @@ -87,7 +89,8 @@ public function testLazyConnectionDefinition()
'ssl_context' => array(),
'keepalive' => false,
'heartbeat' => 0,
'use_socket' => false
'use_socket' => false,
'url' => '',
), $factory->getArgument(1));
$this->assertEquals('%old_sound_rabbit_mq.lazy.connection.class%', $definition->getClass());
}
Expand All @@ -113,7 +116,8 @@ public function testDefaultConnectionDefinition()
'ssl_context' => array(),
'keepalive' => false,
'heartbeat' => 0,
'use_socket' => false
'use_socket' => false,
'url' => '',
), $factory->getArgument(1));
$this->assertEquals('%old_sound_rabbit_mq.connection.class%', $definition->getClass());
}
Expand Down Expand Up @@ -518,14 +522,14 @@ public function testMultipleConsumerDefinition()
$definition->getMethodCalls()
);
}

public function testDynamicConsumerDefinition()
{
$container = $this->getContainer('test.yml');

$this->assertTrue($container->has('old_sound_rabbit_mq.foo_dyn_consumer_dynamic'));
$this->assertTrue($container->has('old_sound_rabbit_mq.bar_dyn_consumer_dynamic'));

$definition = $container->getDefinition('old_sound_rabbit_mq.foo_dyn_consumer_dynamic');
$this->assertEquals(array(
array(
Expand Down
35 changes: 35 additions & 0 deletions Tests/RabbitMq/AMQPConnectionFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,41 @@ public function testStandardConnectionParameters()
), $instance->constructParams);
}

public function testSetConnectionParametersWithUrl()
{
$factory = new AMQPConnectionFactory(
'OldSound\RabbitMqBundle\Tests\RabbitMq\Fixtures\AMQPConnection',
array(
'url' => 'amqp://bar_user:bar_password@bar_host:321/whost?keepalive=1&connection_timeout=6&read_write_timeout=6',
'host' => 'foo_host',
'port' => 123,
'user' => 'foo_user',
'password' => 'foo_password',
'vhost' => '/vhost',
)
);

/** @var AMQPConnection $instance */
$instance = $factory->createConnection();
$this->assertInstanceOf('OldSound\RabbitMqBundle\Tests\RabbitMq\Fixtures\AMQPConnection', $instance);
$this->assertEquals(array(
'bar_host', // host
321, // port
'bar_user', // user
'bar_password', // password
'/whost', // vhost
false, // insist
"AMQPLAIN", // login method
null, // login response
"en_US", // locale
6, // connection timeout
6, // read write timeout
null, // context
true, // keepalive
0, // heartbeat
), $instance->constructParams);
}

public function testSSLConnectionParameters()
{
$factory = new AMQPConnectionFactory(
Expand Down

0 comments on commit 82ee45b

Please sign in to comment.