|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SfCod\QueueBundle\Service; |
|
4
|
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
|
6
|
|
|
use SfCod\QueueBundle\Connector\ConnectorInterface; |
|
7
|
|
|
use SfCod\QueueBundle\Queue\QueueInterface; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class QueueManager |
|
11
|
|
|
* |
|
12
|
|
|
* @author Virchenko Maksim <[email protected]> |
|
13
|
|
|
* |
|
14
|
|
|
* @package SfCod\QueueBundle\Service |
|
15
|
|
|
*/ |
|
16
|
|
|
class QueueManager |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* Queue config |
|
20
|
|
|
* |
|
21
|
|
|
* @var array |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $config = [ |
|
24
|
|
|
'queue.default' => 'default', |
|
25
|
|
|
]; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* The array of resolved queue connections. |
|
29
|
|
|
* |
|
30
|
|
|
* @var array |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $connections = []; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* The array of resolved queue connectors. |
|
36
|
|
|
* |
|
37
|
|
|
* @var array |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $connectors = []; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Determine if the driver is connected. |
|
43
|
|
|
* |
|
44
|
|
|
* @param string $name |
|
45
|
|
|
* |
|
46
|
|
|
* @return bool |
|
47
|
|
|
*/ |
|
48
|
|
|
public function connected(?string $name = null): bool |
|
49
|
|
|
{ |
|
50
|
|
|
return isset($this->connections[$name ?: $this->getDefaultDriver()]); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Resolve a queue connection instance. |
|
55
|
|
|
* |
|
56
|
|
|
* @param string $name |
|
57
|
|
|
* |
|
58
|
|
|
* @return QueueInterface |
|
59
|
|
|
*/ |
|
60
|
|
|
public function connection(?string $name = null): QueueInterface |
|
61
|
|
|
{ |
|
62
|
|
|
$name = $name ?? $this->getDefaultDriver(); |
|
63
|
|
|
|
|
64
|
|
|
// If the connection has not been resolved yet we will resolve it now as all |
|
65
|
|
|
// of the connections are resolved when they are actually needed so we do |
|
66
|
|
|
// not make any unnecessary connection to the various queue end-points. |
|
67
|
|
|
if (!isset($this->connections[$name])) { |
|
68
|
|
|
$this->connections[$name] = $this->resolve($name); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
return $this->connections[$name]; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Add a queue connection resolver. |
|
76
|
|
|
* |
|
77
|
|
|
* @param string $driver |
|
78
|
|
|
* @param ConnectorInterface $resolver |
|
79
|
|
|
* |
|
80
|
|
|
* @return void |
|
81
|
|
|
*/ |
|
82
|
|
|
public function addConnector(string $driver, $resolver) |
|
83
|
|
|
{ |
|
84
|
|
|
$this->connectors[$driver] = $resolver; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Register a connection with the manager. |
|
89
|
|
|
* |
|
90
|
|
|
* @param array $config |
|
91
|
|
|
* @param string $name |
|
92
|
|
|
* |
|
93
|
|
|
* @return void |
|
94
|
|
|
*/ |
|
95
|
|
|
public function addConnection(array $config, ?string $name = null) |
|
96
|
|
|
{ |
|
97
|
|
|
$name = $name ?? $this->getDefaultDriver(); |
|
98
|
|
|
|
|
99
|
|
|
$this->config["queue.connections.{$name}"] = $config; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Get the name of the default queue connection. |
|
104
|
|
|
* |
|
105
|
|
|
* @return string |
|
106
|
|
|
*/ |
|
107
|
|
|
public function getDefaultDriver(): string |
|
108
|
|
|
{ |
|
109
|
|
|
return $this->config['queue.default']; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Set the name of the default queue connection. |
|
114
|
|
|
* |
|
115
|
|
|
* @param string $name |
|
116
|
|
|
* |
|
117
|
|
|
* @return void |
|
118
|
|
|
*/ |
|
119
|
|
|
public function setDefaultDriver(string $name) |
|
120
|
|
|
{ |
|
121
|
|
|
$this->config['queue.default'] = $name; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Get the full name for the given connection. |
|
126
|
|
|
* |
|
127
|
|
|
* @param string $connection |
|
128
|
|
|
* |
|
129
|
|
|
* @return string |
|
130
|
|
|
*/ |
|
131
|
|
|
public function getName(?string $connection = null): string |
|
132
|
|
|
{ |
|
133
|
|
|
return $connection ?? $this->getDefaultDriver(); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Dynamically pass calls to the default connection. |
|
138
|
|
|
* |
|
139
|
|
|
* @param string $method |
|
140
|
|
|
* @param array $parameters |
|
141
|
|
|
* |
|
142
|
|
|
* @return mixed |
|
143
|
|
|
*/ |
|
144
|
|
|
public function __call($method, $parameters) |
|
145
|
|
|
{ |
|
146
|
|
|
return $this->connection()->$method(...$parameters); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Get the queue connection configuration. |
|
151
|
|
|
* |
|
152
|
|
|
* @param string $name |
|
153
|
|
|
* |
|
154
|
|
|
* @return array |
|
155
|
|
|
*/ |
|
156
|
|
|
protected function getConfig(string $name): array |
|
157
|
|
|
{ |
|
158
|
|
|
if (!is_null($name) && 'null' !== $name) { |
|
159
|
|
|
return $this->config["queue.connections.{$name}"]; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
return ['driver' => 'null']; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Resolve a queue connection. |
|
167
|
|
|
* |
|
168
|
|
|
* @param string $name |
|
169
|
|
|
* |
|
170
|
|
|
* @return QueueInterface |
|
171
|
|
|
*/ |
|
172
|
|
|
protected function resolve(string $name): QueueInterface |
|
173
|
|
|
{ |
|
174
|
|
|
$config = $this->getConfig($name); |
|
175
|
|
|
|
|
176
|
|
|
return $this->getConnector($config['driver']) |
|
177
|
|
|
->connect($config) |
|
178
|
|
|
->setConnectionName($name); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* Get the connector for a given driver. |
|
183
|
|
|
* |
|
184
|
|
|
* @param string $driver |
|
185
|
|
|
* |
|
186
|
|
|
* @return ConnectorInterface |
|
187
|
|
|
* |
|
188
|
|
|
* @throws InvalidArgumentException |
|
189
|
|
|
*/ |
|
190
|
|
|
protected function getConnector(string $driver): ConnectorInterface |
|
191
|
|
|
{ |
|
192
|
|
|
if (!isset($this->connectors[$driver])) { |
|
193
|
|
|
throw new InvalidArgumentException("No connector for [$driver]"); |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
return $this->connectors[$driver]; |
|
197
|
|
|
} |
|
198
|
|
|
} |
|
199
|
|
|
|