1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* (c) Anton Medvedev <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Deployer\Task; |
12
|
|
|
|
13
|
|
|
use Deployer\Selector\Selector; |
14
|
|
|
|
15
|
|
|
class Task |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
private $name; |
21
|
|
|
/** |
22
|
|
|
* @var callable|null |
23
|
|
|
*/ |
24
|
|
|
private $callback; |
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
private $description; |
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
private $sourceLocation = ''; |
33
|
|
|
/** |
34
|
|
|
* @var array |
35
|
|
|
*/ |
36
|
|
|
private $before = []; |
37
|
|
|
/** |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
private $after = []; |
41
|
|
|
/** |
42
|
|
|
* @var bool |
43
|
|
|
*/ |
44
|
|
|
private $hidden = false; |
45
|
|
|
/** |
46
|
|
|
* @var bool |
47
|
|
|
*/ |
48
|
|
|
private $once = false; |
49
|
|
|
/** |
50
|
|
|
* @var bool |
51
|
|
|
*/ |
52
|
|
|
private $oncePerNode = false; |
53
|
|
|
/** |
54
|
|
|
* @var int|null |
55
|
|
|
*/ |
56
|
|
|
private $limit = null; |
57
|
|
|
/** |
58
|
|
|
* @var array|null |
59
|
|
|
*/ |
60
|
|
|
private $selector = null; |
61
|
|
|
/** |
62
|
|
|
* @var bool |
63
|
|
|
*/ |
64
|
|
|
private $verbose = false; |
65
|
|
|
/** |
66
|
|
|
* @var bool |
67
|
|
|
*/ |
68
|
|
|
private $enabled = true; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param callable():void $callback |
72
|
|
|
*/ |
73
|
|
|
public function __construct(string $name, ?callable $callback = null) |
74
|
|
|
{ |
75
|
|
|
$this->name = $name; |
76
|
|
|
$this->callback = $callback; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
19 |
|
* @param callable():void $callback |
81
|
|
|
*/ |
82
|
19 |
|
public function setCallback(callable $callback): void |
83
|
19 |
|
{ |
84
|
19 |
|
$this->callback = $callback; |
85
|
19 |
|
} |
86
|
|
|
|
87
|
10 |
|
public function run(Context $context): void |
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
|
16 |
|
public function getName(): string |
103
|
|
|
{ |
104
|
16 |
|
return $this->name; |
105
|
|
|
} |
106
|
|
|
|
107
|
5 |
|
public function __toString(): string |
108
|
|
|
{ |
109
|
5 |
|
return $this->getName(); |
110
|
|
|
} |
111
|
|
|
|
112
|
13 |
|
public function getDescription(): ?string |
113
|
|
|
{ |
114
|
13 |
|
return $this->description; |
115
|
|
|
} |
116
|
|
|
|
117
|
9 |
|
public function desc(string $description): self |
118
|
|
|
{ |
119
|
9 |
|
$this->description = $description; |
120
|
9 |
|
return $this; |
121
|
|
|
} |
122
|
|
|
|
123
|
8 |
|
public function getSourceLocation(): string |
124
|
|
|
{ |
125
|
8 |
|
return $this->sourceLocation; |
126
|
|
|
} |
127
|
|
|
|
128
|
15 |
|
public function setSourceLocation(string $path): void |
129
|
|
|
{ |
130
|
15 |
|
$this->sourceLocation = $path; |
131
|
15 |
|
} |
132
|
15 |
|
|
133
|
|
|
public function saveSourceLocation(): void |
134
|
15 |
|
{ |
135
|
|
|
if (function_exists('debug_backtrace')) { |
136
|
|
|
$trace = debug_backtrace(); |
137
|
|
|
$this->sourceLocation = $trace[1]['file']; |
138
|
|
|
} |
139
|
1 |
|
} |
140
|
|
|
|
141
|
1 |
|
/** |
142
|
1 |
|
* Mark this task to run only once on one of hosts. |
143
|
|
|
*/ |
144
|
|
|
public function once(bool $once = true): self |
145
|
13 |
|
{ |
146
|
|
|
$this->once = $once; |
147
|
13 |
|
return $this; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
public function isOnce(): bool |
151
|
|
|
{ |
152
|
|
|
return $this->once; |
153
|
9 |
|
} |
154
|
|
|
|
155
|
9 |
|
/** |
156
|
9 |
|
* Mark task to only run once per node. |
157
|
|
|
* Node is a group of hosts with same hostname or with same node label. |
158
|
|
|
*/ |
159
|
13 |
|
public function oncePerNode(bool $once = true): self |
160
|
|
|
{ |
161
|
13 |
|
$this->oncePerNode = $once; |
162
|
|
|
return $this; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
public function isOncePerNode(): bool |
166
|
|
|
{ |
167
|
|
|
return $this->oncePerNode; |
168
|
|
|
} |
169
|
9 |
|
|
170
|
|
|
/** |
171
|
9 |
|
* Mark task as hidden and not accessible from CLI. |
172
|
9 |
|
*/ |
173
|
|
|
public function hidden(bool $hidden = true): self |
174
|
|
|
{ |
175
|
13 |
|
$this->hidden = $hidden; |
176
|
|
|
return $this; |
177
|
13 |
|
} |
178
|
|
|
|
179
|
|
|
public function isHidden(): bool |
180
|
2 |
|
{ |
181
|
|
|
return $this->hidden; |
182
|
2 |
|
} |
183
|
2 |
|
|
184
|
|
|
/** |
185
|
|
|
* Make $task being run before this task. |
186
|
13 |
|
*/ |
187
|
|
|
public function addBefore(string $task): self |
188
|
13 |
|
{ |
189
|
13 |
|
array_unshift($this->before, $task); |
190
|
|
|
return $this; |
191
|
|
|
} |
192
|
15 |
|
|
193
|
|
|
/** |
194
|
15 |
|
* Make $task being run after this task |
195
|
|
|
*/ |
196
|
|
|
public function addAfter(string $task): self |
197
|
15 |
|
{ |
198
|
|
|
array_push($this->after, $task); |
199
|
15 |
|
return $this; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
public function getBefore(): array |
203
|
|
|
{ |
204
|
|
|
return $this->before; |
205
|
8 |
|
} |
206
|
|
|
|
207
|
8 |
|
public function getAfter(): array |
208
|
8 |
|
{ |
209
|
|
|
return $this->after; |
210
|
|
|
} |
211
|
12 |
|
|
212
|
|
|
public function getLimit(): ?int |
213
|
12 |
|
{ |
214
|
|
|
return $this->limit; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
public function limit(?int $limit): self |
218
|
|
|
{ |
219
|
12 |
|
$this->limit = $limit; |
220
|
|
|
return $this; |
221
|
12 |
|
} |
222
|
|
|
|
223
|
|
|
public function select(string $selector): self |
224
|
|
|
{ |
225
|
|
|
$this->selector = Selector::parse($selector); |
226
|
|
|
return $this; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* @return array |
231
|
|
|
*/ |
232
|
|
|
public function getSelector(): ?array |
233
|
|
|
{ |
234
|
|
|
return $this->selector; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
public function addSelector(?array $newSelector): void |
238
|
|
|
{ |
239
|
|
|
if ($newSelector !== null) { |
|
|
|
|
240
|
|
|
if ($this->selector === null) { |
241
|
|
|
$this->selector = $newSelector; |
242
|
|
|
} else { |
243
|
|
|
$this->selector = array_merge($this->selector, $newSelector); |
244
|
|
|
} |
245
|
|
|
} |
246
|
|
|
} |
247
|
13 |
|
|
248
|
|
|
public function isVerbose(): bool |
249
|
13 |
|
{ |
250
|
|
|
return $this->verbose; |
251
|
|
|
} |
252
|
5 |
|
|
253
|
|
|
public function verbose(bool $verbose = true): self |
254
|
5 |
|
{ |
255
|
5 |
|
$this->verbose = $verbose; |
256
|
|
|
return $this; |
257
|
|
|
} |
258
|
5 |
|
|
259
|
|
|
public function isEnabled(): bool |
260
|
|
|
{ |
261
|
5 |
|
return $this->enabled; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
public function disable(): self |
265
|
|
|
{ |
266
|
|
|
$this->enabled = false; |
267
|
|
|
return $this; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
public function enable(): self |
271
|
|
|
{ |
272
|
|
|
$this->enabled = true; |
273
|
|
|
return $this; |
274
|
|
|
} |
275
|
|
|
} |
276
|
|
|
|