root/trunk/classes/phing/Target.php

Revision 307, 9.9 kB (checked in by hans, 1 year ago)

Refs #188 - Adding work-in-progress (still-broken!) namespace support.

  • Property svn:keywords set to author date id revision
Line 
1 <?php
2 /*
3  * $Id$
4  *
5  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
6  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
7  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
8  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
9  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
10  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
11  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
12  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
13  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
14  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
15  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
16  *
17  * This software consists of voluntary contributions made by many individuals
18  * and is licensed under the LGPL. For more information please see
19  * <http://phing.info>.
20  */
21
22 namespace phing;
23
24
25 /**
26  *  The Target component. Carries all required target data. Implements the
27  *  abstract class {@link TaskContainer}
28  *
29  *  @author    Andreas Aderhold <andi@binarycloud.com>
30  *  @copyright ᅵ 2001,2002 THYRELL. All rights reserved
31  *  @version   $Revision: 1.10 $ $Date$
32  *  @access    public
33  *  @see       TaskContainer
34  *  @package   phing
35  */
36
37 class Target implements TaskContainer {
38     
39     /** name of target */
40     private $name;
41     
42     /** dependencies */
43     private $dependencies = array();
44     
45     /** holds objects of children of this target */
46     private $children = array();
47     
48     /** the if cond. from xml */
49     private $ifCondition = "";
50     
51     /** the unless cond. from xml */
52     private $unlessCondition = "";
53     
54     /** description of this target */
55     private $description;
56     
57     /** reference to project */
58     private $project;
59
60     /**
61      *  References the project to the current component.
62      *
63      *  @param Project The reference to the current project
64      */
65     public function setProject(Project $project) {
66         $this->project = $project;
67     }
68
69     /**
70      *  Returns reference to current project
71      *
72      *  @return Project Reference to current porject object
73      */
74     public function getProject() {
75         return $this->project;
76     }
77
78     /**
79      *  Sets the target dependencies from xml
80      *
81      *  @param string $depends Comma separated list of targetnames that depend on
82      *                  this target
83      *  @throws BuildException
84      */
85     public function setDepends($depends) {
86         // explode should be faster than strtok
87         $deps = explode(',', $depends);
88         for ($i=0, $size=count($deps); $i < $size; $i++) {
89             $trimmed = trim($deps[$i]);
90             if ($trimmed === "") {
91                 throw new BuildException("Syntax Error: Depend attribute for target ".$this->getName()." is malformed.");
92             }
93             $this->addDependency($trimmed);
94         }
95     }
96
97     /**
98      *  Adds a singular dependent target name to the list
99      *
100      *  @param   string   The dependency target to add
101      *  @access  public
102      */
103     public function addDependency($dependency) {
104         $this->dependencies[] = (string) $dependency;
105     }
106
107     /**
108      *  Returns reference to indexed array of the dependencies this target has.
109      *
110      *  @return  array  Referece to target dependencoes
111      */
112     public function getDependencies() {
113         return $this->dependencies;
114     }
115
116     /**
117      *  Sets the name of the target
118      *
119      *  @param  string   Name of this target
120      */
121     public function setName($name) {
122         $this->name = (string) $name;
123     }
124
125     /**
126      *  Returns name of this target.
127      *
128      *  @return  string     The name of the target
129      *  @access   public
130      */
131     function getName() {
132         return (string) $this->name;
133     }
134
135     /**
136      *  Adds a task element to the list of this targets child elements
137      *
138      *  @param   object  The task object to add
139      *  @access  public
140      */
141     function addTask(Task $task) {
142         $this->children[] = $task;
143     }
144
145     /**
146      *  Adds a runtime configurable element to the list of this targets child
147      *  elements.
148      *
149      *  @param   object  The RuntimeConfigurabel object
150      *  @access  public
151      */
152     function addDataType($rtc) {
153         $this->children[] = $rtc;
154     }
155
156     /**
157      *  Returns an array of all tasks this target has as childrens.
158      *
159      *  The task objects are copied here. Don't use this method to modify
160      *  task objects.
161      *
162      *  @return  array  Task[]
163      */
164     public function getTasks() {
165         $tasks = array();
166         for ($i=0,$size=count($this->children); $i < $size; $i++) {
167             $tsk = $this->children[$i];
168             if ($tsk instanceof Task) {
169                 // note: we're copying objects here!
170                 $tasks[] = clone $tsk;
171             }
172         }
173         return $tasks;
174     }
175
176     /**
177      *  Set the if-condition from the XML tag, if any. The property name given
178      *  as parameter must be present so the if condition evaluates to true
179      *
180      *  @param   string  The property name that has to be present
181      *  @access  public
182      */
183     public function setIf($property) {
184         $this->ifCondition = ($property === null) ? "" : $property;
185     }
186
187     /**
188      *  Set the unless-condition from the XML tag, if any. The property name
189      *  given as parameter must be present so the unless condition evaluates
190      *  to true
191      *
192      *  @param   string  The property name that has to be present
193      *  @access  public
194      */
195     public function setUnless($property) {
196         $this->unlessCondition = ($property === null) ? "" : $property;
197     }
198
199     /**
200      *  Sets a textual description of this target.
201      *
202      *  @param string The description text
203      */
204     public function setDescription($description) {
205         if ($description !== null && strcmp($description, "") !== 0) {
206             $this->description = (string) $description;
207         } else {
208             $this->description = null;
209         }
210     }
211
212     /**
213      *  Returns the description of this target.
214      *
215      *  @return string The description text of this target
216      */
217     public function getDescription() {
218         return $this->description;
219     }
220
221     /**
222      *  Returns a string representation of this target. In our case it
223      *  simply returns the target name field
224      *
225      *  @return string The string representation of this target
226      */
227     function toString() {
228         return (string) $this->name;
229     }
230
231     /**
232      *  The entry point for this class. Does some checking, then processes and
233      *  performs the tasks for this target.
234      *
235      */
236     public function main() {
237         if ($this->testIfCondition() && $this->testUnlessCondition()) {
238             foreach($this->children as $o) {
239                 if ($o instanceof Task) {
240                     // child is a task
241                     $o->perform();
242                 } else {
243                     // child is a RuntimeConfigurable
244                     $o->maybeConfigure($this->project);
245                 }
246             }
247         } elseif (!$this->testIfCondition()) {
248             $this->project->log("Skipped target '".$this->name."' because property '".$this->ifCondition."' not set.", Project::MSG_VERBOSE);
249         } else {
250             $this->project->log("Skipped target '".$this->name."' because property '".$this->unlessCondition."' set.", Project::MSG_VERBOSE);
251         }
252     }
253
254     /**
255      *  Performs the tasks by calling the main method of this target that
256      *  actually executes the tasks.
257      *
258      *  This method is for ZE2 and used for proper exception handling of
259      *  task exceptions.
260      */
261     public function performTasks() {
262         try {// try to execute this target
263             $this->project->fireTargetStarted($this);
264             $this->main();
265             $this->project->fireTargetFinished($this, $null=null);
266         } catch (BuildException $exc) {
267             // log here and rethrow
268             $this->project->fireTargetFinished($this, $exc);
269             throw $exc;
270         }
271     }   
272
273     /**
274      *  Tests if the property set in ifConfiditon exists.
275      *
276      *  @return  boolean  <code>true</code> if the property specified
277      *                    in <code>$this->ifCondition</code> exists;
278      *                    <code>false</code> otherwise
279      */
280     private function testIfCondition() {
281         if ($this->ifCondition === "") {
282             return true;
283         }
284
285         $properties = explode(",", $this->ifCondition);
286
287         $result = true;
288         foreach ($properties as $property) {
289             $test = ProjectConfigurator::replaceProperties($this->getProject(), $property, $this->project->getProperties());
290             $result = $result && ($this->project->getProperty($test) !== null);
291         }
292
293         return $result;
294     }
295
296     /**
297      *  Tests if the property set in unlessCondition exists.
298      *
299      *  @return  boolean  <code>true</code> if the property specified
300      *                    in <code>$this->unlessCondition</code> exists;
301      *                    <code>false</code> otherwise
302      */
303     private function testUnlessCondition() {
304         if ($this->unlessCondition === "") {
305             return true;
306         }
307         
308         $properties = explode(",", $this->unlessCondition);
309
310         $result = true;
311         foreach ($properties as $property) {
312             $test = ProjectConfigurator::replaceProperties($this->getProject(), $property, $this->project->getProperties());
313             $result = $result && ($this->project->getProperty($test) === null);
314         }
315         return $result;
316     }
317
318 }
319
Note: See TracBrowser for help on using the browser.