123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439 |
- <?php
- include_once 'phing/Task.php';
- include_once 'phing/system/util/Properties.php';
- class PropertyTask extends Task {
-
- protected $name;
-
-
- protected $value;
-
- protected $reference;
- protected $env;
- protected $file;
- protected $ref;
- protected $prefix;
- protected $fallback;
-
-
- protected $override = false;
-
-
- protected $userProperty = false;
-
- function setName($name) {
- $this->name = (string) $name;
- }
-
-
- function getName() {
- return $this->name;
- }
-
- function setValue($value) {
- $this->value = (string) $value;
- }
-
-
- public function addText($value) {
- $this->setValue($value);
- }
-
-
- function getValue() {
- return $this->value;
- }
-
-
- function setFile($file) {
- if (is_string($file)) {
- $file = new PhingFile($file);
- }
- $this->file = $file;
- }
-
-
- function getFile() {
- return $this->file;
- }
- function setRefid(Reference $ref) {
- $this->reference = $ref;
- }
-
- function getRefid() {
- return $this->reference;
- }
-
- public function setPrefix($prefix) {
- $this->prefix = $prefix;
- if (!StringHelper::endsWith(".", $prefix)) {
- $this->prefix .= ".";
- }
- }
-
- public function getPrefix() {
- return $this->prefix;
- }
-
- function setEnvironment($env) {
- $this->env = (string) $env;
- }
- function getEnvironment() {
- return $this->env;
- }
-
-
- 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;
- }
-
- function setFallback($p) {
- $this->fallback = $p;
- }
-
- function getFallback() {
- return $this->fallback;
- }
-
- function main() {
- if ($this->name !== null) {
- if ($this->value === null && $this->ref === 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->ref !== null)) {
-
- 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;
- }
- }
- }
- }
-
-
- 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);
- }
-
- 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);
- }
- }
-
- protected function addProperty($name, $value) {
- 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);
- }
- }
- }
-
- protected function loadFile(PhingFile $file) {
- $props = new Properties();
- $this->log("Loading ". $file->getAbsolutePath(), Project::MSG_INFO);
- try {
- 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);
- }
- }
-
-
- protected function resolveAllProperties(Properties $props) {
-
- $keys = $props->keys();
- while(count($keys)) {
-
-
-
-
-
-
- $name = array_shift($keys);
- $value = $props->getProperty($name);
- $resolved = false;
-
- while(!$resolved) {
-
- $fragments = array();
- $propertyRefs = array();
-
- self::parsePropertyString($value, $fragments, $propertyRefs);
- $resolved = true;
- if (count($propertyRefs) !== 0) {
- $sb = "";
- $i = $fragments;
- $j = $propertyRefs;
- while(count($i)) {
- $fragment = array_shift($i);
- if ($fragment === null) {
- $propertyName = array_shift($j);
- if ($propertyName === $name) {
-
-
- throw new BuildException("Property ".$name." was circularly defined.");
- }
- $fragment = $this->getProject()->getProperty($propertyName);
- if ($fragment === null) {
- if ($props->containsKey($propertyName)) {
- $fragment = $props->getProperty($propertyName);
- $resolved = false;
- } else {
- $fragment = "\${".$propertyName."}";
- }
- }
- }
- $sb .= $fragment;
- }
-
- $this->log("Resolved Property \"$value\" to \"$sb\"", Project::MSG_DEBUG);
- $value = $sb;
- $props->setProperty($name, $value);
-
- }
-
- }
-
- }
- }
-
- 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} !== '{' ) {
-
-
- 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));
- }
- }
- }
|