44
55use OldSound \RabbitMqBundle \Provider \ConnectionParametersProviderInterface ;
66use PhpAmqpLib \Connection \AbstractConnection ;
7+ use PhpAmqpLib \Connection \AMQPSocketConnection ;
78use Symfony \Component \Config \Definition \Exception \InvalidConfigurationException ;
89
910class AMQPConnectionFactory
@@ -24,6 +25,7 @@ class AMQPConnectionFactory
2425 'ssl_context ' => null ,
2526 'keepalive ' => false ,
2627 'heartbeat ' => 0 ,
28+ 'hosts ' => []
2729 );
2830
2931 /**
@@ -43,6 +45,15 @@ public function __construct(
4345 $ this ->class = $ class ;
4446 $ this ->parameters = array_merge ($ this ->parameters , $ parameters );
4547 $ this ->parameters = $ this ->parseUrl ($ this ->parameters );
48+
49+ foreach ($ this ->parameters ['hosts ' ] as $ key => $ hostParameters ) {
50+ if (!isset ($ hostParameters ['url ' ])) {
51+ continue ;
52+ }
53+
54+ $ this ->parameters ['hosts ' ][$ key ] = $ this ->parseUrl ($ hostParameters );
55+ }
56+
4657 if (is_array ($ this ->parameters ['ssl_context ' ])) {
4758 $ this ->parameters ['ssl_context ' ] = ! empty ($ this ->parameters ['ssl_context ' ])
4859 ? stream_context_create (array ('ssl ' => $ this ->parameters ['ssl_context ' ]))
@@ -57,50 +68,26 @@ public function __construct(
5768 * Creates the appropriate connection using current parameters.
5869 *
5970 * @return AbstractConnection
71+ * @throws \Exception
6072 */
6173 public function createConnection ()
6274 {
63- $ ref = new \ReflectionClass ($ this ->class );
64-
6575 if (isset ($ this ->parameters ['constructor_args ' ]) && is_array ($ this ->parameters ['constructor_args ' ])) {
66- return $ ref ->newInstanceArgs ($ this ->parameters ['constructor_args ' ]);
76+ $ constructorArgs = array_values ($ this ->parameters ['constructor_args ' ]);
77+ return new $ this ->class (...$ constructorArgs );
6778 }
6879
69- if ($ this ->class == 'PhpAmqpLib\Connection\AMQPSocketConnection ' || is_subclass_of ($ this ->class , 'PhpAmqpLib\Connection\AMQPSocketConnection ' )) {
70- return $ ref ->newInstanceArgs ([
71- $ this ->parameters ['host ' ],
72- $ this ->parameters ['port ' ],
73- $ this ->parameters ['user ' ],
74- $ this ->parameters ['password ' ],
75- $ this ->parameters ['vhost ' ],
76- false , // insist
77- 'AMQPLAIN ' , // login_method
78- null , // login_response
79- 'en_US ' , // locale
80- isset ($ this ->parameters ['read_timeout ' ]) ? $ this ->parameters ['read_timeout ' ] : $ this ->parameters ['read_write_timeout ' ],
81- $ this ->parameters ['keepalive ' ],
82- isset ($ this ->parameters ['write_timeout ' ]) ? $ this ->parameters ['write_timeout ' ] : $ this ->parameters ['read_write_timeout ' ],
83- $ this ->parameters ['heartbeat ' ]
84- ]
85- );
86- } else {
87- return $ ref ->newInstanceArgs ([
88- $ this ->parameters ['host ' ],
89- $ this ->parameters ['port ' ],
90- $ this ->parameters ['user ' ],
91- $ this ->parameters ['password ' ],
92- $ this ->parameters ['vhost ' ],
93- false , // insist
94- 'AMQPLAIN ' , // login_method
95- null , // login_response
96- 'en_US ' , // locale
97- $ this ->parameters ['connection_timeout ' ],
98- $ this ->parameters ['read_write_timeout ' ],
99- $ this ->parameters ['ssl_context ' ],
100- $ this ->parameters ['keepalive ' ],
101- $ this ->parameters ['heartbeat ' ]
102- ]);
80+ $ hosts = $ this ->parameters ['hosts ' ] ?: [$ this ->parameters ];
81+ $ options = $ this ->parameters ;
82+ unset($ options ['hosts ' ]);
83+
84+ if ($ this ->class == AMQPSocketConnection::class || is_subclass_of ($ this ->class , AMQPSocketConnection::class)) {
85+ $ options ['read_timeout ' ] = $ options ['read_timeout ' ] ?? $ this ->parameters ['read_write_timeout ' ];
86+ $ options ['write_timeout ' ] = $ options ['write_timeout ' ] ?? $ this ->parameters ['read_write_timeout ' ];
10387 }
88+
89+ // No need to unpack options, they will be handled inside connection classes
90+ return $ this ->class ::create_connection ($ hosts , $ options );
10491 }
10592
10693 /**
0 commit comments