| 1 | <?php |
|---|
| 2 | /* |
|---|
| 3 | * $Id$ |
|---|
| 4 | * |
|---|
| 5 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|---|
| 6 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|---|
| 7 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|---|
| 8 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|---|
| 9 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|---|
| 10 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|---|
| 11 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|---|
| 12 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|---|
| 13 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|---|
| 14 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|---|
| 15 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 16 | * |
|---|
| 17 | * This software consists of voluntary contributions made by many individuals |
|---|
| 18 | * and is licensed under the LGPL. For more information please see |
|---|
| 19 | * <http://phing.info>. |
|---|
| 20 | */ |
|---|
| 21 | |
|---|
| 22 | require_once('phing/util/properties/PropertySetImpl.php'); |
|---|
| 23 | require_once('phing/util/properties/PropertyExpansionHelper.php'); |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | /** |
|---|
| 27 | * A class for reading and writing property files. |
|---|
| 28 | * |
|---|
| 29 | * This class has been used in the past by various clients (not only |
|---|
| 30 | * within Phing) to read (and in rare cases write) property files. It |
|---|
| 31 | * is now implemented as a façade that exhibits the "old" behaviour, |
|---|
| 32 | * most notably early and transparent ${} placeholder expansion. |
|---|
| 33 | * |
|---|
| 34 | * As it is often used stand-alone, it has no notion of "projects" or other |
|---|
| 35 | * things that might provide property values for expansion. So it might |
|---|
| 36 | * happen that property values returned from this class still contain |
|---|
| 37 | * ${} placeholders that can only be meaningfully resolved at a later |
|---|
| 38 | * stage. |
|---|
| 39 | * |
|---|
| 40 | * @package phing.system.util |
|---|
| 41 | * @version $Revision$ |
|---|
| 42 | * @author many uncredited authors :-) |
|---|
| 43 | * @author Matthias Pigulla <mp@webfactory.de> |
|---|
| 44 | */ |
|---|
| 45 | class Properties implements IteratorAggregate { |
|---|
| 46 | |
|---|
| 47 | protected $properties; |
|---|
| 48 | |
|---|
| 49 | /** |
|---|
| 50 | * Constructor |
|---|
| 51 | * |
|---|
| 52 | * @param array $properties |
|---|
| 53 | */ |
|---|
| 54 | public function __construct($properties = null) |
|---|
| 55 | { |
|---|
| 56 | $this->properties = new PropertyExpansionHelper(new PropertySetImpl()); |
|---|
| 57 | |
|---|
| 58 | if (is_array($properties)) { |
|---|
| 59 | foreach ($properties as $key => $value) { |
|---|
| 60 | $this->setProperty($key, $value); |
|---|
| 61 | } |
|---|
| 62 | } |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | /** |
|---|
| 66 | * Load properties from a file. |
|---|
| 67 | * |
|---|
| 68 | * @param PhingFile $file |
|---|
| 69 | * @return void |
|---|
| 70 | * @throws IOException - if unable to read file. |
|---|
| 71 | */ |
|---|
| 72 | public function load(PhingFile $file, $section = null) { |
|---|
| 73 | require_once('phing/util/properties/PropertyFileReader.php'); |
|---|
| 74 | $r = new PropertyFileReader($this->properties); |
|---|
| 75 | $r->load($file, $section); |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | /** |
|---|
| 79 | * Stores current properties to specified file. |
|---|
| 80 | * |
|---|
| 81 | * @param PhingFile $file File to create/overwrite with properties. |
|---|
| 82 | * @param string $header Header text that will be placed (within comments) at the top of properties file. |
|---|
| 83 | * @return void |
|---|
| 84 | * @throws IOException - on error writing properties file. |
|---|
| 85 | */ |
|---|
| 86 | public function store(PhingFile $file, $header = null) { |
|---|
| 87 | require_once('phing/util/properties/PropertyFileWriter.php'); |
|---|
| 88 | $w = new PropertyFileWriter($this->properties); |
|---|
| 89 | $w->store($file, $header); |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | /** |
|---|
| 93 | * Returns copy of internal properties hash. |
|---|
| 94 | * Mostly for performance reasons, property hashes are often |
|---|
| 95 | * preferable to passing around objects. |
|---|
| 96 | * |
|---|
| 97 | * @return array |
|---|
| 98 | */ |
|---|
| 99 | public function getProperties() { |
|---|
| 100 | return $this->properties; |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | /** |
|---|
| 104 | * Get value for specified property. |
|---|
| 105 | * This is the same as get() method. |
|---|
| 106 | * |
|---|
| 107 | * @param string $prop The property name (key). |
|---|
| 108 | * @return mixed |
|---|
| 109 | * @see get() |
|---|
| 110 | */ |
|---|
| 111 | public function getProperty($prop) { |
|---|
| 112 | return $this->get($prop); |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | /** |
|---|
| 116 | * Get value for specified property. |
|---|
| 117 | * This function exists to provide a hashtable-like interface for |
|---|
| 118 | * properties. |
|---|
| 119 | * |
|---|
| 120 | * @param string $prop The property name (key). |
|---|
| 121 | * @return mixed |
|---|
| 122 | * @see getProperty() |
|---|
| 123 | */ |
|---|
| 124 | public function get($prop) { |
|---|
| 125 | if (!isset($this->properties[$prop])) { |
|---|
| 126 | return null; |
|---|
| 127 | } |
|---|
| 128 | return $this->properties[$prop]; |
|---|
| 129 | } |
|---|
| 130 | |
|---|
| 131 | /** |
|---|
| 132 | * Set the value for a property. |
|---|
| 133 | * |
|---|
| 134 | * @param string $key |
|---|
| 135 | * @param mixed $value |
|---|
| 136 | * @return mixed Old property value or NULL if none was set. |
|---|
| 137 | */ |
|---|
| 138 | public function setProperty($key, $value) { |
|---|
| 139 | return $this->put($key, $value); |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | /** |
|---|
| 143 | * Set the value for a property. |
|---|
| 144 | * This function exists to provide hashtable-lie |
|---|
| 145 | * interface for properties. |
|---|
| 146 | * |
|---|
| 147 | * @param string $key |
|---|
| 148 | * @param mixed $value |
|---|
| 149 | */ |
|---|
| 150 | public function put($key, $value) { |
|---|
| 151 | $oldValue = $this->get($key); |
|---|
| 152 | $this->properties[$key] = $value; |
|---|
| 153 | return $oldValue; |
|---|
| 154 | } |
|---|
| 155 | |
|---|
| 156 | /** |
|---|
| 157 | * Appends a value to a property if it already exists with a delimiter |
|---|
| 158 | * |
|---|
| 159 | * If the property does not, it just adds it. |
|---|
| 160 | * |
|---|
| 161 | * @param string $key |
|---|
| 162 | * @param mixed $value |
|---|
| 163 | * @param string $delimiter |
|---|
| 164 | */ |
|---|
| 165 | public function append($key, $value, $delimiter = ',') { |
|---|
| 166 | $newValue = $value; |
|---|
| 167 | if (($oldValue = $this->get($key)) !== null) { |
|---|
| 168 | $newValue = $oldValue . $delimiter . $value; |
|---|
| 169 | } |
|---|
| 170 | $this->put($key, $newValue); |
|---|
| 171 | } |
|---|
| 172 | |
|---|
| 173 | /** |
|---|
| 174 | * Same as keys() function, returns an array of property names. |
|---|
| 175 | * @return array |
|---|
| 176 | */ |
|---|
| 177 | public function propertyNames() { |
|---|
| 178 | return $this->keys(); |
|---|
| 179 | } |
|---|
| 180 | |
|---|
| 181 | /** |
|---|
| 182 | * Whether loaded properties array contains specified property name. |
|---|
| 183 | * @return boolean |
|---|
| 184 | */ |
|---|
| 185 | public function containsKey($key) { |
|---|
| 186 | return isset($this->properties[$key]); |
|---|
| 187 | } |
|---|
| 188 | |
|---|
| 189 | /** |
|---|
| 190 | * Returns properties keys. |
|---|
| 191 | * Use this for foreach() {} iterations, as this is |
|---|
| 192 | * faster than looping through property values. |
|---|
| 193 | * @return array |
|---|
| 194 | */ |
|---|
| 195 | public function keys() { |
|---|
| 196 | return $this->properties->keys(); |
|---|
| 197 | } |
|---|
| 198 | |
|---|
| 199 | /** |
|---|
| 200 | * Whether properties list is empty. |
|---|
| 201 | * @return boolean |
|---|
| 202 | */ |
|---|
| 203 | public function isEmpty() { |
|---|
| 204 | return $this->properties->isEmpty(); |
|---|
| 205 | } |
|---|
| 206 | |
|---|
| 207 | public function getIterator() { |
|---|
| 208 | return $this->properties->getIterator(); |
|---|
| 209 | } |
|---|
| 210 | |
|---|
| 211 | } |
|---|
| 212 | |
|---|