-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathConnection.php
More file actions
164 lines (153 loc) · 4.16 KB
/
Copy pathConnection.php
File metadata and controls
164 lines (153 loc) · 4.16 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
<?php
declare(strict_types=1);
namespace Bow\Queue;
use Bow\Queue\Adapters\SQSAdapter;
use Bow\Queue\Adapters\SyncAdapter;
use Bow\Queue\Adapters\QueueAdapter;
use Bow\Queue\Adapters\RedisAdapter;
use Bow\Queue\Adapters\KafkaAdapter;
use Bow\Queue\Adapters\DatabaseAdapter;
use Bow\Queue\Adapters\BeanstalkdAdapter;
use Bow\Queue\Adapters\RabbitMQAdapter;
use Bow\Queue\Exceptions\ConnexionException;
use Bow\Queue\Exceptions\MethodCallException;
class Connection
{
/**
* The supported connection
*
* @var array
*/
/**
* Supported connection drivers and their adapter classes
*/
private const SUPPORTED_CONNECTIONS = [
'beanstalkd' => BeanstalkdAdapter::class,
'sqs' => SQSAdapter::class,
'database' => DatabaseAdapter::class,
'sync' => SyncAdapter::class,
'redis' => RedisAdapter::class,
'rabbitmq' => RabbitMQAdapter::class,
'kafka' => KafkaAdapter::class,
];
/**
* The registered connections (can be extended at runtime)
*
* @var array
*/
private static array $connections = self::SUPPORTED_CONNECTIONS;
/**
* The queue configuration array
*
* @var array
*/
private array $config;
/**
* The selected connection driver name
*
* @var ?string
*/
private ?string $connection = null;
/**
* Configuration of worker connection
*
* @param array $config
*/
public function __construct(array $config)
{
$this->config = $config;
}
/**
* Push the new connection support in connectors management
*
* @param string $name
* @param string $classname
* @return bool
* @throws ConnexionException
*/
/**
* Register a new connection adapter at runtime
*
* @param string $name
* @param string $classname
* @return bool
* @throws ConnexionException
*/
public static function pushConnection(string $name, string $classname): bool
{
if (!array_key_exists($name, static::$connections)) {
static::$connections[$name] = $classname;
return true;
}
throw new ConnexionException(
"Another connection with the same name already exists"
);
}
/**
* Set connection
*
* @param string $connection
* @return Connection
*/
/**
* Set the connection driver to use
*
* @param string $connection
* @return $this
*/
public function setConnection(string $connection): self
{
$this->connection = $connection;
return $this;
}
/**
* __call
*
* @param string $name
* @param array $arguments
* @return mixed|null
* @throws MethodCallException
*/
/**
* Proxy method calls to the underlying adapter
*
* @param string $name
* @param array $arguments
* @return mixed
* @throws MethodCallException
*/
public function __call(string $name, array $arguments)
{
$adapter = $this->getAdapter();
if (method_exists($adapter, $name)) {
return $adapter->$name(...$arguments);
}
$class = get_class($adapter);
throw new MethodCallException("Call to undefined method {$class}->{$name}()");
}
/**
* Get the define adapter
*
* @return QueueAdapter
*/
/**
* Get the configured adapter instance
*
* @return QueueAdapter
* @throws ConnexionException
*/
public function getAdapter(): QueueAdapter
{
$driver = $this->connection ?: $this->config['default'];
if (!isset(static::$connections[$driver])) {
throw new ConnexionException("Queue driver '{$driver}' is not supported.");
}
if (!isset($this->config['connections'][$driver])) {
throw new ConnexionException("No configuration found for queue driver '{$driver}'.");
}
$adapterClass = static::$connections[$driver];
/** @var QueueAdapter $adapter */
$adapter = new $adapterClass();
return $adapter->configure($this->config['connections'][$driver]);
}
}