summaryrefslogtreecommitdiff
path: root/buildscripts/phing/classes/phing/tasks/system/IfTask.php
blob: cff06ce14f3209ae843eac54c0d97b807ff99e19 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
<?php

/*
 *  $Id: 4452f066ac71d51d3e2521d39cdee2caf4f7e9cc $
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
 * and is licensed under the LGPL. For more information please see
 * <http://phing.info>.
 */
 
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.
 *
 * <p>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.</p>
 *
 * <p>This task doesn't have any attributes, the condition to test is
 * specified by a nested element - see the documentation of your
 * <code><condition&gt;</code> task (see
 * <a href="http://jakarta.apache.org/ant/manual/CoreTasks/condition.html">the
 * online documentation</a> for example) for a complete list of nested
 * elements.</p>
 *
 * <p>Just like the <code><condition&gt;</code> task, only a single
 * condition can be specified - you combine them using
 * <code><and&gt;</code> or <code><or&gt;</code> conditions.</p>
 *
 * <p>In addition to the condition, you can specify three different
 * child elements, <code><elseif&gt;</code>, <code><then&gt;</code> and
 * <code><else&gt;</code>.  All three subelements are optional.
 *
 * Both <code><then&gt;</code> and <code><else&gt;</code> must not be
 * used more than once inside the if task.  Both are
 * containers for Ant tasks, just like Ant's
 * <code><parallel&gt;</code> and <code><sequential&gt;</code>
 * tasks - in fact they are implemented using the same class as Ant's
 * <code><sequential&gt;</code> task.</p>
 *
 *  The <code><elseif&gt;</code> behaves exactly like an <code><if&gt;</code>
 * except that it cannot contain the <code><else&gt;</code> 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 <code><if&gt;</code> is false, then the first
 * <code><elseif&gt;</code> who's conditional evaluates to true
 * will be executed.  The <code><else&gt;</code> will be executed
 * only if the <code><if&gt;</code> and all <code><elseif&gt;</code>
 * conditions are false.
 *
 * <p>Use the following task to define the <code><if&gt;</code>
 * task before you use it the first time:</p>
 *
 * <pre><code>
 *   &lt;taskdef name=&quot;if&quot; classname=&quot;net.sf.antcontrib.logic.IfTask&quot; /&gt;
 * </code></pre>
 *
 * <h3>Crude Example</h3>
 *
 * <code>
 * <if>
 *  <equals arg1="${foo}" arg2="bar" />
 *  <then>
 *    <echo message="The value of property foo is bar" />
 *  </then>
 *  <else>
 *    <echo message="The value of property foo is not bar" />
 *  </else>
 * </if>
 * </code>
 *
 * <code>
 * <if>
 *  <equals arg1="${foo}" arg2="bar" /&gt;
 *  <then>
 *   <echo message="The value of property foo is 'bar'" />
 *  </then>
 *
 *  <elseif>
 *   <equals arg1="${foo}" arg2="foo" />
 *   <then>
 *    <echo message="The value of property foo is 'foo'" />
 *   </then>
 *  </elseif>
 *
 *  <else>
 *   <echo message="The value of property foo is not 'foo' or 'bar'" />
 *  </else>
 * </if>
 * </code>
 *
 * @author <a href="mailto:stefan.bodewig@freenet.de">Stefan Bodewig</a>
 * @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 <then> element - a container of tasks that will
     * be run if the condition holds true.
     *
     * <p>Not required.</p>
     */
    public function addThen(SequentialTask $t) {
        if ($this->thenTasks != null) {
            throw new BuildException("You must not nest more than one <then> into <if>");
        }
        $this->thenTasks = $t;
    }

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

    public function main() {
    
        if ($this->countConditions() > 1) {
            throw new BuildException("You must not nest more than one condition into <if>");
        }
        if ($this->countConditions() < 1) {
            throw new BuildException("You must nest a condition into <if>");
        }
        $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 <else> tags.
 *
 * @package phing.tasks.system
 */
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 <then> into <elseif>");
            }
            $this->thenTasks = $t;
        }
    
        /**
         * @return boolean
         */
        public function evaluate() {
        
            if ($this->countConditions() > 1) {
                throw new BuildException("You must not nest more than one condition into <elseif>");
            }
            if ($this->countConditions() < 1) {
                throw new BuildException("You must nest a condition into <elseif>");
            }
            
            $conditions = $this->getConditions();
            $c = $conditions[0];

            return $c->evaluate();
        }
        
        /**
         * 
         */
        public function main() {
            if ($this->thenTasks != null) {
                $this->thenTasks->main();
            }
        }
    }