1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace BinaryCube\CarrotMQ\Driver; |
6
|
|
|
|
7
|
|
|
use Psr\Log\LoggerInterface; |
8
|
|
|
use Interop\Amqp\AmqpConnectionFactory; |
9
|
|
|
use BinaryCube\CarrotMQ\Support\Collection; |
10
|
|
|
use BinaryCube\CarrotMQ\Exception\ClassNotFoundException; |
11
|
|
|
use BinaryCube\CarrotMQ\Exception\InvalidConfigException; |
12
|
|
|
use Enqueue\AmqpLib\AmqpConnectionFactory as AMQPLibConnectionFactory; |
13
|
|
|
use Enqueue\AmqpExt\AmqpConnectionFactory as AMQPExtConnectionFactory; |
|
|
|
|
14
|
|
|
use Enqueue\AmqpBunny\AmqpConnectionFactory as AMQPBunnyConnectionFactory; |
|
|
|
|
15
|
|
|
|
16
|
|
|
use function implode; |
17
|
|
|
use function vsprintf; |
18
|
|
|
use function array_keys; |
19
|
|
|
use function array_diff; |
20
|
|
|
use function class_exists; |
21
|
|
|
use function array_filter; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Class AmqpDriver |
25
|
|
|
*/ |
26
|
|
|
class AmqpDriver extends Driver |
27
|
|
|
{ |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @see https://github.com/php-amqplib/php-amqplib |
31
|
|
|
*/ |
32
|
|
|
const EXTENSION_AMQP_LIB = 'amqp-lib'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @see https://pecl.php.net/package/amqp |
36
|
|
|
*/ |
37
|
|
|
const EXTENSION_AMQP_EXT = 'amqp-ext'; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @see https://github.com/jakubkulhan/bunny |
41
|
|
|
*/ |
42
|
|
|
const EXTENSION_AMQP_BUNNY = 'amqp-bunny'; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @const array Default driver parameters |
46
|
|
|
*/ |
47
|
|
|
const DEFAULTS = [ |
48
|
|
|
'extension' => self::EXTENSION_AMQP_LIB, |
49
|
|
|
|
50
|
|
|
'dsn' => null, |
51
|
|
|
'host' => '127.0.0.1', |
52
|
|
|
'port' => 5672, |
53
|
|
|
'username' => 'guest', |
54
|
|
|
'password' => 'guest', |
55
|
|
|
'vhost' => '/', |
56
|
|
|
|
57
|
|
|
'stream' => true, |
58
|
|
|
|
59
|
|
|
'read_timeout' => 3., |
60
|
|
|
'write_timeout' => 3., |
61
|
|
|
'connection_timeout' => 3., |
62
|
|
|
'heartbeat' => null, |
63
|
|
|
'persisted' => null, |
64
|
|
|
|
65
|
|
|
'lazy' => null, |
66
|
|
|
|
67
|
|
|
'qos_global' => null, |
68
|
|
|
'qos_prefetch_size' => null, |
69
|
|
|
'qos_prefetch_count' => null, |
70
|
|
|
|
71
|
|
|
'ssl_on' => null, |
72
|
|
|
'ssl_verify' => null, |
73
|
|
|
'ssl_cacert' => null, |
74
|
|
|
'ssl_cert' => null, |
75
|
|
|
'ssl_key' => null, |
76
|
|
|
]; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* List of supported AMQP interop extensions. |
80
|
|
|
* |
81
|
|
|
* @var string[] |
82
|
|
|
*/ |
83
|
|
|
protected $extensions = [ |
84
|
|
|
self::EXTENSION_AMQP_LIB => AMQPLibConnectionFactory::class, |
85
|
|
|
self::EXTENSION_AMQP_EXT => AMQPExtConnectionFactory::class, |
86
|
|
|
self::EXTENSION_AMQP_BUNNY => AMQPBunnyConnectionFactory::class, |
87
|
|
|
]; |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Constructor. |
91
|
|
|
* |
92
|
|
|
* @param array $config |
93
|
|
|
* @param LoggerInterface|null $logger |
94
|
|
|
*/ |
95
|
|
|
public function __construct($config = [], ?LoggerInterface $logger = null) |
96
|
|
|
{ |
97
|
|
|
parent::__construct($config, $logger); |
98
|
|
|
|
99
|
|
|
$this->logger->debug(vsprintf('Instance of "%s" has been created', [self::class])); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return AmqpConnectionFactory |
104
|
|
|
* |
105
|
|
|
* @throws ClassNotFoundException |
106
|
|
|
* @throws InvalidConfigException |
107
|
|
|
*/ |
108
|
|
|
protected function build(): AmqpConnectionFactory |
109
|
|
|
{ |
110
|
|
|
$config = $this->config; |
111
|
|
|
|
112
|
|
|
if ($diff = array_diff(array_keys($config), array_keys(static::DEFAULTS))) { |
113
|
|
|
throw new InvalidConfigException( |
114
|
|
|
vsprintf( |
115
|
|
|
'Cannot create driver %s, received unknown arguments: %s!', |
116
|
|
|
[ |
117
|
|
|
(string) self::class, |
118
|
|
|
implode(', ', $diff), |
119
|
|
|
] |
120
|
|
|
) |
121
|
|
|
); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
$config = Collection::make(static::DEFAULTS)->merge($config)->all(); |
125
|
|
|
|
126
|
|
|
$extension = $config['extension']; |
127
|
|
|
|
128
|
|
|
if (empty($extension) || ! isset($this->extensions[$extension])) { |
129
|
|
|
throw new InvalidConfigException( |
130
|
|
|
vsprintf( |
131
|
|
|
'The given extension "%s" is not supported. Extensions supported are "%s"', |
132
|
|
|
[ |
133
|
|
|
$extension, |
134
|
|
|
implode('", "', array_keys($this->extensions)), |
135
|
|
|
] |
136
|
|
|
) |
137
|
|
|
); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
$config['user'] = $config['username']; |
141
|
|
|
$config['pass'] = $config['password']; |
142
|
|
|
|
143
|
|
|
// Remove unused properties. |
144
|
|
|
unset($config['extension'], $config['username'], $config['password']); |
145
|
|
|
|
146
|
|
|
// Remove the attributes with null value. |
147
|
|
|
$config = array_filter( |
148
|
|
|
$config, |
149
|
|
|
function ($value) { |
150
|
|
|
return null !== $value; |
151
|
|
|
} |
152
|
|
|
); |
153
|
|
|
|
154
|
|
|
$class = $this->extensions[$extension]; |
155
|
|
|
|
156
|
|
|
if (! class_exists($class)) { |
157
|
|
|
throw new ClassNotFoundException(vsprintf('Class %s not found.', [$class])); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @var AmqpConnectionFactory $connection |
162
|
|
|
*/ |
163
|
|
|
$connection = new $class($config); |
164
|
|
|
|
165
|
|
|
return $connection; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
} |
169
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths