|
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|null |
|
77
|
|
|
*/ |
|
78
|
|
|
private $selector = null; |
|
79
|
|
|
|
|
80
|
14 |
|
public function __construct($name, callable $callback = null) |
|
81
|
|
|
{ |
|
82
|
14 |
|
$this->name = $name; |
|
83
|
14 |
|
$this->callback = $callback; |
|
84
|
14 |
|
} |
|
85
|
|
|
|
|
86
|
2 |
|
public function run(Context $context) |
|
87
|
|
|
{ |
|
88
|
2 |
|
Context::push($context); |
|
89
|
|
|
|
|
90
|
|
|
// Call task |
|
91
|
2 |
|
call_user_func($this->callback); |
|
92
|
|
|
|
|
93
|
|
|
// Clear working_path |
|
94
|
2 |
|
if ($context->getConfig() !== null) { |
|
95
|
|
|
$context->getConfig()->set('working_path', null); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
2 |
|
Context::pop(); |
|
99
|
2 |
|
} |
|
100
|
|
|
|
|
101
|
4 |
|
public function getName() |
|
102
|
|
|
{ |
|
103
|
4 |
|
return $this->name; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
1 |
|
public function __toString() |
|
107
|
|
|
{ |
|
108
|
1 |
|
return $this->getName(); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
1 |
|
public function getDescription() |
|
112
|
|
|
{ |
|
113
|
1 |
|
return $this->description; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
11 |
|
public function desc(string $description) |
|
117
|
|
|
{ |
|
118
|
11 |
|
$this->description = $description; |
|
119
|
11 |
|
return $this; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
public function getSourceLocation(): string |
|
123
|
|
|
{ |
|
124
|
|
|
return $this->sourceLocation; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
10 |
|
public function saveSourceLocation() |
|
128
|
|
|
{ |
|
129
|
10 |
|
if (function_exists('debug_backtrace')) { |
|
130
|
10 |
|
$trace = debug_backtrace(); |
|
131
|
10 |
|
$this->sourceLocation = $trace[1]['file']; |
|
132
|
|
|
} |
|
133
|
10 |
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Mark this task local. |
|
137
|
|
|
*/ |
|
138
|
1 |
|
public function local() |
|
139
|
|
|
{ |
|
140
|
1 |
|
$this->local = true; |
|
141
|
1 |
|
return $this; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
1 |
|
public function isLocal() |
|
145
|
|
|
{ |
|
146
|
1 |
|
return $this->local; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Mark this task to run only once on one of hosts. |
|
151
|
|
|
*/ |
|
152
|
1 |
|
public function once() |
|
153
|
|
|
{ |
|
154
|
1 |
|
$this->once = true; |
|
155
|
1 |
|
return $this; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
1 |
|
public function isOnce() |
|
159
|
|
|
{ |
|
160
|
1 |
|
return $this->once; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* Mark task as hidden and not accessible from CLI. |
|
165
|
|
|
* |
|
166
|
|
|
* @return $this |
|
167
|
|
|
*/ |
|
168
|
1 |
|
public function hidden() |
|
169
|
|
|
{ |
|
170
|
1 |
|
$this->hidden = true; |
|
171
|
1 |
|
return $this; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
1 |
|
public function isHidden() |
|
175
|
|
|
{ |
|
176
|
1 |
|
return $this->hidden; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
2 |
|
public function addBefore(string $task) |
|
180
|
|
|
{ |
|
181
|
2 |
|
array_unshift($this->before, $task); |
|
182
|
2 |
|
return $this; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
1 |
|
public function addAfter(string $task) |
|
186
|
|
|
{ |
|
187
|
1 |
|
array_push($this->after, $task); |
|
188
|
1 |
|
return $this; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
3 |
|
public function getBefore() |
|
192
|
|
|
{ |
|
193
|
3 |
|
return $this->before; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
3 |
|
public function getAfter() |
|
197
|
|
|
{ |
|
198
|
3 |
|
return $this->after; |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* Sets task as shallow. Shallow task will not print execution message/finish messages. |
|
203
|
|
|
*/ |
|
204
|
|
|
public function shallow() |
|
205
|
|
|
{ |
|
206
|
|
|
$this->shallow = true; |
|
207
|
|
|
return $this; |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
public function isShallow() |
|
211
|
|
|
{ |
|
212
|
|
|
return $this->shallow; |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
/** |
|
216
|
|
|
* @return int|null |
|
217
|
|
|
*/ |
|
218
|
|
|
public function getLimit(): ?int |
|
219
|
|
|
{ |
|
220
|
|
|
return $this->limit; |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* @param int|null $limit |
|
225
|
|
|
* @return Task |
|
226
|
|
|
*/ |
|
227
|
|
|
public function limit(?int $limit) |
|
228
|
|
|
{ |
|
229
|
|
|
$this->limit = $limit; |
|
230
|
|
|
return $this; |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
public function select(string $selector) |
|
234
|
|
|
{ |
|
235
|
|
|
$this->selector = Selector::parse($selector); |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
/** |
|
239
|
|
|
* @return array|null |
|
240
|
|
|
*/ |
|
241
|
1 |
|
public function getSelector(): ?array |
|
242
|
|
|
{ |
|
243
|
1 |
|
return $this->selector; |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
1 |
|
public function addSelector(?array $newSelector) |
|
247
|
|
|
{ |
|
248
|
1 |
|
if ($newSelector !== null) { |
|
249
|
|
|
if ($this->selector === null) { |
|
250
|
|
|
$this->selector = $newSelector; |
|
251
|
|
|
} else { |
|
252
|
|
|
$this->selector = array_merge($this->selector, $newSelector); |
|
253
|
|
|
} |
|
254
|
|
|
} |
|
255
|
1 |
|
} |
|
256
|
|
|
} |
|
257
|
|
|
|