-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSeagull.php
171 lines (153 loc) · 4.46 KB
/
Seagull.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
/**
* Configuration helper. Stores configurations on paths.
*/
class Seagull
{
/**
* The separator to use for paths
*/
protected $separator;
/**
* the complete configuration array
*/
protected $config = array();
/**
* Constructor
*
* @param array $data
* default configuration
* @param string $separator
* the separator to use for paths
*/
public function __construct($data = array(), $separator = '.')
{
$this->separator = $separator;
if(!empty($data)) {
$this->config = $data;
}
}
/**
* Recursive function to get an element from, or set an element in the config by its path
*
* @access protected
*
* @param string $path
* path to the element in the array (path.to.element)
* @param array|string &$conf
* the configuration or part of the configuration (on recursion)
* @param mixed $newValue (optional)
* the new value to set in the config on the specified path
*
* @return mixed
* the value found in the config at the specified path, or null if the path doesn't exist
*/
protected function conf($path, &$conf, $newValue = null) {
if(!$path) return $this->config;
$sep = $this->separator;
$path = explode($sep, $path);
$first = array_shift($path);
if(isset($conf[$first])) {
// there's more left on the path, keep following it
if(count($path)) {
goto recurse;
}
if($newValue === '[[seagull-delete]]') {
// for deleting values
unset($conf[$first]);
return;
} elseif($newValue !== null) {
$conf[$first] = $newValue;
}
return $conf[$first];
} elseif($newValue !== null) {
// a new piece of path, create it
if(count($path)) {
$conf[$first] = array();
goto recurse;
}
return $conf[$first] = $newValue;
}
return null;
recurse:
return $this->conf(implode($sep, $path), $conf[$first], $newValue); // <--- RECURSE!!!
}
/**
* Merge override values into the default config
*
* @param array $merge
* the array to merge into the defaults
* @param array $config
* optional, only needed to recurse
*
* @return array
* the original config, with the new values merged into it
*/
protected function doMerge(array $merge, $config = null)
{
$config = $config !== null ? $config : $this->config;
foreach($merge as $key => $value) {
$config[$key] = array_key_exists($key, $config) && is_array($value)
? $this->doMerge($merge[$key], $config[$key]) // <--- RECURSE!!!
: $value;
}
return $config;
}
/**
* Merge override values into the default config
*
* @param array $merge
* the array to merge into the defaults
*
* @return Seagull
* the current instance, for chaining
*/
public function merge($merge)
{
$this->config = $this->doMerge($merge);
return $this;
}
/**
* Get a value from the config, from a specific path.
*
* @param string $path
* path to the element in the config (path.to.element)
*
* @return mixed
* the value found in the config at the specified path, or null if the path doesn't exist
*/
public function get($path = null)
{
return $this->conf($path, $this->config, null);
}
/**
* Set a value in the config, on a specific path.
*
* @param string $path
* path to the element in the config (path.to.element)
* @param mixed $newValue (optional)
* the new value to set in the config on the specified path
*
* @return Seagull
* the current instance, for chaining
*/
public function set($path, $value)
{
$this->conf($path, $this->config, $value);
return $this;
}
/**
* Delete a value on a path
*
* @param string $path
* the path to remove from the config
*
* @return Seagull
* the current instance, for chaining
*/
public function delete($path)
{
$this->conf($path, $this->config, '[[seagull-delete]]');
return $this;
}
}