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