Target.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. <?php
  2. /*
  3. * $Id: Target.php 905 2010-10-05 16:28:03Z mrook $
  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/TaskContainer.php';
  22. /**
  23. * The Target component. Carries all required target data. Implements the
  24. * abstract class {@link TaskContainer}
  25. *
  26. * @author Andreas Aderhold <andi@binarycloud.com>
  27. * @copyright � 2001,2002 THYRELL. All rights reserved
  28. * @version $Revision: 905 $ $Date: 2010-10-05 18:28:03 +0200 (Tue, 05 Oct 2010) $
  29. * @access public
  30. * @see TaskContainer
  31. * @package phing
  32. */
  33. class Target implements TaskContainer {
  34. /** name of target */
  35. private $name;
  36. /** dependencies */
  37. private $dependencies = array();
  38. /** holds objects of children of this target */
  39. private $children = array();
  40. /** the if cond. from xml */
  41. private $ifCondition = "";
  42. /** the unless cond. from xml */
  43. private $unlessCondition = "";
  44. /** description of this target */
  45. private $description;
  46. /** reference to project */
  47. private $project;
  48. /**
  49. * References the project to the current component.
  50. *
  51. * @param Project The reference to the current project
  52. */
  53. public function setProject(Project $project) {
  54. $this->project = $project;
  55. }
  56. /**
  57. * Returns reference to current project
  58. *
  59. * @return Project Reference to current porject object
  60. */
  61. public function getProject() {
  62. return $this->project;
  63. }
  64. /**
  65. * Sets the target dependencies from xml
  66. *
  67. * @param string $depends Comma separated list of targetnames that depend on
  68. * this target
  69. * @throws BuildException
  70. */
  71. public function setDepends($depends) {
  72. // explode should be faster than strtok
  73. $deps = explode(',', $depends);
  74. for ($i=0, $size=count($deps); $i < $size; $i++) {
  75. $trimmed = trim($deps[$i]);
  76. if ($trimmed === "") {
  77. throw new BuildException("Syntax Error: Depend attribute for target ".$this->getName()." is malformed.");
  78. }
  79. $this->addDependency($trimmed);
  80. }
  81. }
  82. /**
  83. * Adds a singular dependent target name to the list
  84. *
  85. * @param string The dependency target to add
  86. * @access public
  87. */
  88. public function addDependency($dependency) {
  89. $this->dependencies[] = (string) $dependency;
  90. }
  91. /**
  92. * Returns reference to indexed array of the dependencies this target has.
  93. *
  94. * @return array Referece to target dependencoes
  95. */
  96. public function getDependencies() {
  97. return $this->dependencies;
  98. }
  99. /**
  100. * Sets the name of the target
  101. *
  102. * @param string Name of this target
  103. */
  104. public function setName($name) {
  105. $this->name = (string) $name;
  106. }
  107. /**
  108. * Returns name of this target.
  109. *
  110. * @return string The name of the target
  111. * @access public
  112. */
  113. function getName() {
  114. return (string) $this->name;
  115. }
  116. /**
  117. * Adds a task element to the list of this targets child elements
  118. *
  119. * @param object The task object to add
  120. * @access public
  121. */
  122. function addTask(Task $task) {
  123. $this->children[] = $task;
  124. }
  125. /**
  126. * Adds a runtime configurable element to the list of this targets child
  127. * elements.
  128. *
  129. * @param object The RuntimeConfigurabel object
  130. * @access public
  131. */
  132. function addDataType($rtc) {
  133. $this->children[] = $rtc;
  134. }
  135. /**
  136. * Returns an array of all tasks this target has as childrens.
  137. *
  138. * The task objects are copied here. Don't use this method to modify
  139. * task objects.
  140. *
  141. * @return array Task[]
  142. */
  143. public function getTasks() {
  144. $tasks = array();
  145. for ($i=0,$size=count($this->children); $i < $size; $i++) {
  146. $tsk = $this->children[$i];
  147. if ($tsk instanceof Task) {
  148. // note: we're copying objects here!
  149. $tasks[] = clone $tsk;
  150. }
  151. }
  152. return $tasks;
  153. }
  154. /**
  155. * Set the if-condition from the XML tag, if any. The property name given
  156. * as parameter must be present so the if condition evaluates to true
  157. *
  158. * @param string The property name that has to be present
  159. * @access public
  160. */
  161. public function setIf($property) {
  162. $this->ifCondition = ($property === null) ? "" : $property;
  163. }
  164. /**
  165. * Set the unless-condition from the XML tag, if any. The property name
  166. * given as parameter must be present so the unless condition evaluates
  167. * to true
  168. *
  169. * @param string The property name that has to be present
  170. * @access public
  171. */
  172. public function setUnless($property) {
  173. $this->unlessCondition = ($property === null) ? "" : $property;
  174. }
  175. /**
  176. * Sets a textual description of this target.
  177. *
  178. * @param string The description text
  179. */
  180. public function setDescription($description) {
  181. if ($description !== null && strcmp($description, "") !== 0) {
  182. $this->description = (string) $description;
  183. } else {
  184. $this->description = null;
  185. }
  186. }
  187. /**
  188. * Returns the description of this target.
  189. *
  190. * @return string The description text of this target
  191. */
  192. public function getDescription() {
  193. return $this->description;
  194. }
  195. /**
  196. * Returns a string representation of this target. In our case it
  197. * simply returns the target name field
  198. *
  199. * @return string The string representation of this target
  200. */
  201. function toString() {
  202. return (string) $this->name;
  203. }
  204. /**
  205. * The entry point for this class. Does some checking, then processes and
  206. * performs the tasks for this target.
  207. *
  208. */
  209. public function main() {
  210. if ($this->testIfCondition() && $this->testUnlessCondition()) {
  211. foreach($this->children as $o) {
  212. if ($o instanceof Task) {
  213. // child is a task
  214. $o->perform();
  215. } else {
  216. // child is a RuntimeConfigurable
  217. $o->maybeConfigure($this->project);
  218. }
  219. }
  220. } elseif (!$this->testIfCondition()) {
  221. $this->project->log("Skipped target '".$this->name."' because property '".$this->ifCondition."' not set.", Project::MSG_VERBOSE);
  222. } else {
  223. $this->project->log("Skipped target '".$this->name."' because property '".$this->unlessCondition."' set.", Project::MSG_VERBOSE);
  224. }
  225. }
  226. /**
  227. * Performs the tasks by calling the main method of this target that
  228. * actually executes the tasks.
  229. *
  230. * This method is for ZE2 and used for proper exception handling of
  231. * task exceptions.
  232. */
  233. public function performTasks() {
  234. try {// try to execute this target
  235. $this->project->fireTargetStarted($this);
  236. $this->main();
  237. $this->project->fireTargetFinished($this, $null=null);
  238. } catch (BuildException $exc) {
  239. // log here and rethrow
  240. $this->project->fireTargetFinished($this, $exc);
  241. throw $exc;
  242. }
  243. }
  244. /**
  245. * Tests if the property set in ifConfiditon exists.
  246. *
  247. * @return boolean <code>true</code> if the property specified
  248. * in <code>$this->ifCondition</code> exists;
  249. * <code>false</code> otherwise
  250. */
  251. private function testIfCondition() {
  252. if ($this->ifCondition === "") {
  253. return true;
  254. }
  255. $properties = explode(",", $this->ifCondition);
  256. $result = true;
  257. foreach ($properties as $property) {
  258. $test = ProjectConfigurator::replaceProperties($this->getProject(), $property, $this->project->getProperties());
  259. $result = $result && ($this->project->getProperty($test) !== null);
  260. }
  261. return $result;
  262. }
  263. /**
  264. * Tests if the property set in unlessCondition exists.
  265. *
  266. * @return boolean <code>true</code> if the property specified
  267. * in <code>$this->unlessCondition</code> exists;
  268. * <code>false</code> otherwise
  269. */
  270. private function testUnlessCondition() {
  271. if ($this->unlessCondition === "") {
  272. return true;
  273. }
  274. $properties = explode(",", $this->unlessCondition);
  275. $result = true;
  276. foreach ($properties as $property) {
  277. $test = ProjectConfigurator::replaceProperties($this->getProject(), $property, $this->project->getProperties());
  278. $result = $result && ($this->project->getProperty($test) === null);
  279. }
  280. return $result;
  281. }
  282. }