IfTask.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. /*
  3. * $Id: IfTask.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. require_once 'phing/tasks/system/condition/ConditionBase.php';
  22. require_once 'phing/tasks/system/SequentialTask.php';
  23. /**
  24. * Perform some tasks based on whether a given condition holds true or
  25. * not.
  26. *
  27. * <p>This task is heavily based on the Condition framework that can
  28. * be found in Ant 1.4 and later, therefore it cannot be used in
  29. * conjunction with versions of Ant prior to 1.4.</p>
  30. *
  31. * <p>This task doesn't have any attributes, the condition to test is
  32. * specified by a nested element - see the documentation of your
  33. * <code><condition&gt;</code> task (see
  34. * <a href="http://jakarta.apache.org/ant/manual/CoreTasks/condition.html">the
  35. * online documentation</a> for example) for a complete list of nested
  36. * elements.</p>
  37. *
  38. * <p>Just like the <code><condition&gt;</code> task, only a single
  39. * condition can be specified - you combine them using
  40. * <code><and&gt;</code> or <code><or&gt;</code> conditions.</p>
  41. *
  42. * <p>In addition to the condition, you can specify three different
  43. * child elements, <code><elseif&gt;</code>, <code><then&gt;</code> and
  44. * <code><else&gt;</code>. All three subelements are optional.
  45. *
  46. * Both <code><then&gt;</code> and <code><else&gt;</code> must not be
  47. * used more than once inside the if task. Both are
  48. * containers for Ant tasks, just like Ant's
  49. * <code><parallel&gt;</code> and <code><sequential&gt;</code>
  50. * tasks - in fact they are implemented using the same class as Ant's
  51. * <code><sequential&gt;</code> task.</p>
  52. *
  53. * The <code><elseif&gt;</code> behaves exactly like an <code><if&gt;</code>
  54. * except that it cannot contain the <code><else&gt;</code> element
  55. * inside of it. You may specify as may of these as you like, and the
  56. * order they are specified is the order they are evaluated in. If the
  57. * condition on the <code><if&gt;</code> is false, then the first
  58. * <code><elseif&gt;</code> who's conditional evaluates to true
  59. * will be executed. The <code><else&gt;</code> will be executed
  60. * only if the <code><if&gt;</code> and all <code><elseif&gt;</code>
  61. * conditions are false.
  62. *
  63. * <p>Use the following task to define the <code><if&gt;</code>
  64. * task before you use it the first time:</p>
  65. *
  66. * <pre><code>
  67. * <taskdef name="if" classname="net.sf.antcontrib.logic.IfTask" /&gt;
  68. * </code></pre>
  69. *
  70. * <h3>Crude Example</h3>
  71. *
  72. * <code>
  73. * <if>
  74. * <equals arg1="${foo}" arg2="bar" />
  75. * <then>
  76. * <echo message="The value of property foo is bar" />
  77. * </then>
  78. * <else>
  79. * <echo message="The value of property foo is not bar" />
  80. * </else>
  81. * </if>
  82. * </code>
  83. *
  84. * <code>
  85. * <if>
  86. * <equals arg1="${foo}" arg2="bar" /&gt;
  87. * <then>
  88. * <echo message="The value of property foo is 'bar'" />
  89. * </then>
  90. *
  91. * <elseif>
  92. * <equals arg1="${foo}" arg2="foo" />
  93. * <then>
  94. * <echo message="The value of property foo is 'foo'" />
  95. * </then>
  96. * </elseif>
  97. *
  98. * <else>
  99. * <echo message="The value of property foo is not 'foo' or 'bar'" />
  100. * </else>
  101. * </if>
  102. * </code>
  103. *
  104. * @author <a href="mailto:stefan.bodewig@freenet.de">Stefan Bodewig</a>
  105. * @package phing.tasks.system
  106. */
  107. class IfTask extends ConditionBase {
  108. private $thenTasks = null;
  109. private $elseIfTasks = array();
  110. private $elseTasks = null;
  111. /***
  112. * A nested Else if task
  113. */
  114. public function addElseIf(ElseIfTask $ei)
  115. {
  116. $this->elseIfTasks[] = $ei;
  117. }
  118. /**
  119. * A nested <then> element - a container of tasks that will
  120. * be run if the condition holds true.
  121. *
  122. * <p>Not required.</p>
  123. */
  124. public function addThen(SequentialTask $t) {
  125. if ($this->thenTasks != null) {
  126. throw new BuildException("You must not nest more than one <then> into <if>");
  127. }
  128. $this->thenTasks = $t;
  129. }
  130. /**
  131. * A nested <else> element - a container of tasks that will
  132. * be run if the condition doesn't hold true.
  133. *
  134. * <p>Not required.</p>
  135. */
  136. public function addElse(SequentialTask $e) {
  137. if ($this->elseTasks != null) {
  138. throw new BuildException("You must not nest more than one <else> into <if>");
  139. }
  140. $this->elseTasks = $e;
  141. }
  142. public function main() {
  143. if ($this->countConditions() > 1) {
  144. throw new BuildException("You must not nest more than one condition into <if>");
  145. }
  146. if ($this->countConditions() < 1) {
  147. throw new BuildException("You must nest a condition into <if>");
  148. }
  149. $conditions = $this->getConditions();
  150. $c = $conditions[0];
  151. if ($c->evaluate()) {
  152. if ($this->thenTasks != null) {
  153. $this->thenTasks->main();
  154. }
  155. } else {
  156. $done = false;
  157. $sz = count($this->elseIfTasks);
  158. for($i=0; $i < $sz && !$done; $i++) {
  159. $ei = $this->elseIfTasks[$i];
  160. if ($ei->evaluate()) {
  161. $done = true;
  162. $ei->main();
  163. }
  164. }
  165. if (!$done && $this->elseTasks != null) {
  166. $this->elseTasks->main();
  167. }
  168. }
  169. }
  170. }
  171. /**
  172. * "Inner" class for IfTask.
  173. * This class has same basic structure as the IfTask, although of course it doesn't support <else> tags.
  174. */
  175. class ElseIfTask extends ConditionBase {
  176. private $thenTasks = null;
  177. public function addThen(SequentialTask $t) {
  178. if ($this->thenTasks != null) {
  179. throw new BuildException("You must not nest more than one <then> into <elseif>");
  180. }
  181. $this->thenTasks = $t;
  182. }
  183. /**
  184. * @return boolean
  185. */
  186. public function evaluate() {
  187. if ($this->countConditions() > 1) {
  188. throw new BuildException("You must not nest more than one condition into <elseif>");
  189. }
  190. if ($this->countConditions() < 1) {
  191. throw new BuildException("You must nest a condition into <elseif>");
  192. }
  193. $conditions = $this->getConditions();
  194. $c = $conditions[0];
  195. return $c->evaluate();
  196. }
  197. /**
  198. *
  199. */
  200. public function main() {
  201. if ($this->thenTasks != null) {
  202. $this->thenTasks->main();
  203. }
  204. }
  205. }