-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAmqpConnectionFactory.php
More file actions
210 lines (179 loc) · 7.04 KB
/
AmqpConnectionFactory.php
File metadata and controls
210 lines (179 loc) · 7.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<?php
namespace Enqueue\AmqpExt;
use Enqueue\AmqpTools\DelayStrategyAware;
use Enqueue\AmqpTools\DelayStrategyAwareTrait;
use Interop\Amqp\AmqpConnectionFactory as InteropAmqpConnectionFactory;
class AmqpConnectionFactory implements InteropAmqpConnectionFactory, DelayStrategyAware
{
use DelayStrategyAwareTrait;
/**
* @var array
*/
private $config;
/**
* @var \AMQPConnection
*/
private $connection;
/**
* The config could be an array, string DSN or null. In case of null it will attempt to connect to localhost with default credentials.
*
* [
* 'host' => 'amqp.host The host to connect too. Note: Max 1024 characters.',
* 'port' => 'amqp.port Port on the host.',
* 'vhost' => 'amqp.vhost The virtual host on the host. Note: Max 128 characters.',
* 'user' => 'amqp.user The user name to use. Note: Max 128 characters.',
* 'pass' => 'amqp.password Password. Note: Max 128 characters.',
* 'read_timeout' => 'Timeout in for income activity. Note: 0 or greater seconds. May be fractional.',
* 'write_timeout' => 'Timeout in for outcome activity. Note: 0 or greater seconds. May be fractional.',
* 'connect_timeout' => 'Connection timeout. Note: 0 or greater seconds. May be fractional.',
* 'persisted' => 'bool, Whether it use single persisted connection or open a new one for every context',
* 'lazy' => 'the connection will be performed as later as possible, if the option set to true',
* 'pre_fetch_count' => 'Controls how many messages could be prefetched',
* 'pre_fetch_size' => 'Controls how many messages could be prefetched',
* 'receive_method' => 'Could be either basic_get or basic_consume',
* ]
*
* or
*
* amqp://user:pass@host:10000/vhost?lazy=true&persisted=false&read_timeout=2
*
* @param array|string $config
*/
public function __construct($config = 'amqp://')
{
if (is_string($config) && 0 === strpos($config, 'amqp+ext://')) {
$config = str_replace('amqp+ext://', 'amqp://', $config);
}
if (empty($config) || 'amqp://' === $config) {
$config = [];
} elseif (is_string($config)) {
$config = $this->parseDsn($config);
} elseif (is_array($config)) {
} else {
throw new \LogicException('The config must be either an array of options, a DSN string or null');
}
$this->config = array_replace($this->defaultConfig(), $config);
$supportedMethods = ['basic_get', 'basic_consume'];
if (false == in_array($this->config['receive_method'], $supportedMethods, true)) {
throw new \LogicException(sprintf(
'Invalid "receive_method" option value "%s". It could be only "%s"',
$this->config['receive_method'],
implode('", "', $supportedMethods)
));
}
if ('basic_consume' == $this->config['receive_method']) {
if (false == (version_compare(phpversion('amqp'), '1.9.1', '>=') || phpversion('amqp') == '1.9.1-dev')) {
// @see https://github.com/php-enqueue/enqueue-dev/issues/110 and https://github.com/pdezwart/php-amqp/issues/281
throw new \LogicException('The "basic_consume" method does not work on amqp extension prior 1.9.1 version.');
}
}
}
/**
* {@inheritdoc}
*
* @return AmqpContext
*/
public function createContext()
{
if ($this->config['lazy']) {
$context = new AmqpContext(function () {
return $this->createExtContext($this->establishConnection());
}, $this->config['receive_method']);
$context->setDelayStrategy($this->delayStrategy);
return $context;
}
$context = new AmqpContext($this->createExtContext($this->establishConnection()), $this->config['receive_method']);
$context->setDelayStrategy($this->delayStrategy);
return $context;
}
/**
* @param \AMQPConnection $extConnection
*
* @return \AMQPChannel
*/
private function createExtContext(\AMQPConnection $extConnection)
{
$channel = new \AMQPChannel($extConnection);
if (false == empty($this->config['pre_fetch_count'])) {
$channel->setPrefetchCount((int) $this->config['pre_fetch_count']);
}
if (false == empty($this->config['pre_fetch_size'])) {
$channel->setPrefetchSize((int) $this->config['pre_fetch_size']);
}
return $channel;
}
/**
* @return \AMQPConnection
*/
private function establishConnection()
{
if (false == $this->connection) {
$config = $this->config;
$config['login'] = $this->config['user'];
$config['password'] = $this->config['pass'];
$this->connection = new \AMQPConnection($config);
$this->config['persisted'] ? $this->connection->pconnect() : $this->connection->connect();
}
if (false == $this->connection->isConnected()) {
$this->config['persisted'] ? $this->connection->preconnect() : $this->connection->reconnect();
}
return $this->connection;
}
/**
* @param string $dsn
*
* @return array
*/
private function parseDsn($dsn)
{
$dsnConfig = parse_url($dsn);
if (false === $dsnConfig) {
throw new \LogicException(sprintf('Failed to parse DSN "%s"', $dsn));
}
$dsnConfig = array_replace([
'scheme' => null,
'host' => null,
'port' => null,
'user' => null,
'pass' => null,
'path' => null,
'query' => null,
], $dsnConfig);
if ('amqp' !== $dsnConfig['scheme']) {
throw new \LogicException(sprintf('The given DSN scheme "%s" is not supported. Could be "amqp" only.', $dsnConfig['scheme']));
}
if ($dsnConfig['query']) {
$query = [];
parse_str($dsnConfig['query'], $query);
$dsnConfig = array_replace($query, $dsnConfig);
}
$dsnConfig['vhost'] = ltrim($dsnConfig['path'], '/');
unset($dsnConfig['scheme'], $dsnConfig['query'], $dsnConfig['fragment'], $dsnConfig['path']);
$config = array_replace($this->defaultConfig(), $dsnConfig);
$config = array_map(function ($value) {
return urldecode($value);
}, $config);
return $config;
}
/**
* @return array
*/
private function defaultConfig()
{
return [
'host' => 'localhost',
'port' => 5672,
'vhost' => '/',
'user' => 'guest',
'pass' => 'guest',
'read_timeout' => null,
'write_timeout' => null,
'connect_timeout' => null,
'persisted' => false,
'lazy' => true,
'pre_fetch_count' => null,
'pre_fetch_size' => null,
'receive_method' => 'basic_get',
];
}
}