1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Marwan Al-Soltany <[email protected]> |
4
|
|
|
* @copyright Marwan Al-Soltany 2020 |
5
|
|
|
* For the full copyright and license information, please view |
6
|
|
|
* the LICENSE file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace MAKS\AmqpAgent; |
10
|
|
|
|
11
|
|
|
use MAKS\AmqpAgent\Config; |
12
|
|
|
use MAKS\AmqpAgent\Worker\Publisher; |
13
|
|
|
use MAKS\AmqpAgent\Worker\Consumer; |
14
|
|
|
use MAKS\AmqpAgent\Helper\Utility; |
15
|
|
|
use MAKS\AmqpAgent\Helper\Serializer; |
16
|
|
|
use MAKS\AmqpAgent\Helper\Logger; |
17
|
|
|
use MAKS\AmqpAgent\Exception\AmqpAgentException; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* A class returns everything AMQP Agent has to offer. A simple service container so to say. |
21
|
|
|
* |
22
|
|
|
* Example: |
23
|
|
|
* ``` |
24
|
|
|
* $config = new Config('path/to/some/config-file.php'); |
25
|
|
|
* $client = new Client($config); |
26
|
|
|
* $publisher = $client->getPublisher(); // or $client->get('publisher'); |
27
|
|
|
* $consumer = $client->getConsumer(); // or $client->get('consumer'); |
28
|
|
|
* ``` |
29
|
|
|
* |
30
|
|
|
* @since 1.0.0 |
31
|
|
|
* @api |
32
|
|
|
*/ |
33
|
|
|
class Client |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* An instance of the configuration object. |
37
|
|
|
* @var Config |
38
|
|
|
*/ |
39
|
|
|
protected $config; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* An instance of the Publisher worker class. |
43
|
|
|
* @var Publisher |
44
|
|
|
*/ |
45
|
|
|
protected $publisher; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* An instance of the Consumer worker class. |
49
|
|
|
* @var Consumer |
50
|
|
|
*/ |
51
|
|
|
protected $consumer; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* An instance of the Serializer class. |
55
|
|
|
* @var Serializer |
56
|
|
|
*/ |
57
|
|
|
protected $serializer; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* An instance of the Logger class. |
61
|
|
|
* @var Logger |
62
|
|
|
*/ |
63
|
|
|
protected $logger; |
64
|
|
|
|
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Client object constructor. |
68
|
|
|
* @param Config|string $config An instance of the Config class or a path to a config file. |
69
|
|
|
* @throws AmqpAgentException |
70
|
|
|
*/ |
71
|
9 |
|
public function __construct($config) |
72
|
|
|
{ |
73
|
9 |
|
if ($config instanceof Config) { |
74
|
9 |
|
$this->config = $config; |
75
|
2 |
|
} elseif (is_string($config) && strlen(trim($config)) > 0) { |
76
|
1 |
|
$this->config = new Config($config); |
77
|
|
|
} else { |
78
|
1 |
|
throw new AmqpAgentException( |
79
|
1 |
|
'A Config instance or a valid path to a config file must be specified.' |
80
|
|
|
); |
81
|
|
|
} |
82
|
9 |
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Gets a class member via public property access notation. |
86
|
|
|
* @param string $member Property name. |
87
|
|
|
* @return mixed |
88
|
|
|
*/ |
89
|
5 |
|
public function __get(string $member) |
90
|
|
|
{ |
91
|
|
|
// using $this->get() to reuse the logic in class methods. |
92
|
5 |
|
return $this->get($member); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Returns an instance of a class by its name (lowercase, UPPERCASE, PascalCase, camelCase, dot.case, kebab-case, or snake_case representation of class name). |
98
|
|
|
* @param string $member Member name. Check out `self::gettable()` for available members. |
99
|
|
|
* @return Config|Publisher|Consumer|Serializer|Logger |
100
|
|
|
* @throws AmqpAgentException |
101
|
|
|
*/ |
102
|
6 |
|
public function get(string $member) |
103
|
|
|
{ |
104
|
6 |
|
$method = __FUNCTION__ . preg_replace('/[\.\-_]+/', '', ucwords(strtolower($member), '.-_')); |
105
|
|
|
|
106
|
6 |
|
if (method_exists($this, $method)) { |
107
|
5 |
|
return $this->{$method}(); |
108
|
|
|
} |
109
|
|
|
|
110
|
1 |
|
$available = Utility::collapse($this->gettable()); |
111
|
1 |
|
throw new AmqpAgentException( |
112
|
1 |
|
"The requested member with the name \"{$member}\" does not exist! Available members are: {$available}." |
113
|
|
|
); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Returns an array of available members that can be obtained via `self::get()`. |
119
|
|
|
* @return array |
120
|
|
|
*/ |
121
|
2 |
|
public static function gettable(): array |
122
|
|
|
{ |
123
|
2 |
|
$methods = get_class_methods(static::class); |
124
|
2 |
|
$gettable = []; |
125
|
2 |
|
$separator = ('.-_')[rand(0, 2)]; |
126
|
|
|
|
127
|
2 |
|
foreach ($methods as $method) { |
128
|
2 |
|
if (preg_match('/get[A-Z][a-z]+/', $method)) { |
129
|
2 |
|
$gettable[] = strtolower(preg_replace(['/get/', '/([a-z])([A-Z])/'], ['', '$1'.$separator.'$2'], $method)); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
2 |
|
return $gettable; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Returns an instance of the Publisher class. |
139
|
|
|
* @return Publisher |
140
|
|
|
* @api |
141
|
|
|
*/ |
142
|
1 |
|
public function getPublisher(): Publisher |
143
|
|
|
{ |
144
|
1 |
|
if (!isset($this->publisher)) { |
145
|
1 |
|
$this->publisher = new Publisher( |
146
|
1 |
|
$this->config->connectionOptions, |
147
|
1 |
|
$this->config->channelOptions, |
148
|
1 |
|
$this->config->queueOptions, |
149
|
1 |
|
$this->config->exchangeOptions, |
150
|
1 |
|
$this->config->bindOptions, |
151
|
1 |
|
$this->config->messageOptions, |
152
|
1 |
|
$this->config->publishOptions |
153
|
|
|
); |
154
|
|
|
} |
155
|
|
|
|
156
|
1 |
|
return $this->publisher; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Returns an instance of the Consumer class. |
161
|
|
|
* @return Consumer |
162
|
|
|
*/ |
163
|
1 |
|
public function getConsumer(): Consumer |
164
|
|
|
{ |
165
|
1 |
|
if (!isset($this->consumer)) { |
166
|
1 |
|
$this->consumer = new Consumer( |
167
|
1 |
|
$this->config->connectionOptions, |
168
|
1 |
|
$this->config->channelOptions, |
169
|
1 |
|
$this->config->queueOptions, |
170
|
1 |
|
$this->config->qosOptions, |
171
|
1 |
|
$this->config->waitOptions, |
172
|
1 |
|
$this->config->consumeOptions |
173
|
|
|
); |
174
|
|
|
} |
175
|
|
|
|
176
|
1 |
|
return $this->consumer; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Returns an instance of the Serializer class. |
181
|
|
|
* @return Serializer |
182
|
|
|
*/ |
183
|
1 |
|
public function getSerializer(): Serializer |
184
|
|
|
{ |
185
|
1 |
|
if (!isset($this->serializer)) { |
186
|
1 |
|
$this->serializer = new Serializer(); |
187
|
|
|
} |
188
|
|
|
|
189
|
1 |
|
return $this->serializer; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Returns an instance of the Logger class. Filename and directory must be set through setters. |
194
|
|
|
* @return Logger |
195
|
|
|
*/ |
196
|
2 |
|
public function getLogger(): Logger |
197
|
|
|
{ |
198
|
2 |
|
if (!isset($this->logger)) { |
199
|
2 |
|
$this->logger = new Logger(null, null); |
200
|
|
|
} |
201
|
|
|
|
202
|
2 |
|
return $this->logger; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Returns the currently used config object. |
207
|
|
|
* @return Config |
208
|
|
|
*/ |
209
|
1 |
|
public function getConfig(): Config |
210
|
|
|
{ |
211
|
1 |
|
return $this->config; |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
|