|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Phoole (PHP7.2+) |
|
5
|
|
|
* |
|
6
|
|
|
* @category Library |
|
7
|
|
|
* @package Phoole\Config |
|
8
|
|
|
* @copyright Copyright (c) 2019 Hong Zhang |
|
9
|
|
|
*/ |
|
10
|
|
|
declare(strict_types=1); |
|
11
|
|
|
|
|
12
|
|
|
namespace Phoole\Config; |
|
13
|
|
|
|
|
14
|
|
|
use Phoole\Base\Tree\Tree; |
|
15
|
|
|
use Phoole\Config\Util\Loader; |
|
16
|
|
|
use Phoole\Base\Reference\ReferenceTrait; |
|
17
|
|
|
use Phoole\Base\Reference\ReferenceInterface; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Config |
|
21
|
|
|
* |
|
22
|
|
|
* @package Phoole\Config |
|
23
|
|
|
*/ |
|
24
|
|
|
class Config implements ConfigInterface, ReferenceInterface |
|
25
|
|
|
{ |
|
26
|
|
|
use ReferenceTrait; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var Tree |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $tree; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Constructor |
|
35
|
|
|
* |
|
36
|
|
|
* ```php |
|
37
|
|
|
* # load from files |
|
38
|
|
|
* $conf = new Config('/my/app/conf', 'product/host1'); |
|
39
|
|
|
* |
|
40
|
|
|
* # load from array |
|
41
|
|
|
* $conf = new Config(['db.user'=> 'root']); |
|
42
|
|
|
* ``` |
|
43
|
|
|
* |
|
44
|
|
|
* @param string|array $dirOrConfData |
|
45
|
|
|
* @param string $environment |
|
46
|
|
|
*/ |
|
47
|
|
|
public function __construct( |
|
48
|
|
|
$dirOrConfData, |
|
49
|
|
|
string $environment = '' |
|
50
|
|
|
) { |
|
51
|
|
|
if (is_string($dirOrConfData)) { |
|
52
|
|
|
$this->tree = (new Loader($dirOrConfData, $environment))->load()->getTree(); |
|
53
|
|
|
} else { |
|
54
|
|
|
$this->tree = new Tree($dirOrConfData); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
// do dereferencing |
|
58
|
|
|
$conf = &$this->tree->get(''); |
|
59
|
|
|
$this->deReference($conf); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* {@inheritDoc} |
|
64
|
|
|
*/ |
|
65
|
|
|
public function get(string $id) |
|
66
|
|
|
{ |
|
67
|
|
|
try { |
|
68
|
|
|
if (0 === strpos($id, 'ENV.')) { |
|
69
|
|
|
return getenv(substr($id, 4)); |
|
70
|
|
|
} else { |
|
71
|
|
|
return $this->tree->get($id); |
|
72
|
|
|
} |
|
73
|
|
|
} catch (\Exception $e) { |
|
74
|
|
|
throw new \RuntimeException($e->getMessage()); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* {@inheritDoc} |
|
80
|
|
|
*/ |
|
81
|
|
|
public function has(string $id): bool |
|
82
|
|
|
{ |
|
83
|
|
|
if (0 === strpos($id, 'ENV.')) { |
|
84
|
|
|
return FALSE !== getenv(substr($id, 4)); |
|
85
|
|
|
} else { |
|
86
|
|
|
return $this->tree->has($id); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Get the tree object |
|
92
|
|
|
* |
|
93
|
|
|
* @return Tree |
|
94
|
|
|
*/ |
|
95
|
|
|
public function getTree(): Tree |
|
96
|
|
|
{ |
|
97
|
|
|
return $this->tree; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* {@inheritDoc} |
|
102
|
|
|
*/ |
|
103
|
|
|
protected function getReference(string $name) |
|
104
|
|
|
{ |
|
105
|
|
|
return $this->get($name); |
|
106
|
|
|
} |
|
107
|
|
|
} |