summaryrefslogtreecommitdiff
path: root/buildscripts/phing/classes/phing/Target.php
blob: 342d11cb211fab92bbb2d6c61f288ec713aa1b35 (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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
<?php
/*
 * $Id: b6779ff7860ec7a7a84d74a40ffcc9efa75255f7 $
 *
 * 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>.
 */

include_once 'phing/TaskContainer.php';

/**
 *  The Target component. Carries all required target data. Implements the
 *  abstract class {@link TaskContainer}
 *
 *  @author    Andreas Aderhold <andi@binarycloud.com>
 *  @copyright 2001,2002 THYRELL. All rights reserved
 *  @version   $Id: b6779ff7860ec7a7a84d74a40ffcc9efa75255f7 $
 *  @access    public
 *  @see       TaskContainer
 *  @package   phing
 */

class Target implements TaskContainer {
    
    /**
     * Name of target
     * @var string
     */
    private $name;
    
    /**
     * Dependencies
     * @var array
     */
    private $dependencies = array();
    
    /**
     * Holds objects of children of this target
     * @var array
     */
    private $children = array();
    
    /**
     * The if condition from xml
     * @var string
     */
    private $ifCondition = "";
    
    /**
     * The unless condition from xml
     * @var string
     */
    private $unlessCondition = "";
    
    /**
     * Description of this target
     * @var string
     */
    private $description;

    /**
     * Whether to hide target in targets list (-list -p switches)
     * @var boolean
     */
    private $hidden = false;
    
    /**
     * Rreference to project 
     * @var Project
     */
    private $project;

    /**
     * References the project to the current component.
     *
     * @param Project $project The reference to the current project
     */
    public function setProject(Project $project) {
        $this->project = $project;
    }

    /**
     * Returns reference to current project
     *
     * @return Project Reference to current porject object
     */
    public function getProject() {
        return $this->project;
    }

    /**
     * Sets the target dependencies from xml
     *
     * @param string $depends Comma separated list of targetnames that depend on
     *                  this target
     * @throws BuildException
     */
    public function setDepends($depends) {
        // explode should be faster than strtok
        $deps = explode(',', $depends);
        for ($i=0, $size=count($deps); $i < $size; $i++) {
            $trimmed = trim($deps[$i]);
            if ($trimmed === "") {
                throw new BuildException("Syntax Error: Depend attribute for target ".$this->getName()." is malformed.");
            } 
            $this->addDependency($trimmed);
        }
    }

    /**
     * Adds a singular dependent target name to the list
     *
     * @param   string  $dependency  The dependency target to add
     * @access  public
     */
    public function addDependency($dependency) {
        $this->dependencies[] = (string) $dependency;
    }

    /**
     * Returns reference to indexed array of the dependencies this target has.
     *
     * @return  array  Referece to target dependencoes
     */
    public function getDependencies() {
        return $this->dependencies;
    }

    /**
     * Sets the name of the target
     *
     * @param  string  $name  Name of this target
     */
    public function setName($name) {
        $this->name = (string) $name;
    }

    /**
     * Returns name of this target.
     *
     * @return  string     The name of the target
     * @access   public
     */
    public function getName() {
        return (string) $this->name;
    }

    /**
     * Set target status. If true, target does not come in phing -list
     *
     * @param  boolean $flag
     * @return Target
     */
    public function setHidden($flag)
    {
        $this->hidden = (boolean) $flag;
        return $this;
    }

    /**
     * Get target status. If true, target does not come in phing -list
     *
     * @return boolean
     */
    public function getHidden()
    {
        return $this->hidden;
    }

    /**
     * Alias for getHidden()
     *
     * @return boolean
     */
    public function isHidden()
    {
        return $this->getHidden();
    }

    /**
     * Adds a task element to the list of this targets child elements
     *
     * @param   Task  $task  The task object to add
     * @access  public
     */
    public function addTask(Task $task) {
        $this->children[] = $task;
    }

    /**
     * Adds a runtime configurable element to the list of this targets child
     * elements.
     *
     * @param   RuntimeConfigurable  $rtc  The RuntimeConfigurable object
     * @access  public
     */
    public function addDataType($rtc) {
        $this->children[] = $rtc;
    }

    /**
     * Returns an array of all tasks this target has as childrens.
     *
     * The task objects are copied here. Don't use this method to modify
     * task objects.
     *
     * @return  array  Task[]
     */
    public function getTasks() {
        $tasks = array();
        for ($i=0,$size=count($this->children); $i < $size; $i++) {
            $tsk = $this->children[$i];
            if ($tsk instanceof Task) {
                // note: we're copying objects here!
                $tasks[] = clone $tsk;
            }
        }
        return $tasks;
    }

    /**
     * Set the if-condition from the XML tag, if any. The property name given
     * as parameter must be present so the if condition evaluates to true
     *
     * @param   string  $property  The property name that has to be present
     * @access  public
     */
    public function setIf($property) {
        $this->ifCondition = ($property === null) ? "" : $property;
    }

    /**
     * Set the unless-condition from the XML tag, if any. The property name
     * given as parameter must be present so the unless condition evaluates
     * to true
     *
     * @param   string  $property  The property name that has to be present
     * @access  public
     */
    public function setUnless($property) {
        $this->unlessCondition = ($property === null) ? "" : $property;
    }

    /**
     * Sets a textual description of this target.
     *
     * @param string $description  The description text
     */
    public function setDescription($description) {
        if ($description !== null && strcmp($description, "") !== 0) {
            $this->description = (string) $description;
        } else {
            $this->description = null;
        }
    }

    /**
     * Returns the description of this target.
     *
     * @return string The description text of this target
     */
    public function getDescription() {
        return $this->description;
    }

    /**
     * Returns a string representation of this target. In our case it
     * simply returns the target name field
     *
     * @return string The string representation of this target
     */
    public function toString() {
        return (string) $this->name;
    }

    /**
     * The entry point for this class. Does some checking, then processes and
     * performs the tasks for this target.
     */
    public function main() {
        if ($this->testIfCondition() && $this->testUnlessCondition()) {
            foreach($this->children as $o) {
                if ($o instanceof Task) {
                    // child is a task
                    $o->perform();
                } else {
                    // child is a RuntimeConfigurable
                    $o->maybeConfigure($this->project);
                }
            }
        } elseif (!$this->testIfCondition()) {
            $this->project->log("Skipped target '".$this->name."' because property '".$this->ifCondition."' not set.", Project::MSG_VERBOSE);
        } else {
            $this->project->log("Skipped target '".$this->name."' because property '".$this->unlessCondition."' set.", Project::MSG_VERBOSE);
        }
    }

    /**
     * Performs the tasks by calling the main method of this target that
     * actually executes the tasks.
     *
     * This method is for ZE2 and used for proper exception handling of
     * task exceptions.
     */
    public function performTasks() {
        try {// try to execute this target
            $this->project->fireTargetStarted($this);
            $this->main();
            $this->project->fireTargetFinished($this, $null=null);
        } catch (BuildException $exc) {
            // log here and rethrow
            $this->project->fireTargetFinished($this, $exc);
            throw $exc;
        }
    }    

    /**
     * Tests if the property set in ifConfiditon exists.
     *
     * @return  boolean  <code>true</code> if the property specified
     *                   in <code>$this->ifCondition</code> exists;
     *                   <code>false</code> otherwise
     */
    private function testIfCondition() {
        if ($this->ifCondition === "") {
            return true;
        }

        $properties = explode(",", $this->ifCondition);

        $result = true;
        foreach ($properties as $property) {
            $test = ProjectConfigurator::replaceProperties($this->getProject(), $property, $this->project->getProperties());
            $result = $result && ($this->project->getProperty($test) !== null);
        }

        return $result;
    }

    /**
     * Tests if the property set in unlessCondition exists.
     *
     * @return  boolean  <code>true</code> if the property specified
     *                   in <code>$this->unlessCondition</code> exists;
     *                    <code>false</code> otherwise
     */
    private function testUnlessCondition() {
        if ($this->unlessCondition === "") {
            return true;
        }
        
        $properties = explode(",", $this->unlessCondition);

        $result = true;
        foreach ($properties as $property) {
            $test = ProjectConfigurator::replaceProperties($this->getProject(), $property, $this->project->getProperties());
            $result = $result && ($this->project->getProperty($test) === null);
        }
        return $result;
    }

}