Skip to content

Commit 3a5e688

Browse files
committed
allow generic parameters to be passed to connection constructors
1 parent ed80391 commit 3a5e688

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

Provider/ConnectionParametersProviderInterface.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,12 @@ interface ConnectionParametersProviderInterface
2525
* 'keepalive' => false,
2626
* 'heartbeat' => 0,
2727
* 'use_socket' => true,
28+
* 'constructor_args' => array(...)
2829
* )
2930
*
31+
* If constructor_args is present, all the other parameters are ignored; constructor_args are passes as constructor
32+
* arguments.
33+
*
3034
* @return array
3135
*/
3236
public function getConnectionParameters();

RabbitMq/AMQPConnectionFactory.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ public function __construct(
6060
*/
6161
public function createConnection()
6262
{
63+
if (isset($this->parameters['constructor_args']) && is_array($this->parameters['constructor_args'])) {
64+
$ref = new \ReflectionClass($this->class);
65+
return $ref->newInstanceArgs($this->parameters['constructor_args']);
66+
}
67+
6368
if ($this->class == 'PhpAmqpLib\Connection\AMQPSocketConnection' || is_subclass_of($this->class , 'PhpAmqpLib\Connection\AMQPSocketConnection')) {
6469
return new $this->class(
6570
$this->parameters['host'],

Tests/RabbitMq/AMQPConnectionFactoryTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,28 @@ public function testSSLConnectionParameters()
269269
), $instance->constructParams);
270270
}
271271

272+
public function testConnectionsParametersProviderWithConstructorArgs()
273+
{
274+
$connectionParametersProvider = $this->prepareConnectionParametersProvider();
275+
$connectionParametersProvider->expects($this->once())
276+
->method('getConnectionParameters')
277+
->will($this->returnValue(
278+
array(
279+
'constructor_args' => array(1,2,3,4)
280+
)
281+
));
282+
$factory = new AMQPConnectionFactory(
283+
'OldSound\RabbitMqBundle\Tests\RabbitMq\Fixtures\AMQPConnection',
284+
array(),
285+
$connectionParametersProvider
286+
);
287+
288+
/** @var AMQPConnection $instance */
289+
$instance = $factory->createConnection();
290+
$this->assertInstanceOf('OldSound\RabbitMqBundle\Tests\RabbitMq\Fixtures\AMQPConnection', $instance);
291+
$this->assertEquals(array(1,2,3,4), $instance->constructParams);
292+
}
293+
272294
public function testConnectionsParametersProvider()
273295
{
274296
$connectionParametersProvider = $this->prepareConnectionParametersProvider();

0 commit comments

Comments
 (0)