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
 * Exits the active build, giving an additional message
22
 * if available.
23
 *
24
 * @author Hans Lellelid <hans@xmpl.org> (Phing)
25
 * @author Nico Seessle <nico@seessle.de> (Ant)
26
 *
27
 * @package phing.tasks.system
28
 */
29
class FailTask extends Task
30
{
31
    /**
32
     * @var string $message
33
     */
34
    protected $message;
35

36
    /**
37
     * @var string
38
     */
39
    protected $ifCondition;
40

41
    /**
42
     * @var string
43
     */
44
    protected $unlessCondition;
45

46
    /**
47
     * @var NestedCondition
48
     */
49
    protected $nestedCondition;
50

51
    /**
52
     * @var integer
53
     */
54
    protected $status;
55

56
    /**
57
     * A message giving further information on why the build exited.
58
     *
59
     * @param string $value message to output
60
     *
61
     * @return void
62
     */
63 1
    public function setMsg($value)
64
    {
65 1
        $this->setMessage($value);
66
    }
67

68
    /**
69
     * A message giving further information on why the build exited.
70
     *
71
     * @param string $value message to output
72
     *
73
     * @return void
74
     */
75 1
    public function setMessage($value)
76
    {
77 1
        $this->message = $value;
78
    }
79

80
    /**
81
     * Only fail if a property of the given name exists in the current project.
82
     *
83
     * @param string $c property name
84
     *
85
     * @return void
86
     */
87 0
    public function setIf($c)
88
    {
89 0
        $this->ifCondition = $c;
90
    }
91

92
    /**
93
     * Only fail if a property of the given name does not
94
     * exist in the current project.
95
     *
96
     * @param string $c property name
97
     *
98
     * @return void
99
     */
100 0
    public function setUnless($c)
101
    {
102 0
        $this->unlessCondition = $c;
103
    }
104

105
    /**
106
     * Set the status code to associate with the thrown Exception.
107
     *
108
     * @param int $int the <code>int</code> status
109
     */
110 0
    public function setStatus($int)
111
    {
112 0
        $this->status = (int) $int;
113
    }
114

115
    /**
116
     * {@inheritdoc}
117
     *
118
     * @return void
119
     *
120
     * @throws BuildException
121
     */
122 1
    public function main()
123
    {
124 1
        $fail = $this->nestedConditionPresent() ? $this->testNestedCondition() :
125 1
            $this->testIfCondition() && $this->testUnlessCondition();
126

127 1
        if ($fail) {
128 1
            $text = null;
129 1
            if ($this->message !== null && strlen(trim($this->message)) > 0) {
130 1
                $text = trim($this->message);
131
            } else {
132 1
                if ($this->ifCondition !== null && $this->ifCondition !== "" && $this->testIfCondition()) {
133 0
                    $text = "if=" . $this->ifCondition;
134
                }
135 1
                if ($this->unlessCondition !== null && $this->unlessCondition !== "" && $this->testUnlessCondition()) {
136 0
                    if ($text === null) {
137 0
                        $text = "";
138
                    } else {
139 0
                        $text .= " and ";
140
                    }
141 0
                    $text .= "unless=" . $this->unlessCondition;
142
                }
143 1
                if ($this->nestedConditionPresent()) {
144 1
                    $text = "condition satisfied";
145
                } else {
146 0
                    if ($text === null) {
147 0
                        $text = "No message";
148
                    }
149
                }
150
            }
151

152 1
            $this->log("failing due to " . $text, Project::MSG_DEBUG);
153 1
            if ($this->status === null) {
154 1
                throw new BuildException($text);
155
            }
156

157 0
            throw new ExitStatusException($text, $this->status);
158
        }
159
    }
160

161
    /**
162
     * Add a condition element.
163
     *
164
     * @return NestedCondition
165
     * @throws BuildException
166
     */
167 1
    public function createCondition()
168
    {
169 1
        if ($this->nestedCondition !== null) {
170 0
            throw new BuildException("Only one nested condition is allowed.");
171
        }
172 1
        $this->nestedCondition = new NestedCondition();
173 1
        return $this->nestedCondition;
174
    }
175

176
    /**
177
     * Set a multiline message.
178
     *
179
     * @param string $msg
180
     *
181
     * @return void
182
     */
183 1
    public function addText($msg)
184
    {
185 1
        if ($this->message === null) {
186 1
            $this->message = "";
187
        }
188 1
        $this->message .= $this->project->replaceProperties($msg);
189
    }
190

191
    /**
192
     * @return boolean
193
     */
194 1
    protected function testIfCondition()
195
    {
196 1
        if ($this->ifCondition === null || $this->ifCondition === "") {
197 1
            return true;
198
        }
199

200 0
        return $this->project->getProperty($this->ifCondition) !== null;
201
    }
202

203
    /**
204
     * @return boolean
205
     */
206 1
    protected function testUnlessCondition()
207
    {
208 1
        if ($this->unlessCondition === null || $this->unlessCondition === "") {
209 1
            return true;
210
        }
211

212 0
        return $this->project->getProperty($this->unlessCondition) === null;
213
    }
214

215
    /**
216
     * test the nested condition
217
     *
218
     * @return bool true if there is none, or it evaluates to true
219
     * @throws BuildException
220
     */
221 1
    private function testNestedCondition()
222
    {
223 1
        $result = $this->nestedConditionPresent();
224

225 1
        if ($result && ($this->ifCondition !== null || $this->unlessCondition !== null)) {
226 0
            throw new BuildException("Nested conditions not permitted in conjunction with if/unless attributes");
227
        }
228

229 1
        return $result && $this->nestedCondition->evaluate();
230
    }
231

232
    /**
233
     * test whether there is a nested condition.
234
     *
235
     * @return boolean
236
     */
237 1
    private function nestedConditionPresent()
238
    {
239 1
        return $this->nestedCondition !== null;
240
    }
241
}

Read our documentation on viewing source code .

Loading