| 1 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/*
|
|
|
4 |
* $Id: Properties.php 325 2007-12-20 15:44:58Z hans $
|
|
|
5 |
*
|
|
|
6 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
7 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
8 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
9 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
10 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
11 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
12 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
13 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
14 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
15 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
16 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
17 |
*
|
|
|
18 |
* This software consists of voluntary contributions made by many individuals
|
|
|
19 |
* and is licensed under the LGPL. For more information please see
|
|
|
20 |
* <http://phing.info>.
|
|
|
21 |
*/
|
|
|
22 |
|
|
|
23 |
include_once 'phing/system/io/PhingFile.php';
|
|
|
24 |
include_once 'phing/system/io/FileWriter.php';
|
|
|
25 |
|
|
|
26 |
/**
|
|
|
27 |
* Convenience class for reading and writing property files.
|
|
|
28 |
*
|
|
|
29 |
* FIXME
|
|
|
30 |
* - Add support for arrays (separated by ',')
|
|
|
31 |
*
|
|
|
32 |
* @package phing.system.util
|
|
|
33 |
* @version $Revision: 1.13 $
|
|
|
34 |
*/
|
|
|
35 |
class Properties {
|
|
|
36 |
|
|
|
37 |
private $properties = array();
|
|
|
38 |
|
|
|
39 |
/**
|
|
|
40 |
* Load properties from a file.
|
|
|
41 |
*
|
|
|
42 |
* @param PhingFile $file
|
|
|
43 |
* @return void
|
|
|
44 |
* @throws IOException - if unable to read file.
|
|
|
45 |
*/
|
|
|
46 |
function load(PhingFile $file) {
|
|
|
47 |
if ($file->canRead()) {
|
|
|
48 |
$this->parse($file->getPath(), false);
|
|
|
49 |
} else {
|
|
|
50 |
throw new IOException("Can not read file ".$file->getPath());
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
}
|
|
|
54 |
|
|
|
55 |
/**
|
|
|
56 |
* Replaces parse_ini_file() or better_parse_ini_file().
|
|
|
57 |
* Saves a step since we don't have to parse and then check return value
|
|
|
58 |
* before throwing an error or setting class properties.
|
|
|
59 |
*
|
|
|
60 |
* @param string $filePath
|
|
|
61 |
* @param boolean $processSections Whether to honor [SectionName] sections in INI file.
|
|
|
62 |
* @return array Properties loaded from file (no prop replacements done yet).
|
|
|
63 |
*/
|
|
|
64 |
protected function parse($filePath) {
|
|
|
65 |
|
|
|
66 |
// load() already made sure that file is readable
|
|
|
67 |
// but we'll double check that when reading the file into
|
|
|
68 |
// an array
|
|
|
69 |
|
|
|
70 |
if (($lines = @file($filePath)) === false) {
|
|
|
71 |
throw new IOException("Unable to parse contents of $filePath");
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
$this->properties = array();
|
|
|
75 |
$sec_name = "";
|
|
|
76 |
|
|
|
77 |
foreach($lines as $line) {
|
|
|
78 |
|
|
|
79 |
$line = trim($line);
|
|
|
80 |
|
|
|
81 |
if($line == "")
|
|
|
82 |
continue;
|
|
|
83 |
|
|
|
84 |
if ($line{0} == '#' or $line{0} == ';') {
|
|
|
85 |
// it's a comment, so continue to next line
|
|
|
86 |
continue;
|
|
|
87 |
} else {
|
|
|
88 |
$pos = strpos($line, '=');
|
|
|
89 |
$property = trim(substr($line, 0, $pos));
|
|
|
90 |
$value = trim(substr($line, $pos + 1));
|
|
|
91 |
$this->properties[$property] = $this->inVal($value);
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
} // for each line
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
/**
|
|
|
98 |
* Process values when being read in from properties file.
|
|
|
99 |
* does things like convert "true" => true
|
|
|
100 |
* @param string $val Trimmed value.
|
|
|
101 |
* @return mixed The new property value (may be boolean, etc.)
|
|
|
102 |
*/
|
|
|
103 |
protected function inVal($val) {
|
|
|
104 |
if ($val === "true") {
|
|
|
105 |
$val = true;
|
|
|
106 |
} elseif ($val === "false") {
|
|
|
107 |
$val = false;
|
|
|
108 |
}
|
|
|
109 |
return $val;
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
/**
|
|
|
113 |
* Process values when being written out to properties file.
|
|
|
114 |
* does things like convert true => "true"
|
|
|
115 |
* @param mixed $val The property value (may be boolean, etc.)
|
|
|
116 |
* @return string
|
|
|
117 |
*/
|
|
|
118 |
protected function outVal($val) {
|
|
|
119 |
if ($val === true) {
|
|
|
120 |
$val = "true";
|
|
|
121 |
} elseif ($val === false) {
|
|
|
122 |
$val = "false";
|
|
|
123 |
}
|
|
|
124 |
return $val;
|
|
|
125 |
}
|
|
|
126 |
|
|
|
127 |
/**
|
|
|
128 |
* Create string representation that can be written to file and would be loadable using load() method.
|
|
|
129 |
*
|
|
|
130 |
* Essentially this function creates a string representation of properties that is ready to
|
|
|
131 |
* write back out to a properties file. This is used by store() method.
|
|
|
132 |
*
|
|
|
133 |
* @return string
|
|
|
134 |
*/
|
|
|
135 |
public function toString() {
|
|
|
136 |
$buf = "";
|
|
|
137 |
foreach($this->properties as $key => $item) {
|
|
|
138 |
$buf .= $key . "=" . $this->outVal($item) . PHP_EOL;
|
|
|
139 |
}
|
|
|
140 |
return $buf;
|
|
|
141 |
}
|
|
|
142 |
|
|
|
143 |
/**
|
|
|
144 |
* Stores current properties to specified file.
|
|
|
145 |
*
|
|
|
146 |
* @param PhingFile $file File to create/overwrite with properties.
|
|
|
147 |
* @param string $header Header text that will be placed (within comments) at the top of properties file.
|
|
|
148 |
* @return void
|
|
|
149 |
* @throws IOException - on error writing properties file.
|
|
|
150 |
*/
|
|
|
151 |
function store(PhingFile $file, $header = null) {
|
|
|
152 |
// stores the properties in this object in the file denoted
|
|
|
153 |
// if file is not given and the properties were loaded from a
|
|
|
154 |
// file prior, this method stores them in the file used by load()
|
|
|
155 |
try {
|
|
|
156 |
$fw = new FileWriter($file);
|
|
|
157 |
if ($header !== null) {
|
|
|
158 |
$fw->write( "# " . $header . PHP_EOL );
|
|
|
159 |
}
|
|
|
160 |
$fw->write($this->toString());
|
|
|
161 |
$fw->close();
|
|
|
162 |
} catch (IOException $e) {
|
|
|
163 |
throw new IOException("Error writing property file: " . $e->getMessage());
|
|
|
164 |
}
|
|
|
165 |
}
|
|
|
166 |
|
|
|
167 |
/**
|
|
|
168 |
* Returns copy of internal properties hash.
|
|
|
169 |
* Mostly for performance reasons, property hashes are often
|
|
|
170 |
* preferable to passing around objects.
|
|
|
171 |
*
|
|
|
172 |
* @return array
|
|
|
173 |
*/
|
|
|
174 |
function getProperties() {
|
|
|
175 |
return $this->properties;
|
|
|
176 |
}
|
|
|
177 |
|
|
|
178 |
/**
|
|
|
179 |
* Get value for specified property.
|
|
|
180 |
* This is the same as get() method.
|
|
|
181 |
*
|
|
|
182 |
* @param string $prop The property name (key).
|
|
|
183 |
* @return mixed
|
|
|
184 |
* @see get()
|
|
|
185 |
*/
|
|
|
186 |
function getProperty($prop) {
|
|
|
187 |
if (!isset($this->properties[$prop])) {
|
|
|
188 |
return null;
|
|
|
189 |
}
|
|
|
190 |
return $this->properties[$prop];
|
|
|
191 |
}
|
|
|
192 |
|
|
|
193 |
/**
|
|
|
194 |
* Get value for specified property.
|
|
|
195 |
* This function exists to provide a hashtable-like interface for
|
|
|
196 |
* properties.
|
|
|
197 |
*
|
|
|
198 |
* @param string $prop The property name (key).
|
|
|
199 |
* @return mixed
|
|
|
200 |
* @see getProperty()
|
|
|
201 |
*/
|
|
|
202 |
function get($prop) {
|
|
|
203 |
if (!isset($this->properties[$prop])) {
|
|
|
204 |
return null;
|
|
|
205 |
}
|
|
|
206 |
return $this->properties[$prop];
|
|
|
207 |
}
|
|
|
208 |
|
|
|
209 |
/**
|
|
|
210 |
* Set the value for a property.
|
|
|
211 |
*
|
|
|
212 |
* @param string $key
|
|
|
213 |
* @param mixed $value
|
|
|
214 |
* @return mixed Old property value or NULL if none was set.
|
|
|
215 |
*/
|
|
|
216 |
function setProperty($key, $value) {
|
|
|
217 |
$oldValue = null;
|
|
|
218 |
if (isset($this->properties[$key])) {
|
|
|
219 |
$oldValue = $this->properties[$key];
|
|
|
220 |
}
|
|
|
221 |
$this->properties[$key] = $value;
|
|
|
222 |
return $oldValue;
|
|
|
223 |
}
|
|
|
224 |
|
|
|
225 |
/**
|
|
|
226 |
* Set the value for a property.
|
|
|
227 |
* This function exists to provide hashtable-lie
|
|
|
228 |
* interface for properties.
|
|
|
229 |
*
|
|
|
230 |
* @param string $key
|
|
|
231 |
* @param mixed $value
|
|
|
232 |
*/
|
|
|
233 |
function put($key, $value) {
|
|
|
234 |
return $this->setProperty($key, $value);
|
|
|
235 |
}
|
|
|
236 |
|
|
|
237 |
/**
|
|
|
238 |
* Same as keys() function, returns an array of property names.
|
|
|
239 |
* @return array
|
|
|
240 |
*/
|
|
|
241 |
function propertyNames() {
|
|
|
242 |
return $this->keys();
|
|
|
243 |
}
|
|
|
244 |
|
|
|
245 |
/**
|
|
|
246 |
* Whether loaded properties array contains specified property name.
|
|
|
247 |
* @return boolean
|
|
|
248 |
*/
|
|
|
249 |
function containsKey($key) {
|
|
|
250 |
return isset($this->properties[$key]);
|
|
|
251 |
}
|
|
|
252 |
|
|
|
253 |
/**
|
|
|
254 |
* Returns properties keys.
|
|
|
255 |
* Use this for foreach() {} iterations, as this is
|
|
|
256 |
* faster than looping through property values.
|
|
|
257 |
* @return array
|
|
|
258 |
*/
|
|
|
259 |
function keys() {
|
|
|
260 |
return array_keys($this->properties);
|
|
|
261 |
}
|
|
|
262 |
|
|
|
263 |
/**
|
|
|
264 |
* Whether properties list is empty.
|
|
|
265 |
* @return boolean
|
|
|
266 |
*/
|
|
|
267 |
function isEmpty() {
|
|
|
268 |
return empty($this->properties);
|
|
|
269 |
}
|
|
|
270 |
|
|
|
271 |
}
|
|
|
272 |
|