1 | <?php |
||
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) |
|
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) |
|
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) |
||
68 | |||
69 | public function putVar($name, $value) |
||
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) |
|
113 | |||
114 | 6 | private function isHash($array) |
|
118 | |||
119 | 6 | private function mergeValue($initial, $additional) |
|
133 | } |
||
134 |