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