4
4
5
5
use OldSound \RabbitMqBundle \Provider \ConnectionParametersProviderInterface ;
6
6
use PhpAmqpLib \Connection \AbstractConnection ;
7
+ use PhpAmqpLib \Connection \AMQPSocketConnection ;
7
8
use Symfony \Component \Config \Definition \Exception \InvalidConfigurationException ;
8
9
9
10
class AMQPConnectionFactory
@@ -24,6 +25,7 @@ class AMQPConnectionFactory
24
25
'ssl_context ' => null ,
25
26
'keepalive ' => false ,
26
27
'heartbeat ' => 0 ,
28
+ 'hosts ' => []
27
29
);
28
30
29
31
/**
@@ -43,6 +45,15 @@ public function __construct(
43
45
$ this ->class = $ class ;
44
46
$ this ->parameters = array_merge ($ this ->parameters , $ parameters );
45
47
$ 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
+
46
57
if (is_array ($ this ->parameters ['ssl_context ' ])) {
47
58
$ this ->parameters ['ssl_context ' ] = ! empty ($ this ->parameters ['ssl_context ' ])
48
59
? stream_context_create (array ('ssl ' => $ this ->parameters ['ssl_context ' ]))
@@ -57,50 +68,26 @@ public function __construct(
57
68
* Creates the appropriate connection using current parameters.
58
69
*
59
70
* @return AbstractConnection
71
+ * @throws \Exception
60
72
*/
61
73
public function createConnection ()
62
74
{
63
- $ ref = new \ReflectionClass ($ this ->class );
64
-
65
75
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 );
67
78
}
68
79
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 ' ];
103
87
}
88
+
89
+ // No need to unpack options, they will be handled inside connection classes
90
+ return $ this ->class ::create_connection ($ hosts , $ options );
104
91
}
105
92
106
93
/**
0 commit comments