|
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
|
|
|
* Make task internal and not visible in CLI. |
|
49
|
|
|
* @var bool |
|
50
|
|
|
*/ |
|
51
|
|
|
private $private = false; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param string $name Tasks name |
|
55
|
|
|
* @param \Closure $callback Task code. |
|
56
|
|
|
*/ |
|
57
|
19 |
|
public function __construct($name, \Closure $callback) |
|
58
|
|
|
{ |
|
59
|
19 |
|
$this->name = $name; |
|
60
|
19 |
|
$this->callback = $callback; |
|
61
|
19 |
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Run task. |
|
65
|
|
|
* |
|
66
|
|
|
* @param Context $context |
|
67
|
|
|
*/ |
|
68
|
13 |
|
public function run(Context $context) |
|
69
|
|
|
{ |
|
70
|
13 |
|
Context::push($context); |
|
71
|
13 |
|
$env = $context->getEnvironment(); |
|
72
|
|
|
|
|
73
|
|
|
// Save cd's working_path path. |
|
74
|
13 |
|
if ($env !== null) { |
|
75
|
12 |
|
$workingPath = $env->get('working_path', false); |
|
76
|
12 |
|
} |
|
77
|
|
|
|
|
78
|
|
|
// Call tasks. |
|
79
|
13 |
|
call_user_func($this->callback); |
|
80
|
|
|
|
|
81
|
|
|
// Restore cd's working_path path. |
|
82
|
13 |
|
if ($env !== null && isset($workingPath)) { |
|
83
|
12 |
|
$env->set('working_path', $workingPath); |
|
84
|
12 |
|
} |
|
85
|
|
|
|
|
86
|
13 |
|
Context::pop(); |
|
87
|
13 |
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @return string |
|
91
|
|
|
*/ |
|
92
|
15 |
|
public function getName() |
|
93
|
|
|
{ |
|
94
|
15 |
|
return $this->name; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @return string |
|
99
|
|
|
*/ |
|
100
|
14 |
|
public function getDescription() |
|
101
|
|
|
{ |
|
102
|
14 |
|
return $this->description; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Set task description. |
|
107
|
|
|
* @param string $description |
|
108
|
|
|
* @return $this |
|
109
|
|
|
*/ |
|
110
|
14 |
|
public function desc($description) |
|
111
|
|
|
{ |
|
112
|
14 |
|
$this->description = $description; |
|
113
|
14 |
|
return $this; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Set this task local and run only once. |
|
118
|
|
|
* @return $this |
|
119
|
|
|
*/ |
|
120
|
15 |
|
public function once() |
|
121
|
|
|
{ |
|
122
|
15 |
|
$this->once = true; |
|
123
|
15 |
|
return $this; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @return bool |
|
128
|
|
|
*/ |
|
129
|
15 |
|
public function isOnce() |
|
130
|
|
|
{ |
|
131
|
15 |
|
return $this->once; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @param array|string $servers |
|
136
|
|
|
* @return $this |
|
137
|
|
|
*/ |
|
138
|
2 |
|
public function onlyOn($servers = []) |
|
139
|
|
|
{ |
|
140
|
2 |
|
$this->onlyOn = array_flip(is_array($servers) ? $servers : func_get_args()); |
|
141
|
2 |
|
return $this; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Indicate for which stages this task should be run. |
|
146
|
|
|
* |
|
147
|
|
|
* @param array|string $stages |
|
148
|
|
|
* @return $this |
|
149
|
|
|
*/ |
|
150
|
4 |
|
public function onlyForStage($stages = []) |
|
151
|
|
|
{ |
|
152
|
4 |
|
$this->onlyForStage = array_flip(is_array($stages) ? $stages: func_get_args()); |
|
153
|
4 |
|
return $this; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* @return array |
|
158
|
|
|
*/ |
|
159
|
1 |
|
public function getOnlyOn() |
|
160
|
|
|
{ |
|
161
|
1 |
|
return $this->onlyOn; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* @return array |
|
166
|
|
|
*/ |
|
167
|
15 |
|
public function getOnlyForStage() |
|
168
|
|
|
{ |
|
169
|
15 |
|
return $this->onlyForStage; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* Decide to run or not to run for these stages. |
|
174
|
|
|
* @param $stages |
|
175
|
|
|
* @return bool |
|
176
|
|
|
*/ |
|
177
|
3 |
|
public function runForStages($stages) |
|
178
|
|
|
{ |
|
179
|
3 |
|
if (empty($this->onlyForStage)) { |
|
180
|
1 |
|
return true; |
|
181
|
|
|
} else { |
|
182
|
3 |
|
return count(array_intersect($stages, array_keys($this->onlyForStage))) > 0; |
|
183
|
|
|
} |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* Decide to run or not to run on this server. |
|
188
|
|
|
* @param string $serverName |
|
189
|
|
|
* @return bool |
|
190
|
|
|
*/ |
|
191
|
15 |
|
public function runOnServer($serverName) |
|
192
|
|
|
{ |
|
193
|
15 |
|
if (empty($this->onlyOn)) { |
|
194
|
15 |
|
return true; |
|
195
|
|
|
} else { |
|
196
|
2 |
|
return array_key_exists($serverName, $this->onlyOn); |
|
197
|
|
|
} |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* @return boolean |
|
202
|
|
|
*/ |
|
203
|
14 |
|
public function isPrivate() |
|
204
|
|
|
{ |
|
205
|
14 |
|
return $this->private; |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* Mark task as private. |
|
210
|
|
|
*/ |
|
211
|
14 |
|
public function setPrivate() |
|
212
|
|
|
{ |
|
213
|
14 |
|
$this->private = true; |
|
214
|
14 |
|
return $this; |
|
215
|
|
|
} |
|
216
|
|
|
} |
|
217
|
|
|
|