diff --git a/README.md b/README.md index 8ad2cdc..870e27a 100644 --- a/README.md +++ b/README.md @@ -57,18 +57,28 @@ In the [README](https://github.com/videlalvaro/RabbitMqBundle/blob/master/README $app->register(new RabbitServiceProvider(), [ 'rabbit.connections' => [ 'default' => [ - 'host' => 'localhost', - 'port' => 5672, - 'user' => 'guest', - 'password' => 'guest', - 'vhost' => '/' + 'host' => 'localhost', + 'port' => 5672, + 'user' => 'guest', + 'password' => 'guest', + 'vhost' => '/', + 'lazy' => false, + 'connection_timeout' => 3, + 'read_write_timeout' => 6, + 'keepalive' => false, + 'heartbeat' => 3 ], 'another' => [ - 'host' => 'another_host', - 'port' => 5672, - 'user' => 'guest', - 'password' => 'guest', - 'vhost' => '/' + 'host' => 'another_host', + 'port' => 5672, + 'user' => 'guest', + 'password' => 'guest', + 'vhost' => '/' + 'lazy' => false, + 'connection_timeout' => 3, + 'read_write_timeout' => 6, + 'keepalive' => false, + 'heartbeat' => 3 ] ], 'rabbit.producers' => [ diff --git a/src/fiunchinho/Silex/Provider/RabbitServiceProvider.php b/src/fiunchinho/Silex/Provider/RabbitServiceProvider.php index 757afc6..d71038a 100644 --- a/src/fiunchinho/Silex/Provider/RabbitServiceProvider.php +++ b/src/fiunchinho/Silex/Provider/RabbitServiceProvider.php @@ -42,33 +42,28 @@ private function loadConnections(Container $container) $connections = []; foreach ($container['rabbit.connections'] as $name => $options) { - $lazyConnection = false; - - if (isset($container['rabbit.connections'][$name]['lazy'])) { - if ($container['rabbit.connections'][$name]['lazy'] === true) { - $lazyConnection = true; - } - } - - switch ($lazyConnection) { - case (true): - $connection = new AMQPLazyConnection( - $container['rabbit.connections'][$name]['host'], - $container['rabbit.connections'][$name]['port'], - $container['rabbit.connections'][$name]['user'], - $container['rabbit.connections'][$name]['password'], - $container['rabbit.connections'][$name]['vhost'] - ); - break; - default: - $connection = new AMQPConnection( - $container['rabbit.connections'][$name]['host'], - $container['rabbit.connections'][$name]['port'], - $container['rabbit.connections'][$name]['user'], - $container['rabbit.connections'][$name]['password'], - $container['rabbit.connections'][$name]['vhost'] - ); - } + $connectionClass = isset($options['lazy']) && $options['lazy'] ? AMQPLazyConnection::class : AMQPConnection::class; + $connectionTimeout = isset($options['connection_timeout']) ? $options['connection_timeout'] : 3; + $readWriteTimeout = isset($options['read_write_timeout']) ? $options['read_write_timeout'] : 6; + $keepalive = isset($options['keepalive']) ? $options['keepalive'] : false; + $heartbeat = isset($options['heartbeat']) ? $options['heartbeat'] : 3; + + $connection = new $connectionClass( + $options['host'], + $options['port'], + $options['user'], + $options['password'], + $options['vhost'], + false, + 'AMQPLAIN', + null, + 'en_US', + $connectionTimeout, + $readWriteTimeout, + null, + $keepalive, + $heartbeat + ); $connections[$name] = $connection; }