|
1
|
|
|
<?php |
|
2
|
|
|
namespace Consolidation\Config\Util; |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* Fetch a configuration value from a configuration group. If the |
|
6
|
|
|
* desired configuration value is not found in the most specific |
|
7
|
|
|
* group named, keep stepping up to the next parent group until a |
|
8
|
|
|
* value is located. |
|
9
|
|
|
* |
|
10
|
|
|
* Given the following constructor inputs: |
|
11
|
|
|
* - $prefix = "command." |
|
12
|
|
|
* - $group = "foo.bar.baz" |
|
13
|
|
|
* - $postfix = ".options." |
|
14
|
|
|
* Then the `get` method will then consider, in order: |
|
15
|
|
|
* - command.foo.bar.baz.options |
|
16
|
|
|
* - command.foo.bar.options |
|
17
|
|
|
* - command.foo.options |
|
18
|
|
|
* If any of these contain an option for "$key", then return its value. |
|
19
|
|
|
*/ |
|
20
|
|
|
class ConfigFallback extends ConfigGroup |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @inheritdoc |
|
24
|
|
|
*/ |
|
25
|
|
|
public function get($key) |
|
26
|
|
|
{ |
|
27
|
|
|
return $this->getWithFallback($key, $this->group, $this->prefix, $this->postfix); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Fetch an option value from a given key, or, if that specific key does |
|
32
|
|
|
* not contain a value, then consult various fallback options until a |
|
33
|
|
|
* value is found. |
|
34
|
|
|
* |
|
35
|
|
|
*/ |
|
36
|
|
|
protected function getWithFallback($key, $group, $prefix = '', $postfix = '.') |
|
37
|
|
|
{ |
|
38
|
|
|
$configKey = "{$prefix}{$group}${postfix}{$key}"; |
|
39
|
|
|
if ($this->config->has($configKey)) { |
|
40
|
|
|
return $this->config->get($configKey); |
|
41
|
|
|
} |
|
42
|
|
|
if ($this->config->hasDefault($configKey)) { |
|
43
|
|
|
return $this->config->getDefault($configKey); |
|
44
|
|
|
} |
|
45
|
|
|
$moreGeneralGroupname = $this->moreGeneralGroupName($group); |
|
46
|
|
|
if ($moreGeneralGroupname) { |
|
47
|
|
|
return $this->getWithFallback($key, $moreGeneralGroupname, $prefix, $postfix); |
|
48
|
|
|
} |
|
49
|
|
|
return null; |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|