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 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
|
<?php
/*
* $Id: b06f2f763fcb033b781df6aa29a5ef860e47d02e $
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information please see
* <http://phing.info>.
*/
include_once 'phing/Task.php';
include_once 'phing/system/util/Properties.php';
/**
* Task for setting properties in buildfiles.
*
* @author Andreas Aderhold <andi@binarycloud.com>
* @author Hans Lellelid <hans@xmpl.org>
* @version $Id: b06f2f763fcb033b781df6aa29a5ef860e47d02e $
* @package phing.tasks.system
*/
class PropertyTask extends Task {
/** name of the property */
protected $name;
/** value of the property */
protected $value;
protected $reference;
protected $env; // Environment
protected $file;
protected $ref;
protected $prefix;
protected $fallback;
/** Whether to force overwrite of existing property. */
protected $override = false;
/** Whether property should be treated as "user" property. */
protected $userProperty = false;
/**
* All filterchain objects assigned to this task
*/
protected $filterChains = array();
/**
* Sets a the name of current property component
*/
function setName($name) {
$this->name = (string) $name;
}
/** Get property component name. */
function getName() {
return $this->name;
}
/**
* Sets a the value of current property component.
* @param mixed Value of name, all scalars allowed
*/
function setValue($value) {
$this->value = (string) $value;
}
/**
* Sets value of property to CDATA tag contents.
* @param string $values
* @since 2.2.0
*/
public function addText($value) {
$this->setValue($value);
}
/** Get the value of current property component. */
function getValue() {
return $this->value;
}
/** Set a file to use as the source for properties. */
function setFile($file) {
if (is_string($file)) {
$file = new PhingFile($file);
}
$this->file = $file;
}
/** Get the PhingFile that is being used as property source. */
function getFile() {
return $this->file;
}
function setRefid(Reference $ref) {
$this->reference = $ref;
}
function getRefid() {
return $this->reference;
}
/**
* Prefix to apply to properties loaded using <code>file</code>.
* A "." is appended to the prefix if not specified.
* @param string $prefix prefix string
* @return void
* @since 2.0
*/
public function setPrefix($prefix) {
$this->prefix = $prefix;
if (!StringHelper::endsWith(".", $prefix)) {
$this->prefix .= ".";
}
}
/**
* @return string
* @since 2.0
*/
public function getPrefix() {
return $this->prefix;
}
/**
* the prefix to use when retrieving environment variables.
* Thus if you specify environment="myenv"
* you will be able to access OS-specific
* environment variables via property names "myenv.PATH" or
* "myenv.TERM".
* <p>
* Note that if you supply a property name with a final
* "." it will not be doubled. ie environment="myenv." will still
* allow access of environment variables through "myenv.PATH" and
* "myenv.TERM". This functionality is currently only implemented
* on select platforms. Feel free to send patches to increase the number of platforms
* this functionality is supported on ;).<br>
* Note also that properties are case sensitive, even if the
* environment variables on your operating system are not, e.g. it
* will be ${env.Path} not ${env.PATH} on Windows 2000.
* @param env prefix
*/
function setEnvironment($env) {
$this->env = (string) $env;
}
function getEnvironment() {
return $this->env;
}
/**
* Set whether this is a user property (ro).
* This is deprecated in Ant 1.5, but the userProperty attribute
* of the class is still being set via constructor, so Phing will
* allow this method to function.
* @param boolean $v
*/
function setUserProperty($v) {
$this->userProperty = (boolean) $v;
}
function getUserProperty() {
return $this->userProperty;
}
function setOverride($v) {
$this->override = (boolean) $v;
}
function getOverride() {
return $this->override;
}
function toString() {
return (string) $this->value;
}
/**
* @param Project $p
*/
function setFallback($p) {
$this->fallback = $p;
}
function getFallback() {
return $this->fallback;
}
/**
* Creates a filterchain
*
* @access public
* @return object The created filterchain object
*/
public function createFilterChain() {
$num = array_push($this->filterChains, new FilterChain($this->project));
return $this->filterChains[$num-1];
}
/**
* set the property in the project to the value.
* if the task was give a file or env attribute
* here is where it is loaded
*/
function main() {
if ($this->name !== null) {
if ($this->value === null && $this->reference === null) {
throw new BuildException("You must specify value or refid with the name attribute", $this->getLocation());
}
} else {
if ($this->file === null && $this->env === null ) {
throw new BuildException("You must specify file or environment when not using the name attribute", $this->getLocation());
}
}
if ($this->file === null && $this->prefix !== null) {
throw new BuildException("Prefix is only valid when loading from a file.", $this->getLocation());
}
if (($this->name !== null) && ($this->value !== null)) {
$this->addProperty($this->name, $this->value);
}
if ($this->file !== null) {
$this->loadFile($this->file);
}
if ( $this->env !== null ) {
$this->loadEnvironment($this->env);
}
if (($this->name !== null) && ($this->reference !== null)) {
// get the refereced property
try {
$this->addProperty($this->name, $this->reference->getReferencedObject($this->project)->toString());
} catch (BuildException $be) {
if ($this->fallback !== null) {
$this->addProperty($this->name, $this->reference->getReferencedObject($this->fallback)->toString());
} else {
throw $be;
}
}
}
}
/**
* load the environment values
* @param string $prefix prefix to place before them
*/
protected function loadEnvironment($prefix) {
$props = new Properties();
if ( substr($prefix, strlen($prefix)-1) == '.' ) {
$prefix .= ".";
}
$this->log("Loading Environment $prefix", Project::MSG_VERBOSE);
foreach($_ENV as $key => $value) {
$props->setProperty($prefix . '.' . $key, $value);
}
$this->addProperties($props);
}
/**
* iterate through a set of properties,
* resolve them then assign them
*/
protected function addProperties($props) {
$this->resolveAllProperties($props);
foreach($props->keys() as $name) {
$value = $props->getProperty($name);
$v = $this->project->replaceProperties($value);
if ($this->prefix !== null) {
$name = $this->prefix . $name;
}
$this->addProperty($name, $v);
}
}
/**
* add a name value pair to the project property set
* @param string $name name of property
* @param string $value value to set
*/
protected function addProperty($name, $value) {
if (count($this->filterChains) > 0) {
$in = FileUtils::getChainedReader(new StringReader($value), $this->filterChains, $this->project);
$value = $in->read();
}
if ($this->userProperty) {
if ($this->project->getUserProperty($name) === null || $this->override) {
$this->project->setInheritedProperty($name, $value);
} else {
$this->log("Override ignored for " . $name, Project::MSG_VERBOSE);
}
} else {
if ($this->override) {
$this->project->setProperty($name, $value);
} else {
$this->project->setNewProperty($name, $value);
}
}
}
/**
* load properties from a file.
* @param PhingFile $file
*/
protected function loadFile(PhingFile $file) {
$props = new Properties();
$this->log("Loading ". $file->getAbsolutePath(), Project::MSG_INFO);
try { // try to load file
if ($file->exists()) {
$props->load($file);
$this->addProperties($props);
} else {
$this->log("Unable to find property file: ". $file->getAbsolutePath() ."... skipped", Project::MSG_WARN);
}
} catch (IOException $ioe) {
throw new BuildException("Could not load properties from file.", $ioe);
}
}
/**
* Given a Properties object, this method goes through and resolves
* any references to properties within the object.
*
* @param Properties $props The collection of Properties that need to be resolved.
* @return void
*/
protected function resolveAllProperties(Properties $props) {
foreach ($props->keys() as $name) {
// There may be a nice regex/callback way to handle this
// replacement, but at the moment it is pretty complex, and
// would probably be a lot uglier to work into a preg_replace_callback()
// system. The biggest problem is the fact that a resolution may require
// multiple passes.
$value = $props->getProperty($name);
$resolved = false;
$resolveStack = array();
while(!$resolved) {
$fragments = array();
$propertyRefs = array();
// [HL] this was ::parsePropertyString($this->value ...) ... this seems wrong
self::parsePropertyString($value, $fragments, $propertyRefs);
$resolved = true;
if (count($propertyRefs) == 0) {
continue;
}
$sb = "";
$j = $propertyRefs;
foreach ($fragments as $fragment) {
if ($fragment !== null) {
$sb .= $fragment;
continue;
}
$propertyName = array_shift($j);
if (in_array($propertyName, $resolveStack) ) {
// Should we maybe just log this as an error & move on?
// $this->log("Property ".$name." was circularly defined.", Project::MSG_ERR);
throw new BuildException("Property ".$propertyName." was circularly defined.");
}
$fragment = $this->getProject()->getProperty($propertyName);
if ($fragment !== null) {
$sb .= $fragment;
continue;
}
if ($props->containsKey($propertyName)) {
$fragment = $props->getProperty($propertyName);
if (strpos($fragment, '${') !== false) {
$resolveStack[] = $propertyName;
$resolved = false; // parse again (could have been replaced w/ another var)
}
} else {
$fragment = "\${".$propertyName."}";
}
$sb .= $fragment;
}
$this->log("Resolved Property \"$value\" to \"$sb\"", Project::MSG_DEBUG);
$value = $sb;
$props->setProperty($name, $value);
} // while (!$resolved)
} // while (count($keys)
}
/**
* This method will parse a string containing ${value} style
* property values into two lists. The first list is a collection
* of text fragments, while the other is a set of string property names
* null entries in the first list indicate a property reference from the
* second list.
*
* This is slower than regex, but useful for this class, which has to handle
* multiple parsing passes for properties.
*
* @param string $value The string to be scanned for property references
* @param array &$fragments The found fragments
* @param array &$propertyRefs The found refs
*/
protected function parsePropertyString($value, &$fragments, &$propertyRefs) {
$prev = 0;
$pos = 0;
while (($pos = strpos($value, '$', $prev)) !== false) {
if ($pos > $prev) {
array_push($fragments, StringHelper::substring($value, $prev, $pos-1));
}
if ($pos === (strlen($value) - 1)) {
array_push($fragments, '$');
$prev = $pos + 1;
} elseif ($value{$pos+1} !== '{' ) {
// the string positions were changed to value-1 to correct
// a fatal error coming from function substring()
array_push($fragments, StringHelper::substring($value, $pos, $pos + 1));
$prev = $pos + 2;
} else {
$endName = strpos($value, '}', $pos);
if ($endName === false) {
throw new BuildException("Syntax error in property: $value");
}
$propertyName = StringHelper::substring($value, $pos + 2, $endName-1);
array_push($fragments, null);
array_push($propertyRefs, $propertyName);
$prev = $endName + 1;
}
}
if ($prev < strlen($value)) {
array_push($fragments, StringHelper::substring($value, $prev));
}
}
}
|