root/trunk/classes/phing/Task.php

Revision 307, 7.8 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 use phing::parser::Location;
24 use phing::system::util::Register;
25
26 /**
27  * The base class for all Tasks.
28  *
29  * Use {@link Project#createTask} to register a new Task.
30  *
31  * @author    Andreas Aderhold <andi@binarycloud.com>
32  * @copyright ᅵ 2001,2002 THYRELL. All rights reserved
33  * @version   $Revision: 1.11 $
34  * @see       Project#createTask()
35  * @package   phing
36  */
37 abstract class Task extends ProjectComponent {
38
39     /**
40      * @var Target owning Target object
41      */
42     protected $target;
43     
44     /**
45      * @var string description of the task
46      */
47     protected $description;
48     
49     /**
50      * @var string internal taskname (req)
51      */
52     protected $taskType;
53     
54     /**
55      * @var string Taskname for logger
56      */
57     protected $taskName;
58     
59     /**
60      * @var Location stored buildfile location
61      */
62     protected $location;
63     
64     /**
65      * @var RuntimeConfigurable wrapper of the task
66      */
67     protected $wrapper;
68
69     /**
70      * Sets the owning target this task belongs to.
71      *
72      * @param Target Reference to owning target
73      */
74     public function setOwningTarget(Target $target) {
75         $this->target = $target;
76     }
77
78     /**
79      * Returns the owning target of this task.
80      *
81      * @return Target The target object that owns this task
82      */
83     public function getOwningTarget() {
84         return $this->target;
85     }
86
87     /**
88      * Returns the name of task, used only for log messages
89      *
90      * @return string Name of this task
91      */
92     public function getTaskName() {
93         if ($this->taskName === null) {
94             // if no task name is set, then it's possible
95             // this task was created from within another task.  We don't
96             // therefore know the XML tag name for this task, so we'll just
97             // use the class name stripped of "task" suffix.  This is only
98             // for log messages, so we don't have to worry much about accuracy.
99             return preg_replace('/task$/i', '', get_class($this));
100         }
101         return $this->taskName;
102     }
103
104     /**
105      * Sets the name of this task for log messages
106      *
107      * @return string A string representing the name of this task for log
108      */
109     public function setTaskName($name) {
110         $this->taskName = (string) $name;
111     }
112
113     /**
114      * Returns the name of the task under which it was invoked,
115      * usually the XML tagname
116      *
117      * @return string The type of this task (XML Tag)
118      */
119     public function getTaskType() {
120         return $this->taskType;
121     }
122
123     /**
124      * Sets the type of the task. Usually this is the name of the XML tag
125      *
126      * @param string The type of this task (XML Tag)
127      */
128     public function setTaskType($name) {
129         $this->taskType = (string) $name;
130     }
131     
132     /**
133      * Returns a name
134      *
135      */
136     protected function getRegisterSlot($slotName) {
137         return Register::getSlot('task.' . $this->getTaskName() . '.' . $slotName);
138     }
139     
140     /**
141      * Provides a project level log event to the task.
142      *
143      * @param string  The message to log
144      * @param integer The priority of the message
145      * @see BuildEvent
146      * @see BuildListener
147      */
148     function log($msg, $level = Project::MSG_INFO) {
149         $this->project->logObject($this, $msg, $level);
150     }
151
152     /**
153      * Sets a textual description of the task
154      *
155      * @param string $desc The text describing the task
156      */
157     public function setDescription($desc) {
158         $this->description = $desc;
159     }
160
161     /**
162      * Returns the textual description of the task
163      *
164      * @return string The text description of the task
165      */
166     public function getDescription() {
167         return $this->description;
168     }
169
170     /**
171      * Called by the parser to let the task initialize properly.
172      * Should throw a BuildException if something goes wrong with the build
173      *
174      * This is abstract here, but may not be overloaded by subclasses.
175      *
176      * @throws BuildException
177      */
178     public function init() {
179     }
180
181     /**
182      *  Called by the project to let the task do it's work. This method may be
183      *  called more than once, if the task is invoked more than once. For
184      *  example, if target1 and target2 both depend on target3, then running
185      *  <em>phing target1 target2</em> will run all tasks in target3 twice.
186      *
187      *  Should throw a BuildException if someting goes wrong with the build
188      *
189      *  This is abstract here. Must be overloaded by real tasks.
190      */
191     abstract public function main();
192
193     /**
194      * Returns the location within the buildfile this task occurs. Used
195      * by {@link BuildException} to give detailed error messages.
196      *
197      * @return Location The location object describing the position of this
198      *                  task within the buildfile.
199      */
200     function getLocation() {
201         return $this->location;
202     }
203
204     /**
205      * Sets the location within the buildfile this task occurs. Called by
206      * the parser to set location information.
207      *
208      * @param Location $location The location object describing the position of this
209      *                           task within the buildfile.
210      */
211     function setLocation(Location $location) {
212         $this->location = $location;
213     }
214
215     /**
216      * Returns the wrapper object for runtime configuration
217      *
218      * @return RuntimeConfigurable The wrapper object used by this task
219      */
220     function getRuntimeConfigurableWrapper() {
221         if ($this->wrapper === null) {
222             $this->wrapper = new RuntimeConfigurable($this, $this->getTaskName());
223         }
224         return $this->wrapper;
225     }
226
227     /**
228      *  Sets the wrapper object this task should use for runtime
229      *  configurable elements.
230      *
231      * @param RuntimeConfigurable $wrapper The wrapper object this task should use
232      */
233     function setRuntimeConfigurableWrapper(RuntimeConfigurable $wrapper) {
234         $this->wrapper = $wrapper;
235     }
236
237     /**
238      *  Configure this task if it hasn't been done already.
239      */
240     public function maybeConfigure() {
241         if ($this->wrapper !== null) {
242             $this->wrapper->maybeConfigure($this->project);
243         }
244     }
245
246     /**
247      * Perfrom this task
248      */
249     public function perform() {
250
251         try { // try executing task
252             $this->project->fireTaskStarted($this);
253             $this->maybeConfigure();
254             $this->main();
255             $this->project->fireTaskFinished($this, $null=null);
256         } catch (Exception $exc) {
257             if ($exc instanceof BuildException) {
258                 if ($exc->getLocation() === null) {
259                     $exc->setLocation($this->getLocation());
260                 }
261             }
262             $this->project->fireTaskFinished($this, $exc);
263             throw $exc;
264         }
265     }
266 }
267
Note: See TracBrowser for help on using the browser.