-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfig.php
241 lines (206 loc) · 6.15 KB
/
Config.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
<?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 Exception;
use MAKS\AmqpAgent\Helper\ArrayProxy;
use MAKS\AmqpAgent\Exception\ConfigFileNotFoundException;
/**
* A class that turns the configuration file into an object.
*
* Example:
* ```
* $config = new Config('path/to/some/config-file.php'); // specific config
* $config = new Config(); // default config
* ```
*
* @since 1.0.0
* @property array $connectionOptions
* @property array $channelOptions
* @property array $queueOptions
* @property array $exchangeOptions
* @property array $bindOptions
* @property array $qosOptions
* @property array $waitOptions
* @property array $messageOptions
* @property array $publishOptions
* @property array $consumeOptions
* @property array $rpcConnectionOptions
* @property string $rpcQueueName
*/
final class Config
{
/**
* The default name of the configuration file.
* @var string
*/
public const DEFAULT_CONFIG_FILE_NAME = 'maks-amqp-agent-config';
/**
* The default path of the configuration file.
* @var string
*/
public const DEFAULT_CONFIG_FILE_PATH = __DIR__ . DIRECTORY_SEPARATOR . 'Config' . DIRECTORY_SEPARATOR . self::DEFAULT_CONFIG_FILE_NAME . '.php';
/**
* The multidimensional configuration array.
* @var array
*/
private $config;
/**
* Configuration file path.
* @var string
*/
private $configPath;
/**
* Config object constructor.
* @param string|null $configPath [optional] The path to AMQP Agent configuration file.
* @throws ConfigFileNotFoundException
*/
public function __construct(?string $configPath = null)
{
$configFile = realpath($configPath ?? self::DEFAULT_CONFIG_FILE_PATH);
if (!$configFile || !file_exists($configFile)) {
throw new ConfigFileNotFoundException(
"AMQP Agent configuration file cloud not be found, check if the given path \"{$configPath}\" exists."
);
}
$this->config = include($configFile);
$this->configPath = $configFile;
$this->repair();
}
/**
* Gets the the given key from the configuration array via public property access notation.
* @param string $key
* @return mixed
*/
public function __get(string $key)
{
return $this->config[$key];
}
/**
* Sets the the given key in the configuration array via public property assignment notation.
* @param string $key
* @param mixed $value
* @return void
*/
public function __set(string $key, $value)
{
$this->config[$key] = $value;
}
/**
* Returns config file path if the object was casted to a string.
* @return string
*/
public function __toString()
{
return $this->configPath;
}
/**
* Repairs the config array if first-level of the passed array does not have all keys.
* @return void
*/
private function repair(): void
{
$config = require(self::DEFAULT_CONFIG_FILE_PATH);
foreach ($config as $key => $value) {
if (!array_key_exists($key, $this->config)) {
$this->config[$key] = [];
}
}
unset($config);
}
/**
* Checks whether a value exists in the configuration array via dot-notation representation.
* @since 1.2.2
* @param string $key The dotted key representation.
* @return bool True if key is set otherwise false.
*/
public function has(string $key): bool
{
$value = ArrayProxy::getArrayValueByKey($this->config, $key, null);
return isset($value);
}
/**
* Gets a value of a key from the configuration array via dot-notation representation.
* @since 1.2.2
* @param string $key The dotted key representation.
* @return mixed The requested value or null.
*/
public function get(string $key)
{
$value = ArrayProxy::getArrayValueByKey($this->config, $key);
return $value;
}
/**
* Sets a value of a key from the configuration array via dot-notation representation.
* @since 1.2.2
* @param string $key The dotted key representation.
* @param mixed $value The value to set.
* @return self
*/
public function set(string $key, $value)
{
ArrayProxy::setArrayValueByKey($this->config, $key, $value);
return $this;
}
/**
* Returns the default configuration array.
* @return array
*/
public function getDefaultConfig(): array
{
return include(self::DEFAULT_CONFIG_FILE_PATH);
}
/**
* Returns the current configuration array.
* @return array
*/
public function getConfig(): array
{
return $this->config;
}
/**
* Sets a new configuration array to be used instead of the current.
* @param array $config
* @return self
*/
public function setConfig(array $config)
{
$this->config = $config;
$this->repair();
return $this;
}
/**
* Returns the path of the configuration file.
* @return string
*/
public function getConfigPath(): string
{
return $this->configPath;
}
/**
* Sets the path of the configuration file and rebuilds the internal state of the object.
* @param string $configPath
* @return self
* @throws ConfigFileNotFoundException
*/
public function setConfigPath(string $configPath)
{
try {
$this->config = include($configPath);
$this->configPath = $configPath;
$this->repair();
} catch (Exception $error) {
throw new ConfigFileNotFoundException(
"Something went wrong when trying to include the file and rebuild the configuration, check if the given path \"{$configPath}\" exists.",
(int)$error->getCode(),
$error
);
}
return $this;
}
}