1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Glenn\Config\Facade; |
4
|
|
|
|
5
|
|
|
use Glenn\Config\Contracts\FacadeContract; |
6
|
|
|
use Glenn\Config\Contracts\ManagerContract; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Config Facade to help with retrieving data, from the config, which is pertintent to one specific section. |
10
|
|
|
* |
11
|
|
|
* This means we can have interfaced Config Managers to aid with ensuring that we don't rely |
12
|
|
|
* on the config key names, which will make the code more readable and testable. |
13
|
|
|
* |
14
|
|
|
* @author Glenn McEwan <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
abstract class AbstractFacade implements FacadeContract |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Config manager. |
20
|
|
|
* |
21
|
|
|
* @var ManagerContract |
22
|
|
|
*/ |
23
|
|
|
protected $config; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* The parent key of this Facade's config entries. |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $parentKey = null; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Construct a new Facade and pass in the config. |
34
|
|
|
* |
35
|
|
|
* @param ManagerContract $config |
36
|
|
|
* |
37
|
|
|
* @author Glenn McEwan <[email protected]> |
38
|
|
|
*/ |
39
|
2 |
|
public function __construct(ManagerContract $config) |
40
|
|
|
{ |
41
|
2 |
|
$this->config = $config; |
42
|
2 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Get a value from the config. |
46
|
|
|
* Decorated to pull from the parent if this Facade uses a parent key. |
47
|
|
|
* |
48
|
|
|
* @param string $key The Config key to fetch the value for |
49
|
|
|
* @param mixed $default the default, if the config key does not exist |
50
|
|
|
* |
51
|
|
|
* @return mixed the config value, could be a string, array, etc |
52
|
|
|
* |
53
|
|
|
* @author Glenn McEwan <[email protected]> |
54
|
|
|
*/ |
55
|
2 |
|
public function get($key, $default = null) |
56
|
|
|
{ |
57
|
2 |
|
if ($this->isNested()) { |
58
|
1 |
|
$key = $this->parentKey . '.' . $key; |
59
|
1 |
|
} |
60
|
|
|
|
61
|
2 |
|
return $this->config->get($key, $default); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* When fetching config entries from this facade, |
66
|
|
|
* are they nested under one parent / head key? |
67
|
|
|
* |
68
|
|
|
* @return bool |
69
|
|
|
* |
70
|
|
|
* @author Glenn McEwan <[email protected]> |
71
|
|
|
*/ |
72
|
2 |
|
protected function isNested() |
73
|
|
|
{ |
74
|
2 |
|
return $this->parentKey ? true : false; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|