1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the slack-cli package. |
5
|
|
|
* |
6
|
|
|
* (c) Cas Leentfaar <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace CL\SlackCli\Config; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @author Jordi Boggiano <[email protected]> |
16
|
|
|
* @author Cas Leentfaar <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
class Config |
19
|
|
|
{ |
20
|
|
|
public static $defaultConfig = [ |
21
|
|
|
'default_token' => null, |
22
|
|
|
]; |
23
|
|
|
|
24
|
|
|
private $config; |
25
|
|
|
private $configSource; |
26
|
|
|
private $useEnvironment; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param boolean $useEnvironment Use SLACK_CLI_ environment variables to replace config settings |
30
|
|
|
*/ |
31
|
5 |
|
public function __construct($useEnvironment = true) |
32
|
|
|
{ |
33
|
|
|
// load defaults |
34
|
5 |
|
$this->config = static::$defaultConfig; |
35
|
5 |
|
$this->useEnvironment = (bool) $useEnvironment; |
36
|
5 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param $source |
40
|
|
|
*/ |
41
|
5 |
|
public function setConfigSource($source) |
42
|
|
|
{ |
43
|
5 |
|
$this->configSource = $source; |
44
|
5 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @return mixed |
48
|
|
|
*/ |
49
|
|
|
public function getConfigSource() |
50
|
|
|
{ |
51
|
|
|
return $this->configSource; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Merges new config values with the existing ones (overriding) |
56
|
|
|
* |
57
|
|
|
* @param array $config |
58
|
|
|
*/ |
59
|
4 |
|
public function merge($config) |
60
|
|
|
{ |
61
|
|
|
// override defaults with given config |
62
|
4 |
|
if (!empty($config['config']) && is_array($config['config'])) { |
63
|
1 |
|
foreach ($config['config'] as $key => $val) { |
64
|
1 |
|
$this->config[$key] = $val; |
65
|
1 |
|
} |
66
|
1 |
|
} |
67
|
4 |
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Returns a setting |
71
|
|
|
* |
72
|
|
|
* @param string $key |
73
|
|
|
* |
74
|
|
|
* @throws \RuntimeException |
75
|
|
|
* |
76
|
|
|
* @return mixed |
77
|
|
|
*/ |
78
|
2 |
|
public function get($key) |
79
|
|
|
{ |
80
|
|
|
switch ($key) { |
81
|
2 |
|
case 'home': |
82
|
|
|
return rtrim($this->process($this->config[$key]), '/\\'); |
83
|
2 |
|
default: |
84
|
2 |
|
if (!isset($this->config[$key])) { |
85
|
2 |
|
return; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $this->process($this->config[$key]); |
89
|
2 |
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return array |
94
|
|
|
*/ |
95
|
1 |
|
public function all() |
96
|
|
|
{ |
97
|
1 |
|
$all = []; |
98
|
1 |
|
foreach (array_keys($this->config) as $key) { |
99
|
1 |
|
$all['config'][$key] = $this->get($key); |
100
|
1 |
|
} |
101
|
|
|
|
102
|
1 |
|
return $all; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @return array |
107
|
|
|
*/ |
108
|
1 |
|
public function raw() |
109
|
|
|
{ |
110
|
|
|
return [ |
111
|
1 |
|
'config' => $this->config, |
112
|
1 |
|
]; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Checks whether a setting exists |
117
|
|
|
* |
118
|
|
|
* @param string $key |
119
|
|
|
* |
120
|
|
|
* @return bool |
121
|
|
|
*/ |
122
|
2 |
|
public function has($key) |
123
|
|
|
{ |
124
|
2 |
|
return array_key_exists($key, $this->config); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Replaces {$refs} inside a config string |
129
|
|
|
* |
130
|
|
|
* @param string $value a config string that can contain {$refs-to-other-config} |
131
|
|
|
* |
132
|
|
|
* @return string |
133
|
|
|
*/ |
134
|
|
|
private function process($value) |
135
|
|
|
{ |
136
|
|
|
$config = $this; |
137
|
|
|
|
138
|
|
|
if (!is_string($value)) { |
139
|
|
|
return $value; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
return preg_replace_callback('#\{\$(.+)\}#', function ($match) use ($config) { |
143
|
|
|
return $config->get($match[1]); |
144
|
|
|
}, $value); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|