PageRenderTime 47ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/vendor/symfony/lib/plugins/sfPropelPlugin/lib/vendor/phing/tasks/system/PropertyTask.php

https://github.com/IDCI-Consulting/WebsiteEval
PHP | 438 lines | 235 code | 63 blank | 140 comment | 43 complexity | f6ca1e230ac4b6bb59f0fcc141a075e3 MD5 | raw file
  1. <?php
  2. /*
  3. * $Id: PropertyTask.php 144 2007-02-05 15:19:00Z hans $
  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. include_once 'phing/Task.php';
  22. include_once 'phing/system/util/Properties.php';
  23. /**
  24. * Task for setting properties in buildfiles.
  25. *
  26. * @author Andreas Aderhold <andi@binarycloud.com>
  27. * @author Hans Lellelid <hans@xmpl.org>
  28. * @version $Revision$
  29. * @package phing.tasks.system
  30. */
  31. class PropertyTask extends Task {
  32. /** name of the property */
  33. protected $name;
  34. /** value of the property */
  35. protected $value;
  36. protected $reference;
  37. protected $env; // Environment
  38. protected $file;
  39. protected $ref;
  40. protected $prefix;
  41. protected $fallback;
  42. /** Whether to force overwrite of existing property. */
  43. protected $override = false;
  44. /** Whether property should be treated as "user" property. */
  45. protected $userProperty = false;
  46. /**
  47. * Sets a the name of current property component
  48. */
  49. function setName($name) {
  50. $this->name = (string) $name;
  51. }
  52. /** Get property component name. */
  53. function getName() {
  54. return $this->name;
  55. }
  56. /**
  57. * Sets a the value of current property component.
  58. * @param mixed Value of name, all scalars allowed
  59. */
  60. function setValue($value) {
  61. $this->value = (string) $value;
  62. }
  63. /**
  64. * Sets value of property to CDATA tag contents.
  65. * @param string $values
  66. * @since 2.2.0
  67. */
  68. public function addText($value) {
  69. $this->setValue($value);
  70. }
  71. /** Get the value of current property component. */
  72. function getValue() {
  73. return $this->value;
  74. }
  75. /** Set a file to use as the source for properties. */
  76. function setFile($file) {
  77. if (is_string($file)) {
  78. $file = new PhingFile($file);
  79. }
  80. $this->file = $file;
  81. }
  82. /** Get the PhingFile that is being used as property source. */
  83. function getFile() {
  84. return $this->file;
  85. }
  86. function setRefid(Reference $ref) {
  87. $this->reference = $ref;
  88. }
  89. function getRefid() {
  90. return $this->reference;
  91. }
  92. /**
  93. * Prefix to apply to properties loaded using <code>file</code>.
  94. * A "." is appended to the prefix if not specified.
  95. * @param string $prefix prefix string
  96. * @return void
  97. * @since 2.0
  98. */
  99. public function setPrefix($prefix) {
  100. $this->prefix = $prefix;
  101. if (!StringHelper::endsWith(".", $prefix)) {
  102. $this->prefix .= ".";
  103. }
  104. }
  105. /**
  106. * @return string
  107. * @since 2.0
  108. */
  109. public function getPrefix() {
  110. return $this->prefix;
  111. }
  112. /**
  113. * the prefix to use when retrieving environment variables.
  114. * Thus if you specify environment="myenv"
  115. * you will be able to access OS-specific
  116. * environment variables via property names "myenv.PATH" or
  117. * "myenv.TERM".
  118. * <p>
  119. * Note that if you supply a property name with a final
  120. * "." it will not be doubled. ie environment="myenv." will still
  121. * allow access of environment variables through "myenv.PATH" and
  122. * "myenv.TERM". This functionality is currently only implemented
  123. * on select platforms. Feel free to send patches to increase the number of platforms
  124. * this functionality is supported on ;).<br>
  125. * Note also that properties are case sensitive, even if the
  126. * environment variables on your operating system are not, e.g. it
  127. * will be ${env.Path} not ${env.PATH} on Windows 2000.
  128. * @param env prefix
  129. */
  130. function setEnvironment($env) {
  131. $this->env = (string) $env;
  132. }
  133. function getEnvironment() {
  134. return $this->env;
  135. }
  136. /**
  137. * Set whether this is a user property (ro).
  138. * This is deprecated in Ant 1.5, but the userProperty attribute
  139. * of the class is still being set via constructor, so Phing will
  140. * allow this method to function.
  141. * @param boolean $v
  142. */
  143. function setUserProperty($v) {
  144. $this->userProperty = (boolean) $v;
  145. }
  146. function getUserProperty() {
  147. return $this->userProperty;
  148. }
  149. function setOverride($v) {
  150. $this->override = (boolean) $v;
  151. }
  152. function getOverride() {
  153. return $this->override;
  154. }
  155. function toString() {
  156. return (string) $this->value;
  157. }
  158. /**
  159. * @param Project $p
  160. */
  161. function setFallback($p) {
  162. $this->fallback = $p;
  163. }
  164. function getFallback() {
  165. return $this->fallback;
  166. }
  167. /**
  168. * set the property in the project to the value.
  169. * if the task was give a file or env attribute
  170. * here is where it is loaded
  171. */
  172. function main() {
  173. if ($this->name !== null) {
  174. if ($this->value === null && $this->ref === null) {
  175. throw new BuildException("You must specify value or refid with the name attribute", $this->getLocation());
  176. }
  177. } else {
  178. if ($this->file === null && $this->env === null ) {
  179. throw new BuildException("You must specify file or environment when not using the name attribute", $this->getLocation());
  180. }
  181. }
  182. if ($this->file === null && $this->prefix !== null) {
  183. throw new BuildException("Prefix is only valid when loading from a file.", $this->getLocation());
  184. }
  185. if (($this->name !== null) && ($this->value !== null)) {
  186. $this->addProperty($this->name, $this->value);
  187. }
  188. if ($this->file !== null) {
  189. $this->loadFile($this->file);
  190. }
  191. if ( $this->env !== null ) {
  192. $this->loadEnvironment($this->env);
  193. }
  194. if (($this->name !== null) && ($this->ref !== null)) {
  195. // get the refereced property
  196. try {
  197. $this->addProperty($this->name, $this->reference->getReferencedObject($this->project)->toString());
  198. } catch (BuildException $be) {
  199. if ($this->fallback !== null) {
  200. $this->addProperty($this->name, $this->reference->getReferencedObject($this->fallback)->toString());
  201. } else {
  202. throw $be;
  203. }
  204. }
  205. }
  206. }
  207. /**
  208. * load the environment values
  209. * @param string $prefix prefix to place before them
  210. */
  211. protected function loadEnvironment($prefix) {
  212. $props = new Properties();
  213. if ( substr($prefix, strlen($prefix)-1) == '.' ) {
  214. $prefix .= ".";
  215. }
  216. $this->log("Loading Environment $prefix", Project::MSG_VERBOSE);
  217. foreach($_ENV as $key => $value) {
  218. $props->setProperty($prefix . '.' . $key, $value);
  219. }
  220. $this->addProperties($props);
  221. }
  222. /**
  223. * iterate through a set of properties,
  224. * resolve them then assign them
  225. */
  226. protected function addProperties($props) {
  227. $this->resolveAllProperties($props);
  228. foreach($props->keys() as $name) {
  229. $value = $props->getProperty($name);
  230. $v = $this->project->replaceProperties($value);
  231. if ($this->prefix !== null) {
  232. $name = $this->prefix . $name;
  233. }
  234. $this->addProperty($name, $v);
  235. }
  236. }
  237. /**
  238. * add a name value pair to the project property set
  239. * @param string $name name of property
  240. * @param string $value value to set
  241. */
  242. protected function addProperty($name, $value) {
  243. if ($this->userProperty) {
  244. if ($this->project->getUserProperty($name) === null || $this->override) {
  245. $this->project->setInheritedProperty($name, $value);
  246. } else {
  247. $this->log("Override ignored for " . $name, Project::MSG_VERBOSE);
  248. }
  249. } else {
  250. if ($this->override) {
  251. $this->project->setProperty($name, $value);
  252. } else {
  253. $this->project->setNewProperty($name, $value);
  254. }
  255. }
  256. }
  257. /**
  258. * load properties from a file.
  259. * @param PhingFile $file
  260. */
  261. protected function loadFile(PhingFile $file) {
  262. $props = new Properties();
  263. $this->log("Loading ". $file->getAbsolutePath(), Project::MSG_INFO);
  264. try { // try to load file
  265. if ($file->exists()) {
  266. $props->load($file);
  267. $this->addProperties($props);
  268. } else {
  269. $this->log("Unable to find property file: ". $file->getAbsolutePath() ."... skipped", Project::MSG_WARN);
  270. }
  271. } catch (IOException $ioe) {
  272. throw new BuildException("Could not load properties from file.", $ioe);
  273. }
  274. }
  275. /**
  276. * Given a Properties object, this method goes through and resolves
  277. * any references to properties within the object.
  278. *
  279. * @param Properties $props The collection of Properties that need to be resolved.
  280. * @return void
  281. */
  282. protected function resolveAllProperties(Properties $props) {
  283. $keys = $props->keys();
  284. while(count($keys)) {
  285. // There may be a nice regex/callback way to handle this
  286. // replacement, but at the moment it is pretty complex, and
  287. // would probably be a lot uglier to work into a preg_replace_callback()
  288. // system. The biggest problem is the fact that a resolution may require
  289. // multiple passes.
  290. $name = array_shift($keys);
  291. $value = $props->getProperty($name);
  292. $resolved = false;
  293. while(!$resolved) {
  294. $fragments = array();
  295. $propertyRefs = array();
  296. // [HL] this was ::parsePropertyString($this->value ...) ... this seems wrong
  297. self::parsePropertyString($value, $fragments, $propertyRefs);
  298. $resolved = true;
  299. if (count($propertyRefs) !== 0) {
  300. $sb = "";
  301. $i = $fragments;
  302. $j = $propertyRefs;
  303. while(count($i)) {
  304. $fragment = array_shift($i);
  305. if ($fragment === null) {
  306. $propertyName = array_shift($j);
  307. if ($propertyName === $name) {
  308. // Should we maybe just log this as an error & move on?
  309. // $this->log("Property ".$name." was circularly defined.", Project::MSG_ERR);
  310. throw new BuildException("Property ".$name." was circularly defined.");
  311. }
  312. $fragment = $this->getProject()->getProperty($propertyName);
  313. if ($fragment === null) {
  314. if ($props->containsKey($propertyName)) {
  315. $fragment = $props->getProperty($propertyName);
  316. $resolved = false; // parse again (could have been replaced w/ another var)
  317. } else {
  318. $fragment = "\${".$propertyName."}";
  319. }
  320. }
  321. }
  322. $sb .= $fragment;
  323. }
  324. $this->log("Resolved Property \"$value\" to \"$sb\"", Project::MSG_DEBUG);
  325. $value = $sb;
  326. $props->setProperty($name, $value);
  327. } // if (count($propertyRefs))
  328. } // while (!$resolved)
  329. } // while (count($keys)
  330. }
  331. /**
  332. * This method will parse a string containing ${value} style
  333. * property values into two lists. The first list is a collection
  334. * of text fragments, while the other is a set of string property names
  335. * null entries in the first list indicate a property reference from the
  336. * second list.
  337. *
  338. * This is slower than regex, but useful for this class, which has to handle
  339. * multiple parsing passes for properties.
  340. *
  341. * @param string $value The string to be scanned for property references
  342. * @param array &$fragments The found fragments
  343. * @param array &$propertyRefs The found refs
  344. */
  345. protected function parsePropertyString($value, &$fragments, &$propertyRefs) {
  346. $prev = 0;
  347. $pos = 0;
  348. while (($pos = strpos($value, '$', $prev)) !== false) {
  349. if ($pos > $prev) {
  350. array_push($fragments, StringHelper::substring($value, $prev, $pos-1));
  351. }
  352. if ($pos === (strlen($value) - 1)) {
  353. array_push($fragments, '$');
  354. $prev = $pos + 1;
  355. } elseif ($value{$pos+1} !== '{' ) {
  356. // the string positions were changed to value-1 to correct
  357. // a fatal error coming from function substring()
  358. array_push($fragments, StringHelper::substring($value, $pos, $pos + 1));
  359. $prev = $pos + 2;
  360. } else {
  361. $endName = strpos($value, '}', $pos);
  362. if ($endName === false) {
  363. throw new BuildException("Syntax error in property: $value");
  364. }
  365. $propertyName = StringHelper::substring($value, $pos + 2, $endName-1);
  366. array_push($fragments, null);
  367. array_push($propertyRefs, $propertyName);
  368. $prev = $endName + 1;
  369. }
  370. }
  371. if ($prev < strlen($value)) {
  372. array_push($fragments, StringHelper::substring($value, $prev));
  373. }
  374. }
  375. }