|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Botonomous; |
|
4
|
|
|
|
|
5
|
|
|
use Botonomous\utility\ArrayUtility; |
|
6
|
|
|
use Botonomous\utility\StringUtility; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class AbstractConfig. |
|
10
|
|
|
*/ |
|
11
|
|
|
abstract class AbstractConfig |
|
12
|
|
|
{ |
|
13
|
|
|
protected static $configs; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @param $key |
|
17
|
|
|
* @param array $replacements |
|
18
|
|
|
* @param null $plugin |
|
19
|
|
|
* |
|
20
|
|
|
* @throws \Exception |
|
21
|
|
|
* |
|
22
|
|
|
* @return mixed |
|
23
|
|
|
*/ |
|
24
|
108 |
|
public function get(string $key, array $replacements = [], $plugin = null) |
|
25
|
|
|
{ |
|
26
|
108 |
|
$configs = static::$configs; |
|
27
|
|
|
|
|
28
|
|
|
// overwrite $configs if $plugin is presented |
|
29
|
108 |
|
if ($plugin !== null) { |
|
30
|
|
|
try { |
|
31
|
3 |
|
$configs = $this->getPluginConfigs($plugin); |
|
32
|
1 |
|
} catch (\Exception $e) { |
|
33
|
1 |
|
throw $e; |
|
34
|
|
|
} |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
107 |
|
if (!array_key_exists($key, $configs)) { |
|
38
|
1 |
|
throw new BotonomousException("Key: '{$key}' does not exist in configs"); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
106 |
|
$found = $configs[$key]; |
|
42
|
|
|
|
|
43
|
106 |
|
return (new StringUtility())->applyReplacements($found, $replacements); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param $plugin |
|
48
|
|
|
* |
|
49
|
|
|
* @throws \Exception |
|
50
|
|
|
* |
|
51
|
|
|
* @return self |
|
|
|
|
|
|
52
|
|
|
*/ |
|
53
|
4 |
|
private function getPluginConfigObject($plugin): self |
|
54
|
|
|
{ |
|
55
|
4 |
|
$pluginConfigClass = __NAMESPACE__.'\\plugin\\'.strtolower($plugin) |
|
56
|
4 |
|
.'\\'.ucfirst($plugin).'Config'; |
|
57
|
|
|
|
|
58
|
4 |
|
if (!class_exists($pluginConfigClass)) { |
|
59
|
2 |
|
throw new BotonomousException("Config file: '{$pluginConfigClass}.php' does not exist"); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
2 |
|
return new $pluginConfigClass(); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param $plugin |
|
67
|
|
|
* |
|
68
|
|
|
* @throws \Exception |
|
69
|
|
|
* |
|
70
|
|
|
* @return array |
|
71
|
|
|
*/ |
|
72
|
3 |
|
public function getPluginConfigs($plugin) |
|
73
|
|
|
{ |
|
74
|
|
|
try { |
|
75
|
3 |
|
return $this->getPluginConfigObject($plugin)->getConfigs(); |
|
76
|
1 |
|
} catch (\Exception $e) { |
|
77
|
1 |
|
throw $e; |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @return array |
|
83
|
|
|
*/ |
|
84
|
2 |
|
public function getConfigs(): array |
|
85
|
|
|
{ |
|
86
|
2 |
|
return static::$configs; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @param $key |
|
91
|
|
|
* @param $value |
|
92
|
|
|
* @param null $plugin |
|
93
|
|
|
* |
|
94
|
|
|
* @throws \Exception |
|
95
|
|
|
*/ |
|
96
|
38 |
|
public function set($key, $value, $plugin = null) |
|
97
|
|
|
{ |
|
98
|
38 |
|
if ($plugin !== null) { |
|
99
|
|
|
try { |
|
100
|
2 |
|
$this->getPluginConfigObject($plugin)->set($key, $value); |
|
101
|
1 |
|
} catch (\Exception $e) { |
|
102
|
1 |
|
throw $e; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
37 |
|
is_array($key) ? (new ArrayUtility())->setNestedArrayValue(static::$configs, $key, $value) |
|
107
|
20 |
|
: static::$configs[$key] = $value; |
|
108
|
37 |
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.