2
2
3
3
namespace Enqueue \AmqpBunny ;
4
4
5
- use Bunny \ Client ;
5
+ use Enqueue \ AmqpTools \ ConnectionConfig ;
6
6
use Enqueue \AmqpTools \DelayStrategyAware ;
7
7
use Enqueue \AmqpTools \DelayStrategyAwareTrait ;
8
8
use Interop \Amqp \AmqpConnectionFactory as InteropAmqpConnectionFactory ;
@@ -12,75 +12,37 @@ class AmqpConnectionFactory implements InteropAmqpConnectionFactory, DelayStrate
12
12
use DelayStrategyAwareTrait;
13
13
14
14
/**
15
- * @var array
15
+ * @var ConnectionConfig
16
16
*/
17
17
private $ config ;
18
18
19
19
/**
20
- * @var Client
20
+ * @var BunnyClient
21
21
*/
22
22
private $ client ;
23
23
24
24
/**
25
- * The config could be an array, string DSN or null. In case of null it will attempt to connect to localhost with default credentials.
25
+ * @see \Enqueue\AmqpTools\ConnectionConfig for possible config formats and values
26
26
*
27
- * [
28
- * 'host' => 'amqp.host The host to connect too. Note: Max 1024 characters.',
29
- * 'port' => 'amqp.port Port on the host.',
30
- * 'vhost' => 'amqp.vhost The virtual host on the host. Note: Max 128 characters.',
31
- * 'user' => 'amqp.user The user name to use. Note: Max 128 characters.',
32
- * 'pass' => 'amqp.password Password. Note: Max 128 characters.',
33
- * 'lazy' => 'the connection will be performed as later as possible, if the option set to true',
34
- * 'receive_method' => 'Could be either basic_get or basic_consume',
35
- * 'qos_prefetch_size' => 'The server will send a message in advance if it is equal to or smaller in size than the available prefetch size. May be set to zero, meaning "no specific limit"',
36
- * 'qos_prefetch_count' => 'Specifies a prefetch window in terms of whole messages.',
37
- * 'qos_global' => 'If "false" the QoS settings apply to the current channel only. If this field is "true", they are applied to the entire connection.',
38
- * ]
27
+ * In addition this factory accepts next options:
28
+ * receive_method - Could be either basic_get or basic_consume
39
29
*
40
- * or
41
- *
42
- * amqp://user:pass@host:10000/vhost?lazy=true&socket=true
43
- *
44
- * @param array|string $config
30
+ * @param array|string|null $config
45
31
*/
46
32
public function __construct ($ config = 'amqp: ' )
47
33
{
48
- if (is_string ($ config ) && 0 === strpos ($ config , 'amqp+bunny: ' )) {
49
- $ config = str_replace ('amqp+bunny: ' , 'amqp: ' , $ config );
50
- }
51
-
52
- if (empty ($ config ) || 'amqp: ' === $ config ) {
53
- $ config = [];
54
- } elseif (is_string ($ config )) {
55
- $ config = $ this ->parseDsn ($ config );
56
- } elseif (is_array ($ config )) {
57
- } else {
58
- throw new \LogicException ('The config must be either an array of options, a DSN string or null ' );
59
- }
60
-
61
- $ config = array_replace ($ this ->defaultConfig (), $ config );
62
-
63
- $ config = array_replace ($ this ->defaultConfig (), $ config );
64
- if (array_key_exists ('qos_global ' , $ config )) {
65
- $ config ['qos_global ' ] = (bool ) $ config ['qos_global ' ];
66
- }
67
- if (array_key_exists ('qos_prefetch_count ' , $ config )) {
68
- $ config ['qos_prefetch_count ' ] = (int ) $ config ['qos_prefetch_count ' ];
69
- }
70
- if (array_key_exists ('qos_prefetch_size ' , $ config )) {
71
- $ config ['qos_prefetch_size ' ] = (int ) $ config ['qos_prefetch_size ' ];
72
- }
73
- if (array_key_exists ('lazy ' , $ config )) {
74
- $ config ['lazy ' ] = (bool ) $ config ['lazy ' ];
75
- }
76
-
77
- $ this ->config = $ config ;
34
+ $ this ->config = (new ConnectionConfig ($ config ))
35
+ ->addSupportedScheme ('amqp+bunny ' )
36
+ ->addDefaultOption ('receive_method ' , 'basic_get ' )
37
+ ->addDefaultOption ('tcp_nodelay ' , null )
38
+ ->parse ()
39
+ ;
78
40
79
41
$ supportedMethods = ['basic_get ' , 'basic_consume ' ];
80
- if (false == in_array ($ this ->config [ 'receive_method ' ] , $ supportedMethods , true )) {
42
+ if (false == in_array ($ this ->config -> getOption ( 'receive_method ' ) , $ supportedMethods , true )) {
81
43
throw new \LogicException (sprintf (
82
44
'Invalid "receive_method" option value "%s". It could be only "%s" ' ,
83
- $ this ->config [ 'receive_method ' ] ,
45
+ $ this ->config -> getOption ( 'receive_method ' ) ,
84
46
implode ('", " ' , $ supportedMethods )
85
47
));
86
48
}
@@ -91,99 +53,66 @@ public function __construct($config = 'amqp:')
91
53
*/
92
54
public function createContext ()
93
55
{
94
- if ($ this ->config [ ' lazy ' ] ) {
56
+ if ($ this ->config -> isLazy () ) {
95
57
$ context = new AmqpContext (function () {
96
58
$ channel = $ this ->establishConnection ()->channel ();
97
- $ channel ->qos ($ this ->config [ ' qos_prefetch_size ' ] , $ this ->config [ ' qos_prefetch_count ' ] , $ this ->config [ ' qos_global ' ] );
59
+ $ channel ->qos ($ this ->config -> getQosPrefetchSize () , $ this ->config -> getQosPrefetchCount () , $ this ->config -> isQosGlobal () );
98
60
99
61
return $ channel ;
100
- }, $ this ->config );
62
+ }, $ this ->config -> getConfig () );
101
63
$ context ->setDelayStrategy ($ this ->delayStrategy );
102
64
103
65
return $ context ;
104
66
}
105
67
106
- $ context = new AmqpContext ($ this ->establishConnection ()->channel (), $ this ->config );
68
+ $ context = new AmqpContext ($ this ->establishConnection ()->channel (), $ this ->config -> getConfig () );
107
69
$ context ->setDelayStrategy ($ this ->delayStrategy );
108
- $ context ->setQos ($ this ->config [ ' qos_prefetch_size ' ] , $ this ->config [ ' qos_prefetch_count ' ] , $ this ->config [ ' qos_global ' ] );
70
+ $ context ->setQos ($ this ->config -> getQosPrefetchSize () , $ this ->config -> getQosPrefetchCount () , $ this ->config -> isQosGlobal () );
109
71
110
72
return $ context ;
111
73
}
112
74
113
75
/**
114
- * @return Client
76
+ * @return ConnectionConfig
115
77
*/
116
- private function establishConnection ()
78
+ public function getConfig ()
117
79
{
118
- if (false == $ this ->client ) {
119
- $ this ->client = new Client ($ this ->config );
120
- $ this ->client ->connect ();
121
- }
122
-
123
- return $ this ->client ;
80
+ return $ this ->config ;
124
81
}
125
82
126
83
/**
127
- * @param string $dsn
128
- *
129
- * @return array
84
+ * @return BunnyClient
130
85
*/
131
- private function parseDsn ( $ dsn )
86
+ private function establishConnection ( )
132
87
{
133
- $ dsnConfig = parse_url ($ dsn );
134
- if (false === $ dsnConfig ) {
135
- throw new \LogicException (sprintf ('Failed to parse DSN "%s" ' , $ dsn ));
136
- }
137
-
138
- $ dsnConfig = array_replace ([
139
- 'scheme ' => null ,
140
- 'host ' => null ,
141
- 'port ' => null ,
142
- 'user ' => null ,
143
- 'pass ' => null ,
144
- 'path ' => null ,
145
- 'query ' => null ,
146
- ], $ dsnConfig );
147
-
148
- if ('amqp ' !== $ dsnConfig ['scheme ' ]) {
149
- throw new \LogicException (sprintf ('The given DSN scheme "%s" is not supported. Could be "amqp" only. ' , $ dsnConfig ['scheme ' ]));
150
- }
151
-
152
- if ($ dsnConfig ['query ' ]) {
153
- $ query = [];
154
- parse_str ($ dsnConfig ['query ' ], $ query );
155
-
156
- $ dsnConfig = array_replace ($ query , $ dsnConfig );
88
+ if (false == $ this ->client ) {
89
+ $ bunnyConfig = [];
90
+ $ bunnyConfig ['host ' ] = $ this ->config ->getHost ();
91
+ $ bunnyConfig ['port ' ] = $ this ->config ->getPort ();
92
+ $ bunnyConfig ['vhost ' ] = $ this ->config ->getVHost ();
93
+ $ bunnyConfig ['user ' ] = $ this ->config ->getUser ();
94
+ $ bunnyConfig ['password ' ] = $ this ->config ->getPass ();
95
+ $ bunnyConfig ['read_write_timeout ' ] = min ($ this ->config ->getReadTimeout (), $ this ->config ->getWriteTimeout ());
96
+ $ bunnyConfig ['timeout ' ] = $ this ->config ->getConnectionTimeout ();
97
+
98
+ // @see https://github.com/php-enqueue/enqueue-dev/issues/229
99
+ // $bunnyConfig['persistent'] = $this->config->isPersisted();
100
+ // if ($this->config->isPersisted()) {
101
+ // $bunnyConfig['path'] = 'enqueue';//$this->config->getOption('path', $this->config->getOption('vhost'));
102
+ // }
103
+
104
+ if ($ this ->config ->getHeartbeat ()) {
105
+ $ bunnyConfig ['heartbeat ' ] = $ this ->config ->getHeartbeat ();
106
+ }
107
+
108
+ if (null !== $ this ->config ->getOption ('tcp_nodelay ' )) {
109
+ $ bunnyConfig ['tcp_nodelay ' ] = $ this ->config ->getOption ('tcp_nodelay ' );
110
+ }
111
+
112
+ $ this ->client = new BunnyClient ($ bunnyConfig );
113
+ $ this ->client ->connect ();
157
114
}
158
115
159
- $ dsnConfig ['vhost ' ] = ltrim ($ dsnConfig ['path ' ], '/ ' );
160
-
161
- unset($ dsnConfig ['scheme ' ], $ dsnConfig ['query ' ], $ dsnConfig ['fragment ' ], $ dsnConfig ['path ' ]);
162
-
163
- $ dsnConfig = array_map (function ($ value ) {
164
- return urldecode ($ value );
165
- }, $ dsnConfig );
166
-
167
- return $ dsnConfig ;
168
- }
169
-
170
- /**
171
- * @return array
172
- */
173
- private function defaultConfig ()
174
- {
175
- return [
176
- 'host ' => 'localhost ' ,
177
- 'port ' => 5672 ,
178
- 'user ' => 'guest ' ,
179
- 'pass ' => 'guest ' ,
180
- 'lazy ' => true ,
181
- 'vhost ' => '/ ' ,
182
- 'heartbeat ' => 0 ,
183
- 'receive_method ' => 'basic_get ' ,
184
- 'qos_prefetch_size ' => 0 ,
185
- 'qos_prefetch_count ' => 1 ,
186
- 'qos_global ' => false ,
187
- ];
116
+ return $ this ->client ;
188
117
}
189
118
}
0 commit comments