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
|
|
|
class Task |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
private $name; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Task code. |
19
|
|
|
* @var callable |
20
|
|
|
*/ |
21
|
|
|
private $callback; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Task description. |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
private $description; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Should run this task only once and locally? |
31
|
|
|
* @var bool |
32
|
|
|
*/ |
33
|
|
|
private $once = false; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* List of stages in which this task should be executed. |
37
|
|
|
* @var array Key contains stage names. |
38
|
|
|
*/ |
39
|
|
|
private $onlyForStage = []; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* List of servers names there this task should be executed. |
43
|
|
|
* @var array Key contains server names. |
44
|
|
|
*/ |
45
|
|
|
private $onlyOn = []; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* List of task names to run before. |
49
|
|
|
* @var array |
50
|
|
|
*/ |
51
|
|
|
private $before = []; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* List of task names to run after. |
55
|
|
|
* @var array |
56
|
|
|
*/ |
57
|
|
|
private $after = []; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Make task internal and not visible in CLI. |
61
|
|
|
* @var bool |
62
|
|
|
*/ |
63
|
|
|
private $private = false; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param string $name Tasks name |
67
|
|
|
* @param callable $callback Task code. |
68
|
|
|
*/ |
69
|
20 |
|
public function __construct($name, callable $callback = null) |
70
|
|
|
{ |
71
|
20 |
|
$this->name = $name; |
72
|
20 |
|
$this->callback = $callback; |
73
|
20 |
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Run task. |
77
|
|
|
* |
78
|
|
|
* @param Context $context |
79
|
|
|
*/ |
80
|
14 |
|
public function run(Context $context) |
81
|
|
|
{ |
82
|
14 |
|
Context::push($context); |
83
|
|
|
|
84
|
|
|
// Call task |
85
|
14 |
|
call_user_func($this->callback); |
86
|
|
|
|
87
|
|
|
// Clear working_path |
88
|
14 |
|
$env = $context->getEnvironment(); |
89
|
14 |
|
if ($env !== null) { |
90
|
12 |
|
$env->set('working_path', false); |
91
|
12 |
|
} |
92
|
|
|
|
93
|
14 |
|
Context::pop(); |
94
|
14 |
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return string |
98
|
|
|
*/ |
99
|
17 |
|
public function getName() |
100
|
|
|
{ |
101
|
17 |
|
return $this->name; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return string |
106
|
|
|
*/ |
107
|
12 |
|
public function getDescription() |
108
|
|
|
{ |
109
|
12 |
|
return $this->description; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Set task description. |
114
|
|
|
* @param string $description |
115
|
|
|
* @return Task |
116
|
|
|
*/ |
117
|
12 |
|
public function desc($description) |
118
|
|
|
{ |
119
|
12 |
|
$this->description = $description; |
120
|
12 |
|
return $this; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Set this task local and run only once. |
125
|
|
|
* @return Task |
126
|
|
|
*/ |
127
|
13 |
|
public function once() |
128
|
|
|
{ |
129
|
13 |
|
$this->once = true; |
130
|
13 |
|
return $this; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @return bool |
135
|
|
|
*/ |
136
|
13 |
|
public function isOnce() |
137
|
|
|
{ |
138
|
13 |
|
return $this->once; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param array|string $servers |
143
|
|
|
* @return Task |
144
|
|
|
*/ |
145
|
2 |
|
public function onlyOn($servers = []) |
146
|
|
|
{ |
147
|
2 |
|
$this->onlyOn = array_flip(is_array($servers) ? $servers : func_get_args()); |
148
|
2 |
|
return $this; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Indicate for which stages this task should be run. |
153
|
|
|
* |
154
|
|
|
* @param array|string $stages |
155
|
|
|
* @return Task |
156
|
|
|
*/ |
157
|
1 |
|
public function onlyForStage($stages = []) |
158
|
|
|
{ |
159
|
1 |
|
$this->onlyForStage = array_flip(is_array($stages) ? $stages: func_get_args()); |
160
|
1 |
|
return $this; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @return array |
165
|
|
|
*/ |
166
|
1 |
|
public function getOnlyOn() |
167
|
|
|
{ |
168
|
1 |
|
return $this->onlyOn; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @return array |
173
|
|
|
*/ |
174
|
1 |
|
public function getOnlyForStage() |
175
|
|
|
{ |
176
|
1 |
|
return $this->onlyForStage; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Decide to run or not to run for these stages. |
181
|
|
|
* @param $stages |
182
|
|
|
* @return bool |
183
|
|
|
*/ |
184
|
1 |
|
public function isForStages($stages) |
185
|
|
|
{ |
186
|
1 |
|
if (empty($this->onlyForStage)) { |
187
|
1 |
|
return true; |
188
|
|
|
} else { |
189
|
1 |
|
return count(array_intersect((array)$stages, array_keys($this->onlyForStage))) > 0; |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Decide to run or not to run on this server. |
195
|
|
|
* @param string $serverName |
196
|
|
|
* @return bool |
197
|
|
|
*/ |
198
|
11 |
|
public function isOnServer($serverName) |
199
|
|
|
{ |
200
|
11 |
|
if (empty($this->onlyOn)) { |
201
|
11 |
|
return true; |
202
|
|
|
} else { |
203
|
2 |
|
return array_key_exists($serverName, $this->onlyOn); |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @return boolean |
209
|
|
|
*/ |
210
|
12 |
|
public function isPrivate() |
211
|
|
|
{ |
212
|
12 |
|
return $this->private; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Mark task as private. |
217
|
|
|
* @return Task |
218
|
|
|
*/ |
219
|
12 |
|
public function setPrivate() |
220
|
|
|
{ |
221
|
12 |
|
$this->private = true; |
222
|
12 |
|
return $this; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* @param string $task |
227
|
|
|
*/ |
228
|
2 |
|
public function addBefore($task) |
229
|
|
|
{ |
230
|
2 |
|
if (!is_string($task)) { |
231
|
|
|
throw new \InvalidArgumentException('Invalid argument to `before` hook of "' . $this->getName() . '" task.'); |
232
|
|
|
} |
233
|
|
|
|
234
|
2 |
|
array_unshift($this->before, $task); |
235
|
2 |
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* @param string $task |
239
|
|
|
*/ |
240
|
2 |
|
public function addAfter($task) |
241
|
|
|
{ |
242
|
2 |
|
if (!is_string($task)) { |
243
|
|
|
throw new \InvalidArgumentException('Invalid argument to `after` hook of "' . $this->getName() . '" task.'); |
244
|
|
|
} |
245
|
|
|
|
246
|
2 |
|
array_push($this->after, $task); |
247
|
2 |
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Get before tasks names. |
251
|
|
|
* @return string[] |
252
|
|
|
*/ |
253
|
15 |
|
public function getBefore() |
254
|
|
|
{ |
255
|
15 |
|
return $this->before; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* Get after tasks names. |
260
|
|
|
* @return string[] |
261
|
|
|
*/ |
262
|
15 |
|
public function getAfter() |
263
|
|
|
{ |
264
|
15 |
|
return $this->after; |
265
|
|
|
} |
266
|
|
|
} |
267
|
|
|
|