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
|
20 |
|
public function __construct($name, callable $callback = null) |
81
|
|
|
{ |
82
|
20 |
|
$this->name = $name; |
83
|
20 |
|
$this->callback = $callback; |
84
|
20 |
|
} |
85
|
|
|
|
86
|
7 |
|
public function run(Context $context) |
87
|
|
|
{ |
88
|
7 |
|
Context::push($context); |
89
|
|
|
|
90
|
|
|
try { |
91
|
7 |
|
call_user_func($this->callback); // call task |
92
|
7 |
|
} finally { |
93
|
7 |
|
if ($context->getConfig() !== null) { |
94
|
5 |
|
$context->getConfig()->set('working_path', null); |
95
|
|
|
} |
96
|
|
|
|
97
|
7 |
|
Context::pop(); |
98
|
|
|
} |
99
|
7 |
|
} |
100
|
|
|
|
101
|
9 |
|
public function getName() |
102
|
|
|
{ |
103
|
9 |
|
return $this->name; |
104
|
|
|
} |
105
|
|
|
|
106
|
2 |
|
public function __toString() |
107
|
|
|
{ |
108
|
2 |
|
return $this->getName(); |
109
|
|
|
} |
110
|
|
|
|
111
|
7 |
|
public function getDescription() |
112
|
|
|
{ |
113
|
7 |
|
return $this->description; |
114
|
|
|
} |
115
|
|
|
|
116
|
17 |
|
public function desc(string $description) |
117
|
|
|
{ |
118
|
17 |
|
$this->description = $description; |
119
|
17 |
|
return $this; |
120
|
|
|
} |
121
|
|
|
|
122
|
5 |
|
public function getSourceLocation(): string |
123
|
|
|
{ |
124
|
5 |
|
return $this->sourceLocation; |
125
|
|
|
} |
126
|
|
|
|
127
|
16 |
|
public function saveSourceLocation() |
128
|
|
|
{ |
129
|
16 |
|
if (function_exists('debug_backtrace')) { |
130
|
16 |
|
$trace = debug_backtrace(); |
131
|
16 |
|
$this->sourceLocation = $trace[1]['file']; |
132
|
|
|
} |
133
|
16 |
|
} |
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
|
7 |
|
public function hidden() |
169
|
|
|
{ |
170
|
7 |
|
$this->hidden = true; |
171
|
7 |
|
return $this; |
172
|
|
|
} |
173
|
|
|
|
174
|
7 |
|
public function isHidden() |
175
|
|
|
{ |
176
|
7 |
|
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
|
8 |
|
public function getBefore() |
192
|
|
|
{ |
193
|
8 |
|
return $this->before; |
194
|
|
|
} |
195
|
|
|
|
196
|
8 |
|
public function getAfter() |
197
|
|
|
{ |
198
|
8 |
|
return $this->after; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Sets task as shallow. Shallow task will not print execution message/finish messages. |
203
|
|
|
*/ |
204
|
6 |
|
public function shallow() |
205
|
|
|
{ |
206
|
6 |
|
$this->shallow = true; |
207
|
6 |
|
return $this; |
208
|
|
|
} |
209
|
|
|
|
210
|
5 |
|
public function isShallow() |
211
|
|
|
{ |
212
|
5 |
|
return $this->shallow; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* @return int|null |
217
|
|
|
*/ |
218
|
5 |
|
public function getLimit(): ?int |
219
|
|
|
{ |
220
|
5 |
|
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
|
6 |
|
public function getSelector(): ?array |
242
|
|
|
{ |
243
|
6 |
|
return $this->selector; |
244
|
|
|
} |
245
|
|
|
|
246
|
6 |
|
public function addSelector(?array $newSelector) |
247
|
|
|
{ |
248
|
6 |
|
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
|
6 |
|
} |
256
|
|
|
} |
257
|
|
|
|