. */ require_once 'phing/tasks/system/condition/ConditionBase.php'; require_once 'phing/tasks/system/SequentialTask.php'; /** * Perform some tasks based on whether a given condition holds true or * not. * *

This task is heavily based on the Condition framework that can * be found in Ant 1.4 and later, therefore it cannot be used in * conjunction with versions of Ant prior to 1.4.

* *

This task doesn't have any attributes, the condition to test is * specified by a nested element - see the documentation of your * task (see * the * online documentation for example) for a complete list of nested * elements.

* *

Just like the task, only a single * condition can be specified - you combine them using * or conditions.

* *

In addition to the condition, you can specify three different * child elements, , and * . All three subelements are optional. * * Both and must not be * used more than once inside the if task. Both are * containers for Ant tasks, just like Ant's * and * tasks - in fact they are implemented using the same class as Ant's * task.

* * The behaves exactly like an * except that it cannot contain the element * inside of it. You may specify as may of these as you like, and the * order they are specified is the order they are evaluated in. If the * condition on the is false, then the first * who's conditional evaluates to true * will be executed. The will be executed * only if the and all * conditions are false. * *

Use the following task to define the * task before you use it the first time:

* *

 *   
* *

Crude Example

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * @author Stefan Bodewig * @package phing.tasks.system */ class IfTask extends ConditionBase { private $thenTasks = null; private $elseIfTasks = array(); private $elseTasks = null; /*** * A nested Else if task */ public function addElseIf(ElseIfTask $ei) { $this->elseIfTasks[] = $ei; } /** * A nested element - a container of tasks that will * be run if the condition holds true. * *

Not required.

*/ public function addThen(SequentialTask $t) { if ($this->thenTasks != null) { throw new BuildException("You must not nest more than one into "); } $this->thenTasks = $t; } /** * A nested element - a container of tasks that will * be run if the condition doesn't hold true. * *

Not required.

*/ public function addElse(SequentialTask $e) { if ($this->elseTasks != null) { throw new BuildException("You must not nest more than one into "); } $this->elseTasks = $e; } public function main() { if ($this->countConditions() > 1) { throw new BuildException("You must not nest more than one condition into "); } if ($this->countConditions() < 1) { throw new BuildException("You must nest a condition into "); } $conditions = $this->getConditions(); $c = $conditions[0]; if ($c->evaluate()) { if ($this->thenTasks != null) { $this->thenTasks->main(); } } else { $done = false; $sz = count($this->elseIfTasks); for($i=0; $i < $sz && !$done; $i++) { $ei = $this->elseIfTasks[$i]; if ($ei->evaluate()) { $done = true; $ei->main(); } } if (!$done && $this->elseTasks != null) { $this->elseTasks->main(); } } } } /** * "Inner" class for IfTask. * This class has same basic structure as the IfTask, although of course it doesn't support tags. */ class ElseIfTask extends ConditionBase { private $thenTasks = null; public function addThen(SequentialTask $t) { if ($this->thenTasks != null) { throw new BuildException("You must not nest more than one into "); } $this->thenTasks = $t; } /** * @return boolean */ public function evaluate() { if ($this->countConditions() > 1) { throw new BuildException("You must not nest more than one condition into "); } if ($this->countConditions() < 1) { throw new BuildException("You must nest a condition into "); } $conditions = $this->getConditions(); $c = $conditions[0]; return $c->evaluate(); } /** * */ public function main() { if ($this->thenTasks != null) { $this->thenTasks->main(); } } }