-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClient.php
271 lines (235 loc) · 7.01 KB
/
Client.php
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
<?php
/**
* @author Marwan Al-Soltany <[email protected]>
* @copyright Marwan Al-Soltany 2020
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
declare(strict_types=1);
namespace MAKS\AmqpAgent;
use MAKS\AmqpAgent\Config;
use MAKS\AmqpAgent\Worker\Publisher;
use MAKS\AmqpAgent\Worker\Consumer;
use MAKS\AmqpAgent\RPC\ClientEndpoint;
use MAKS\AmqpAgent\RPC\ServerEndpoint;
use MAKS\AmqpAgent\Helper\ArrayProxy;
use MAKS\AmqpAgent\Helper\Serializer;
use MAKS\AmqpAgent\Helper\Logger;
use MAKS\AmqpAgent\Exception\AmqpAgentException;
/**
* A class returns everything AMQP Agent has to offer. A simple service container so to say.
*
* Example:
* ```
* $config = new Config('path/to/some/config-file.php');
* $client = new Client($config);
* $publisher = $client->getPublisher(); // or $client->get('publisher');
* $consumer = $client->getConsumer(); // or $client->get('consumer');
* ```
*
* @since 1.0.0
* @api
*/
class Client
{
/**
* An instance of the configuration object.
* @var Config
*/
protected $config;
/**
* An instance of the Publisher class.
* @var Publisher
*/
protected $publisher;
/**
* An instance of the Consumer class.
* @var Consumer
*/
protected $consumer;
/**
* An instance of the RPC Client class.
* @var ClientEndpoint
*/
protected $clientEndpoint;
/**
* An instance of the RPC Server class.
* @var ServerEndpoint
*/
protected $serverEndpoint;
/**
* An instance of the Serializer class.
* @var Serializer
*/
protected $serializer;
/**
* An instance of the Logger class.
* @var Logger
*/
protected $logger;
/**
* Client object constructor.
* @param Config|string $config An instance of the Config class or a path to a config file.
* @throws AmqpAgentException
*/
public function __construct($config)
{
if ($config instanceof Config) {
$this->config = $config;
} elseif (is_string($config) && strlen(trim($config)) > 0) {
$this->config = new Config($config);
} else {
throw new AmqpAgentException(
'A Config instance or a valid path to a config file must be specified.'
);
}
}
/**
* Gets a class member via public property access notation.
* @param string $member Property name.
* @return mixed
* @throws AmqpAgentException
*/
public function __get(string $member)
{
// using $this->get() to reuse the logic in get() method.
return $this->get($member);
}
/**
* Returns an instance of a class by its name (lowercase, UPPERCASE, PascalCase, camelCase, dot.case, kebab-case, or snake_case representation of class name).
* @param string $member Member name. Check out `self::gettable()` for available members.
* @return Config|Publisher|Consumer|Serializer|Logger
* @throws AmqpAgentException
*/
public function get(string $member)
{
$method = __FUNCTION__ . preg_replace('/[\.\-_]+/', '', ucwords(strtolower($member), '.-_'));
if (method_exists($this, $method)) {
return $this->{$method}();
}
$available = ArrayProxy::castArrayToString($this->gettable());
throw new AmqpAgentException(
"The requested member with the name \"{$member}\" does not exist! Available members are: {$available}."
);
}
/**
* Returns an array of available members that can be obtained via `self::get()`.
* @since 1.2.1
* @return array
*/
public static function gettable(): array
{
$methods = get_class_methods(static::class);
$gettable = [];
$separator = ('.-_')[rand(0, 2)];
foreach ($methods as $method) {
if (preg_match('/get[A-Z][a-z]+/', $method)) {
$gettable[] = strtolower(
preg_replace(
['/get/', '/([a-z])([A-Z])/'],
['', '$1' . $separator . '$2'],
$method
)
);
}
}
return $gettable;
}
/**
* Returns an instance of the Publisher class.
* @return Publisher
* @api
*/
public function getPublisher(): Publisher
{
if (!isset($this->publisher)) {
$this->publisher = new Publisher(
$this->config->connectionOptions,
$this->config->channelOptions,
$this->config->queueOptions,
$this->config->exchangeOptions,
$this->config->bindOptions,
$this->config->messageOptions,
$this->config->publishOptions
);
}
return $this->publisher;
}
/**
* Returns an instance of the Consumer class.
* @return Consumer
*/
public function getConsumer(): Consumer
{
if (!isset($this->consumer)) {
$this->consumer = new Consumer(
$this->config->connectionOptions,
$this->config->channelOptions,
$this->config->queueOptions,
$this->config->qosOptions,
$this->config->waitOptions,
$this->config->consumeOptions
);
}
return $this->consumer;
}
/**
* Returns an instance of the RPC Client class.
* @return ClientEndpoint
*/
public function getClientEndpoint(): ClientEndpoint
{
if (!isset($this->clientEndpoint)) {
$this->clientEndpoint = new ClientEndpoint(
$this->config->rpcConnectionOptions,
$this->config->rpcQueueName
);
}
return $this->clientEndpoint;
}
/**
* Returns an instance of the RPC Server class.
* @return ServerEndpoint
*/
public function getServerEndpoint(): ServerEndpoint
{
if (!isset($this->serverEndpoint)) {
$this->serverEndpoint = new ServerEndpoint(
$this->config->rpcConnectionOptions,
$this->config->rpcQueueName
);
}
return $this->serverEndpoint;
}
/**
* Returns an instance of the Serializer class.
* @return Serializer
*/
public function getSerializer(): Serializer
{
if (!isset($this->serializer)) {
$this->serializer = new Serializer();
}
return $this->serializer;
}
/**
* Returns an instance of the Logger class.
* Filename and directory must be set through setters.
* @return Logger
*/
public function getLogger(): Logger
{
if (!isset($this->logger)) {
$this->logger = new Logger(null, null);
}
return $this->logger;
}
/**
* Returns the currently used config object.
* @return Config
*/
public function getConfig(): Config
{
return $this->config;
}
}