1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Ariadne Component Library. |
5
|
|
|
* |
6
|
|
|
* (c) Muze <[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 arc\config; |
13
|
|
|
|
14
|
|
|
class Configuration implements ConfigurationInterface |
15
|
|
|
{ |
16
|
1 |
|
use \arc\traits\Proxy { |
17
|
|
|
\arc\traits\Proxy::__construct as private ProxyConstruct; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
private $tree = null; |
21
|
|
|
|
22
|
6 |
|
public function __construct($tree) |
23
|
|
|
{ |
24
|
6 |
|
$this->ProxyConstruct( $tree ); |
25
|
6 |
|
$this->tree = $tree; |
26
|
6 |
|
} |
27
|
|
|
|
28
|
6 |
|
public function acquire($name) |
29
|
|
|
{ |
30
|
6 |
|
return \arc\tree::dive( |
31
|
6 |
|
$this->tree, |
32
|
6 |
|
function ($node) use ($name) { |
33
|
6 |
|
return $this->getValueIfRoot( $name, $node->nodeValue ); |
34
|
6 |
|
}, |
35
|
6 |
|
function ($node, $result) use ($name) { |
36
|
6 |
|
return $this->mergeValue( $result, $this->getValue( $name, $node->nodeValue ) ); |
37
|
6 |
|
} |
38
|
|
|
); |
39
|
|
|
} |
40
|
|
|
|
41
|
6 |
|
public function configure($name, $value) |
42
|
|
|
{ |
43
|
6 |
|
if (!isset( $this->tree->nodeValue )) { |
44
|
6 |
|
$this->tree->nodeValue = array(); |
45
|
|
|
} |
46
|
6 |
|
$this->setValue( $name, $value, $this->tree->nodeValue ); |
47
|
|
|
|
48
|
6 |
|
return $this; |
49
|
|
|
} |
50
|
|
|
|
51
|
4 |
|
public function cd($path) |
52
|
|
|
{ |
53
|
4 |
|
return new Configuration( $this->tree->cd( $path ) ); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function ls() |
57
|
|
|
{ |
58
|
|
|
return $this->tree->ls( function ($node) { |
59
|
|
|
return new Configuration( $node ); |
60
|
|
|
} ); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
// \arc\KeyValueStoreInterface |
64
|
|
|
public function getVar($name) |
65
|
|
|
{ |
66
|
|
|
return $this->acquire( $name ); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function putVar($name, $value) |
70
|
|
|
{ |
71
|
|
|
return $this->configure( $name, $value ); |
72
|
|
|
} |
73
|
|
|
|
74
|
6 |
|
private function getValue($name, $config) |
75
|
|
|
{ |
76
|
6 |
|
$vars = explode('.', $name); |
77
|
6 |
|
$entry = $config; |
78
|
6 |
|
foreach ($vars as $var) { |
79
|
6 |
|
if (!isset( $entry[$var] )) { |
80
|
2 |
|
return null; |
81
|
|
|
} |
82
|
6 |
|
$entry = $entry[$var]; |
83
|
|
|
} |
84
|
|
|
|
85
|
6 |
|
return $entry; |
86
|
|
|
} |
87
|
|
|
|
88
|
6 |
|
private function setValue($name, $value, &$config) |
89
|
|
|
{ |
90
|
6 |
|
$vars = explode('.', $name); |
91
|
6 |
|
$lastName = array_pop( $vars ); |
92
|
6 |
|
$entry = &$config; |
93
|
6 |
|
foreach ($vars as $var) { |
94
|
6 |
|
if (!isset( $entry[$var] )) { |
95
|
6 |
|
$entry[$var] = array(); |
96
|
|
|
} |
97
|
6 |
|
$entry = &$entry[$var]; |
98
|
|
|
} |
99
|
6 |
|
if ( !is_array( $entry ) ) { |
100
|
|
|
throw \arc\ConfigError( 'Unable to configure '.$name.', parent set to non-array value', \arc\exceptions::CONFIGURATION_ERROR ); |
101
|
|
|
} else { |
102
|
6 |
|
$entry[ $lastName ] = $value; |
103
|
|
|
} |
104
|
6 |
|
} |
105
|
|
|
|
106
|
6 |
|
private function getValueIfRoot($name, $config) |
107
|
|
|
{ |
108
|
6 |
|
$value = $this->getValue( $name, $config ); |
109
|
6 |
|
if (!$this->isHash( $value )) { |
110
|
4 |
|
return $value; |
111
|
|
|
} |
112
|
2 |
|
} |
113
|
|
|
|
114
|
6 |
|
private function isHash($array) |
115
|
|
|
{ |
116
|
6 |
|
return ( is_array( $array ) && !is_numeric( key( $array ) ) ); |
117
|
|
|
} |
118
|
|
|
|
119
|
6 |
|
private function mergeValue($initial, $additional) |
120
|
|
|
{ |
121
|
6 |
|
if (isset( $additional )) { |
122
|
6 |
|
if (!$this->isHash( $initial )) { |
123
|
6 |
|
return $additional; |
124
|
2 |
|
} elseif ( $this->isHash( $additional ) ) { |
125
|
2 |
|
return array_replace_recursive( $initial, $additional ); |
126
|
|
|
} else { |
127
|
|
|
return $additional; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
2 |
|
return $initial; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|