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
|
|
|
private $invalid; |
61
|
|
|
/** |
62
|
|
|
* Replacement element used if this task is invalidated. |
63
|
|
|
* @var UnknownElement |
64
|
|
|
*/ |
65
|
|
|
private $replacement; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return RuntimeConfigurable |
69
|
|
|
*/ |
70
|
667 |
|
public function getWrapper() |
71
|
|
|
{ |
72
|
667 |
|
return $this->wrapper; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Provides a project level log event to the task. |
77
|
|
|
* |
78
|
|
|
* @param string $msg The message to log |
79
|
|
|
* @param int $level The priority of the message |
80
|
|
|
* @param Exception|null $t |
81
|
|
|
* @see BuildEvent |
82
|
|
|
* @see BuildListener |
83
|
|
|
*/ |
84
|
544 |
|
public function log($msg, $level = Project::MSG_INFO, Exception $t = null) |
85
|
|
|
{ |
86
|
544 |
|
if ($this->getProject() !== null) { |
87
|
544 |
|
$this->getProject()->logObject($this, $msg, $level, $t); |
88
|
|
|
} else { |
89
|
1 |
|
parent::log($msg, $level); |
90
|
|
|
} |
91
|
544 |
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Called by the parser to let the task initialize properly. |
95
|
|
|
* Should throw a BuildException if something goes wrong with the build |
96
|
|
|
* |
97
|
|
|
* This is abstract here, but may not be overloaded by subclasses. |
98
|
|
|
* |
99
|
|
|
* @throws BuildException |
100
|
|
|
*/ |
101
|
629 |
|
public function init() |
102
|
|
|
{ |
103
|
629 |
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Called by the project to let the task do it's work. This method may be |
107
|
|
|
* called more than once, if the task is invoked more than once. For |
108
|
|
|
* example, if target1 and target2 both depend on target3, then running |
109
|
|
|
* <em>phing target1 target2</em> will run all tasks in target3 twice. |
110
|
|
|
* |
111
|
|
|
* Should throw a BuildException if someting goes wrong with the build |
112
|
|
|
* |
113
|
|
|
* This is abstract here. Must be overloaded by real tasks. |
114
|
|
|
*/ |
115
|
|
|
abstract public function main(); |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Returns the wrapper object for runtime configuration |
119
|
|
|
* |
120
|
|
|
* @return RuntimeConfigurable The wrapper object used by this task |
121
|
|
|
*/ |
122
|
33 |
|
public function getRuntimeConfigurableWrapper() |
123
|
|
|
{ |
124
|
33 |
|
if ($this->wrapper === null) { |
125
|
|
|
$this->wrapper = new RuntimeConfigurable($this, $this->getTaskName()); |
126
|
|
|
} |
127
|
|
|
|
128
|
33 |
|
return $this->wrapper; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Returns the name of task, used only for log messages |
133
|
|
|
* |
134
|
|
|
* @return string Name of this task |
135
|
|
|
*/ |
136
|
67 |
|
public function getTaskName() |
137
|
|
|
{ |
138
|
67 |
|
if ($this->taskName === null) { |
139
|
|
|
// if no task name is set, then it's possible |
140
|
|
|
// this task was created from within another task. We don't |
141
|
|
|
// therefore know the XML tag name for this task, so we'll just |
142
|
|
|
// use the class name stripped of "task" suffix. This is only |
143
|
|
|
// for log messages, so we don't have to worry much about accuracy. |
144
|
|
|
return preg_replace('/task$/i', '', get_class($this)); |
145
|
|
|
} |
146
|
|
|
|
147
|
67 |
|
return $this->taskName; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Sets the name of this task for log messages |
152
|
|
|
* |
153
|
|
|
* @param string $name |
154
|
|
|
*/ |
155
|
804 |
|
public function setTaskName($name) |
156
|
|
|
{ |
157
|
804 |
|
$this->taskName = (string) $name; |
158
|
804 |
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Marks this task as invalid. Any further use of this task |
162
|
|
|
* will go through a replacement with the updated definition. |
163
|
|
|
*/ |
164
|
|
|
public function markInvalid() |
165
|
|
|
{ |
166
|
|
|
$this->invalid = true; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Perfrom this task |
171
|
|
|
* |
172
|
|
|
* @return void |
173
|
|
|
* |
174
|
|
|
* @throws BuildException |
175
|
|
|
* @throws Error |
176
|
|
|
*/ |
177
|
670 |
|
public function perform(): void |
178
|
|
|
{ |
179
|
670 |
|
if ($this->invalid) { |
180
|
|
|
$this->getReplacement()->getTask()->perform(); |
181
|
|
|
} else { |
182
|
670 |
|
$reason = null; |
183
|
|
|
try { // try executing task |
184
|
670 |
|
$this->project->fireTaskStarted($this); |
185
|
670 |
|
$this->maybeConfigure(); |
186
|
668 |
|
DispatchUtils::main($this); |
187
|
166 |
|
} catch (\BuildException $ex) { |
188
|
156 |
|
$loc = $ex->getLocation(); |
189
|
156 |
|
if ($loc === null || (string) $loc === '') { |
190
|
147 |
|
$ex->setLocation($this->getLocation()); |
191
|
|
|
} |
192
|
156 |
|
$reason = $ex; |
193
|
156 |
|
throw $ex; |
194
|
10 |
|
} catch (\Exception $ex) { |
195
|
10 |
|
$reason = $ex; |
196
|
10 |
|
$be = new \BuildException($ex); |
197
|
10 |
|
$be->setLocation($this->getLocation()); |
198
|
10 |
|
throw $be; |
199
|
|
|
} catch (\Error $ex) { |
200
|
|
|
$reason = $ex; |
201
|
|
|
throw $ex; |
202
|
632 |
|
} finally { |
203
|
670 |
|
$this->project->fireTaskFinished($this, $reason); |
204
|
|
|
} |
205
|
|
|
} |
206
|
632 |
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Configure this task if it hasn't been done already. |
210
|
|
|
*/ |
211
|
665 |
|
public function maybeConfigure() |
212
|
|
|
{ |
213
|
665 |
|
if ($this->invalid) { |
214
|
|
|
$this->getReplacement(); |
215
|
665 |
|
} elseif ($this->wrapper !== null) { |
216
|
662 |
|
$this->wrapper->maybeConfigure($this->project); |
217
|
|
|
} |
218
|
664 |
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Force the task to be reconfigured from its RuntimeConfigurable. |
222
|
|
|
*/ |
223
|
|
|
public function reconfigure() |
224
|
|
|
{ |
225
|
|
|
if ($this->wrapper !== null) { |
226
|
|
|
$this->wrapper->reconfigure($this->getProject()); |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
private function getReplacement(): \UnknownElement |
231
|
|
|
{ |
232
|
|
|
if ($this->replacement === null) { |
233
|
|
|
$this->replacement = new UnknownElement($this->taskType); |
234
|
|
|
$this->replacement->setProject($this->getProject()); |
235
|
|
|
$this->replacement->setTaskType($this->taskType); |
236
|
|
|
$this->replacement->setTaskName($this->taskName); |
237
|
|
|
$this->replacement->setLocation($this->getLocation()); |
238
|
|
|
$this->replacement->setOwningTarget($this->target); |
239
|
|
|
$this->replacement->setRuntimeConfigurableWrapper($this->wrapper); |
240
|
|
|
$this->wrapper->setProxy($this->replacement); |
241
|
|
|
$this->replaceChildren($this->wrapper, $this->replacement); |
242
|
|
|
$this->target->replaceChild($this, $this->replacement); |
243
|
|
|
$this->replacement->maybeConfigure(); |
244
|
|
|
} |
245
|
|
|
return $this->replacement; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Sets the owning target this task belongs to. |
250
|
|
|
* |
251
|
|
|
* @param Target Reference to owning target |
252
|
|
|
*/ |
253
|
807 |
|
public function setOwningTarget(Target $target): void |
254
|
|
|
{ |
255
|
807 |
|
$this->target = $target; |
256
|
807 |
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* Sets the wrapper object this task should use for runtime |
260
|
|
|
* configurable elements. |
261
|
|
|
* |
262
|
|
|
* @param RuntimeConfigurable $wrapper The wrapper object this task should use |
263
|
|
|
*/ |
264
|
804 |
|
public function setRuntimeConfigurableWrapper(RuntimeConfigurable $wrapper): void |
265
|
|
|
{ |
266
|
804 |
|
$this->wrapper = $wrapper; |
267
|
804 |
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* Recursively adds an UnknownElement instance for each child |
271
|
|
|
* element of replacement. |
272
|
|
|
* @param RuntimeConfigurable $wrapper |
273
|
|
|
* @param UnknownElement $parentElement |
274
|
|
|
*/ |
275
|
|
|
private function replaceChildren(RuntimeConfigurable $wrapper, UnknownElement $parentElement): void |
276
|
|
|
{ |
277
|
|
|
foreach ($wrapper->getChildren() as $childWrapper) { |
278
|
|
|
$childElement = new UnknownElement($childWrapper->getElementTag()); |
279
|
|
|
$parentElement->addChild($childElement); |
280
|
|
|
$childElement->setProject($this->getProject()); |
281
|
|
|
$childElement->setRuntimeConfigurableWrapper($childWrapper); |
282
|
|
|
$childWrapper->setProxy($childElement); |
283
|
|
|
$this->replaceChildren($childWrapper, $childElement); |
284
|
|
|
} |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* Bind a task to another; use this when configuring a newly created |
289
|
|
|
* task to do work on behalf of another. |
290
|
|
|
* Project, OwningTarget, TaskName, Location and Description are all copied |
291
|
|
|
* |
292
|
|
|
* Important: this method does not call {@link Task#init()}. |
293
|
|
|
* If you are creating a task to delegate work to, call {@link Task#init()} |
294
|
|
|
* to initialize it. |
295
|
|
|
* |
296
|
|
|
* @param Task $owner owning target |
297
|
|
|
*/ |
298
|
13 |
|
public function bindToOwner(Task $owner): void |
299
|
|
|
{ |
300
|
13 |
|
$this->setProject($owner->getProject()); |
301
|
13 |
|
$this->setOwningTarget($owner->getOwningTarget()); |
302
|
13 |
|
$this->setTaskName($owner->getTaskName()); |
303
|
13 |
|
$this->setDescription($owner->getDescription()); |
304
|
13 |
|
$this->setLocation($owner->getLocation()); |
305
|
13 |
|
$this->setTaskType($owner->getTaskType()); |
306
|
13 |
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* Returns the owning target of this task. |
310
|
|
|
* |
311
|
|
|
* @return Target The target object that owns this task |
312
|
|
|
*/ |
313
|
671 |
|
public function getOwningTarget() |
314
|
|
|
{ |
315
|
671 |
|
return $this->target; |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
/** |
319
|
|
|
* Returns the name of the task under which it was invoked, |
320
|
|
|
* usually the XML tagname |
321
|
|
|
* |
322
|
|
|
* @return string The type of this task (XML Tag) |
323
|
|
|
*/ |
324
|
14 |
|
public function getTaskType() |
325
|
|
|
{ |
326
|
14 |
|
return $this->taskType; |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* Sets the type of the task. Usually this is the name of the XML tag |
331
|
|
|
* |
332
|
|
|
* @param string $name The type of this task (XML Tag) |
333
|
|
|
*/ |
334
|
804 |
|
public function setTaskType($name) |
335
|
|
|
{ |
336
|
804 |
|
$this->taskType = (string) $name; |
337
|
804 |
|
} |
338
|
|
|
|
339
|
|
|
/** |
340
|
|
|
* Returns a name |
341
|
|
|
* |
342
|
|
|
* @param string $slotName |
343
|
|
|
* @return \RegisterSlot |
344
|
|
|
*/ |
345
|
36 |
|
protected function getRegisterSlot($slotName) |
346
|
|
|
{ |
347
|
36 |
|
return Register::getSlot('task.' . $this->getTaskName() . '.' . $slotName); |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
/** |
351
|
|
|
* Has this task been marked invalid? |
352
|
|
|
* |
353
|
|
|
* @return bool true if this task is no longer valid. A new task should be |
354
|
|
|
* configured in this case. |
355
|
|
|
*/ |
356
|
|
|
protected function isInvalid() |
357
|
|
|
{ |
358
|
|
|
return $this->invalid; |
359
|
|
|
} |
360
|
|
|
} |
361
|
|
|
|