PHPArray.php
9.4 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
<?php
// +----------------------------------------------------------------------+
// | PHP Version 4 |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2003 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.0 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available at through the world-wide-web at |
// | http://www.php.net/license/2_02.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Authors: Bertrand Mansion <bmansion@mamasam.com> |
// +----------------------------------------------------------------------+
//
// $Id$
/**
* Config parser for common PHP configuration array
* such as found in the horde project.
*
* Options expected is:
* 'name' => 'conf'
* Name of the configuration array.
* Default is $conf[].
* 'useAttr' => true
* Whether we render attributes
*
* @author Bertrand Mansion <bmansion@mamasam.com>
* @package Config
*/
class Config_Container_PHPArray {
/**
* This class options:
* - name of the config array to parse/output
* Ex: $options['name'] = 'myconf';
* - Whether to add attributes to the array
* Ex: $options['useAttr'] = false;
*
* @var array
*/
var $options = array('name' => 'conf',
'useAttr' => true);
/**
* Constructor
*
* @access public
* @param string $options Options to be used by renderer
*/
function Config_Container_PHPArray($options = array())
{
foreach ($options as $key => $value) {
$this->options[$key] = $value;
}
} // end constructor
/**
* Parses the data of the given configuration file
*
* @access public
* @param string $datasrc path to the configuration file
* @param object $obj reference to a config object
* @return mixed returns a PEAR_ERROR, if error occurs or true if ok
*/
function &parseDatasrc($datasrc, &$obj)
{
if (empty($datasrc)) {
return PEAR::raiseError("Datasource file path is empty.", null, PEAR_ERROR_RETURN);
}
if (is_array($datasrc)) {
$this->_parseArray($datasrc, $obj->container);
} else {
if (!file_exists($datasrc)) {
return PEAR::raiseError("Datasource file does not exist.", null, PEAR_ERROR_RETURN);
} else {
include($datasrc);
if (!isset(${$this->options['name']}) || !is_array(${$this->options['name']})) {
return PEAR::raiseError("File '$datasrc' does not contain a required '".$this->options['name']."' array.", null, PEAR_ERROR_RETURN);
}
}
$this->_parseArray(${$this->options['name']}, $obj->container);
}
return true;
} // end func parseDatasrc
/**
* Parses the PHP array recursively
* @param array $array array values from the config file
* @param object $container reference to the container object
* @access private
* @return void
*/
function _parseArray($array, &$container)
{
foreach ($array as $key => $value) {
switch ((string)$key) {
case '@':
$container->setAttributes($value);
break;
case '#':
$container->setType('directive');
$container->setContent($value);
break;
default:
/* if (is_array($value)) {
$section =& $container->createSection($key);
$this->_parseArray($value, $section);
} else {
$container->createDirective($key, $value);
}*/
if (is_array($value)) {
if (is_integer(key($value))) {
foreach ($value as $nestedValue) {
$section =& $container->createSection($key);
$this->_parseArray($nestedValue, $section);
}
} else {
$section =& $container->createSection($key);
$this->_parseArray($value, $section);
}
} else {
$container->createDirective($key, $value);
}
}
}
} // end func _parseArray
/**
* Returns a formatted string of the object
* @param object $obj Container object to be output as string
* @access public
* @return string
*/
function toString(&$obj)
{
if (!isset($string)) {
$string = '';
}
switch ($obj->type) {
case 'blank':
$string .= "\n";
break;
case 'comment':
$string .= '// '.$obj->content."\n";
break;
case 'directive':
$attrString = '';
$parentString = $this->_getParentString($obj);
$attributes = $obj->getAttributes();
if ($this->options['useAttr'] && is_array($attributes) && count($attributes) > 0) {
// Directive with attributes '@' and value '#'
$string .= $parentString."['#']";
foreach ($attributes as $attr => $val) {
$attrString .= $parentString."['@']"
."['".$attr."'] = '".addslashes($val)."';\n";
}
} else {
$string .= $parentString;
}
$string .= ' = ';
if (is_string($obj->content)) {
$string .= "'".addslashes($obj->content)."'";
} elseif (is_int($obj->content) || is_float($obj->content)) {
$string .= $obj->content;
} elseif (is_bool($obj->content)) {
$string .= ($obj->content) ? 'true' : 'false';
}
$string .= ";\n";
$string .= $attrString;
break;
case 'section':
$attrString = '';
$attributes = $obj->getAttributes();
if ($this->options['useAttr'] && is_array($attributes) && count($attributes) > 0) {
$parentString = $this->_getParentString($obj);
foreach ($attributes as $attr => $val) {
$attrString .= $parentString."['@']"
."['".$attr."'] = '".addslashes($val)."';\n";
}
}
$string .= $attrString;
if ($count = count($obj->children)) {
for ($i = 0; $i < $count; $i++) {
$string .= $this->toString($obj->getChild($i));
}
}
break;
default:
$string = '';
}
return $string;
} // end func toString
/**
* Returns a formatted string of the object parents
* @access private
* @return string
*/
function _getParentString(&$obj)
{
$string = '';
if (!$obj->isRoot()) {
if (!$obj->parent->isRoot()) {
$string = is_int($obj->name) ? "[".$obj->name."]" : "['".$obj->name."']";
} else {
if (empty($this->options['name'])) {
$string .= '$'.$obj->name;
} else {
$string .= '$'.$this->options['name']."['".$obj->name."']";
}
}
$string = $this->_getParentString($obj->parent).$string;
$count = $obj->parent->countChildren(null, $obj->name);
if ($count > 1) {
$string .= '['.$obj->getItemPosition().']';
}
}
return $string;
} // end func _getParentString
/**
* Writes the configuration to a file
*
* @param mixed datasrc info on datasource such as path to the configuraton file
* @param string configType (optional)type of configuration
* @access public
* @return string
*/
function writeDatasrc($datasrc, &$obj)
{
$fp = @fopen($datasrc, 'w');
if ($fp) {
$string = "<?php\n". $this->toString($obj) ."?>"; // <? : Fix my syntax coloring
$len = strlen($string);
@flock($fp, LOCK_EX);
@fwrite($fp, $string, $len);
@flock($fp, LOCK_UN);
@fclose($fp);
return true;
} else {
return PEAR::raiseError('Cannot open datasource for writing.', 1, PEAR_ERROR_RETURN);
}
} // end func writeDatasrc
} // end class Config_Container_PHPArray
?>