|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* See class comment |
|
4
|
|
|
* |
|
5
|
|
|
* PHP Version 5 |
|
6
|
|
|
* |
|
7
|
|
|
* @category Netresearch |
|
8
|
|
|
* @package Netresearch\Kite |
|
9
|
|
|
* @author Christian Opitz <[email protected]> |
|
10
|
|
|
* @license http://www.netresearch.de Netresearch Copyright |
|
11
|
|
|
* @link http://www.netresearch.de |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Netresearch\Kite; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Base Task Class |
|
18
|
|
|
* |
|
19
|
|
|
* {@see configureVariables() for option information} |
|
20
|
|
|
* |
|
21
|
|
|
* @category Netresearch |
|
22
|
|
|
* @package Netresearch\Kite |
|
23
|
|
|
* @author Christian Opitz <[email protected]> |
|
24
|
|
|
* @license http://www.netresearch.de Netresearch Copyright |
|
25
|
|
|
* @link http://www.netresearch.de |
|
26
|
|
|
*/ |
|
27
|
|
|
abstract class Task extends Variables |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* @var \Netresearch\Kite\Service\Console |
|
31
|
|
|
*/ |
|
32
|
|
|
public $console; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var Job |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $job; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Task constructor. |
|
41
|
|
|
* |
|
42
|
|
|
* @param Variables $parent Parent object (Task/Job/Workflow) |
|
43
|
|
|
*/ |
|
44
|
|
|
public function __construct(Variables $parent) |
|
45
|
|
|
{ |
|
46
|
|
|
$this->job = $parent instanceof Job ? $parent : $parent->get('job'); |
|
47
|
|
|
$this->console = $this->job->console; |
|
48
|
|
|
|
|
49
|
|
|
parent::__construct($parent); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Configures the available options |
|
54
|
|
|
* |
|
55
|
|
|
* @return array |
|
56
|
|
|
*/ |
|
57
|
|
|
protected function configureVariables() |
|
58
|
|
|
{ |
|
59
|
|
|
return array( |
|
60
|
|
|
'name' => array( |
|
61
|
|
|
'type' => 'string', |
|
62
|
|
|
'label' => 'Name of the task' |
|
63
|
|
|
), |
|
64
|
|
|
'after' => array( |
|
65
|
|
|
'type' => 'string', |
|
66
|
|
|
'label' => 'Name of the task to execute this task after' |
|
67
|
|
|
), |
|
68
|
|
|
'before' => array( |
|
69
|
|
|
'type' => 'string', |
|
70
|
|
|
'label' => 'Name of the task to execute this task before' |
|
71
|
|
|
), |
|
72
|
|
|
'onBefore' => array( |
|
73
|
|
|
'type' => 'array', |
|
74
|
|
|
'label' => 'Array of sub tasks to execute prior to this task' |
|
75
|
|
|
), |
|
76
|
|
|
'onAfter' => array( |
|
77
|
|
|
'type' => 'array', |
|
78
|
|
|
'label' => 'Array of sub tasks to execute after this task' |
|
79
|
|
|
), |
|
80
|
|
|
'message' => array( |
|
81
|
|
|
'type' => 'string', |
|
82
|
|
|
'label' => 'Message to output when job is run with --dry-run or prior to execution' |
|
83
|
|
|
), |
|
84
|
|
|
'if' => array( |
|
85
|
|
|
'type' => array('string', 'callback', 'bool'), |
|
86
|
|
|
'label' => 'Expression string, callback returning true or false or boolean. Depending of that the task will be executed or not' |
|
87
|
|
|
), |
|
88
|
|
|
'executeInPreview' => array( |
|
89
|
|
|
'type' => 'bool', |
|
90
|
|
|
'default' => false, |
|
91
|
|
|
'label' => 'Whether to execute this task even when job is run with --dry-run' |
|
92
|
|
|
), |
|
93
|
|
|
'force' => array( |
|
94
|
|
|
'type' => 'bool', |
|
95
|
|
|
'default' => false, |
|
96
|
|
|
'label' => 'Whether this task should be run even when prior tasks (inside the current workflow) failed, exited or broke execution.' |
|
97
|
|
|
), |
|
98
|
|
|
'toVar' => array( |
|
99
|
|
|
'type' => 'string', |
|
100
|
|
|
'label' => 'The variable to save the return value of the execute method of the task to.' |
|
101
|
|
|
), |
|
102
|
|
|
); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Handle onBefore, onAfter and name |
|
107
|
|
|
* |
|
108
|
|
|
* @param mixed $offset The name of the variable |
|
109
|
|
|
* @param mixed $value The value |
|
110
|
|
|
* |
|
111
|
|
|
* @return void |
|
112
|
|
|
*/ |
|
113
|
|
|
public function offsetSet($offset, $value) |
|
114
|
|
|
{ |
|
115
|
|
|
if ($offset === 'onBefore' || $offset == 'onAfter') { |
|
116
|
|
|
$type = lcfirst(substr($offset, 2)); |
|
117
|
|
|
$name = $this->offsetGet('name'); |
|
118
|
|
|
$factory = $this->console->getFactory(); |
|
119
|
|
|
foreach ((array) $this->expand($value) as $subTask) { |
|
120
|
|
|
$subTask = $factory->createTask($this->expand($subTask), $this, [$type => $name]); |
|
121
|
|
|
$this->job->addTask($subTask); |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
if ($offset === 'name' && parent::offsetGet('name')) { |
|
|
|
|
|
|
125
|
|
|
throw new Exception('name may not be set doubly (try putting it at top of the task/job/workflow configuration'); |
|
126
|
|
|
} |
|
127
|
|
|
parent::offsetSet($offset, $value); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Re-add the onBefore and onAfter tasks and regenerate name |
|
132
|
|
|
* |
|
133
|
|
|
* @return void |
|
134
|
|
|
*/ |
|
135
|
|
|
public function __clone() |
|
136
|
|
|
{ |
|
137
|
|
|
parent::offsetSet('name', null); |
|
|
|
|
|
|
138
|
|
|
parent::__clone(); |
|
139
|
|
|
foreach (['onBefore', 'onAfter'] as $type) { |
|
140
|
|
|
if ($val = $this->offsetGet($type)) { |
|
141
|
|
|
$this->offsetSet($type, $val); |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Handle name |
|
148
|
|
|
* |
|
149
|
|
|
* @param mixed $offset The variable name |
|
150
|
|
|
* |
|
151
|
|
|
* @return bool |
|
152
|
|
|
*/ |
|
153
|
|
|
public function offsetExists($offset) |
|
154
|
|
|
{ |
|
155
|
|
|
return $offset === 'name' || parent::offsetExists($offset); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Generate name if it doesn't exist |
|
160
|
|
|
* |
|
161
|
|
|
* @param mixed $offset The name of the variable |
|
162
|
|
|
* |
|
163
|
|
|
* @return mixed |
|
164
|
|
|
*/ |
|
165
|
|
|
public function &offsetGet($offset) |
|
166
|
|
|
{ |
|
167
|
|
|
if ($offset === 'name' && !parent::offsetGet('name')) { |
|
168
|
|
|
parent::offsetSet('name', spl_object_hash($this)); |
|
|
|
|
|
|
169
|
|
|
} |
|
170
|
|
|
return parent::offsetGet($offset); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Called from parent task as soon as task is ready to run - which doesn't |
|
175
|
|
|
* necessarely mean that it'll be run. |
|
176
|
|
|
* |
|
177
|
|
|
* @return void |
|
178
|
|
|
*/ |
|
179
|
|
|
protected function initialize() |
|
180
|
|
|
{ |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* Shortcut to set() |
|
185
|
|
|
* |
|
186
|
|
|
* @param bool $flag The flag |
|
187
|
|
|
* |
|
188
|
|
|
* @return $this |
|
189
|
|
|
*/ |
|
190
|
|
|
public function executeInPreview($flag = true) |
|
191
|
|
|
{ |
|
192
|
|
|
return $this->set(__FUNCTION__, $flag); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* Shortcut to set() |
|
197
|
|
|
* |
|
198
|
|
|
* @param string $var The variable name |
|
199
|
|
|
* |
|
200
|
|
|
* @return $this |
|
201
|
|
|
*/ |
|
202
|
|
|
public function toVar($var) |
|
203
|
|
|
{ |
|
204
|
|
|
return $this->set(__FUNCTION__, $var); |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* Shortcut to set() |
|
209
|
|
|
* |
|
210
|
|
|
* @param bool $flag The flag |
|
211
|
|
|
* |
|
212
|
|
|
* @return $this |
|
213
|
|
|
*/ |
|
214
|
|
|
public function force($flag = true) |
|
215
|
|
|
{ |
|
216
|
|
|
return $this->set(__FUNCTION__, $flag); |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
|
|
* Shortcut to set() |
|
221
|
|
|
* |
|
222
|
|
|
* @param string $message The message |
|
223
|
|
|
* |
|
224
|
|
|
* @return $this |
|
225
|
|
|
*/ |
|
226
|
|
|
public function message($message) |
|
227
|
|
|
{ |
|
228
|
|
|
return $this->set(__FUNCTION__, $message); |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
/** |
|
232
|
|
|
* Shortcut to set() |
|
233
|
|
|
* |
|
234
|
|
|
* @param string|callable|boolean $condition The condition |
|
235
|
|
|
* |
|
236
|
|
|
* @return $this |
|
237
|
|
|
*/ |
|
238
|
|
|
public function when($condition) |
|
239
|
|
|
{ |
|
240
|
|
|
return $this->set('if', $condition); |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
/** |
|
244
|
|
|
* Run this task |
|
245
|
|
|
* |
|
246
|
|
|
* @throws Exception |
|
247
|
|
|
* |
|
248
|
|
|
* @return mixed |
|
249
|
|
|
*/ |
|
250
|
|
|
public function run() |
|
251
|
|
|
{ |
|
252
|
|
|
$this->preview(); |
|
253
|
|
|
if ($this->shouldExecute()) { |
|
254
|
|
|
return $this->execute(); |
|
255
|
|
|
} |
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
|
|
/** |
|
259
|
|
|
* Determines whether this task should be executed |
|
260
|
|
|
* |
|
261
|
|
|
* @return bool |
|
262
|
|
|
*/ |
|
263
|
|
|
protected function shouldExecute() |
|
264
|
|
|
{ |
|
265
|
|
|
return !$this->job->isDryRun() || $this->get('executeInPreview'); |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
/** |
|
269
|
|
|
* The preview for this task - this is called right before execution and |
|
270
|
|
|
* in preview mode |
|
271
|
|
|
* |
|
272
|
|
|
* @return void |
|
273
|
|
|
*/ |
|
274
|
|
|
public function preview() |
|
275
|
|
|
{ |
|
276
|
|
|
$message = $this->get('message'); |
|
277
|
|
|
if ($message) { |
|
278
|
|
|
$this->console->output($message); |
|
279
|
|
|
} |
|
280
|
|
|
} |
|
281
|
|
|
|
|
282
|
|
|
/** |
|
283
|
|
|
* Actually execute this task |
|
284
|
|
|
* |
|
285
|
|
|
* @return mixed |
|
286
|
|
|
*/ |
|
287
|
|
|
public function execute() |
|
288
|
|
|
{ |
|
289
|
|
|
throw new Exception('Method not implemented'); |
|
290
|
|
|
} |
|
291
|
|
|
} |
|
292
|
|
|
?> |
|
293
|
|
|
|
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()method in theSoncalls the wrong method in the parent class.