Skip to content

Commit 914fe28

Browse files
committed
Introduce configuration to set up connections from an URI
Something like amqp://foo:bar@localhost/baz The intention is to allow to specify exactly one configuration option per connection, which is easier to outsource into parameter files.
1 parent a7f2745 commit 914fe28

File tree

5 files changed

+95
-7
lines changed

5 files changed

+95
-7
lines changed

DependencyInjection/Configuration.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ protected function addConnections(ArrayNodeDefinition $node)
5050
->canBeUnset()
5151
->prototype('array')
5252
->children()
53+
->scalarNode('url')->defaultValue('')->end()
5354
->scalarNode('host')->defaultValue('localhost')->end()
5455
->scalarNode('port')->defaultValue(5672)->end()
5556
->scalarNode('user')->defaultValue('guest')->end()

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,11 @@ old_sound_rabbit_mq:
113113
114114
#requires php_sockets.dll
115115
use_socket: true # default false
116+
another:
117+
# A different (unused) connection defined by an URL. One can omit all parts,
118+
# except the scheme (amqp:). If both segment in the URL and a key value (see above)
119+
# are given the value from the URL takes precedence.
120+
url: 'amqp://guest:password@localhost:5672/vhost?lazy=1&connection_timeout=6'
116121
producers:
117122
upload_picture:
118123
connection: default

RabbitMq/AMQPConnectionFactory.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22

33
namespace OldSound\RabbitMqBundle\RabbitMq;
44

5+
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
6+
57
class AMQPConnectionFactory
68
{
79
/** @var \ReflectionClass */
810
private $class;
911

1012
/** @var array */
1113
private $parameters = array(
14+
'url' => '',
1215
'host' => 'localhost',
1316
'port' => 5672,
1417
'user' => 'guest',
@@ -31,6 +34,7 @@ public function __construct($class, array $parameters)
3134
{
3235
$this->class = $class;
3336
$this->parameters = array_merge($this->parameters, $parameters);
37+
$this->parameters = $this->parseUrl($this->parameters);
3438
if (is_array($this->parameters['ssl_context'])) {
3539
$this->parameters['ssl_context'] = ! empty($this->parameters['ssl_context'])
3640
? stream_context_create(array('ssl' => $this->parameters['ssl_context']))
@@ -57,4 +61,43 @@ public function createConnection()
5761
$this->parameters['heartbeat']
5862
);
5963
}
64+
65+
private function parseUrl($parameters)
66+
{
67+
if (!$parameters['url']) {
68+
return $parameters;
69+
}
70+
71+
$url = parse_url($parameters['url']);
72+
73+
if ($url === false || !isset($url['scheme']) || $url['scheme'] !== 'amqp') {
74+
throw new InvalidConfigurationException('Malformed parameter "url".');
75+
}
76+
77+
if (isset($url['host'])) {
78+
$parameters['host'] = $url['host'];
79+
}
80+
if (isset($url['port'])) {
81+
$parameters['port'] = $url['port'];
82+
}
83+
if (isset($url['user'])) {
84+
$parameters['user'] = $url['user'];
85+
}
86+
if (isset($url['pass'])) {
87+
$parameters['password'] = $url['pass'];
88+
}
89+
if (isset($url['path'])) {
90+
$parameters['vhost'] = $url['path'];
91+
}
92+
93+
if (isset($url['query'])) {
94+
$query = array();
95+
parse_str($url['query'], $query);
96+
$parameters = array_merge($parameters, $query);
97+
}
98+
99+
unset($parameters['url']);
100+
101+
return $parameters;
102+
}
60103
}

Tests/DependencyInjection/OldSoundRabbitMqExtensionTest.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ public function testFooConnectionDefinition()
3333
'ssl_context' => array(),
3434
'keepalive' => false,
3535
'heartbeat' => 0,
36-
'use_socket' => false
36+
'use_socket' => false,
37+
'url' => '',
3738
), $factory->getArgument(1));
3839
$this->assertEquals('%old_sound_rabbit_mq.connection.class%', $definition->getClass());
3940
}
@@ -61,7 +62,8 @@ public function testSslConnectionDefinition()
6162
),
6263
'keepalive' => false,
6364
'heartbeat' => 0,
64-
'use_socket' => false
65+
'use_socket' => false,
66+
'url' => '',
6567
), $factory->getArgument(1));
6668
$this->assertEquals('%old_sound_rabbit_mq.connection.class%', $definition->getClass());
6769
}
@@ -87,7 +89,8 @@ public function testLazyConnectionDefinition()
8789
'ssl_context' => array(),
8890
'keepalive' => false,
8991
'heartbeat' => 0,
90-
'use_socket' => false
92+
'use_socket' => false,
93+
'url' => '',
9194
), $factory->getArgument(1));
9295
$this->assertEquals('%old_sound_rabbit_mq.lazy.connection.class%', $definition->getClass());
9396
}
@@ -113,7 +116,8 @@ public function testDefaultConnectionDefinition()
113116
'ssl_context' => array(),
114117
'keepalive' => false,
115118
'heartbeat' => 0,
116-
'use_socket' => false
119+
'use_socket' => false,
120+
'url' => '',
117121
), $factory->getArgument(1));
118122
$this->assertEquals('%old_sound_rabbit_mq.connection.class%', $definition->getClass());
119123
}
@@ -518,14 +522,14 @@ public function testMultipleConsumerDefinition()
518522
$definition->getMethodCalls()
519523
);
520524
}
521-
525+
522526
public function testDynamicConsumerDefinition()
523527
{
524528
$container = $this->getContainer('test.yml');
525-
529+
526530
$this->assertTrue($container->has('old_sound_rabbit_mq.foo_dyn_consumer_dynamic'));
527531
$this->assertTrue($container->has('old_sound_rabbit_mq.bar_dyn_consumer_dynamic'));
528-
532+
529533
$definition = $container->getDefinition('old_sound_rabbit_mq.foo_dyn_consumer_dynamic');
530534
$this->assertEquals(array(
531535
array(

Tests/RabbitMq/AMQPConnectionFactoryTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,41 @@ public function testStandardConnectionParameters()
6969
), $instance->constructParams);
7070
}
7171

72+
public function testSetConnectionParametersWithUrl()
73+
{
74+
$factory = new AMQPConnectionFactory(
75+
'OldSound\RabbitMqBundle\Tests\RabbitMq\Fixtures\AMQPConnection',
76+
array(
77+
'url' => 'amqp://bar_user:bar_password@bar_host:321/whost?keepalive=1&connection_timeout=6&read_write_timeout=6',
78+
'host' => 'foo_host',
79+
'port' => 123,
80+
'user' => 'foo_user',
81+
'password' => 'foo_password',
82+
'vhost' => '/vhost',
83+
)
84+
);
85+
86+
/** @var AMQPConnection $instance */
87+
$instance = $factory->createConnection();
88+
$this->assertInstanceOf('OldSound\RabbitMqBundle\Tests\RabbitMq\Fixtures\AMQPConnection', $instance);
89+
$this->assertEquals(array(
90+
'bar_host', // host
91+
321, // port
92+
'bar_user', // user
93+
'bar_password', // password
94+
'/whost', // vhost
95+
false, // insist
96+
"AMQPLAIN", // login method
97+
null, // login response
98+
"en_US", // locale
99+
6, // connection timeout
100+
6, // read write timeout
101+
null, // context
102+
true, // keepalive
103+
0, // heartbeat
104+
), $instance->constructParams);
105+
}
106+
72107
public function testSSLConnectionParameters()
73108
{
74109
$factory = new AMQPConnectionFactory(

0 commit comments

Comments
 (0)