| 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 | include_once 'phing/Task.php'; |
|---|
| 23 | |
|---|
| 24 | /** |
|---|
| 25 | * Task for setting properties in buildfiles. |
|---|
| 26 | * |
|---|
| 27 | * @author Andreas Aderhold <andi@binarycloud.com> |
|---|
| 28 | * @author Hans Lellelid <hans@xmpl.org> |
|---|
| 29 | * @author Matthias Pigulla <mp@webfactory.de> |
|---|
| 30 | * @version $Revision$ |
|---|
| 31 | * @package phing.tasks.system |
|---|
| 32 | */ |
|---|
| 33 | class PropertyTask extends Task { |
|---|
| 34 | |
|---|
| 35 | /** name of the property */ |
|---|
| 36 | protected $name; |
|---|
| 37 | |
|---|
| 38 | /** value of the property */ |
|---|
| 39 | protected $value; |
|---|
| 40 | |
|---|
| 41 | protected $reference; |
|---|
| 42 | protected $env; // Environment |
|---|
| 43 | protected $file; |
|---|
| 44 | protected $ref; |
|---|
| 45 | protected $prefix; |
|---|
| 46 | protected $section; |
|---|
| 47 | protected $fallback; |
|---|
| 48 | |
|---|
| 49 | /** Whether to force overwrite of existing property. */ |
|---|
| 50 | protected $override = false; |
|---|
| 51 | |
|---|
| 52 | /** Whether property should be treated as "user" property. */ |
|---|
| 53 | protected $userProperty = false; |
|---|
| 54 | |
|---|
| 55 | /** |
|---|
| 56 | * Sets a the name of current property component |
|---|
| 57 | */ |
|---|
| 58 | function setName($name) { |
|---|
| 59 | $this->name = (string) $name; |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | /** Get property component name. */ |
|---|
| 63 | function getName() { |
|---|
| 64 | return $this->name; |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | /** |
|---|
| 68 | * Sets a the value of current property component. |
|---|
| 69 | * @param mixed Value of name, all scalars allowed |
|---|
| 70 | */ |
|---|
| 71 | function setValue($value) { |
|---|
| 72 | $this->value = (string) $value; |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | /** |
|---|
| 76 | * Sets value of property to CDATA tag contents. |
|---|
| 77 | * @param string $values |
|---|
| 78 | * @since 2.2.0 |
|---|
| 79 | */ |
|---|
| 80 | public function addText($value) { |
|---|
| 81 | $this->setValue($value); |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | /** Get the value of current property component. */ |
|---|
| 85 | function getValue() { |
|---|
| 86 | return $this->value; |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | /** Set a file to use as the source for properties. */ |
|---|
| 90 | function setFile($file) { |
|---|
| 91 | if (is_string($file)) { |
|---|
| 92 | $file = new PhingFile($file); |
|---|
| 93 | } |
|---|
| 94 | $this->file = $file; |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | /** Get the PhingFile that is being used as property source. */ |
|---|
| 98 | function getFile() { |
|---|
| 99 | return $this->file; |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | function setRefid(Reference $ref) { |
|---|
| 103 | $this->reference = $ref; |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | function getRefid() { |
|---|
| 107 | return $this->reference; |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | /** |
|---|
| 111 | * Prefix to apply to properties loaded using <code>file</code>. |
|---|
| 112 | * A "." is appended to the prefix if not specified. |
|---|
| 113 | * @param string $prefix prefix string |
|---|
| 114 | * @return void |
|---|
| 115 | * @since 2.0 |
|---|
| 116 | */ |
|---|
| 117 | public function setPrefix($prefix) { |
|---|
| 118 | $this->prefix = $prefix; |
|---|
| 119 | if (!StringHelper::endsWith(".", $prefix)) { |
|---|
| 120 | $this->prefix .= "."; |
|---|
| 121 | } |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | /** |
|---|
| 125 | * @return string |
|---|
| 126 | * @since 2.0 |
|---|
| 127 | */ |
|---|
| 128 | public function getPrefix() { |
|---|
| 129 | return $this->prefix; |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | /** |
|---|
| 133 | * Section to load when using <code>file</code>. |
|---|
| 134 | * Only properties from this section and inherited sections will be |
|---|
| 135 | * loaded. |
|---|
| 136 | */ |
|---|
| 137 | public function setSection($s) { |
|---|
| 138 | $this->section = $s; |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | /** @return string */ |
|---|
| 142 | public function getSection() { |
|---|
| 143 | return $this->section; |
|---|
| 144 | } |
|---|
| 145 | |
|---|
| 146 | /** |
|---|
| 147 | * the prefix to use when retrieving environment variables. |
|---|
| 148 | * Thus if you specify environment="myenv" |
|---|
| 149 | * you will be able to access OS-specific |
|---|
| 150 | * environment variables via property names "myenv.PATH" or |
|---|
| 151 | * "myenv.TERM". |
|---|
| 152 | * <p> |
|---|
| 153 | * Note that if you supply a property name with a final |
|---|
| 154 | * "." it will not be doubled. ie environment="myenv." will still |
|---|
| 155 | * allow access of environment variables through "myenv.PATH" and |
|---|
| 156 | * "myenv.TERM". This functionality is currently only implemented |
|---|
| 157 | * on select platforms. Feel free to send patches to increase the number of platforms |
|---|
| 158 | * this functionality is supported on ;).<br> |
|---|
| 159 | * Note also that properties are case sensitive, even if the |
|---|
| 160 | * environment variables on your operating system are not, e.g. it |
|---|
| 161 | * will be ${env.Path} not ${env.PATH} on Windows 2000. |
|---|
| 162 | * @param env prefix |
|---|
| 163 | */ |
|---|
| 164 | function setEnvironment($env) { |
|---|
| 165 | $this->env = (string) $env; |
|---|
| 166 | } |
|---|
| 167 | |
|---|
| 168 | function getEnvironment() { |
|---|
| 169 | return $this->env; |
|---|
| 170 | } |
|---|
| 171 | |
|---|
| 172 | /** |
|---|
| 173 | * Set whether this is a user property (ro). |
|---|
| 174 | * This is deprecated in Ant 1.5, but the userProperty attribute |
|---|
| 175 | * of the class is still being set via constructor, so Phing will |
|---|
| 176 | * allow this method to function. |
|---|
| 177 | * @param boolean $v |
|---|
| 178 | */ |
|---|
| 179 | function setUserProperty($v) { |
|---|
| 180 | $this->userProperty = (boolean) $v; |
|---|
| 181 | } |
|---|
| 182 | |
|---|
| 183 | function getUserProperty() { |
|---|
| 184 | return $this->userProperty; |
|---|
| 185 | } |
|---|
| 186 | |
|---|
| 187 | function setOverride($v) { |
|---|
| 188 | $this->override = (boolean) $v; |
|---|
| 189 | } |
|---|
| 190 | |
|---|
| 191 | function getOverride() { |
|---|
| 192 | return $this->override; |
|---|
| 193 | } |
|---|
| 194 | |
|---|
| 195 | function toString() { |
|---|
| 196 | return (string) $this->value; |
|---|
| 197 | } |
|---|
| 198 | |
|---|
| 199 | /** |
|---|
| 200 | * @param Project $p |
|---|
| 201 | */ |
|---|
| 202 | function setFallback($p) { |
|---|
| 203 | $this->fallback = $p; |
|---|
| 204 | } |
|---|
| 205 | |
|---|
| 206 | function getFallback() { |
|---|
| 207 | return $this->fallback; |
|---|
| 208 | } |
|---|
| 209 | /** |
|---|
| 210 | * set the property in the project to the value. |
|---|
| 211 | * if the task was give a file or env attribute |
|---|
| 212 | * here is where it is loaded |
|---|
| 213 | */ |
|---|
| 214 | function main() { |
|---|
| 215 | if ($this->name !== null) { |
|---|
| 216 | if ($this->value === null && $this->ref === null) { |
|---|
| 217 | throw new BuildException("You must specify value or refid with the name attribute", $this->getLocation()); |
|---|
| 218 | } |
|---|
| 219 | } else { |
|---|
| 220 | if ($this->file === null && $this->env === null ) { |
|---|
| 221 | throw new BuildException("You must specify file or environment when not using the name attribute", $this->getLocation()); |
|---|
| 222 | } |
|---|
| 223 | } |
|---|
| 224 | |
|---|
| 225 | if ($this->file === null && $this->prefix !== null) { |
|---|
| 226 | throw new BuildException("Prefix is only valid when loading from a file.", $this->getLocation()); |
|---|
| 227 | } |
|---|
| 228 | |
|---|
| 229 | if ($this->file === null && $this->section !== null) { |
|---|
| 230 | throw new BuildException("Section is only valid when loading from a file.", $this->getLocation()); |
|---|
| 231 | } |
|---|
| 232 | |
|---|
| 233 | if (($this->name !== null) && ($this->value !== null)) { |
|---|
| 234 | $this->addProperty($this->name, $this->value); |
|---|
| 235 | } |
|---|
| 236 | |
|---|
| 237 | if ($this->file !== null) { |
|---|
| 238 | $this->loadFile($this->file); |
|---|
| 239 | } |
|---|
| 240 | |
|---|
| 241 | if ( $this->env !== null ) { |
|---|
| 242 | $this->loadEnvironment($this->env); |
|---|
| 243 | } |
|---|
| 244 | |
|---|
| 245 | if (($this->name !== null) && ($this->ref !== null)) { |
|---|
| 246 | // get the refereced property |
|---|
| 247 | try { |
|---|
| 248 | $this->addProperty($this->name, $this->reference->getReferencedObject($this->project)->toString()); |
|---|
| 249 | } catch (BuildException $be) { |
|---|
| 250 | if ($this->fallback !== null) { |
|---|
| 251 | $this->addProperty($this->name, $this->reference->getReferencedObject($this->fallback)->toString()); |
|---|
| 252 | } else { |
|---|
| 253 | throw $be; |
|---|
| 254 | } |
|---|
| 255 | } |
|---|
| 256 | } |
|---|
| 257 | } |
|---|
| 258 | |
|---|
| 259 | /** |
|---|
| 260 | * load the environment values |
|---|
| 261 | * @param string $prefix prefix to place before them |
|---|
| 262 | */ |
|---|
| 263 | protected function loadEnvironment($prefix) { |
|---|
| 264 | |
|---|
| 265 | require_once('phing/util/properties/PropertySetImpl.php'); |
|---|
| 266 | $props = new PropertySetImpl(); |
|---|
| 267 | |
|---|
| 268 | if ( substr($prefix, strlen($prefix)-1) == '.' ) { |
|---|
| 269 | $prefix .= "."; |
|---|
| 270 | } |
|---|
| 271 | $this->log("Loading Environment $prefix", Project::MSG_VERBOSE); |
|---|
| 272 | foreach($_ENV as $key => $value) { |
|---|
| 273 | $props["$prefix.$key"] = $value; |
|---|
| 274 | } |
|---|
| 275 | $this->addProperties($props); |
|---|
| 276 | } |
|---|
| 277 | |
|---|
| 278 | /** |
|---|
| 279 | * iterate through a set of properties, |
|---|
| 280 | * resolve them then assign them |
|---|
| 281 | */ |
|---|
| 282 | protected function addProperties(PropertySet $props) { |
|---|
| 283 | foreach($props as $key => $value) { |
|---|
| 284 | if ($this->prefix) $key = "{$this->prefix}$key"; |
|---|
| 285 | $this->addProperty($key, $value); |
|---|
| 286 | } |
|---|
| 287 | } |
|---|
| 288 | |
|---|
| 289 | /** |
|---|
| 290 | * add a name value pair to the project property set |
|---|
| 291 | * @param string $name name of property |
|---|
| 292 | * @param string $value value to set |
|---|
| 293 | */ |
|---|
| 294 | protected function addProperty($name, $value) { |
|---|
| 295 | if ($this->userProperty) { |
|---|
| 296 | if ($this->project->getUserProperty($name) === null || $this->override) { |
|---|
| 297 | $this->project->setInheritedProperty($name, $value); |
|---|
| 298 | } else { |
|---|
| 299 | $this->log("Override ignored for " . $name, Project::MSG_VERBOSE); |
|---|
| 300 | } |
|---|
| 301 | } else { |
|---|
| 302 | if ($this->override) { |
|---|
| 303 | $this->project->setProperty($name, $value); |
|---|
| 304 | } else { |
|---|
| 305 | $this->project->setNewProperty($name, $value); |
|---|
| 306 | } |
|---|
| 307 | } |
|---|
| 308 | } |
|---|
| 309 | |
|---|
| 310 | /** |
|---|
| 311 | * load properties from a file. |
|---|
| 312 | * @param PhingFile $file |
|---|
| 313 | */ |
|---|
| 314 | protected function loadFile(PhingFile $file) { |
|---|
| 315 | $this->log("Loading ". $file->getAbsolutePath(), Project::MSG_INFO); |
|---|
| 316 | try { // try to load file |
|---|
| 317 | if ($file->exists()) { |
|---|
| 318 | $this->addProperties($this->fetchPropertiesFromFile($file)); |
|---|
| 319 | } else { |
|---|
| 320 | $this->log("Unable to find property file: ". $file->getAbsolutePath() ."... skipped", Project::MSG_WARN); |
|---|
| 321 | } |
|---|
| 322 | } catch (IOException $ioe) { |
|---|
| 323 | throw new BuildException("Could not load properties from file.", $ioe); |
|---|
| 324 | } |
|---|
| 325 | } |
|---|
| 326 | |
|---|
| 327 | protected function fetchPropertiesFromFile(PhingFile $f) { |
|---|
| 328 | require_once('phing/util/properties/PropertySetImpl.php'); |
|---|
| 329 | require_once('phing/util/properties/PropertyFileReader.php'); |
|---|
| 330 | |
|---|
| 331 | // do not use the "Properties" façade to defer property expansion |
|---|
| 332 | // (the Project will take care of it) |
|---|
| 333 | $p = new PropertySetImpl(); |
|---|
| 334 | $r = new PropertyFileReader($p); |
|---|
| 335 | $r->load($f, $this->section); |
|---|
| 336 | return $p; |
|---|
| 337 | } |
|---|
| 338 | } |
|---|