root/tags/2.2.0RC1/classes/phing/Target.php

Revision 1, 9.6 kB (checked in by hans, 3 years ago)

Initial checkin

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