1
|
|
|
<?php |
2
|
|
|
/* (c) Anton Medvedev <[email protected]> |
3
|
|
|
* |
4
|
|
|
* For the full copyright and license information, please view the LICENSE |
5
|
|
|
* file that was distributed with this source code. |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Deployer\Task; |
9
|
|
|
|
10
|
|
|
use Deployer\Selector\Selector; |
11
|
|
|
|
12
|
|
|
class Task |
13
|
|
|
{ |
14
|
|
|
private $name; |
15
|
|
|
private $callback; |
16
|
|
|
private $description; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Task source file location. |
20
|
|
|
* |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
private $sourceLocation = ''; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Should we run this task locally? |
27
|
|
|
* |
28
|
|
|
* @var bool |
29
|
|
|
*/ |
30
|
|
|
private $local = false; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* List of task names to run before. |
34
|
|
|
* |
35
|
|
|
* @var string[] |
36
|
|
|
*/ |
37
|
|
|
private $before = []; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* List of task names to run after. |
41
|
|
|
* |
42
|
|
|
* @var string[] |
43
|
|
|
*/ |
44
|
|
|
private $after = []; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Make task internal and not visible in CLI. |
48
|
|
|
* |
49
|
|
|
* @var bool |
50
|
|
|
*/ |
51
|
|
|
private $hidden = false; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Run task only once on one of hosts. |
55
|
|
|
* |
56
|
|
|
* @var bool |
57
|
|
|
*/ |
58
|
|
|
private $once = false; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Shallow task will not print execution message/finish messages. |
62
|
|
|
* Useful for success messages and info printing. |
63
|
|
|
* |
64
|
|
|
* @var bool |
65
|
|
|
*/ |
66
|
|
|
private $shallow = false; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Limit parallel execution of the task. |
70
|
|
|
* |
71
|
|
|
* @var int|null |
72
|
|
|
*/ |
73
|
|
|
private $limit = null; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @var array |
77
|
|
|
*/ |
78
|
|
|
private $selector; |
79
|
|
|
|
80
|
23 |
|
public function __construct($name, callable $callback = null) |
81
|
|
|
{ |
82
|
23 |
|
$this->name = $name; |
83
|
23 |
|
$this->callback = $callback; |
84
|
23 |
|
$this->selector = Selector::parse('all'); |
85
|
23 |
|
} |
86
|
|
|
|
87
|
10 |
|
public function run(Context $context) |
88
|
|
|
{ |
89
|
10 |
|
Context::push($context); |
90
|
|
|
|
91
|
|
|
try { |
92
|
10 |
|
call_user_func($this->callback); // call task |
93
|
10 |
|
} finally { |
94
|
10 |
|
if ($context->getConfig() !== null) { |
95
|
8 |
|
$context->getConfig()->set('working_path', null); |
96
|
|
|
} |
97
|
|
|
|
98
|
10 |
|
Context::pop(); |
99
|
|
|
} |
100
|
10 |
|
} |
101
|
|
|
|
102
|
12 |
|
public function getName() |
103
|
|
|
{ |
104
|
12 |
|
return $this->name; |
105
|
|
|
} |
106
|
|
|
|
107
|
2 |
|
public function __toString() |
108
|
|
|
{ |
109
|
2 |
|
return $this->getName(); |
110
|
|
|
} |
111
|
|
|
|
112
|
10 |
|
public function getDescription() |
113
|
|
|
{ |
114
|
10 |
|
return $this->description; |
115
|
|
|
} |
116
|
|
|
|
117
|
20 |
|
public function desc(string $description) |
118
|
|
|
{ |
119
|
20 |
|
$this->description = $description; |
120
|
20 |
|
return $this; |
121
|
|
|
} |
122
|
|
|
|
123
|
8 |
|
public function getSourceLocation(): string |
124
|
|
|
{ |
125
|
8 |
|
return $this->sourceLocation; |
126
|
|
|
} |
127
|
|
|
|
128
|
19 |
|
public function saveSourceLocation() |
129
|
|
|
{ |
130
|
19 |
|
if (function_exists('debug_backtrace')) { |
131
|
19 |
|
$trace = debug_backtrace(); |
132
|
19 |
|
$this->sourceLocation = $trace[1]['file']; |
133
|
|
|
} |
134
|
19 |
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Mark this task local. |
138
|
|
|
*/ |
139
|
1 |
|
public function local() |
140
|
|
|
{ |
141
|
1 |
|
$this->local = true; |
142
|
1 |
|
return $this; |
143
|
|
|
} |
144
|
|
|
|
145
|
1 |
|
public function isLocal() |
146
|
|
|
{ |
147
|
1 |
|
return $this->local; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Mark this task to run only once on one of hosts. |
152
|
|
|
*/ |
153
|
1 |
|
public function once() |
154
|
|
|
{ |
155
|
1 |
|
$this->once = true; |
156
|
1 |
|
return $this; |
157
|
|
|
} |
158
|
|
|
|
159
|
9 |
|
public function isOnce() |
160
|
|
|
{ |
161
|
9 |
|
return $this->once; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Mark task as hidden and not accessible from CLI. |
166
|
|
|
* |
167
|
|
|
* @return $this |
168
|
|
|
*/ |
169
|
10 |
|
public function hidden() |
170
|
|
|
{ |
171
|
10 |
|
$this->hidden = true; |
172
|
10 |
|
return $this; |
173
|
|
|
} |
174
|
|
|
|
175
|
10 |
|
public function isHidden() |
176
|
|
|
{ |
177
|
10 |
|
return $this->hidden; |
178
|
|
|
} |
179
|
|
|
|
180
|
2 |
|
public function addBefore(string $task) |
181
|
|
|
{ |
182
|
2 |
|
array_unshift($this->before, $task); |
183
|
2 |
|
return $this; |
184
|
|
|
} |
185
|
|
|
|
186
|
1 |
|
public function addAfter(string $task) |
187
|
|
|
{ |
188
|
1 |
|
array_push($this->after, $task); |
189
|
1 |
|
return $this; |
190
|
|
|
} |
191
|
|
|
|
192
|
11 |
|
public function getBefore() |
193
|
|
|
{ |
194
|
11 |
|
return $this->before; |
195
|
|
|
} |
196
|
|
|
|
197
|
11 |
|
public function getAfter() |
198
|
|
|
{ |
199
|
11 |
|
return $this->after; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Sets task as shallow. Shallow task will not print execution message/finish messages. |
204
|
|
|
*/ |
205
|
9 |
|
public function shallow() |
206
|
|
|
{ |
207
|
9 |
|
$this->shallow = true; |
208
|
9 |
|
return $this; |
209
|
|
|
} |
210
|
|
|
|
211
|
8 |
|
public function isShallow() |
212
|
|
|
{ |
213
|
8 |
|
return $this->shallow; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* @return int|null |
218
|
|
|
*/ |
219
|
8 |
|
public function getLimit(): ?int |
220
|
|
|
{ |
221
|
8 |
|
return $this->limit; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* @param int|null $limit |
226
|
|
|
* @return Task |
227
|
|
|
*/ |
228
|
|
|
public function limit(?int $limit) |
229
|
|
|
{ |
230
|
|
|
$this->limit = $limit; |
231
|
|
|
return $this; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
public function select(string $selector) |
235
|
|
|
{ |
236
|
|
|
$this->selector = Selector::parse($selector); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* @return array |
241
|
|
|
*/ |
242
|
9 |
|
public function getSelector(): ?array |
243
|
|
|
{ |
244
|
9 |
|
return $this->selector; |
245
|
|
|
} |
246
|
|
|
|
247
|
6 |
|
public function addSelector(?array $newSelector) |
248
|
|
|
{ |
249
|
6 |
|
if ($newSelector !== null) { |
250
|
6 |
|
if ($this->selector === null) { |
251
|
|
|
$this->selector = $newSelector; |
252
|
|
|
} else { |
253
|
6 |
|
$this->selector = array_merge($this->selector, $newSelector); |
254
|
|
|
} |
255
|
|
|
} |
256
|
6 |
|
} |
257
|
|
|
} |
258
|
|
|
|