Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

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