1
|
|
<?php
|
2
|
|
/**
|
3
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
4
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
5
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
6
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
7
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
8
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
9
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
10
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
11
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
12
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
13
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
14
|
|
*
|
15
|
|
* This software consists of voluntary contributions made by many individuals
|
16
|
|
* and is licensed under the LGPL. For more information please see
|
17
|
|
* <http://phing.info>.
|
18
|
|
*/
|
19
|
|
|
20
|
|
/**
|
21
|
|
* Perform some tasks based on whether a given condition holds true or
|
22
|
|
* not.
|
23
|
|
*
|
24
|
|
* <p>This task is heavily based on the Condition framework that can
|
25
|
|
* be found in Ant 1.4 and later, therefore it cannot be used in
|
26
|
|
* conjunction with versions of Ant prior to 1.4.</p>
|
27
|
|
*
|
28
|
|
* <p>This task doesn't have any attributes, the condition to test is
|
29
|
|
* specified by a nested element - see the documentation of your
|
30
|
|
* <code><condition></code> task (see
|
31
|
|
* <a href="http://jakarta.apache.org/ant/manual/CoreTasks/condition.html">the
|
32
|
|
* online documentation</a> for example) for a complete list of nested
|
33
|
|
* elements.</p>
|
34
|
|
*
|
35
|
|
* <p>Just like the <code><condition></code> task, only a single
|
36
|
|
* condition can be specified - you combine them using
|
37
|
|
* <code><and></code> or <code><or></code> conditions.</p>
|
38
|
|
*
|
39
|
|
* <p>In addition to the condition, you can specify three different
|
40
|
|
* child elements, <code><elseif></code>, <code><then></code> and
|
41
|
|
* <code><else></code>. All three subelements are optional.
|
42
|
|
*
|
43
|
|
* Both <code><then></code> and <code><else></code> must not be
|
44
|
|
* used more than once inside the if task. Both are
|
45
|
|
* containers for Ant tasks, just like Ant's
|
46
|
|
* <code><parallel></code> and <code><sequential></code>
|
47
|
|
* tasks - in fact they are implemented using the same class as Ant's
|
48
|
|
* <code><sequential></code> task.</p>
|
49
|
|
*
|
50
|
|
* The <code><elseif></code> behaves exactly like an <code><if></code>
|
51
|
|
* except that it cannot contain the <code><else></code> element
|
52
|
|
* inside of it. You may specify as may of these as you like, and the
|
53
|
|
* order they are specified is the order they are evaluated in. If the
|
54
|
|
* condition on the <code><if></code> is false, then the first
|
55
|
|
* <code><elseif></code> who's conditional evaluates to true
|
56
|
|
* will be executed. The <code><else></code> will be executed
|
57
|
|
* only if the <code><if></code> and all <code><elseif></code>
|
58
|
|
* conditions are false.
|
59
|
|
*
|
60
|
|
* <p>Use the following task to define the <code><if></code>
|
61
|
|
* task before you use it the first time:</p>
|
62
|
|
*
|
63
|
|
* <pre><code>
|
64
|
|
* <taskdef name="if" classname="net.sf.antcontrib.logic.IfTask" />
|
65
|
|
* </code></pre>
|
66
|
|
*
|
67
|
|
* <h3>Crude Example</h3>
|
68
|
|
*
|
69
|
|
* <code>
|
70
|
|
* <if>
|
71
|
|
* <equals arg1="${foo}" arg2="bar" />
|
72
|
|
* <then>
|
73
|
|
* <echo message="The value of property foo is bar" />
|
74
|
|
* </then>
|
75
|
|
* <else>
|
76
|
|
* <echo message="The value of property foo is not bar" />
|
77
|
|
* </else>
|
78
|
|
* </if>
|
79
|
|
* </code>
|
80
|
|
*
|
81
|
|
* <code>
|
82
|
|
* <if>
|
83
|
|
* <equals arg1="${foo}" arg2="bar" />
|
84
|
|
* <then>
|
85
|
|
* <echo message="The value of property foo is 'bar'" />
|
86
|
|
* </then>
|
87
|
|
*
|
88
|
|
* <elseif>
|
89
|
|
* <equals arg1="${foo}" arg2="foo" />
|
90
|
|
* <then>
|
91
|
|
* <echo message="The value of property foo is 'foo'" />
|
92
|
|
* </then>
|
93
|
|
* </elseif>
|
94
|
|
*
|
95
|
|
* <else>
|
96
|
|
* <echo message="The value of property foo is not 'foo' or 'bar'" />
|
97
|
|
* </else>
|
98
|
|
* </if>
|
99
|
|
* </code>
|
100
|
|
*
|
101
|
|
* @author <a href="mailto:stefan.bodewig@freenet.de">Stefan Bodewig</a>
|
102
|
|
* @package phing.tasks.system
|
103
|
|
*/
|
104
|
|
class IfTask extends ConditionBase
|
105
|
|
{
|
106
|
|
private $thenTasks = null;
|
107
|
|
private $elseIfTasks = [];
|
108
|
|
private $elseTasks = null;
|
109
|
|
|
110
|
|
/***
|
111
|
|
* A nested Else if task
|
112
|
|
* @param ElseIfTask $ei
|
113
|
|
*/
|
114
|
1
|
public function addElseIf(ElseIfTask $ei)
|
115
|
|
{
|
116
|
1
|
$this->elseIfTasks[] = $ei;
|
117
|
|
}
|
118
|
|
|
119
|
|
/**
|
120
|
|
* A nested <then> element - a container of tasks that will
|
121
|
|
* be run if the condition holds true.
|
122
|
|
*
|
123
|
|
* <p>Not required.</p>
|
124
|
|
*
|
125
|
|
* @param SequentialTask $t
|
126
|
|
* @throws BuildException
|
127
|
|
*/
|
128
|
1
|
public function addThen(SequentialTask $t)
|
129
|
|
{
|
130
|
1
|
if ($this->thenTasks != null) {
|
131
|
1
|
throw new BuildException("You must not nest more than one <then> into <if>");
|
132
|
|
}
|
133
|
1
|
$this->thenTasks = $t;
|
134
|
|
}
|
135
|
|
|
136
|
|
/**
|
137
|
|
* A nested <else> element - a container of tasks that will
|
138
|
|
* be run if the condition doesn't hold true.
|
139
|
|
*
|
140
|
|
* <p>Not required.</p>
|
141
|
|
*
|
142
|
|
* @param SequentialTask $e
|
143
|
|
* @throws BuildException
|
144
|
|
*/
|
145
|
1
|
public function addElse(SequentialTask $e)
|
146
|
|
{
|
147
|
1
|
if ($this->elseTasks != null) {
|
148
|
1
|
throw new BuildException("You must not nest more than one <else> into <if>");
|
149
|
|
}
|
150
|
1
|
$this->elseTasks = $e;
|
151
|
|
}
|
152
|
|
|
153
|
1
|
public function main()
|
154
|
|
{
|
155
|
1
|
if ($this->countConditions() > 1) {
|
156
|
1
|
throw new BuildException("You must not nest more than one condition into <if>");
|
157
|
|
}
|
158
|
1
|
if ($this->countConditions() < 1) {
|
159
|
0
|
throw new BuildException("You must nest a condition into <if>");
|
160
|
|
}
|
161
|
1
|
$conditions = $this->getConditions();
|
162
|
1
|
$c = $conditions[0];
|
163
|
|
|
164
|
1
|
if ($c->evaluate()) {
|
165
|
1
|
if ($this->thenTasks != null) {
|
166
|
1
|
$this->thenTasks->main();
|
167
|
|
}
|
168
|
|
} else {
|
169
|
1
|
$done = false;
|
170
|
1
|
$sz = count($this->elseIfTasks);
|
171
|
1
|
for ($i = 0; $i < $sz && !$done; $i++) {
|
172
|
1
|
$ei = $this->elseIfTasks[$i];
|
173
|
1
|
if ($ei->evaluate()) {
|
174
|
1
|
$done = true;
|
175
|
1
|
$ei->main();
|
176
|
|
}
|
177
|
|
}
|
178
|
|
|
179
|
1
|
if (!$done && $this->elseTasks != null) {
|
180
|
1
|
$this->elseTasks->main();
|
181
|
|
}
|
182
|
|
}
|
183
|
|
}
|
184
|
|
}
|