Passed
Push — master ( a0b722...864342 )
by Michiel
11:10
created

Task   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 251
Duplicated Lines 0 %

Test Coverage

Coverage 92.65%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 52
dl 0
loc 251
ccs 63
cts 68
cp 0.9265
rs 10
c 2
b 0
f 0
wmc 24

15 Methods

Rating   Name   Duplication   Size   Complexity  
A setOwningTarget() 0 3 1
A setTaskType() 0 3 1
A getRegisterSlot() 0 3 1
B perform() 0 24 6
A setTaskName() 0 3 1
A getRuntimeConfigurableWrapper() 0 7 2
A init() 0 2 1
A getTaskType() 0 3 1
A maybeConfigure() 0 4 2
A setRuntimeConfigurableWrapper() 0 3 1
A log() 0 6 2
A getTaskName() 0 12 2
A getOwningTarget() 0 3 1
A getWrapper() 0 3 1
A bindToOwner() 0 8 1
1
<?php
2
/**
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the LGPL. For more information please see
17
 * <http://phing.info>.
18
 */
19
20
/**
21
 * The base class for all Tasks.
22
 *
23
 * Use {@link Project#createTask} to register a new Task.
24
 *
25
 * @author    Andreas Aderhold <[email protected]>
26
 * @copyright 2001,2002 THYRELL. All rights reserved
27
 * @see       Project#createTask()
28
 * @package   phing
29
 */
30
abstract class Task extends ProjectComponent
31
{
32
33
    /**
34
     * Owning Target object
35
     *
36
     * @var Target
37
     */
38
    protected $target;
39
40
    /**
41
     * Internal taskname (req)
42
     *
43
     * @var string
44
     */
45
    protected $taskType;
46
47
    /**
48
     * Taskname for logger
49
     *
50
     * @var string
51
     */
52
    protected $taskName;
53
54
    /**
55
     * Wrapper of the task
56
     *
57
     * @var RuntimeConfigurable
58
     */
59
    protected $wrapper;
60
61
    /**
62
     * Sets the owning target this task belongs to.
63
     *
64
     * @param Target Reference to owning target
65
     */
66 789
    public function setOwningTarget(Target $target)
67
    {
68 789
        $this->target = $target;
69 789
    }
70
71
    /**
72
     * @return RuntimeConfigurable
73
     */
74 5
    public function getWrapper()
75
    {
76 5
        return $this->wrapper;
77
    }
78
79
    /**
80
     * Returns the owning target of this task.
81
     *
82
     * @return Target The target object that owns this task
83
     */
84 653
    public function getOwningTarget()
85
    {
86 653
        return $this->target;
87
    }
88
89
    /**
90
     * Returns the name of task, used only for log messages
91
     *
92
     * @return string Name of this task
93
     */
94 64
    public function getTaskName()
95
    {
96 64
        if ($this->taskName === null) {
97
            // if no task name is set, then it's possible
98
            // this task was created from within another task.  We don't
99
            // therefore know the XML tag name for this task, so we'll just
100
            // use the class name stripped of "task" suffix.  This is only
101
            // for log messages, so we don't have to worry much about accuracy.
102
            return preg_replace('/task$/i', '', get_class($this));
103
        }
104
105 64
        return $this->taskName;
106
    }
107
108
    /**
109
     * Sets the name of this task for log messages
110
     *
111
     * @param string $name
112
     */
113 786
    public function setTaskName($name)
114
    {
115 786
        $this->taskName = (string) $name;
116 786
    }
117
118
    /**
119
     * Returns the name of the task under which it was invoked,
120
     * usually the XML tagname
121
     *
122
     * @return string The type of this task (XML Tag)
123
     */
124 14
    public function getTaskType()
125
    {
126 14
        return $this->taskType;
127
    }
128
129
    /**
130
     * Sets the type of the task. Usually this is the name of the XML tag
131
     *
132
     * @param string $name The type of this task (XML Tag)
133
     */
134 786
    public function setTaskType($name)
135
    {
136 786
        $this->taskType = (string) $name;
137 786
    }
138
139
    /**
140
     * Returns a name
141
     *
142
     * @param  string $slotName
143
     * @return \RegisterSlot
144
     */
145 36
    protected function getRegisterSlot($slotName)
146
    {
147 36
        return Register::getSlot('task.' . $this->getTaskName() . '.' . $slotName);
148
    }
149
150
    /**
151
     * Provides a project level log event to the task.
152
     *
153
     * @param string $msg The message to log
154
     * @param int $level The priority of the message
155
     * @param Exception|null $t
156
     * @see   BuildEvent
157
     * @see   BuildListener
158
     */
159 526
    public function log($msg, $level = Project::MSG_INFO, Exception $t = null)
160
    {
161 526
        if ($this->getProject() !== null) {
162 526
            $this->getProject()->logObject($this, $msg, $level, $t);
163
        } else {
164 1
            parent::log($msg, $level);
165
        }
166 526
    }
167
168
    /**
169
     * Called by the parser to let the task initialize properly.
170
     * Should throw a BuildException if something goes wrong with the build
171
     *
172
     * This is abstract here, but may not be overloaded by subclasses.
173
     *
174
     * @throws BuildException
175
     */
176 614
    public function init()
177
    {
178 614
    }
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 wrapper object for runtime configuration
194
     *
195
     * @return RuntimeConfigurable The wrapper object used by this task
196
     */
197 33
    public function getRuntimeConfigurableWrapper()
198
    {
199 33
        if ($this->wrapper === null) {
200
            $this->wrapper = new RuntimeConfigurable($this, $this->getTaskName());
201
        }
202
203 33
        return $this->wrapper;
204
    }
205
206
    /**
207
     *  Sets the wrapper object this task should use for runtime
208
     *  configurable elements.
209
     *
210
     * @param RuntimeConfigurable $wrapper The wrapper object this task should use
211
     */
212 786
    public function setRuntimeConfigurableWrapper(RuntimeConfigurable $wrapper)
213
    {
214 786
        $this->wrapper = $wrapper;
215 786
    }
216
217
    /**
218
     *  Configure this task if it hasn't been done already.
219
     */
220 647
    public function maybeConfigure()
221
    {
222 647
        if ($this->wrapper !== null) {
223 644
            $this->wrapper->maybeConfigure($this->project);
224
        }
225 646
    }
226
227
    /**
228
     * Perfrom this task
229
     *
230
     * @return void
231
     *
232
     * @throws BuildException
233
     * @throws Error
234
     */
235 652
    public function perform(): void
236
    {
237 652
        $reason = null;
238
        try { // try executing task
239 652
            $this->project->fireTaskStarted($this);
240 652
            $this->maybeConfigure();
241 650
            DispatchUtils::main($this);
242 159
        } catch (\BuildException $ex) {
243 149
            $loc = $ex->getLocation();
244 149
            if ($loc === null || (string) $loc === '') {
245 140
                $ex->setLocation($this->getLocation());
246
            }
247 149
            $reason = $ex;
248 149
            throw $ex;
249 10
        } catch (\Exception $ex) {
250 10
            $reason = $ex;
251 10
            $be = new \BuildException($ex);
252 10
            $be->setLocation($this->getLocation());
253 10
            throw $be;
254
        } catch (\Error $ex) {
255
            $reason = $ex;
256
            throw $ex;
257 617
        } finally {
258 652
            $this->project->fireTaskFinished($this, $reason);
259
        }
260 617
    }
261
262
    /**
263
     * Bind a task to another; use this when configuring a newly created
264
     * task to do work on behalf of another.
265
     * Project, OwningTarget, TaskName, Location and Description are all copied
266
     *
267
     * Important: this method does not call {@link Task#init()}.
268
     * If you are creating a task to delegate work to, call {@link Task#init()}
269
     * to initialize it.
270
     *
271
     * @param Task $owner owning target
272
     */
273 13
    public function bindToOwner(Task $owner): void
274
    {
275 13
        $this->setProject($owner->getProject());
276 13
        $this->setOwningTarget($owner->getOwningTarget());
277 13
        $this->setTaskName($owner->getTaskName());
278 13
        $this->setDescription($owner->getDescription());
279 13
        $this->setLocation($owner->getLocation());
280 13
        $this->setTaskType($owner->getTaskType());
281 13
    }
282
}
283