[ Index ] |
|
Code source de Symfony 1.0.0 |
1 <?php 2 3 /* 4 * $Id: Properties.php 3076 2006-12-18 08:52:12Z fabien $ 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) . Phing::getProperty('line.separator'); 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 $fw->open(); 158 if ($header !== null) { 159 $fw->write( "# " . $header . Phing::getProperty("line.separator") ); 160 } 161 $fw->write($this->toString()); 162 $fw->close(); 163 } catch (IOException $e) { 164 throw new IOException("Error writing property file: " . $e->getMessage()); 165 } 166 } 167 168 /** 169 * Returns copy of internal properties hash. 170 * Mostly for performance reasons, property hashes are often 171 * preferable to passing around objects. 172 * 173 * @return array 174 */ 175 function getProperties() { 176 return $this->properties; 177 } 178 179 /** 180 * Get value for specified property. 181 * This is the same as get() method. 182 * 183 * @param string $prop The property name (key). 184 * @return mixed 185 * @see get() 186 */ 187 function getProperty($prop) { 188 if (!isset($this->properties[$prop])) { 189 return null; 190 } 191 return $this->properties[$prop]; 192 } 193 194 /** 195 * Get value for specified property. 196 * This function exists to provide a hashtable-like interface for 197 * properties. 198 * 199 * @param string $prop The property name (key). 200 * @return mixed 201 * @see getProperty() 202 */ 203 function get($prop) { 204 if (!isset($this->properties[$prop])) { 205 return null; 206 } 207 return $this->properties[$prop]; 208 } 209 210 /** 211 * Set the value for a property. 212 * 213 * @param string $key 214 * @param mixed $value 215 * @return mixed Old property value or NULL if none was set. 216 */ 217 function setProperty($key, $value) { 218 $oldValue = @$this->properties[$key]; 219 $this->properties[$key] = $value; 220 return $oldValue; 221 } 222 223 /** 224 * Set the value for a property. 225 * This function exists to provide hashtable-lie 226 * interface for properties. 227 * 228 * @param string $key 229 * @param mixed $value 230 */ 231 function put($key, $value) { 232 return $this->setProperty($key, $value); 233 } 234 235 /** 236 * Same as keys() function, returns an array of property names. 237 * @return array 238 */ 239 function propertyNames() { 240 return $this->keys(); 241 } 242 243 /** 244 * Whether loaded properties array contains specified property name. 245 * @return boolean 246 */ 247 function containsKey($key) { 248 return isset($this->properties[$key]); 249 } 250 251 /** 252 * Returns properties keys. 253 * Use this for foreach() {} iterations, as this is 254 * faster than looping through property values. 255 * @return array 256 */ 257 function keys() { 258 return array_keys($this->properties); 259 } 260 261 /** 262 * Whether properties list is empty. 263 * @return boolean 264 */ 265 function isEmpty() { 266 return empty($this->properties); 267 } 268 269 } 270 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Fri Mar 16 22:42:14 2007 | par Balluche grâce à PHPXref 0.7 |